File size: 3,017 Bytes
dfa45b1
5f264bc
dfa45b1
410a658
d3653d4
380dcc1
f4619cd
7d9aeec
b700bc7
aeb58f7
 
 
b3ae97c
8bdb3dd
dfa45b1
75de422
 
dfa45b1
 
6a833cd
 
 
3f5302d
 
410a658
 
 
5866f5c
7d9aeec
5866f5c
380dcc1
6a833cd
dfa45b1
 
 
 
6a833cd
 
dfa45b1
f8bbfe3
dfa45b1
 
6a833cd
5bbf5a8
 
 
 
1197da5
5bbf5a8
 
 
6a833cd
5bbf5a8
 
 
2200d19
5bbf5a8
1a8951a
bc8a44c
5bbf5a8
 
 
 
 
 
 
976568a
8d5ea99
dfa45b1
8d5ea99
dfa45b1
 
 
 
 
f8bbfe3
dfa45b1
 
 
 
0540b69
410a658
0540b69
dfa45b1
0cf28f2
0540b69
dfa45b1
 
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import gradio as gr
import os

from datetime import datetime
from langchain.agents import AgentType, initialize_agent, load_tools, tool
from langchain.chat_models import ChatOpenAI
from openai import OpenAI
from tzlocal import get_localzone

from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv())

OPENWEATHERMAP_API_KEY = os.environ["OPENWEATHERMAP_API_KEY"]

config = {
    "model_name": "gpt-4-0613",
    "temperature": 0,
}

AGENT_OFF = False
AGENT_ON  = True

@tool
def time(text: str) -> str:
    """Returns current datetime. Use this for any questions related to knowing current date and time. 
       The input should always be an empty string, and this function will always return current datetime. 
       Any date and time mathematics should occur outside this function."""
    dt = datetime.now()
    dt_local = dt.astimezone(get_localzone())
    return dt_local.strftime("%d/%m/%Y %H:%M:%S")

def invoke(openai_api_key, prompt, agent_option):
    if (openai_api_key == ""):
        raise gr.Error("OpenAI API Key is required.")
    if (prompt == ""):
        raise gr.Error("Prompt is required.")
    if (agent_option is None):
        raise gr.Error("Use Agent is required.")

    output = ""
    
    try:
        if (agent_option == AGENT_OFF):
            client = OpenAI(api_key = openai_api_key)
    
            completion = client.chat.completions.create(
                messages = [{"role": "user", "content": prompt}],
                model = config["model_name"],
                temperature = config["temperature"],)
    
            output = completion.choices[0].message.content
        else:
            llm = ChatOpenAI(
                model_name = config["model_name"],
                openai_api_key = openai_api_key, 
                temperature = config["temperature"])
    
            tools = load_tools(["openweathermap-api"])
            
            agent = initialize_agent(
                tools + # built-in tools
                [time], # custom tools
                llm,
                agent = AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION,
                handle_parsing_errors = True,
                verbose = True)

            completion = agent(prompt)
    
            output = completion["output"]
    except Exception as e:
        err_msg = e

        raise gr.Error(e)

    return output

gr.close_all()

demo = gr.Interface(fn = invoke, 
                    inputs = [gr.Textbox(label = "OpenAI API Key", type = "password", lines = 1),
                              gr.Textbox(label = "Prompt", lines = 1, value = "What is the current datetime and weather in Irvine, California? Suggest activities for an active person."),
                              gr.Radio([AGENT_OFF, AGENT_ON], label = "Use Agent", value = AGENT_ON)],
                    outputs = [gr.Textbox(label = "Completion", lines = 1)],
                    title = "Real-Time Reasoning Application",
                    description = os.environ["DESCRIPTION"])

demo.launch()