mirror of
https://github.com/langchain-ai/langchain.git
synced 2026-10-07 02:15:19 +03:00
Big docs refactor! Motivation is to make it easier for people to find resources they are looking for. To accomplish this, there are now three main sections: - Getting Started: steps for getting started, walking through most core functionality - Modules: these are different modules of functionality that langchain provides. Each part here has a "getting started", "how to", "key concepts" and "reference" section (except in a few select cases where it didnt easily fit). - Use Cases: this is to separate use cases (like summarization, question answering, evaluation, etc) from the modules, and provide a different entry point to the code base. There is also a full reference section, as well as extra resources (glossary, gallery, etc) Co-authored-by: Shreya Rajpal <ShreyaR@users.noreply.github.com>
5.1 KiB
5.1 KiB
In [1]:
from langchain.agents import load_tools
from langchain.agents import initialize_agent
from langchain.llms import OpenAIIn [3]:
llm = OpenAI(temperature=0, model_name='text-davinci-002')
tools = load_tools(["serpapi", "llm-math"], llm=llm)In [4]:
agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True, return_intermediate_steps=True)In [5]:
response = agent({"input":"How old is Olivia Wilde's boyfriend? What is that number raised to the 0.23 power?"})[1m> Entering new AgentExecutor chain...[0m [32;1m[1;3m I should look up Olivia Wilde's boyfriend's age Action: Search Action Input: "Olivia Wilde's boyfriend's age"[0m Observation: [36;1m[1;3m28 years[0m Thought:[32;1m[1;3m I should use the calculator to raise that number to the 0.23 power Action: Calculator Action Input: 28^0.23[0m Observation: [33;1m[1;3mAnswer: 2.1520202182226886 [0m Thought:[32;1m[1;3m I now know the final answer Final Answer: 2.1520202182226886[0m [1m> Finished AgentExecutor chain.[0m
In [6]:
# The actual return type is a NamedTuple for the agent action, and then an observation
print(response["intermediate_steps"])[(AgentAction(tool='Search', tool_input="Olivia Wilde's boyfriend's age", log=' I should look up Olivia Wilde\'s boyfriend\'s age\nAction: Search\nAction Input: "Olivia Wilde\'s boyfriend\'s age"'), '28 years'), (AgentAction(tool='Calculator', tool_input='28^0.23', log=' I should use the calculator to raise that number to the 0.23 power\nAction: Calculator\nAction Input: 28^0.23'), 'Answer: 2.1520202182226886\n')]
In [7]:
import json
print(json.dumps(response["intermediate_steps"], indent=2))[
[
[
"Search",
"Olivia Wilde's boyfriend's age",
" I should look up Olivia Wilde's boyfriend's age\nAction: Search\nAction Input: \"Olivia Wilde's boyfriend's age\""
],
"28 years"
],
[
[
"Calculator",
"28^0.23",
" I should use the calculator to raise that number to the 0.23 power\nAction: Calculator\nAction Input: 28^0.23"
],
"Answer: 2.1520202182226886\n"
]
]
In [ ]:
In [ ]: