-
Notifications
You must be signed in to change notification settings - Fork 15.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
expose memory key name #808
Conversation
output_key: Optional[str] = None | ||
input_key: Optional[str] = None | ||
store: Dict[str, Optional[str]] = {} | ||
entity_cache: List[str] = [] | ||
k: int = 3 | ||
chat_history_key: str = "history" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this constant? If so you can use the const
parameter from Pydantic:
chat_history_key: str = Field("history", const=True)
While I appreciate the efforts, I still can't use this feature due to the very same issue. | ERROR | 2023-03-03 18:03:15 | /api/prompt/prompt.py | PromptMaker Could NOT execute LangChain's agent code!
Details:
Missing some input keys: {'chat_history'} File "/opt/gpt3/api/prompt/prompt.py", line 134, in _run
results = agent_chain.run(
File "/usr/local/lib/python3.10/site-packages/langchain/chains/base.py", line 242, in run
return self(kwargs)[self.output_keys[0]]
File "/usr/local/lib/python3.10/site-packages/langchain/chains/base.py", line 132, in __call__
inputs = self.prep_inputs(inputs)
File "/usr/local/lib/python3.10/site-packages/langchain/chains/base.py", line 221, in prep_inputs
self._validate_inputs(inputs)
File "/usr/local/lib/python3.10/site-packages/langchain/chains/base.py", line 101, in _validate_inputs
raise ValueError(f"Missing some input keys: {missing_keys}") But my code does indeed specify to look for 'chat_history'; in multiple places even: prompt = ConversationalAgent.create_prompt(
tools,
prefix=TEMPLATE_OBJECT[self.language]["prefix"],
suffix=TEMPLATE_OBJECT[self.language]["suffix"],
input_variables=['input', 'chat_history',
'agent_scratchpad', 'context']
)
memory = ConversationBufferMemory(memory_key="chat_history")
llm_chain = LLMChain(llm=self.llm, prompt=prompt)
tools = load_tools(["serpapi", "llm-math"], llm=self.llm)
# ConversationalAgent seems to have issues with chat_history
# TODO: Check ConversationalAgent + {chat_history} !!!
# agent = ConversationalAgent(
# llm_chain=llm_chain, tools=tools, verbose=True)
agent = ConversationalAgent(
llm_chain=llm_chain, tools=tools, verbose=True)
agent_chain = AgentExecutor.from_agent_and_tools(
agent, tools, verbose=True, mempry=memory)
results = agent_chain.run(
input=self.prompt, context=self.context) Is there anything I am missing? Am I the one being silly? 😢 |
Oh my goodness.... It was all my fault in the end.... Spelling mistakes 😭 agent_chain = AgentExecutor.from_agent_and_tools(
agent, tools, verbose=True, mempry=memory) --> agent_chain = AgentExecutor.from_agent_and_tools(
agent, tools, verbose=True, memory=memory) Thanks for your great work! This tool is freakin' amazeballs :) |
about the chat_history ... how do you handle the sessions with the client ??? using a fingerprint or something to make a history not shared with others users ... |
For that, I just use This is my code for loading the memory (part of a larger client class). You're welcome :) def _load_memory(self, index: str, session: str):
"""
Method that loads memory (if it exists).
"""
history = ConversationBufferWindowMemory(
k=3, return_messages=True, memory_key='chat_history')
hist_index = "_".join(["hist", index, session])
if self.indices.exists(index=hist_index).body:
query = {
"query": {
"match_all": {}
},
"sort": [
{
"timestamp": {
"order": "desc",
"unmapped_type": "date"
}
}
]
}
results = self.search(
index=hist_index, query=query['query'], size=3, sort=query['sort'])
hist_docs = results['hits']['hits']
# self.logger.msg = "Documents found:"
# self.logger.info(extra_msg=str(hist_docs))
for doc in hist_docs:
history.chat_memory.add_user_message(doc['_source']['user'])
history.chat_memory.add_ai_message(doc['_source']['ai'])
else:
settings = {
"settings": {
"index": {
"number_of_shards": 1,
"number_of_replicas": 0
}
}
}
mappings = {
"mappings": {
"properties": {
"user": {
"type": "text"
},
"ai": {
"type": "text"
},
"timestamp": {
"type": "date"
}
}
}
}
mappings.update(settings)
try:
self.indices.create(
index=hist_index, mappings=mappings['mappings'], settings=mappings['settings'])
except Exception as err:
self.logger.msg = "Something went wrong when trying to create index " +\
Fore.LIGHTRED_EX + hist_index + Fore.RESET + "!"
self.logger.error(extra_msg=str(err))
raise self.logger from err
return history |
No description provided.