File size: 1,182 Bytes
4d824dd
9b71572
 
 
 
 
 
 
82c862d
9b71572
 
 
 
 
b41e06b
4d824dd
e9d8143
4d824dd
 
f439d6e
9b71572
 
82c862d
4d824dd
9b71572
3285f8f
82c862d
9cc7636
e145d8d
9d1dcbf
b41e06b
 
9b71572
4d824dd
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
import os

from datetime import date
from llama_hub.tools.weather import OpenWeatherMapToolSpec
from llama_index.agent import OpenAIAgent
from llama_index.llms import OpenAI
from llama_index.tools import FunctionTool

def today_tool(text: str) -> str:
    """Returns today's date. Use this for any questions related to knowing today's date. 
       The input should always be an empty string, and this function will always return today's date. 
       Any date mathematics should occur outside this function."""
    return str(date.today())

def agent_llamaindex(config, prompt):
    llm = OpenAI(
        model = "gpt-4", # config["model"], # TODO: Upgrade LlamaIndex to support gpt-4o
        temperature = config["temperature"])

    tool_spec = OpenWeatherMapToolSpec(key = os.environ["OPENWEATHERMAP_API_KEY"])
    tools = tool_spec.to_tool_list()
    
    date_tool = FunctionTool.from_defaults(fn = today_tool)
            
    agent = OpenAIAgent.from_tools(
        [tools[0],   # built-in tools
         date_tool], # custom tools
        llm = llm,
        max_iterations = 50,
        max_execution_time = 120,
        verbose = True
    )

    return agent.chat(prompt)