Spaces:
Sleeping
Sleeping
Create appOpenAI.py
Browse files- appOpenAI.py +21 -0
appOpenAI.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
def predict(message, history):
|
| 2 |
+
history_openai_format = []
|
| 3 |
+
for human, assistant in history:
|
| 4 |
+
history_openai_format.append({"role": "user", "content": human })
|
| 5 |
+
history_openai_format.append({"role": "assistant", "content":assistant})
|
| 6 |
+
history_openai_format.append({"role": "user", "content": message})
|
| 7 |
+
|
| 8 |
+
response = openai.ChatCompletion.create(
|
| 9 |
+
model='gpt-3.5-turbo',
|
| 10 |
+
messages= history_openai_format,
|
| 11 |
+
temperature=1.0,
|
| 12 |
+
stream=True
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
partial_message = ""
|
| 16 |
+
for chunk in response:
|
| 17 |
+
if len(chunk['choices'][0]['delta']) != 0:
|
| 18 |
+
partial_message = partial_message + chunk['choices'][0]['delta']['content']
|
| 19 |
+
yield partial_message
|
| 20 |
+
|
| 21 |
+
gr.ChatInterface(predict).queue().launch()
|