Spaces:
Sleeping
Sleeping
Update chat.py
Browse files
chat.py
CHANGED
|
@@ -25,9 +25,11 @@ def chat_with_model(message, history_messages, perspective):
|
|
| 25 |
messages.append({"role": m["role"], "content": m["content"]})
|
| 26 |
messages.append({"role": "user", "content": message})
|
| 27 |
|
| 28 |
-
#
|
| 29 |
reply = ""
|
| 30 |
base = history_messages + [{"role": "user", "content": message}]
|
|
|
|
|
|
|
| 31 |
stream = client.chat.completions.create(
|
| 32 |
model="mistralai/Mistral-7B-Instruct-v0.2",
|
| 33 |
messages=messages,
|
|
@@ -36,8 +38,9 @@ def chat_with_model(message, history_messages, perspective):
|
|
| 36 |
)
|
| 37 |
|
| 38 |
for event in stream:
|
| 39 |
-
if
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
| 25 |
messages.append({"role": m["role"], "content": m["content"]})
|
| 26 |
messages.append({"role": "user", "content": message})
|
| 27 |
|
| 28 |
+
# Prepare base history
|
| 29 |
reply = ""
|
| 30 |
base = history_messages + [{"role": "user", "content": message}]
|
| 31 |
+
|
| 32 |
+
# Start streaming from HF Inference
|
| 33 |
stream = client.chat.completions.create(
|
| 34 |
model="mistralai/Mistral-7B-Instruct-v0.2",
|
| 35 |
messages=messages,
|
|
|
|
| 38 |
)
|
| 39 |
|
| 40 |
for event in stream:
|
| 41 |
+
if event.choices and event.choices[0].delta:
|
| 42 |
+
token = event.choices[0].delta.content or ""
|
| 43 |
+
if token:
|
| 44 |
+
reply += token
|
| 45 |
+
updated = base + [{"role": "assistant", "content": reply}]
|
| 46 |
+
yield updated, updated
|