langchain-ai / langchain

🦜🔗 Build context-aware reasoning applications
https://python.langchain.com
MIT License
94.45k stars 15.27k forks source link

Add memeory to langchain AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION agent #13845

Closed saadShirgaonkar closed 7 months ago

saadShirgaonkar commented 11 months ago

System Info

I am write a code and i want to add history to my langchain agent. History is present in chats list

Who can help?

No response

Information

Related Components

Reproduction

`async def chat_with_agent(user_input, formatting_data, chats: list): """ Initiates a chat with the agent based on the user input. """ try:

Initialize the chat model

    llm_model = "gpt-4-1106-preview"
    llm = ChatOpenAI(temperature=0.3, model=llm_model)

    # Load necessary tools
    tool = StructuredTool.from_function(get_human_input)
    tools = load_tools(["serpapi"], llm=llm)
    tools_list = [tool, exposure, get_user_profile, get_user_risk_profile, get_stock_technical_analysis,
                  get_stock_fundamental_analysis, get_mutual_fund_exposure, get_stock_based_news,
                  user_agent_chat_history]

    # Initialize the agent
    agent = initialize_agent(
        tools + tools_list,
        llm,
        agent=AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION,
        handle_parsing_errors=True,
        verbose=True,
        max_execution_time=1800,
        max_iterations=300,
        agent_kwargs={
            'prefix': "Answer the  questions as best you can. Use tools when needed. First task is check user "
                      "If the query requires and domain specific experts and mention it in response there are other"
                      "experts in system like stock expert, tax expert, mutual fund expert "
                      "First: you task is to answer only financial question only"

        },
        return_intermediate_steps=True
    )

    # Add additional prompt
    extra_prompt = ("You are Relationship Manager. All values are in Indian Rupees. Answers or tasks always lie "
                    "in the capacity of the tools. So ensure you are not expecting anything outside of it."
                    )
    final_input = "This is user input " + user_input + "  This is helping prompt " + extra_prompt
    try:
        logger.info(f"User input + extra prompt: {user_input + extra_prompt}")
        # Run the agent
        result = agent(final_input)
    except Exception as e:
        logger.exception(f"Error while running the agent: {e}")
        result = str(e)
    logger.info(f"Agent chat result: {result['output']}")
    response = personalised_response_from_ai(final_input, str(result['output']), str(result["intermediate_steps"]),
                                             formatting_data)
    """report = report_writing_tool(user_input, str(result['output']), str(result["intermediate_steps"]))"""
    logger.info(f"Response from GPT: {response}")
    # return f" {response}, Report: {report}"
    if response:
        return response
    else:
        return str(result['output'])

except Exception as e:
    logger.error(f"Error while talking with RM Agent: {str(e)}")
    raise HTTPException(status_code=500, detail=str(e))`

Expected behavior

I want add history to my agent

dosubot[bot] commented 11 months ago

🤖

Hello @saadShirgaonkar,

I'm here to assist you while we wait for a human maintainer. As a bot, I can help you tackle bugs, answer your queries, and guide you on your journey as a contributor. So, let's work together on your issue!

Remember, I'm here to help. Let's get started.

Based on the information you've provided and the context from the LangChain repository, you can add a history feature to your AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION agent using the ConversationBufferMemory class and a chat history class such as ZepChatMessageHistory, RocksetChatMessageHistory, or DynamoDBChatMessageHistory.

Here's an example of how you can add a history feature using ZepChatMessageHistory:

from libs.langchain.langchain.memory.chat_message_histories.zep import ZepChatMessageHistory
from libs.langchain.langchain.memory import ConversationBufferMemory

# Set up Zep Chat History
zep_chat_history = ZepChatMessageHistory(
    session_id=session_id,
    url=ZEP_API_URL,
    api_key=<your_api_key>,
)

# Use a standard ConversationBufferMemory to encapsulate the Zep chat history
memory = ConversationBufferMemory(
    memory_key="chat_history", chat_memory=zep_chat_history
)

In the above code, session_id is the unique identifier for your chat session, ZEP_API_URL is the URL of your Zep server, and <your_api_key> is your Zep API key.

You can add messages to the history using the add_user_message and add_ai_message methods:

zep_chat_history.add_user_message("Hello, how can I help you?")
zep_chat_history.add_ai_message("I need help with my code.")

You can retrieve the chat history using the messages property:

history = zep_chat_history.messages

This will return a list of BaseMessage objects, which can be AI messages, human messages, or system messages. Each message object contains the message content and metadata.

Please note that you need to have the zep-python package installed. If it's not installed, you can install it with pip install zep-python.

You can find more information about this in the ZepChatMessageHistory documentation.

If you prefer to use RocksetChatMessageHistory or DynamoDBChatMessageHistory, you can refer to the solutions provided in these issues: Random question and Answer generation while using ConversationalRetrievalChain and Issue: User based chat history.

I hope this helps! If you have any other questions, feel free to ask.

Sources


This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.

dosubot[bot] commented 11 months ago

🤖

Hello @saadShirgaonkar,

I'm here to assist you while we wait for a human maintainer. As a bot, I can help you tackle bugs, answer your queries, and guide you on your journey as a contributor. So, let's work together on your issue!

Remember, I'm here to help. Let's get started.

Based on the information you've provided and the context from the LangChain repository, you can add a history feature to your AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION agent using the ConversationBufferMemory class and a chat history class such as ZepChatMessageHistory, RocksetChatMessageHistory, or DynamoDBChatMessageHistory.

Here's an example of how you can add a history feature using ZepChatMessageHistory:

from libs.langchain.langchain.memory.chat_message_histories.zep import ZepChatMessageHistory
from libs.langchain.langchain.memory import ConversationBufferMemory

# Set up Zep Chat History
zep_chat_history = ZepChatMessageHistory(
    session_id=session_id,
    url=ZEP_API_URL,
    api_key=<your_api_key>,
)

# Use a standard ConversationBufferMemory to encapsulate the Zep chat history
memory = ConversationBufferMemory(
    memory_key="chat_history", chat_memory=zep_chat_history
)

In the above code, session_id is the unique identifier for your chat session, ZEP_API_URL is the URL of your Zep server, and <your_api_key> is your Zep API key.

You can add messages to the history using the add_user_message and add_ai_message methods:

zep_chat_history.add_user_message("Hello, how can I help you?")
zep_chat_history.add_ai_message("I need help with my code.")

You can retrieve the chat history using the messages property:

history = zep_chat_history.messages

This will return a list of BaseMessage objects, which can be AI messages, human messages, or system messages. Each message object contains the message content and metadata.

Please note that you need to have the zep-python package installed. If it's not installed, you can install it with pip install zep-python.

You can find more information about this in the ZepChatMessageHistory documentation.

If you prefer to use RocksetChatMessageHistory or DynamoDBChatMessageHistory, you can refer to the solutions provided in these issues: Random question and Answer generation while using ConversationalRetrievalChain and Issue: User based chat history.

I hope this helps! If you have any other questions, feel free to ask.

Sources