In this code
from langchain.prompts import PromptTemplate
from langchain.chat_models.base import BaseChatModel
from abc import ABC, abstractmethod
from langchain.schema.messages import HumanMessage, SystemMessage, AIMessage, ChatMessage
from enum import Enum
from utilities.utilities import prompt_utilities
import json
class agent_types(Enum):
STORAGE="storage",
SUMMARY="summary",
ASSIST="story-assist"
class function_agent(ABC):
def __init__(
self,
key: str,
summary: str,
required_prompt_input_variables : list[str],
agent_type : agent_types) -> None:
self.key = key
self.summary = summary
self.required_prompt_input_variables = required_prompt_input_variables
self.agent_type = agent_type
@abstractmethod
def call(self, variablesdict, utilities : prompt_utilities):
pass
def key_and_summary(self):
data = {
'key': self.key,
'summary': self.summary,
'type': self.agent_type.name,
'required_input_variables': ','.join(self.required_prompt_input_variables)
}
output = json.dumps(data, indent=4)
output = output.replace('{', '{{').replace('}', '}}')
return output
class agent(function_agent):
def init(
self,
key: str,
summary: str,
system_prompt_template : str,
prompt_template: str,
required_prompt_input_variables : list[str],
llm : BaseChatModel,
agent_type : agent_types) -> None:
super().init(
key,
summary,
required_prompt_input_variables,
agent_type
)
self.system_prompt_template = system_prompt_template
self.prompt_template = prompt_template
self.llm = llm
pass
def validate_variable(self, variabledict: dict):
for l in self.required_prompt_input_variables:
if l not in variabledict:
raise ValueError(f"{l} not included in vars passed to {__name__}.call")
def call(self, variablesdict, context, utilities : prompt_utilities):
self.validate_variable(variabledict=variablesdict)
prompt = self.get_prompt(variablesdict, context)
response = None
calls = [SystemMessage(content=self.system_prompt_template),
HumanMessage(content=prompt)]
try:
utilities.log_prompt(title=f"Calling {self.key}", messages=calls)
response = self.llm(calls)
except Exception as e:
return f"An error occured: {e.__str__()}. Please try again"
return response.content
def get_prompt(self, variablesdict, context):
prompt = self.prompt_template
for k, v in variablesdict.items():
prompt = prompt.replace(k, v)
return prompt
When I call utilities.log_prompt(title=f"Calling {self.key}", messages=calls), in the class above, why does it expect the self parameter to be passed when Its part of a class?
from .prompt_logger import prompt_logger
from .system_prompt_builder import system_prompt_builder
from .history import history_manager
from langchain.schema.messages import BaseMessage
class prompt_utilities:
def init(self, agents):
self.agents = agents
self.system_prompt_builder = system_prompt_builder(agents=self.agents)
self.prompt_logger = prompt_logger(self.agents)
self.history_manager = history_manager(self.system_prompt_builder)
def log_prompt(self, title : str, messages):
return self.prompt_logger.log_prompt(title, messages)
def get_prompt_history(self):
temp = [
self.system_prompt_builder.build_system_prompt(),
*self.history_manager.history.copy()]
self.prompt_logger.log_prompt("Initial Call:", temp)
return temp
def add_prompt_to_history(self, prompt : BaseMessage):
self.history_manager.append(prompt)