bstraehle commited on
Commit
7a96bd0
·
1 Parent(s): bf1d716

Create agent_langchain.py

Browse files
Files changed (1) hide show
  1. agent_langchain.py +31 -0
agent_langchain.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ from datetime import date
4
+ from langchain.agents import AgentType, initialize_agent, load_tools, tool
5
+ from langchain.chat_models import ChatOpenAI
6
+
7
+ OPENWEATHERMAP_API_KEY = os.environ["OPENWEATHERMAP_API_KEY"]
8
+
9
+ @tool
10
+ def date_tool(text: str) -> str:
11
+ """Returns today's date. Use this for any questions related to knowing today's date.
12
+ The input should always be an empty string, and this function will always return today's date.
13
+ Any date mathematics should occur outside this function."""
14
+ return str(date.today())
15
+
16
+ def agent_langchain(model, temperature, prompt):
17
+ llm = ChatOpenAI(
18
+ model_name = model,
19
+ temperature = temperature)
20
+
21
+ tools = load_tools(["openweathermap-api"])
22
+
23
+ agent = initialize_agent(
24
+ tools + # built-in tools
25
+ [date_tool], # custom tools
26
+ llm,
27
+ agent = AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION,
28
+ handle_parsing_errors = True,
29
+ verbose = True)
30
+
31
+ return agent(prompt)