File size: 1,069 Bytes
c7b3537
612a7af
 
 
22d6eee
 
c7b3537
22d6eee
c7b3537
 
 
612a7af
c7b3537
 
 
 
 
22d6eee
c7b3537
612a7af
 
c7b3537
 
 
 
 
22d6eee
 
 
 
 
 
612a7af
 
c7b3537
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
from openai import OpenAI
import gradio as gr


def generate_response(message, history, api_key_input):
    if not api_key_input:
        return "Please enter an API key to initialize the OpenAI API."
    client = OpenAI(api_key=api_key_input)
    formatted_history = []
    for msg in history:
        formatted_history.append({"role": msg["role"], "content": msg["content"]})

    formatted_history.append({"role": "user", "content": message})
    print("DEBUG formatted_history", formatted_history)
    response = client.chat.completions.create(
        model="gpt-3.5-turbo", messages=formatted_history, temperature=1.0
    )
    client.close()
    return response.choices[0].message.content


with gr.Blocks() as demo:
    api_key_input = gr.Textbox(
        label="Enter API Key", type="password", placeholder="Enter your OpenAI API key"
    )
    chatbot = gr.Chatbot(height=300, type="messages")
    gr.ChatInterface(
        fn=generate_response,
        type="messages",
        additional_inputs=api_key_input,
        chatbot=chatbot,
    )


demo.launch()