Skip to content
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

Merged
merged 1 commit into from
Jan 30, 2023
Merged

expose memory key name #808

merged 1 commit into from
Jan 30, 2023

Conversation

hwchase17
Copy link
Contributor

No description provided.

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"
Copy link
Contributor

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)

@hwchase17 hwchase17 merged commit f46f1d2 into master Jan 30, 2023
@hwchase17 hwchase17 deleted the harrison/expose-memory-name branch January 30, 2023 22:48
@VBoB13
Copy link

VBoB13 commented Mar 3, 2023

While I appreciate the efforts, I still can't use this feature due to the very same issue.
Error:

| 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? 😢

@VBoB13
Copy link

VBoB13 commented Mar 3, 2023

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 :)

@postix
Copy link

postix commented Apr 4, 2023

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 ...

@VBoB13
Copy link

VBoB13 commented May 12, 2023

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 session_id as an identifier and save each question and answer into a DB.
Then, for every time a user asks something from my Agent, I load memory through Elasticsearch and shove it into the Agent.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants