













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()
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。