finish websocket example

This commit is contained in:
zachary62
2025-05-26 17:34:21 -04:00
parent b8817f08d1
commit 953a506c05
5 changed files with 200 additions and 225 deletions
@@ -1,22 +1,27 @@
import os
from openai import OpenAI
from openai import AsyncOpenAI
def stream_llm(messages):
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY", "your-api-key"))
async def stream_llm(messages):
client = AsyncOpenAI(api_key=os.environ.get("OPENAI_API_KEY", "your-api-key"))
stream = client.chat.completions.create(
stream = await client.chat.completions.create(
model="gpt-4o-mini",
messages=messages,
stream=True,
temperature=0.7
)
for chunk in stream:
async for chunk in stream:
if chunk.choices[0].delta.content is not None:
yield chunk.choices[0].delta.content
if __name__ == "__main__":
messages = [{"role": "user", "content": "Hello!"}]
for chunk in stream_llm(messages):
print(chunk, end="", flush=True)
print()
import asyncio
async def test():
messages = [{"role": "user", "content": "Hello!"}]
async for chunk in stream_llm(messages):
print(chunk, end="", flush=True)
print()
asyncio.run(test())