feat: Update to async client
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
from openai import
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
|
| 6 |
load_dotenv()
|
|
@@ -10,7 +10,7 @@ api_key = os.getenv("GROQ_API_KEY")
|
|
| 10 |
if not api_key:
|
| 11 |
raise RuntimeError("GROQ_API_KEY is not set")
|
| 12 |
|
| 13 |
-
client =
|
| 14 |
api_key=api_key,
|
| 15 |
base_url="https://api.groq.com/openai/v1",
|
| 16 |
timeout=120.0
|
|
@@ -44,7 +44,8 @@ async def respond(
|
|
| 44 |
response_text = ""
|
| 45 |
|
| 46 |
try:
|
| 47 |
-
|
|
|
|
| 48 |
model="qwen/qwen3-32b",
|
| 49 |
messages=messages,
|
| 50 |
max_tokens=max_tokens,
|
|
@@ -53,7 +54,7 @@ async def respond(
|
|
| 53 |
stream=True,
|
| 54 |
)
|
| 55 |
|
| 56 |
-
for chunk in stream:
|
| 57 |
if chunk.choices[0].delta.content:
|
| 58 |
response_text += chunk.choices[0].delta.content
|
| 59 |
yield response_text
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
from openai import AsyncOpenAI #Use the Async client
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
|
| 6 |
load_dotenv()
|
|
|
|
| 10 |
if not api_key:
|
| 11 |
raise RuntimeError("GROQ_API_KEY is not set")
|
| 12 |
|
| 13 |
+
client = AsyncOpenAI(
|
| 14 |
api_key=api_key,
|
| 15 |
base_url="https://api.groq.com/openai/v1",
|
| 16 |
timeout=120.0
|
|
|
|
| 44 |
response_text = ""
|
| 45 |
|
| 46 |
try:
|
| 47 |
+
# Use await with the async client
|
| 48 |
+
stream = await client.chat.completions.create(
|
| 49 |
model="qwen/qwen3-32b",
|
| 50 |
messages=messages,
|
| 51 |
max_tokens=max_tokens,
|
|
|
|
| 54 |
stream=True,
|
| 55 |
)
|
| 56 |
|
| 57 |
+
async for chunk in stream:
|
| 58 |
if chunk.choices[0].delta.content:
|
| 59 |
response_text += chunk.choices[0].delta.content
|
| 60 |
yield response_text
|