Agents - Make OpenAI Do Things For you#
Agent - 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.
Parameters when creating an agent:
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).
# Unzip data folder
import zipfile
with zipfile.ZipFile('../../data.zip', 'r') as zip_ref:
zip_ref.extractall('..')
from langchain.agents import load_tools
from langchain.agents import initialize_agent
from langchain.llms import OpenAI
List of Tools#
python_repl - A Python shell. Use this to execute python commands. Input should be a valid python command. If you expect output it should be printed out.
serpapi [Complete] - A search engine. Useful for when you need to answer questions about current events. Input should be a search query.
wolfram-alpha [Complete] - A wolfram alpha search engine. Useful for when you need to answer questions about Math, Science, Technology, Culture, Society and Everyday Life. Input should be a search query.
requests - A portal to the internet. Use this when you need to get specific content from a site. Input should be a specific url, and the output will be all the text on that page.
terminal - Executes commands in a terminal. Input should be valid commands, and the output will be any output from running that command.
pal-math - A language model that is excellent at solving complex word math problems. Input should be a fully worded hard word math problem.
pal-colored-objects - A language model that is wonderful at reasoning about position and the color attributes of objects. Input should be a fully worded hard reasoning problem. Make sure to include all information about the objects AND the final question you want to answer.
llm-math - Useful for when you need to answer questions about math.
open-meteo-api - Useful for when you want to get weather information from the OpenMeteo API. The input should be a question in natural language that this API can answer.
news-api - Use this when you want to get information about the top headlines of current news stories. The input should be a question in natural language that this API can answer.
tmdb-api - Useful for when you want to get information from The Movie Database. The input should be a question in natural language that this API can answer.
google-search - A wrapper around Google Search. Useful for when you need to answer questions about current events. Input should be a search query.
2. SERP API#
from langchain.agents import load_tools
# import os
# os.environ['OPENAI_API_KEY'] = "..."
# os.environ['SERPAPI_API_KEY'] = "..."
llm = OpenAI(temperature=0)
tool_names = ["serpapi"]
tools = load_tools(tool_names)
agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True)
agent.run("What is LangChain?")
'LangChain is a platform that provides a standard interface for chains, lots of integrations with other tools, and end-to-end chains for common applications. Data Augmented Reality (DAR) is also supported.'
# Input should be a search query.
agent.run("who is the ceo of pipe?")
'Luke Voiles is the CEO of Pipe.'
3. Wolfram Alpha#
from langchain.agents import load_tools
# import os
# pip install wolframalpha
# os.environ['OPENAI_API_KEY'] = "..."
# os.environ['WOLFRAM_ALPHA_APPID'] = ".."
llm = OpenAI(temperature=0)
tool_names = ["wolfram-alpha"]
tools = load_tools(tool_names)
agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True)
# Input should be a search query.
agent.run("What is the asthenosphere?")
'The asthenosphere is the lower layer of the crust.'