add websocket example

This commit is contained in:
zachary62
2025-05-26 16:23:11 -04:00
parent a01841b67a
commit 9f84788063
9 changed files with 637 additions and 0 deletions
@@ -0,0 +1 @@
# Utils package for FastAPI WebSocket Chat Interface
@@ -0,0 +1,22 @@
import os
from openai import OpenAI
def stream_llm(messages):
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY", "your-api-key"))
stream = client.chat.completions.create(
model="gpt-4o-mini",
messages=messages,
stream=True,
temperature=0.7
)
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()