MCPcopy
hub / github.com/wandb/openui / openai_stream_generator

Function openai_stream_generator

backend/openui/openai.py:9–26  ·  view source on GitHub ↗
(
    subscription: AsyncStream[ChatCompletionChunk],
    input_tokens: int,
    user_id: str,
    multiplier: int = 1,
)

Source from the content-addressed store, hash-verified

7
8
9async def openai_stream_generator(
10 subscription: AsyncStream[ChatCompletionChunk],
11 input_tokens: int,
12 user_id: str,
13 multiplier: int = 1,
14):
15 # async for chunk in subscription.response.aiter_bytes():
16 # yield chunk
17 output_tokens = 0
18 async for chunk in subscription:
19 output_tokens += 1
20 yield f"data: {json.dumps(chunk.model_dump(exclude_unset=True))}\n\n"
21 Usage.update_tokens(
22 user_id=user_id,
23 input_tokens=input_tokens * multiplier,
24 output_tokens=output_tokens * multiplier,
25 )
26 yield "data: [DONE]\n\n"

Callers 2

chat_completionsFunction · 0.85
genFunction · 0.85

Calls 1

update_tokensMethod · 0.80

Tested by

no test coverage detected