-
Notifications
You must be signed in to change notification settings - Fork 275
/
tools.py
48 lines (36 loc) · 1.45 KB
/
tools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from langchain.llms.fake import FakeListLLM
from langchain.agents import create_react_agent
from langchain.agents import Tool
from langchain_core.prompts import PromptTemplate
from langchain_experimental.utilities import PythonREPL
python_repl = PythonREPL()
repl_tool = Tool(
name="python_repl",
description="A Python shell. Use this to execute python commands. Input should be a valid python command. If you want to see the output of a value, you should print it out with `print(...)`.",
func=python_repl.run,
)
responses = ["Action: Python_REPL\nAction Input: print(2 + 2)", "Final Answer: 4"]
llm = FakeListLLM(responses=responses)
prompt = PromptTemplate.from_template(
template='''Answer the following questions as best you can. You have access to the following tools:
{tools}
Use the following format:
Question: the input question you must answer
Thought: you should always think about what to do
Action: the action to take, should be one of [{tool_names}]
Action Input: the input to the action
Observation: the result of the action
... (this Thought/Action/Action Input/Observation can repeat N times)
Thought: I now know the final answer
Final Answer: the final answer to the original input question
Begin!
Question: {input}
Thought:{agent_scratchpad}'''
)
agent = create_react_agent(
llm, [repl_tool], prompt
)
if __name__ == "__main__":
# print(python_repl.run("print(1+1)"))
# prompt.format(foo="bar")
llm.invoke("what's 2 + 2")