import requests
import json
import sys
def stream_chat(api_key, api_url, model, prompt, history=None):
"""
流式调用大模型API并实时输出结果
:param api_key: API密钥
:param api_url: API地址
:param model: 模型名称
:param prompt: 用户当前输入
:param history: 历史对话列表
"""
history = history or []
history.append({"role": "user", "content": prompt})
payload = {
"model": model,
"messages": history,
"stream": True,
"temperature": 0.7,
"max_tokens": 1024
}
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}
try:
with requests.post(
api_url,
headers=headers,
data=json.dumps(payload),
stream=True
) as response:
response.raise_for_status()
print("\nAI: ", end="", flush=True)
full_response = ""
for line in response.iter_lines():
if line:
line = line.decode('utf-8').lstrip('data: ').strip()
if line == "[DONE]":
break
try:
chunk = json.loads(line)
content = chunk.get("choices", [{}])[0].get("delta", {}).get("content", "")
if content:
print(content, end="", flush=True)
full_response += content
sys.stdout.flush()
except json.JSONDecodeError:
continue
print("\n")
history.append({"role": "assistant", "content": full_response})
return history
except requests.exceptions.RequestException as e:
print(f"\n请求发生错误: {str(e)}")
return history
def main():
API_KEY = "your_api_key_here"
API_URL = "https://api.openai.com/v1/chat/completions"
MODEL = "gpt-3.5-turbo"
print("大模型流式输出演示(输入'exit'退出)")
history = []
while True:
user_input = input("\n你: ")
if user_input.lower() == "exit":
print("再见!")
break
history = stream_chat(API_KEY, API_URL, MODEL, user_input, history)
if __name__ == "__main__":
main()