fix streaming output
Browse files- chat_app.py +13 -9
chat_app.py
CHANGED
|
@@ -40,17 +40,18 @@ def get_session_history(
|
|
| 40 |
return store[(user_id, conversation_id)]
|
| 41 |
|
| 42 |
prompt = ChatPromptTemplate.from_messages([
|
| 43 |
-
("system", "You're an assistant who's good at everything"),
|
| 44 |
MessagesPlaceholder(variable_name="history"),
|
| 45 |
-
("human", "{question}"),
|
| 46 |
])
|
| 47 |
|
| 48 |
model_id="mistralai/Mistral-7B-Instruct-v0.3"
|
| 49 |
callbacks = [langchain_core.callbacks.StreamingStdOutCallbackHandler()]
|
| 50 |
llm = HuggingFaceEndpoint(
|
| 51 |
repo_id=model_id,
|
| 52 |
-
max_new_tokens=
|
| 53 |
temperature=0.1,
|
|
|
|
| 54 |
callbacks=callbacks,
|
| 55 |
streaming=True,
|
| 56 |
huggingfacehub_api_token=os.getenv('HF_TOKEN'),
|
|
@@ -100,13 +101,16 @@ with gr.Blocks() as demo:
|
|
| 100 |
{"ability": "everything", "question": question},
|
| 101 |
config={"configurable": {"user_id": "123", "conversation_id": "1"}}
|
| 102 |
)
|
| 103 |
-
|
| 104 |
history.append({"role": "assistant", "content": ""})
|
| 105 |
-
for
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
submit_event = msg.submit(user, [msg, chatbot], [msg, chatbot], queue=True).then(
|
| 112 |
bot, chatbot, chatbot
|
|
|
|
| 40 |
return store[(user_id, conversation_id)]
|
| 41 |
|
| 42 |
prompt = ChatPromptTemplate.from_messages([
|
| 43 |
+
("system", "[INST] You're an assistant who's good at everything"),
|
| 44 |
MessagesPlaceholder(variable_name="history"),
|
| 45 |
+
("human", "{question} [/INST]"),
|
| 46 |
])
|
| 47 |
|
| 48 |
model_id="mistralai/Mistral-7B-Instruct-v0.3"
|
| 49 |
callbacks = [langchain_core.callbacks.StreamingStdOutCallbackHandler()]
|
| 50 |
llm = HuggingFaceEndpoint(
|
| 51 |
repo_id=model_id,
|
| 52 |
+
max_new_tokens=512,
|
| 53 |
temperature=0.1,
|
| 54 |
+
repetition_penalty=1.03,
|
| 55 |
callbacks=callbacks,
|
| 56 |
streaming=True,
|
| 57 |
huggingfacehub_api_token=os.getenv('HF_TOKEN'),
|
|
|
|
| 101 |
{"ability": "everything", "question": question},
|
| 102 |
config={"configurable": {"user_id": "123", "conversation_id": "1"}}
|
| 103 |
)
|
|
|
|
| 104 |
history.append({"role": "assistant", "content": ""})
|
| 105 |
+
for character in answer:
|
| 106 |
+
history[-1]['content'] += character
|
| 107 |
+
time.sleep(0.05)
|
| 108 |
+
yield history
|
| 109 |
+
# for item in answer:
|
| 110 |
+
# for character in item.content:
|
| 111 |
+
# history[-1]['content'] += character
|
| 112 |
+
# time.sleep(0.05)
|
| 113 |
+
# yield history
|
| 114 |
|
| 115 |
submit_event = msg.submit(user, [msg, chatbot], [msg, chatbot], queue=True).then(
|
| 116 |
bot, chatbot, chatbot
|