Liusuthu commited on
Commit
4fbb55c
β€’
1 Parent(s): e305b28

Create chat.py

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