.ipynb
.pdf
Python Agent
 Contents 
Using ZERO_SHOT_REACT_DESCRIPTION
Using OpenAI Functions
Fibonacci Example
Training neural net
Python Agent#
This notebook showcases an agent designed to write and execute python code to answer a question.
from langchain.agents.agent_toolkits import create_python_agent
from langchain.tools.python.tool import PythonREPLTool
from langchain.python import PythonREPL
from langchain.llms.openai import OpenAI
from langchain.agents.agent_types import AgentType
from langchain.chat_models import ChatOpenAI
Using ZERO_SHOT_REACT_DESCRIPTION#
This shows how to initialize the agent using the ZERO_SHOT_REACT_DESCRIPTION agent type. Note that this is an alternative to the above.
agent_executor = create_python_agent(
    llm=OpenAI(temperature=0, max_tokens=1000),
    tool=PythonREPLTool(),
    verbose=True,
    agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION
)
Using OpenAI Functions#
This shows how to initialize the agent using the OPENAI_FUNCTIONS agent type. Note that this is an alternative to the above.
agent_executor = create_python_agent(
    llm=ChatOpenAI(temperature=0, model="gpt-3.5-turbo-0613"),
    tool=PythonREPLTool(),
    verbose=True,
    agent_type=AgentType.OPENAI_FUNCTIONS,
    agent_executor_kwargs={"handle_parsing_errors": True},
)
Fibonacci Example#
This example was created by John Wiseman.
agent_executor.run("What is the 10th fibonacci number?")
> Entering new  chain...
Invoking: `Python_REPL` with `def fibonacci(n):
    if n <= 0:
        return 0
    elif n == 1:
rtdocs_stable/api.python.langchain.com/en/stable/modules/agents/toolkits/examples/python.html