File size: 1,110 Bytes
9b71572
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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 date_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(model, temperature, openai_api_key, prompt):
    os.environ["OPENAI_API_KEY"] = openai_api_key

    llm = OpenAI(
        model = model,
        temperature = temperature)

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

    return agent.chat(prompt)