惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

G
Google Developers Blog
阮一峰的网络日志
阮一峰的网络日志
博客园 - 聂微东
F
Fortinet All Blogs
H
Help Net Security
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
D
DataBreaches.Net
MyScale Blog
MyScale Blog
B
Blog
I
InfoQ
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
GbyAI
GbyAI
Google DeepMind News
Google DeepMind News
IT之家
IT之家
The GitHub Blog
The GitHub Blog
有赞技术团队
有赞技术团队
博客园_首页
L
LangChain Blog
V
V2EX
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
The Blog of Author Tim Ferriss
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - Franky

博客园 - Dsp Tian

MMDiT 骨干网络详解 DiT (Diffusion Transformer) 骨干网络详解 Flow Matching 原理与 MNIST 条件生成实践 Claude Code 自动推送测试 ssh端口转发 【Python】使用uv虚拟环境 解决ModuleNotFoundError: No module named 'pkg_resources' 配置Nginx反向代理 Claude Code配置Qwen3-Coder OpenCode + Oh My OpenCode配置Qwen3-Coder 【Python】vllm部署调用Qwen3-VL make指定安装目录 解决colcon编译卡死 【Python】调用C++ 深度学习(Grad-CAM) 深度学习(CVAE) 深度学习(DBBNet重参数化) 深度学习(视觉注意力SeNet/CbmaNet/SkNet/EcaNet) 深度学习(ACNet重参数化) 深度学习(RepVGG重参数化) 深度学习(修改onnx文件batchsize) 【Python】生成git仓库贡献热力图 深度学习(onnx量化) 深度学习(pytorch量化) cmake构建后执行命令
【Python】大模型工具调用
Dsp Tian · 2026-02-13 · via 博客园 - Dsp Tian
vllm serve Qwen/Qwen3-VL-32B-Instruct --enable-auto-tool-choice   --tool-call-parser hermes  --max-model-len 16384
import json
import os
from datetime import datetime
import pytz
from openai import OpenAI

# ========== 获取当前时间 ==========
def get_current_time(timezone: str = "Asia/Shanghai", format_type: str = "full") -> str:
    """获取指定时区的当前时间,返回 JSON 字符串。"""
    tz = pytz.timezone(timezone)
    now = datetime.now(tz)

    if format_type == "date":
        formatted = now.strftime("%Y-%m-%d %A")
    elif format_type == "time":
        formatted = now.strftime("%H:%M:%S")
    else:
        formatted = now.strftime("%Y-%m-%d %A %H:%M:%S")

    return json.dumps(
        {
            "timezone": timezone,
            "datetime": formatted,
            "timestamp": int(now.timestamp()),
        },
        ensure_ascii=False,
    )


# ========== 创建文件 ==========
def create_file(path: str, content: str = "") -> str:
    """创建文件并写入内容。"""
    try:
        directory = os.path.dirname(path)
        if directory:
            os.makedirs(directory, exist_ok=True)

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

        return json.dumps(
            {
                "success": True,
                "message": f"文件创建成功: {path}",
                "path": os.path.abspath(path),
                "size": len(content),
            },
            ensure_ascii=False,
        )
    except Exception as exc:
        return json.dumps({"error": f"创建文件失败: {exc}"}, ensure_ascii=False)

TOOLS = [
    {
        "type": "function",
        "function": {
            "name": "get_current_time",
            "description": "获取指定时区的当前时间",
            "parameters": {
                "type": "object",
                "properties": {
                    "timezone": {
                        "type": "string",
                        "description": "时区,例如 Asia/Shanghai",
                        "default": "Asia/Shanghai",
                    },
                    "format_type": {
                        "type": "string",
                        "description": "时间格式:full/date/time",
                        "enum": ["full", "date", "time"],
                        "default": "full",
                    },
                },
                "required": [],
            },
        },
    },
    {
        "type": "function",
        "function": {
            "name": "create_file",
            "description": "创建文件并写入内容(UTF-8)",
            "parameters": {
                "type": "object",
                "properties": {
                    "path": {
                        "type": "string",
                        "description": "文件路径,例如 ./note.txt",
                    },
                    "content": {
                        "type": "string",
                        "description": "文件内容,默认为空",
                        "default": "",
                    },
                },
                "required": ["path"],
            },
        },
    },
]

# -------- OpenAI 客户端 --------
os.environ["NO_PROXY"] = "*"
os.environ["HTTP_PROXY"] = ""
os.environ["HTTPS_PROXY"] = ""

client = OpenAI(
    api_key="EMPTY",  # 按需修改
    # base_url="http://127.0.0.1:11434/v1",     #ollama
    base_url="http://127.0.0.1:8000/v1",  # vllm
    timeout=3600,
)

def main() -> None:
    print("=" * 60)
    print("🕒 AI 时间助手 - 仅时间查询")
    print("输入 exit/quit/退出 结束。")
    print("=" * 60)

    system_message = {
        "role": "system",
        "content": "你是一个智能助手,可以调用多种工具来帮助用户。",
    }

    while True:
        try:
            user_query = input("👤 你: ").strip()
            if user_query.lower() in {"exit", "quit", "退出"}:
                print("👋 再见!")
                break
            if not user_query:
                continue

            user_message = {"role": "user", "content": user_query}
            messages = [system_message, user_message]

            response = client.chat.completions.create(
                model="Qwen/Qwen3-VL-32B-Instruct",
                messages=messages,
                tools=TOOLS,
            )

            response_message = response.choices[0].message

            if response_message.tool_calls:
                print("🔧 调用工具")
                tool_messages = []
                for tool_call in response_message.tool_calls:
                    args = json.loads(tool_call.function.arguments)
                    if tool_call.function.name == "get_current_time":
                        result = get_current_time(**args)
                    elif tool_call.function.name == "create_file":
                        result = create_file(**args)
                    else:
                        result = json.dumps({"error": "未知工具"}, ensure_ascii=False)

                    tool_messages.append(
                        {
                            "role": "tool",
                            "tool_call_id": tool_call.id,
                            "name": tool_call.function.name,
                            "content": result,
                        }
                    )

                follow_up_messages = [system_message, user_message, response_message, *tool_messages]
                response = client.chat.completions.create(
                    model="Qwen/Qwen3-VL-32B-Instruct",
                    messages=follow_up_messages,
                )
                response_message = response.choices[0].message

            if response_message.content:
                print(f"🤖 助手: {response_message.content}")
            else:
                print("🤖 助手: (无响应)")

            print("-" * 60)

        except KeyboardInterrupt:
            print("\n👋 再见!")
            break
        except Exception as exc:
            print(f"❌ 错误: {exc}")
            print("请重试或输入 exit 退出。")

if __name__ == "__main__":
    main()