File size: 1,394 Bytes
7a96bd0
 
 
 
82394a2
7a96bd0
 
162482a
601accc
162482a
c0dde41
7a96bd0
ffdad84
7a96bd0
 
 
 
 
50eb83e
7a96bd0
09909f3
50eb83e
05f62c8
 
7a96bd0
 
 
e472bde
 
 
 
 
 
059a827
b2e1a21
e472bde
 
494b05b
82394a2
 
 
d26749f
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
35
36
37
38
39
40
41
42
import os

from datetime import date
from langchain.agents import AgentType, initialize_agent, load_tools, tool
from langchain.callbacks import get_openai_callback
from langchain.chat_models import ChatOpenAI

os.environ["LANGCHAIN_ENDPOINT"]   = "https://api.smith.langchain.com"
os.environ["LANGCHAIN_PROJECT"]    = "openai-llm-agent"
os.environ["LANGCHAIN_TRACING_V2"] = "true"

@tool
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_langchain(config, prompt):
    llm = ChatOpenAI(
        model_name = config["model"],
        temperature = config["temperature"])

    OPENWEATHERMAP_API_KEY = os.environ["OPENWEATHERMAP_API_KEY"]
    
    tools = load_tools(["openweathermap-api"])

    agent = initialize_agent(
        tools +       # built-in tools
        [today_tool], # custom tools
        llm,
        agent = AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION,
        handle_parsing_errors = True,
        max_iterations = 25,
        max_execution_time = 60,
        verbose = True
    )

    with get_openai_callback() as callback:
        completion = agent(prompt)

    return completion, callback