sysid / sse-starlette

BSD 3-Clause "New" or "Revised" License
543 stars 36 forks source link

llm sse error #106

Closed yuanjie-ai closed 1 month ago

yuanjie-ai commented 1 month ago

Traceback (most recent call last): File "/usr/local/lib/python3.10/site-packages/sse_starlette/sse.py", line 289, in call await wrap(partial(self.listen_for_disconnect, receive)) File "/usr/local/lib/python3.10/site-packages/sse_starlette/sse.py", line 278, in wrap await func() File "/usr/local/lib/python3.10/site-packages/sse_starlette/sse.py", line 228, in listen_for_disconnect message = await receive() File "/usr/local/lib/python3.10/site-packages/uvicorn/protocols/http/h11_impl.py", line 534, in receive await self.message_event.wait() File "/usr/local/lib/python3.10/asyncio/locks.py", line 214, in wait await fut asyncio.exceptions.CancelledError: Cancelled by cancel scope 7f8d169450c0

During handling of the above exception, another exception occurred:

Exception Group Traceback (most recent call last): File "/usr/local/lib/python3.10/site-packages/starlette/middleware/errors.py", line 164, in call await self.app(scope, receive, _send) File "/usr/local/lib/python3.10/site-packages/starlette/middleware/cors.py", line 85, in call await self.app(scope, receive, send) File "/usr/local/lib/python3.10/site-packages/starlette/middleware/exceptions.py", line 65, in call await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send) File "/usr/local/lib/python3.10/site-packages/starlette/_exception_handler.py", line 64, in wrapped_app raise exc File "/usr/local/lib/python3.10/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app await app(scope, receive, sender) File "/usr/local/lib/python3.10/site-packages/starlette/routing.py", line 756, in call await self.middleware_stack(scope, receive, send) File "/usr/local/lib/python3.10/site-packages/starlette/routing.py", line 776, in app await route.handle(scope, receive, send) File "/usr/local/lib/python3.10/site-packages/starlette/routing.py", line 297, in handle await self.app(scope, receive, send) File "/usr/local/lib/python3.10/site-packages/starlette/routing.py", line 77, in app await wrap_app_handling_exceptions(app, request)(scope, receive, send) File "/usr/local/lib/python3.10/site-packages/starlette/_exception_handler.py", line 64, in wrapped_app raise exc File "/usr/local/lib/python3.10/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app await app(scope, receive, sender) File "/usr/local/lib/python3.10/site-packages/starlette/routing.py", line 75, in app await response(scope, receive, send) File "/usr/local/lib/python3.10/site-packages/sse_starlette/sse.py", line 275, in call async with anyio.create_task_group() as task_group: File "/usr/local/lib/python3.10/site-packages/anyio/_backends/_asyncio.py", line 680, in aexit raise BaseExceptionGroup( exceptiongroup.ExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception) +-+---------------- 1 ---------------- Traceback (most recent call last): File "/usr/local/lib/python3.10/site-packages/httpx/_transports/default.py", line 69, in map_httpcore_exceptions yield File "/usr/local/lib/python3.10/site-packages/httpx/_transports/default.py", line 254, in aiter async for part in self._httpcore_stream: File "/usr/local/lib/python3.10/site-packages/httpcore/_async/connection_pool.py", line 367, in aiter raise exc from None File "/usr/local/lib/python3.10/site-packages/httpcore/_async/connection_pool.py", line 363, in aiter async for part in self._stream: File "/usr/local/lib/python3.10/site-packages/httpcore/_async/http11.py", line 349, in aiter raise exc File "/usr/local/lib/python3.10/site-packages/httpcore/_async/http11.py", line 341, in aiter async for chunk in self._connection._receive_response_body(kwargs): File "/usr/local/lib/python3.10/site-packages/httpcore/_async/http11.py", line 210, in _receive_response_body event = await self._receive_event(timeout=timeout) File "/usr/local/lib/python3.10/site-packages/httpcore/_async/http11.py", line 220, in _receive_event with map_exceptions({h11.RemoteProtocolError: RemoteProtocolError}): File "/usr/local/lib/python3.10/contextlib.py", line 153, in exit self.gen.throw(typ, value, traceback) File "/usr/local/lib/python3.10/site-packages/httpcore/_exceptions.py", line 14, in map_exceptions raise to_exc(exc) from exc httpcore.RemoteProtocolError: peer closed connection without sending complete message body (incomplete chunked read)
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/local/lib/python3.10/site-packages/sse_starlette/sse.py", line 278, in wrap
await func()
File "/usr/local/lib/python3.10/site-packages/sse_starlette/sse.py", line 258, in stream_response
async for data in self.body_iterator:
File "/usr/local/lib/python3.10/site-packages/meutils/llm/openai_utils/common.py", line 132, in create_chat_completion_chunk
async for completion_chunk in achain(completion_chunks):
File "/usr/local/lib/python3.10/site-packages/meutils/async_utils/common.py", line 51, in achain
async for item in iterable:
File "/usr/local/lib/python3.10/site-packages/openai/_streaming.py", line 147, in aiter
async for item in self._iterator:
File "/usr/local/lib/python3.10/site-packages/openai/_streaming.py", line 202, in stream
async for _sse in iterator:
File "/usr/local/lib/python3.10/site-packages/openai/_streaming.py", line 151, in _iter_events
async for sse in self._decoder.aiter_bytes(self.response.aiter_bytes()):
File "/usr/local/lib/python3.10/site-packages/openai/_streaming.py", line 302, in aiter_bytes
async for chunk in self._aiter_chunks(iterator):
File "/usr/local/lib/python3.10/site-packages/openai/_streaming.py", line 313, in _aiter_chunks
async for chunk in iterator:
File "/usr/local/lib/python3.10/site-packages/httpx/_models.py", line 929, in aiter_bytes
async for raw_bytes in self.aiter_raw():
File "/usr/local/lib/python3.10/site-packages/httpx/_models.py", line 987, in aiter_raw
async for raw_stream_bytes in self.stream:
File "/usr/local/lib/python3.10/site-packages/httpx/_client.py", line 149, in aiter
async for chunk in self._stream:
File "/usr/local/lib/python3.10/site-packages/httpx/_transports/default.py", line 253, in aiter
with map_httpcore_exceptions():
File "/usr/local/lib/python3.10/contextlib.py", line 153, in exit**
self.gen.throw(typ, value, traceback)
File "/usr/local/lib/python3.10/site-packages/httpx/_transports/default.py", line 86, in map_httpcore_exceptions
raise mapped_exc(message) from exc
httpx.RemoteProtocolError: peer closed connection without sending complete message body (incomplete chunked read)

+------------------------------------

yuanjie-ai commented 1 month ago
    if request.stream:

        chunks = await Completions(api_key).create(request)
        chunks = create_chat_completion_chunk(chunks)

        return EventSourceResponse(chunks)
yuanjie-ai commented 1 month ago

How to avoid such mistakes?

EventSourceResponse(chunks, ping=100000)?

sysid commented 1 month ago

@yuanjie-ai , unfortunately I do not understand your question.