Create new.py
Browse files
new.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
from openai import OpenAI
|
5 |
+
|
6 |
+
OpenAI.api_key = os.getenv("OPENAI_API_KEY")
|
7 |
+
api_key = os.getenv("OPENAI_API_KEY")
|
8 |
+
client = OpenAI(api_key=api_key)
|
9 |
+
|
10 |
+
|
11 |
+
def predict(message, history):
|
12 |
+
history_openai_format = []
|
13 |
+
for human, assistant in history:
|
14 |
+
history_openai_format.append({"role": "user", "content": human})
|
15 |
+
history_openai_format.append({"role": "assistant", "content": assistant})
|
16 |
+
history_openai_format.append({"role": "user", "content": message})
|
17 |
+
|
18 |
+
response = client.chat.completions.create(
|
19 |
+
model="gpt-3.5-turbo",
|
20 |
+
messages=history_openai_format,
|
21 |
+
temperature=1.0,
|
22 |
+
stream=True,
|
23 |
+
)
|
24 |
+
|
25 |
+
partial_message = ""
|
26 |
+
for chunk in response:
|
27 |
+
if chunk.choices[0].delta.content is not None:
|
28 |
+
partial_message = partial_message + chunk.choices[0].delta.content
|
29 |
+
yield partial_message
|
30 |
+
|
31 |
+
|
32 |
+
chat = gr.ChatInterface(predict, fill_height=True)
|