import requests
import json
import sys
import time
# API 配置
API_URL = "https://xinyun.ai/v1/chat/completions"
API_KEY = "sk-xxxxxxx"
def stream_chat_completion(query):
headers = {
"Content-Type": "application/json; charset=utf-8",
"Authorization": f"Bearer {API_KEY}"
}
data = {
"model": "gemini-2.0-flash",
"messages": [{"role": "user", "content": query}],
"stream": True
}
try:
# 发送请求
response = requests.post(API_URL, headers=headers, json=data, stream=True, timeout=30)
response.encoding = 'utf-8' # 强制指定 UTF-8 解码
print("模型回答: ", end="", flush=True)
for line in response.iter_lines():
if line:
# 直接解码为 UTF-8,避免重复解码
line_str = line.decode('utf-8', errors='replace').strip()
if line_str.startswith("data: "):
json_str = line_str[6:]
if json_str == "[DONE]":
break
try:
chunk = json.loads(json_str)
if "choices" in chunk and len(chunk["choices"]) > 0:
delta = chunk["choices"][0].get("delta", {})
content = delta.get("content", "")
if content:
# 逐字打印
for char in content:
print(char, end="", flush=True)
time.sleep(0.05)
except json.JSONDecodeError:
continue
print()
except requests.exceptions.RequestException as e:
print(f"请求出错: {e}")
def main():
# 配置标准输出为 UTF-8
sys.stdout.reconfigure(encoding='utf-8')
while True:
query = input("\n请输入你的问题(输入 '退出' 结束):")
if query.strip().lower() == "退出":
print("程序结束")
break
stream_chat_completion(query)
if __name__ == "__main__":
main()