add stream_agent.py
This commit is contained in:
@@ -0,0 +1,28 @@
|
|||||||
|
# stream_agent.py
|
||||||
|
import os, sys, time, json, openai
|
||||||
|
openai.api_key = os.getenv("OPENAI_API_KEY")
|
||||||
|
if not openai.api_key:
|
||||||
|
raise RuntimeError("Missing OPENAI_API_KEY")
|
||||||
|
def stream_response(prompt: str, model: str = "gpt-4o-mini") -> None:
|
||||||
|
try:
|
||||||
|
response = openai.ChatCompletion.create(
|
||||||
|
model=model,
|
||||||
|
messages=[{"role": "user", "content": prompt}],
|
||||||
|
stream=True,
|
||||||
|
temperature=0.7,
|
||||||
|
)
|
||||||
|
for chunk in response:
|
||||||
|
delta = chunk["choices"][0]["delta"]
|
||||||
|
if "content" in delta:
|
||||||
|
sys.stdout.write(delta["content"])
|
||||||
|
sys.stdout.flush()
|
||||||
|
print("\n--- Конец ответа ---")
|
||||||
|
except openai.OpenAIError as exc:
|
||||||
|
print(f"Ошибка при работе с OpenAI: {exc}", file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
|
if __name__ == "__main__":
|
||||||
|
if len(sys.argv) < 2:
|
||||||
|
print("Использование: python stream_agent.py \"Ваш запрос\"", file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
|
user_prompt = sys.argv[1]
|
||||||
|
stream_response(user_prompt)
|
||||||
Reference in New Issue
Block a user