140 lines
4.5 KiB
Python
140 lines
4.5 KiB
Python
# sourcery skip: do-not-use-staticmethod
|
|
"""
|
|
A module that contains the AIConfig class object that contains the configuration
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
from pathlib import Path
|
|
from typing import Optional, Type
|
|
|
|
import yaml
|
|
|
|
from autogpt.prompts.generator import PromptGenerator
|
|
|
|
|
|
class AIConfig:
|
|
"""
|
|
A class object that contains the configuration information for the AI
|
|
|
|
Attributes:
|
|
ai_name (str): The name of the AI.
|
|
ai_role (str): The description of the AI's role.
|
|
ai_goals (list): The list of objectives the AI is supposed to complete.
|
|
"""
|
|
|
|
def __init__(
|
|
self, ai_name: str = "", ai_role: str = "", ai_goals: list | None = None
|
|
) -> None:
|
|
"""
|
|
Initialize a class instance
|
|
|
|
Parameters:
|
|
ai_name (str): The name of the AI.
|
|
ai_role (str): The description of the AI's role.
|
|
ai_goals (list): The list of objectives the AI is supposed to complete.
|
|
Returns:
|
|
None
|
|
"""
|
|
if ai_goals is None:
|
|
ai_goals = []
|
|
self.ai_name = ai_name
|
|
self.ai_role = ai_role
|
|
self.ai_goals = ai_goals
|
|
self.prompt_generator = None
|
|
self.command_registry = None
|
|
|
|
# Soon this will go in a folder where it remembers more stuff about the run(s)
|
|
SAVE_FILE = Path(os.getcwd()) / "ai_settings.yaml"
|
|
|
|
@staticmethod
|
|
def load(config_file: str = SAVE_FILE) -> "AIConfig":
|
|
"""
|
|
Returns class object with parameters (ai_name, ai_role, ai_goals) loaded from
|
|
yaml file if yaml file exists,
|
|
else returns class with no parameters.
|
|
|
|
Parameters:
|
|
config_file (int): The path to the config yaml file.
|
|
DEFAULT: "../ai_settings.yaml"
|
|
|
|
Returns:
|
|
cls (object): An instance of given cls object
|
|
"""
|
|
|
|
try:
|
|
with open(config_file, encoding="utf-8") as file:
|
|
config_params = yaml.load(file, Loader=yaml.FullLoader)
|
|
except FileNotFoundError:
|
|
config_params = {}
|
|
|
|
ai_name = config_params.get("ai_name", "")
|
|
ai_role = config_params.get("ai_role", "")
|
|
ai_goals = config_params.get("ai_goals", [])
|
|
# type: Type[AIConfig]
|
|
return AIConfig(ai_name, ai_role, ai_goals)
|
|
|
|
def save(self, config_file: str = SAVE_FILE) -> None:
|
|
"""
|
|
Saves the class parameters to the specified file yaml file path as a yaml file.
|
|
|
|
Parameters:
|
|
config_file(str): The path to the config yaml file.
|
|
DEFAULT: "../ai_settings.yaml"
|
|
|
|
Returns:
|
|
None
|
|
"""
|
|
|
|
config = {
|
|
"ai_name": self.ai_name,
|
|
"ai_role": self.ai_role,
|
|
"ai_goals": self.ai_goals,
|
|
}
|
|
with open(config_file, "w", encoding="utf-8") as file:
|
|
yaml.dump(config, file, allow_unicode=True)
|
|
|
|
def construct_full_prompt(
|
|
self, prompt_generator: Optional[PromptGenerator] = None
|
|
) -> str:
|
|
"""
|
|
Returns a prompt to the user with the class information in an organized fashion.
|
|
|
|
Parameters:
|
|
None
|
|
|
|
Returns:
|
|
full_prompt (str): A string containing the initial prompt for the user
|
|
including the ai_name, ai_role and ai_goals.
|
|
"""
|
|
|
|
prompt_start = (
|
|
"Your decisions must always be made independently without"
|
|
" seeking user assistance. Play to your strengths as an LLM and pursue"
|
|
" simple strategies with no legal complications."
|
|
""
|
|
)
|
|
|
|
from autogpt.config import Config
|
|
from autogpt.prompts.prompt import build_default_prompt_generator
|
|
|
|
cfg = Config()
|
|
if prompt_generator is None:
|
|
prompt_generator = build_default_prompt_generator()
|
|
prompt_generator.goals = self.ai_goals
|
|
prompt_generator.name = self.ai_name
|
|
prompt_generator.role = self.ai_role
|
|
prompt_generator.command_registry = self.command_registry
|
|
for plugin in cfg.plugins:
|
|
if not plugin.can_handle_post_prompt():
|
|
continue
|
|
prompt_generator = plugin.post_prompt(prompt_generator)
|
|
|
|
# Construct full prompt
|
|
full_prompt = f"You are {prompt_generator.name}, {prompt_generator.role}\n{prompt_start}\n\nGOALS:\n\n"
|
|
for i, goal in enumerate(self.ai_goals):
|
|
full_prompt += f"{i+1}. {goal}\n"
|
|
self.prompt_generator = prompt_generator
|
|
full_prompt += f"\n\n{prompt_generator.generate_prompt_string()}"
|
|
return full_prompt
|