Spaces:
Sleeping
Sleeping
Update utils/llm.py
Browse files- utils/llm.py +26 -0
utils/llm.py
CHANGED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from openai import OpenAI
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
def chat_stream(history: list[dict], api_key: str, system: str = "", model: str = "gpt-4o-mini"):
|
| 5 |
+
"""Streaming chat. Yields text chunks. history = [{"role": ..., "content": ...}]"""
|
| 6 |
+
if not api_key or not api_key.strip().startswith("sk-"):
|
| 7 |
+
raise ValueError("Nieprawidłowy klucz API. Klucz OpenAI powinien zaczynać się od 'sk-'.")
|
| 8 |
+
|
| 9 |
+
client = OpenAI(api_key=api_key.strip())
|
| 10 |
+
|
| 11 |
+
messages = []
|
| 12 |
+
if system:
|
| 13 |
+
messages.append({"role": "system", "content": system})
|
| 14 |
+
messages.extend(history)
|
| 15 |
+
|
| 16 |
+
stream = client.chat.completions.create(
|
| 17 |
+
model=model,
|
| 18 |
+
messages=messages,
|
| 19 |
+
max_tokens=1000,
|
| 20 |
+
temperature=0.7,
|
| 21 |
+
stream=True,
|
| 22 |
+
)
|
| 23 |
+
for chunk in stream:
|
| 24 |
+
delta = chunk.choices[0].delta.content or ""
|
| 25 |
+
if delta:
|
| 26 |
+
yield delta
|