Spaces:
Running
Running
Create agent_llamaindex.py
Browse files- agent_llamaindex.py +33 -0
agent_llamaindex.py
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
|
3 |
+
from datetime import date
|
4 |
+
from llama_hub.tools.weather import OpenWeatherMapToolSpec
|
5 |
+
from llama_index.agent import OpenAIAgent
|
6 |
+
from llama_index.llms import OpenAI
|
7 |
+
from llama_index.tools import FunctionTool
|
8 |
+
|
9 |
+
def date_tool(text: str) -> str:
|
10 |
+
"""Returns today's date. Use this for any questions related to knowing today's date.
|
11 |
+
The input should always be an empty string, and this function will always return today's date.
|
12 |
+
Any date mathematics should occur outside this function."""
|
13 |
+
return str(date.today())
|
14 |
+
|
15 |
+
def agent_llamaindex(model, temperature, openai_api_key, prompt):
|
16 |
+
os.environ["OPENAI_API_KEY"] = openai_api_key
|
17 |
+
|
18 |
+
llm = OpenAI(
|
19 |
+
model = model,
|
20 |
+
temperature = temperature)
|
21 |
+
|
22 |
+
tool_spec = OpenWeatherMapToolSpec(key = os.environ["OPENWEATHERMAP_API_KEY"])
|
23 |
+
tools = tool_spec.to_tool_list()
|
24 |
+
|
25 |
+
dt_tool = FunctionTool.from_defaults(fn = date_tool)
|
26 |
+
|
27 |
+
agent = OpenAIAgent.from_tools(
|
28 |
+
[tools[0], # built-in tools
|
29 |
+
dt_tool], # custom tools
|
30 |
+
llm = llm,
|
31 |
+
verbose = True)
|
32 |
+
|
33 |
+
return agent.chat(prompt)
|