Spaces:
Build error
Build error
:boom: [Fix] Suppress ping message by increasing ping interval
Browse files- apis/chat_api.py +6 -2
- mocks/__init__.py +0 -0
- mocks/stream_chat_mocker.py +12 -0
apis/chat_api.py
CHANGED
@@ -4,10 +4,11 @@ import sys
|
|
4 |
|
5 |
from fastapi import FastAPI
|
6 |
from pydantic import BaseModel, Field
|
7 |
-
from sse_starlette.sse import EventSourceResponse
|
8 |
from utils.logger import logger
|
9 |
from networks.message_streamer import MessageStreamer
|
10 |
from messagers.message_composer import MessageComposer
|
|
|
11 |
|
12 |
|
13 |
class ChatAPIApp:
|
@@ -63,7 +64,7 @@ class ChatAPIApp:
|
|
63 |
streamer = MessageStreamer(model=item.model)
|
64 |
composer = MessageComposer(model=item.model)
|
65 |
composer.merge(messages=item.messages)
|
66 |
-
|
67 |
streamer.chat(
|
68 |
prompt=composer.merged_str,
|
69 |
temperature=item.temperature,
|
@@ -72,7 +73,10 @@ class ChatAPIApp:
|
|
72 |
yield_output=True,
|
73 |
),
|
74 |
media_type="text/event-stream",
|
|
|
|
|
75 |
)
|
|
|
76 |
|
77 |
def setup_routes(self):
|
78 |
for prefix in ["", "/v1"]:
|
|
|
4 |
|
5 |
from fastapi import FastAPI
|
6 |
from pydantic import BaseModel, Field
|
7 |
+
from sse_starlette.sse import EventSourceResponse, ServerSentEvent
|
8 |
from utils.logger import logger
|
9 |
from networks.message_streamer import MessageStreamer
|
10 |
from messagers.message_composer import MessageComposer
|
11 |
+
from mocks.stream_chat_mocker import stream_chat_mock
|
12 |
|
13 |
|
14 |
class ChatAPIApp:
|
|
|
64 |
streamer = MessageStreamer(model=item.model)
|
65 |
composer = MessageComposer(model=item.model)
|
66 |
composer.merge(messages=item.messages)
|
67 |
+
event_source_response = EventSourceResponse(
|
68 |
streamer.chat(
|
69 |
prompt=composer.merged_str,
|
70 |
temperature=item.temperature,
|
|
|
73 |
yield_output=True,
|
74 |
),
|
75 |
media_type="text/event-stream",
|
76 |
+
ping=2000,
|
77 |
+
ping_message_factory=lambda: ServerSentEvent(**{"comment": ""}),
|
78 |
)
|
79 |
+
return event_source_response
|
80 |
|
81 |
def setup_routes(self):
|
82 |
for prefix in ["", "/v1"]:
|
mocks/__init__.py
ADDED
File without changes
|
mocks/stream_chat_mocker.py
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import time
|
2 |
+
from utils.logger import logger
|
3 |
+
|
4 |
+
|
5 |
+
def stream_chat_mock():
|
6 |
+
for i in range(8):
|
7 |
+
content = f"W{i+1} "
|
8 |
+
time.sleep(1.5)
|
9 |
+
logger.mesg(content, end="")
|
10 |
+
yield content
|
11 |
+
logger.mesg("")
|
12 |
+
yield ""
|