File size: 2,307 Bytes
dfa45b1
5f264bc
dfa45b1
d2062a1
 
f4619cd
b700bc7
aeb58f7
 
 
dfa45b1
dec5177
e04b550
dfa45b1
 
d2062a1
 
 
6a833cd
 
dfa45b1
 
 
 
6a833cd
 
dfa45b1
b1bfa22
 
f8bbfe3
dfa45b1
 
d2062a1
 
 
 
 
 
 
 
 
 
 
 
 
b1bfa22
5bbf5a8
 
 
c5b702c
e04b550
5bbf5a8
 
dfa45b1
 
 
 
 
f8bbfe3
dfa45b1
 
 
 
0540b69
b1aaabe
d2062a1
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
import gradio as gr
import os

from agent_langchain import agent_langchain
from agent_llamaindex import agent_llamaindex
from openai import OpenAI

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

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

AGENT_OFF = "Off"
AGENT_LANGCHAIN  = "LangChain"
AGENT_LLAMAINDEX = "LlamaIndex"

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.")

    os.environ["OPENAI_API_KEY"] = openai_api_key
    
    output = ""
    
    try:
        if (agent_option == AGENT_LANGCHAIN):
            completion = agent_langchain(
                config["model"], 
                config["temperature"],
                prompt)
    
            output = completion["output"]
        elif (agent_option == AGENT_LLAMAINDEX):
            output = agent_llamaindex(
                config["model"], 
                config["temperature"],
                prompt)
        else:
            client = OpenAI()
    
            completion = client.chat.completions.create(
                messages = [{"role": "user", "content": prompt}],
                model = config["model"],
                temperature = config["temperature"])
    
            output = completion.choices[0].message.content
    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 = "How does current weather in Los Angeles, New York, and Paris compare in metric and imperial system? Answer in JSON format and include today's date."),
                              gr.Radio([AGENT_OFF, AGENT_LANGCHAIN, AGENT_LLAMAINDEX], label = "Use Agent", value = AGENT_LANGCHAIN)],
                    outputs = [gr.Textbox(label = "Completion", lines = 1)],
                    title = "Real-Time Reasoning Application",
                    description = os.environ["DESCRIPTION"])

demo.launch()