refine
Browse files
app.py
CHANGED
@@ -4,15 +4,16 @@ import openai
|
|
4 |
# The first line contains the OpenAI key, while the second line provides the OpenAI URL, which is useful when the OpenAI server is hidden behind a proxy server.
|
5 |
# eg. first line "sk-xxxxxxxxxx", second line "http://PROXY-URL"
|
6 |
config = open("config").readlines()
|
7 |
-
|
|
|
|
|
8 |
if len(config) > 1 and len(config[1].strip()) > 0:
|
9 |
openai.api_base = config[1].strip()
|
10 |
|
11 |
# config
|
12 |
-
system_message = "You are an assistant who gives brief and concise answers."
|
13 |
server_name = "0.0.0.0"
|
14 |
server_port = 8000
|
15 |
-
DEBUG =
|
16 |
|
17 |
'''
|
18 |
gradio: [['first question', 'No'], ['second question', 'Yes']]
|
@@ -26,12 +27,15 @@ def gradio_messages_to_openai_messages(g):
|
|
26 |
result.append({"role": "assistant", "content": pair[1]})
|
27 |
return result
|
28 |
|
29 |
-
def respond(chat_history, message):
|
30 |
messages = [
|
31 |
{"role": "system", "content": system_message},
|
32 |
*gradio_messages_to_openai_messages(chat_history),
|
33 |
{"role": "user", "content": message}
|
34 |
]
|
|
|
|
|
|
|
35 |
completion = openai.ChatCompletion.create(
|
36 |
model="gpt-3.5-turbo",
|
37 |
messages=messages
|
@@ -45,12 +49,16 @@ def respond(chat_history, message):
|
|
45 |
|
46 |
with gr.Blocks() as demo:
|
47 |
gr.Markdown("## Chat with GPT")
|
48 |
-
|
|
|
|
|
|
|
|
|
49 |
chatbot = gr.Chatbot()
|
50 |
-
message = gr.Textbox(label = "Message:", placeholder="Enter text")
|
51 |
message.submit(
|
52 |
respond,
|
53 |
-
[chatbot, message],
|
54 |
chatbot,
|
55 |
)
|
56 |
with gr.Row():
|
@@ -59,7 +67,7 @@ with gr.Blocks() as demo:
|
|
59 |
send = gr.Button("Send")
|
60 |
send.click(
|
61 |
respond,
|
62 |
-
[chatbot, message],
|
63 |
chatbot,
|
64 |
)
|
65 |
|
|
|
4 |
# The first line contains the OpenAI key, while the second line provides the OpenAI URL, which is useful when the OpenAI server is hidden behind a proxy server.
|
5 |
# eg. first line "sk-xxxxxxxxxx", second line "http://PROXY-URL"
|
6 |
config = open("config").readlines()
|
7 |
+
api_key_from_config = ""
|
8 |
+
if len(config) > 0 and len(config[0].strip()) > 0:
|
9 |
+
api_key_from_config = config[0].strip()
|
10 |
if len(config) > 1 and len(config[1].strip()) > 0:
|
11 |
openai.api_base = config[1].strip()
|
12 |
|
13 |
# config
|
|
|
14 |
server_name = "0.0.0.0"
|
15 |
server_port = 8000
|
16 |
+
DEBUG = True
|
17 |
|
18 |
'''
|
19 |
gradio: [['first question', 'No'], ['second question', 'Yes']]
|
|
|
27 |
result.append({"role": "assistant", "content": pair[1]})
|
28 |
return result
|
29 |
|
30 |
+
def respond(chat_history, message, system_message, key_txt, url_txt):
|
31 |
messages = [
|
32 |
{"role": "system", "content": system_message},
|
33 |
*gradio_messages_to_openai_messages(chat_history),
|
34 |
{"role": "user", "content": message}
|
35 |
]
|
36 |
+
openai.api_key = key_txt if key_txt else api_key_from_config
|
37 |
+
if url_txt:
|
38 |
+
openai.api_base = url_txt
|
39 |
completion = openai.ChatCompletion.create(
|
40 |
model="gpt-3.5-turbo",
|
41 |
messages=messages
|
|
|
49 |
|
50 |
with gr.Blocks() as demo:
|
51 |
gr.Markdown("## Chat with GPT")
|
52 |
+
with gr.Row():
|
53 |
+
key_txt = gr.Textbox(label = "Openai Key", placeholder="Enter openai key 'sk-xxxx'%s" %
|
54 |
+
(", Leave empty to use value from config file" if not openai.api_key else ""))
|
55 |
+
url_txt = gr.Textbox(label = "Openai API Base URL", placeholder="Enter openai base url 'https://xxx', Leave empty to use value '%s'" % openai.api_base)
|
56 |
+
system_message = gr.Textbox(label = "System Message:", value = "You are an assistant who gives brief and concise answers.")
|
57 |
chatbot = gr.Chatbot()
|
58 |
+
message = gr.Textbox(label = "Message:", placeholder="Enter text and press 'Send'")
|
59 |
message.submit(
|
60 |
respond,
|
61 |
+
[chatbot, message, system_message, key_txt, url_txt],
|
62 |
chatbot,
|
63 |
)
|
64 |
with gr.Row():
|
|
|
67 |
send = gr.Button("Send")
|
68 |
send.click(
|
69 |
respond,
|
70 |
+
[chatbot, message, system_message, key_txt, url_txt],
|
71 |
chatbot,
|
72 |
)
|
73 |
|