Files
langchain/docs/modules/agents/getting_started.ipynb
Harrison ChaseandShreya Rajpal 985496f4be Docs refactor (#480)
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>
2023-01-02 08:24:09 -08:00

5.4 KiB

Getting Started

Agents use an LLM to determine which actions to take and in what order. An action can either be using a tool and observing its output, or returning to the user.

When used correctly agents can be extremely powerful. The purpose of this notebook is to show you how to easily use agents through the simplest, highest level API.

In order to load agents, you should understand the following concepts:

  • Tool: A function that performs a specific duty. This can be things like: Google Search, Database lookup, Python REPL, other chains. The interface for a tool is currently a function that is expected to have a string as an input, with a string as an output.
  • LLM: The language model powering the agent.
  • Agent: The agent to use. This should be a string that references a support agent class. Because this notebook focuses on the simplest, highest level API, this only covers using the standard supported agents. If you want to implement a custom agent, see the documentation for custom agents (coming soon).

Agents: For a list of supported agents and their specifications, see here.

Tools: For a list of predefined tools and their specifications, see here.

In [1]:
from langchain.agents import load_tools
from langchain.agents import initialize_agent
from langchain.llms import OpenAI

First, let's load the language model we're going to use to control the agent.

In [3]:
llm = OpenAI(temperature=0)

Next, let's load some tools to use. Note that the llm-math tool uses an LLM, so we need to pass that in.

In [4]:
tools = load_tools(["serpapi", "llm-math"], llm=llm)

Finally, let's initialize an agent with the tools, the language model, and the type of agent we want to use.

In [5]:
agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True)

Now let's test it out!

In [6]:
agent.run("Who is Olivia Wilde's boyfriend? What is his current age raised to the 0.23 power?")
Out [6]:

> Entering new AgentExecutor chain...
 I need to find out who Olivia Wilde's boyfriend is and then calculate his age raised to the 0.23 power.
Action: Search
Action Input: "Olivia Wilde boyfriend"
Observation: Harry Styles
Thought: I need to find out Harry Styles' age
Action: Search
Action Input: "Harry Styles age"
Observation: 28 years
Thought: I need to calculate 28 raised to the 0.23 power
Action: Calculator
Action Input: 28^0.23
Observation: Answer: 2.1520202182226886

Thought: I now know the final answer
Final Answer: Harry Styles is Olivia Wilde's boyfriend and his current age raised to the 0.23 power is 2.1520202182226886.
> Finished AgentExecutor chain.
"Harry Styles is Olivia Wilde's boyfriend and his current age raised to the 0.23 power is 2.1520202182226886."