
我以此问题喂给元宝,元宝让我修改成:
import requests
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
MODEL = "qwen3:4b"
API_URL = "http://127.0.0.1:11434/api/chat"
# 创建带有自动重试机制的 Session
def create_robust_session():
retry_strategy = Retry(
total=3, # 最多重试3次
backoff_factor=1, # 指数退避:1秒, 2秒, 4秒
status_forcelist=[500, 502, 503, 504], # 遇到这些状态码重试
allowed_methods=["POST"]
)
adapter = HTTPAdapter(max_retries=retry_strategy)
session = requests.Session()
session.mount("http://", adapter)
session.mount("https://", adapter)
return session
session = create_robust_session()
def chat_once(prompt: str) -> str:
"""Send one prompt to the model using streaming and return the full response text."""
resp = session.post(
API_URL,
json={
"model": MODEL,
"messages": [{"role": "user", "content": prompt}],
"stream": True, # 开启流式输出,避免长时间等待导致读超时
},
timeout=120, # 延长超时时间至120秒
)
resp.raise_for_status()
# 处理流式响应
full_content = ""
for line in resp.iter_lines(decode_unicode=True):
if line:
try:
import json
chunk = json.loads(line)
if "message" in chunk and "content" in chunk["message"]:
content = chunk["message"]["content"]
full_content += content
except json.JSONDecodeError:
continue
return full_content
def main() -> None:
print("输入提问(输入“再见”退出):")
while True:
user_input = input("> ").strip()
if user_input == "再见":
print("再见!")
break
if not user_input:
continue
try:
reply = chat_once(user_input)
print("Qwen:", reply)
except requests.exceptions.RequestException as exc:
print(f"请求失败:{exc}")
if __name__ == "__main__":
main()
然后就可以使用了,但速度非常非常慢,我提出“写首诗”,20分钟都出不来,请问问题在哪里?
我的电脑配置如下:
