bstraehle commited on
Commit
03394b8
·
verified ·
1 Parent(s): c41687f

Create agentlangchain.py

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