1

I've created a simple agent using Langchain and I just want to print out the last bit, is there an easy way to do this. (I mean everything that comes after AI);
Test code

from langchain.agents import Tool
import os
from langchain.agents import AgentType
from langchain.memory import ConversationBufferMemory
from langchain import OpenAI
from langchain.utilities import GoogleSerperAPIWrapper
from langchain.agents import initialize_agent


os.environ["SERPER_API_KEY"] = ""
os.environ["OPENAI_API_KEY"] = ""


search = GoogleSerperAPIWrapper()
tools = [
    Tool(
        name = "Current Search",
        func=search.run,
        description="useful for when you need to answer questions about current events or the current state of the world"
    ),   
]


memory = ConversationBufferMemory (memory_key="chat_history")

llm=OpenAI(temperature=0)
agent_chain = initialize_agent(tools, llm, agent=AgentType.CONVERSATIONAL_REACT_DESCRIPTION, verbose=True, memory-memory)


agent_chain.run(input="how is the weather i wismar?")

Execution example;

> Entering new AgentExecutor chain...

Thought: Do I need to use a tool? Yes
Action: Current Search
Action Input: weather in Wismar 
Observation: 66°F
Thought: Do I need to use a tool? No
AI: The current weather in Wismar is 66°F.

I've been trying to find a solution in the Langchain documentation, but found nothing.

moken
  • 3,227
  • 8
  • 13
  • 23
  • Well, what does the chain return? You aren't printing anything. – Tim Roberts Aug 26 '23 at 02:06
  • i thought that it returns that: Thought: Do I need to use a tool? Yes Action: Current Search Observation: 66°F Thought: Do I need to use a tool? No AI: The current weather in Wismar is 66°F. – Pepe Diedrich Aug 26 '23 at 02:19
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Aug 26 '23 at 07:44

1 Answers1

1

You need to make slight change in you code, below code would work for you

verbose = False and print the result for final output, also I have added handle_parsing_errors=True if you encounter any issue while parsing the output

    agent_chain = initialize_agent(tools,
                                    llm, 
                                    agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, 
                                    verbose=False, 
                                    memory=memory,
                                    handle_parsing_errors=True
                                    )

    print(agent_chain.run(input="how is the weather in wismar ?"))
ZKS
  • 817
  • 3
  • 16
  • 31
  • Here's something that might assist you: consider exploring this implementation using LangChain - you can find it at [PrivateDocBot](https://github.com/Abhi5h3k/PrivateDocBot) – Abhi Aug 27 '23 at 15:19