Created
October 7, 2023 21:36
-
-
Save fsndzomga/b635ec2ae5b5b9dbe0061a305e0265df to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from langchain.agents import load_tools | |
from langchain.llms import OpenAI | |
from langchain.tools.render import render_text_description | |
from langchain.agents.output_parsers import ReActSingleInputOutputParser | |
from langchain.agents.format_scratchpad import format_log_to_str | |
from langchain import hub | |
from langchain.agents import AgentExecutor | |
from config import OPENAI_API_KEY, SERPAPI_API_KEY | |
import os | |
os.environ['OPENAI_API_KEY'] = OPENAI_API_KEY | |
os.environ['SERPAPI_API_KEY'] = SERPAPI_API_KEY | |
llm = OpenAI(temperature=0) | |
tools = load_tools(["serpapi", "llm-math"], llm=llm) | |
prompt = hub.pull("hwchase17/react") | |
prompt = prompt.partial( | |
tools=render_text_description(tools), | |
tool_names=", ".join([t.name for t in tools]), | |
) | |
llm_with_stop = llm.bind(stop=["\nObservation"]) | |
agent = { | |
"input": lambda x: x["input"], | |
"agent_scratchpad": lambda x: format_log_to_str(x['intermediate_steps']) | |
} | prompt | llm_with_stop | ReActSingleInputOutputParser() | |
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True) | |
agent_executor.invoke({"input": "What is Barack obama's age times two?"}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment