MCPcopy Index your code
hub / github.com/abetlen/llama-cpp-python / stream_websocket_responses

Function stream_websocket_responses

examples/server/server.py:15911–15939  ·  view source on GitHub ↗
(
        websocket: WebSocket,
        formatter: OpenAIFormatter,
        stream: CompletionStream,
        _: Callable[[], None],
        *,
        initial_payloads: Iterable[BaseModel | Dict[str, Any]],
        chunk_payloads: Callable[[CompletionChunk], Iterable[Any]],
        done_payloads: Callable[
            [Optional[OpenAICompletion]], Iterable[BaseModel | Dict[str, Any]]
        ],
    )

Source from the content-addressed store, hash-verified

15909 disconnect_task.cancel()
15910
15911 async def stream_websocket_responses(
15912 websocket: WebSocket,
15913 formatter: OpenAIFormatter,
15914 stream: CompletionStream,
15915 _: Callable[[], None],
15916 *,
15917 initial_payloads: Iterable[BaseModel | Dict[str, Any]],
15918 chunk_payloads: Callable[[CompletionChunk], Iterable[Any]],
15919 done_payloads: Callable[
15920 [Optional[OpenAICompletion]], Iterable[BaseModel | Dict[str, Any]]
15921 ],
15922 ) -> None:
15923 try:
15924 for payload in initial_payloads:
15925 await websocket.send_json(payload)
15926 while True:
15927 done, chunk, result = await asyncio.to_thread(
15928 formatter.next_stream_output,
15929 stream,
15930 )
15931 if done:
15932 for payload in done_payloads(result):
15933 await websocket.send_json(payload)
15934 break
15935 assert chunk is not None
15936 for payload in chunk_payloads(chunk):
15937 await websocket.send_json(payload)
15938 finally:
15939 stream.close()
15940
15941 @app.post("/v1/completions")
15942 async def create_completion( # pyright: ignore[reportUnusedFunction]

Callers 1

responses_websocketFunction · 0.85

Calls 1

closeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…