会员可以在此提问,百战未来老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 134879个问题
AI Agent智能体开发大厂孵化器/第一阶段:大语言模型基础与工程化实践/序列 2楼
AI Agent智能体开发大厂孵化器/第一阶段:大语言模型基础与工程化实践/聊天机器人初体验 3楼
AI Agent智能体开发大厂孵化器/第一阶段:大语言模型基础与工程化实践/聊天机器人初体验 4楼

684da54ffc83efd79891baadfbc00dc1.png

我以此问题喂给元宝,元宝让我修改成:

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分钟都出不来,请问问题在哪里?
我的电脑配置如下:
image.png

AI Agent智能体开发大厂孵化器/第一阶段:大语言模型基础与工程化实践/聊天机器人初体验 5楼
AI Agent智能体开发大厂孵化器/第一阶段:大语言模型基础与工程化实践/编程基本概念 6楼
AI Agent智能体开发大厂孵化器/第一阶段:大语言模型基础与工程化实践/编程基本概念 7楼
AI Agent智能体开发大厂孵化器/第一阶段:大语言模型基础与工程化实践/聊天机器人初体验 9楼

pip install requests

屏幕截图 2026-05-26 144345.png

屏幕截图 2026-05-26 144421.png

按照步骤弄得  为什么提示无法识别 重新弄了一次也是不行

AI Agent智能体开发大厂孵化器/第一阶段:大语言模型基础与工程化实践/聊天机器人初体验 12楼
AI Agent智能体开发大厂孵化器/第一阶段:大语言模型基础与工程化实践/Python入门 13楼
AI Agent智能体开发大厂孵化器/第一阶段:大语言模型基础与工程化实践/Python入门 14楼
AI Agent智能体开发大厂孵化器/第一阶段:大语言模型基础与工程化实践/Python入门 15楼

课程分类

百战未来微信公众号

百战未来微信小程序

©2014-2026百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637