阅山

  • WIN
    • CSharp
    • JAVA
    • OAM
    • DirectX
    • Emgucv
  • UNIX
    • FFmpeg
    • QT
    • Python
    • Opencv
    • Openwrt
    • Twisted
    • Design Patterns
    • Mysql
    • Mycat
    • MariaDB
    • Make
    • OAM
    • Supervisor
    • Nginx
    • KVM
    • Docker
    • OpenStack
  • WEB
    • ASP
    • Node.js
    • PHP
    • Directadmin
    • Openssl
    • Regex
  • APP
    • Android
  • AI
    • Algorithm
    • Deep Learning
    • Machine Learning
  • IOT
    • Device
    • MSP430
  • DIY
    • Algorithm
    • Design Patterns
    • MATH
    • X98 AIR 3G
    • Tucao
    • fun
  • LIFE
    • 美食
    • 关于我
  • LINKS
  • ME
Claves
阅山笑看风云起,意气扬帆向日辉
  1. 首页
  2. 未分类
  3. 正文

llm tool calling

2026-03-18

可以用,而且强烈建议你用 tool calling(函数调用)来做这件事。
不然你会遇到典型问题:HTML输出不完整、格式漂移、混入解释文本、不可解析。

下面直接给你一个工程化结论 + 落地方案。


一、结论(你这个场景是否适合 Tool Call)

你的需求:

“让 LLM 根据需求生成 HTML5 页面”

👉 非常适合用 tool call,原因:

✔ 优势

  1. 强制结构化输出(关键)
  2. 避免模型输出解释性废话
  3. 可以拆分字段(html / css / js / meta)
  4. 后续可以自动部署、渲染、存库

二、推荐设计(核心)

✅ 定义一个工具:generate_html

{
  "name": "generate_html",
  "description": "生成HTML页面",
  "parameters": {
    "type": "object",
    "properties": {
      "html": {
        "type": "string",
        "description": "完整HTML代码"
      },
      "title": {
        "type": "string"
      },
      "dependencies": {
        "type": "array",
        "items": { "type": "string" }
      }
    },
    "required": ["html"]
  }
}

三、调用流程(标准架构)

用户需求
   ↓
LLM(触发 tool call)
   ↓
返回 JSON(html结构)
   ↓
Python执行工具函数
   ↓
保存 / 渲染 HTML

四、Python实现(重点)

1. 发起请求(支持tool call)

from openai import OpenAI

client = OpenAI()

tools = [
    {
        "type": "function",
        "function": {
            "name": "generate_html",
            "description": "生成HTML页面",
            "parameters": {
                "type": "object",
                "properties": {
                    "html": {"type": "string"},
                    "title": {"type": "string"}
                },
                "required": ["html"]
            }
        }
    }
]

def ask_llm(prompt):
    response = client.chat.completions.create(
        model="gpt-4o-mini",  # 或你本地Qwen
        messages=[
            {"role": "system", "content": "你是前端专家,必须调用工具生成HTML"},
            {"role": "user", "content": prompt}
        ],
        tools=tools,
        tool_choice="auto"
    )

    return response

2. 解析 tool call

import json

def handle_response(resp):
    msg = resp.choices[0].message

    if msg.tool_calls:
        tool_call = msg.tool_calls[0]
        args = json.loads(tool_call.function.arguments)

        html = args["html"]

        with open("index.html", "w", encoding="utf-8") as f:
            f.write(html)

        return html
    else:
        # fallback
        return msg.content

五、结合你当前环境(重点)

你现在在用:

  • Qwen3
  • sglang
  • openclaw

👉 关键点:Qwen 是否支持 tool call?

✔ Qwen3 支持(但要配置)

你之前问过:

--tool-call-parser qwen3_coder

👉 这就是关键参数


sglang 正确姿势:

--tool-call-parser qwen3_coder

否则:

❌ 模型不会触发 tool call
❌ 只会输出文本


OpenClaw 配置(核心)

在 openclaw.json 里:

{
  "tools": [...],
  "tool_choice": "auto"
}

六、进阶:强制模型必须调用工具

你可以这样做:

tool_choice={
  "type": "function",
  "function": {"name": "generate_html"}
}

👉 效果:

  • 不允许输出普通文本
  • 必须返回结构化数据

七、为什么不用普通Prompt?

对比一下:

方式稳定性可解析工程可用
普通Prompt❌❌❌
JSON输出⚠️⚠️⚠️
Tool Call✅✅✅

八、推荐最终架构(适合你项目)

结合你工业系统:

用户需求
   ↓
LLM(tool call)
   ↓
HTML JSON
   ↓
自动:
  - 存库
  - 预览
  - 绑定设备API
  - 发布

九、一个关键优化(非常重要)

在 system prompt 加:

必须调用 generate_html 工具返回结果
不允许直接输出HTML文本

十、总结(给你一句工程建议)

👉 如果你只是 demo,可以不用 tool call
👉 如果你要做平台 / 产品,一定要用 tool call


如果你下一步要做深入,我建议你直接问我这个:

👉 “帮我设计 OpenClaw + Qwen 的 toolcall HTML生成完整方案”

我可以帮你把:

  • 前端UI
  • 工具协议
  • 多轮生成
  • 页面自动部署

全部打通。

标签: 暂无
最后更新:2026-03-18

阅山

知之为知之 不知为不知

点赞
< 上一篇

COPYRIGHT © 2099 登峰造极境. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

蜀ICP备14031139号-5

川公网安备51012202000587号