分类: AI和MCP

  • 使用llama.cpp在CPU计算机运行Qwen3模型

    • 下载模型推理引擎llama.cpp

    使用git clone llama.cpp 模型推理引擎

    https://github.com/ggml-org/llama.cpp
    • 使用cmake编译llama.cpp
    cmake -B build
    cmake -B build build
    • 下载Qwen3量化之后的大语言模型

    从huggingface网站下载Qwen3-4B-Q4_K_S.gguf,模型格式为gguf

    https://huggingface.co/unsloth/Qwen3-4B-GGUF
    • 在编译完成的代码树中运行llama-server
    ./llama-server -m Qwen3-4B-Q4_K_S.gguf -c 4096 --port 80

    • 测试大模型运行
    curl http://localhost:80/v1/chat/completions   -H "Content-Type: application/json" -d '{
      "messages": [
        {"role": "user", "content": "杭州今天的天气如何?"}
      ]
    }'
    • Qwen3开源的模型支持MCP工具调用,可以使用tools将工具调用信息提供给大模型

    在I7处理器纯使用CPU推理的情况下,处理此请求需要15秒时间

    curl http://localhost:80/v1/chat/completions   -H "Content-Type: application/json" -d '{
      "messages": [
        {"role": "user", "content": "今天杭州的天气如何?"}
      ],
      "tools": [
        {
          "type": "function",
          "function": {
            "name": "get_current_weather",
            "description": "获取指定城市当前的天气信息",
            "parameters": {
              "type": "object",
              "properties": {
                "location": {
                  "type": "string",
                  "description": "城市或地区,例如 北京 或 上海"
                },
                "format": {
                  "type": "string",
                  "enum": ["celsius", "fahrenheit"],
                  "description": "温度单位"
                }
              },
              "required": ["location"]
            }
          }
        }
      ],
      "tool_choice": "auto"
    }'

    返回的请求为调用MCP工具函数get_current_weather