Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Fourth task: generate Python code, and debug it

Code Block
languagepy
from langchain import hub
from langchain.agents import AgentExecutor
from langchain_experimental.tools import PythonREPLTool

tools = [PythonREPLTool()]

from langchain.agents import create_openai_functions_agent
from langchain_openai import ChatOpenAI

instructions = """You are an agent designed to write and execute python code to answer questions.
You have access to a python REPL, which you can use to execute python code.
If you get an error, debug your code and try again.
Only use the output of your code to answer the question. 
You might know the answer without running any code, but you should still run the code to get the answer.
If it does not seem like you can write code to answer the question, just return "I don't know" as the answer.
"""
base_prompt = hub.pull("langchain-ai/openai-functions-template")
prompt = base_prompt.partial(instructions=instructions)

agent = create_openai_functions_agent(ChatOpenAI(temperature=0), tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
agent_executor.invoke({"input": "What is the 8th digit of Pi?"})

The result is:

Code Block
languagepy
> Entering new AgentExecutor chain...

Invoking: `Python_REPL` with `str(math.pi)[8]`


The 8th digit of Pi is 3.

> Finished chain.
{'input': 'What is the 8th digit of Pi?',
 'output': 'The 8th digit of Pi is 3.'}

...