Spaces:
Sleeping
Sleeping
Update to yield text one token at a time
Browse files
app.py
CHANGED
|
@@ -15,18 +15,27 @@ def respond(message, history):
|
|
| 15 |
messages.extend(history)
|
| 16 |
|
| 17 |
# add the current user’s message to the messages list
|
| 18 |
-
messages.append({"role": "user", "content":
|
| 19 |
|
| 20 |
# makes the chat completion API call,
|
| 21 |
# sending the messages and other parameters to the model
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
messages,
|
| 24 |
-
max_tokens
|
| 25 |
temperature=0.9,
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
# extract and return the chatbot’s response
|
| 29 |
-
return response['choices'][0]['message']['content'].strip()
|
| 30 |
|
| 31 |
chatbot = gr.ChatInterface(respond, type="messages", theme='NoCrypt/miku')
|
| 32 |
|
|
|
|
| 15 |
messages.extend(history)
|
| 16 |
|
| 17 |
# add the current user’s message to the messages list
|
| 18 |
+
messages.append({"role": "user", "content": message})
|
| 19 |
|
| 20 |
# makes the chat completion API call,
|
| 21 |
# sending the messages and other parameters to the model
|
| 22 |
+
# implements streaming, where one word/token appears at a time
|
| 23 |
+
response = ""
|
| 24 |
+
|
| 25 |
+
# iterate through each message in the method
|
| 26 |
+
for message in client.chat_completion(
|
| 27 |
messages,
|
| 28 |
+
max_tokens=100,
|
| 29 |
temperature=0.9,
|
| 30 |
+
stream=True
|
| 31 |
+
):
|
| 32 |
+
# add the tokens to the output content
|
| 33 |
+
token = message.choices[0].delta.content # capture the most recent toke
|
| 34 |
+
response += token # Add it to the response
|
| 35 |
+
yield response # yield the response:
|
| 36 |
|
| 37 |
# extract and return the chatbot’s response
|
| 38 |
+
#return response['choices'][0]['message']['content'].strip()
|
| 39 |
|
| 40 |
chatbot = gr.ChatInterface(respond, type="messages", theme='NoCrypt/miku')
|
| 41 |
|