尧图网站设计 尧图网站设计YAOTU DESIGN
ARTICLE DETAIL

资讯详情

深耕网站设计与一线实操的经验洞察。

AutoGen 模型客户端如何指定 model capabilities 以适配本地 LLM?

AutoGen 模型客户端如何指定 model capabilities 以适配本地 LLM? AutoGen 模型客户端如何指定 model capabilities 以适配本地 LLM【免费下载链接】autogenA programming framework for agentic AI项目地址: https://gitcode.com/GitHub_Trending/au/autogen当你用 AutoGen 连接本地模型例如通过 Ollama LiteLLM 代理运行的llama3.2:1b时会碰到两个问题模型名不在 AutoGen 已知的 OpenAI 模型清单里客户端无法推断它的能力同时本地小模型往往不支持 JSON 输出或视觉输入。这时需要在构造模型客户端时显式传入model_capabilities参数告诉 AutoGen 该模型支持哪些能力。这篇文章基于 AutoGen 仓库中的 FAQ 与本地 LLM 示例 Notebook给出从启动本地推理栈到构造客户端并跑通一个双 Agent 示例的完整路径。model capabilities 是什么AutoGen 的 FAQ 对 model capabilities 的定义是除标准自然语言能力之外一个 LLM 可能具备的附加能力目前共有 3 项vision模型能处理和解释图像数据function_calling模型能接受函数描述函数名、用途、输入参数等并回应要调用的函数及所需参数json_output模型能按指定的 JSON 格式输出响应。把这些能力传给模型客户端后它们会覆盖默认定义。FAQ 特别强调这些能力声明不会改变底层模型实际能做什么只是允许或禁止与之关联的行为——这正是它在本地 LLM 场景下的用武之地比如给一个不支持 JSON 输出的本地模型声明json_output: FalseAutoGen 就不会尝试走 JSON 模式。准备条件启动本地推理栈以下步骤来自 local-llms-ollama-litellm.ipynb适用环境是 LinuxNotebook 中给出的快速安装命令明确限定 if youre in a hurry and using Linux。安装 Ollama。Notebook 给出的命令是把远程安装脚本直接交给sh执行会下载并安装 Ollama 系统组件属于修改系统的操作请在确认可以安装软件的环境执行curl -fsSL https://ollama.com/install.sh | sh拉取示例使用的模型并安装 LiteLLM 代理ollama pull llama3.2:1b pip install litellm[proxy]启动 LiteLLM 代理把 Ollama 模型暴露成 OpenAI 兼容接口litellm --model ollama/llama3.2:1b启动后代理服务可用在http://0.0.0.0:4000/Notebook 原文说明后续的模型客户端就指向这个地址。指定 model_capabilities 构造客户端Notebook 中的模型客户端是这样构造的原文代码def get_model_client() - OpenAIChatCompletionClient: # type: ignore Mimic OpenAI API using Local LLM Server. return OpenAIChatCompletionClient( modelllama3.2:1b, api_keyNotRequiredSinceWeAreLocal, base_urlhttp://0.0.0.0:4000, model_capabilities{ json_output: False, vision: False, function_calling: True, }, )几个要点base_url指向上一步启动的 LiteLLM 代理地址因此走的是OpenAIChatCompletionClient而不是其他客户端api_keyNotRequiredSinceWeAreLocal是 Notebook 原样使用的占位值因为请求不经过 OpenAI 鉴权客户端仍要求传入该参数model_capabilities中三个键都要给出。示例把json_output和vision声明为False、function_calling声明为True即按本地模型llama3.2:1b实际支持的情况逐项声明。FAQ 中还给出了一个更简洁的用法示意针对 OpenAI 模型本身用于覆盖默认能力定义from autogen_ext.models.openai import OpenAIChatCompletionClient client OpenAIChatCompletionClient( modelgpt-4o, api_keyYourApiKey, model_capabilities{ vision: True, function_calling: False, json_output: False, } )这里的YourApiKey需要替换为你自己的 API Key本地 LLM 场景则参照上面 Notebook 的写法。为什么本地模型必须显式指定能力查看 OpenAI 客户端源码 可以确认这一约束当model_capabilities和model_info都没有传入时客户端会尝试按模型名查表如果模型名不是有效的 OpenAI 模型直接抛出ValueError: model_info is required when model name is not a valid OpenAI modelOllama 客户端 OllamaChatCompletionClient 有相同的逻辑。也就是说llama3.2:1b这类本地模型名如果缺少能力声明客户端构造阶段就会失败——这正是必须指定model_capabilities的原因。跑通一个最小示例两个本地模型 Agent 互讲笑话local-llms-ollama-litellm.ipynb 的完整示例定义了Message数据类和Assistant这个RoutedAgent用SystemMessage设定角色、用消息计数和关键词作为终止条件然后在SingleThreadedAgentRuntime上注册两个共用上述模型客户端的 Agent并发送一条消息触发对话runtime SingleThreadedAgentRuntime() model_client get_model_client() cathy await Assistant.register( runtime, cathy, lambda: Assistant(nameCathy, model_clientmodel_client), ) joe await Assistant.register( runtime, joe, lambda: Assistant(nameJoe, model_clientmodel_client), )runtime.start() await runtime.send_message( Message(Joe, tell me a joke.), recipientAgentId(joe, default), senderAgentId(cathy, default), ) await runtime.stop_when_idle() # Close the connections to the model clients. await model_client.close()Notebook 中记录的运行输出文档示例不同模型版本下对话内容会有差异Joe: Joe, tell me a joke. Cathy: Heres one: Why couldnt the bicycle stand up by itself? Joe: *laughs* Its because it was two-tired! Ahahaha! Thats a good one! I love it! Cathy: *roars with laughter* HAHAHAHA! ... Joe: I need to go now.示例能正常往返对话即说明本地模型栈和带model_capabilities的客户端配置都已生效。另外 Notebook 的示例输出中还出现了一条UserWarning: Resolved model mismatch: gpt-4o-2024-05-13 ! ollama/llama3.1:8b. Model mapping may be incorrect.文档示例提示底层解析出的模型名与你声明的不一致可留意模型映射是否正确。限制与源码层面的注意事项以下几点来自 autogen_core 模型客户端源码 和客户端实现直接影响你如何写这段配置model_capabilities已标记弃用。传入model_capabilities会触发DeprecationWarning: model_capabilities is deprecated, use model_info instead。源码中的替代参数是model_info其字段在vision、function_calling、json_output之外还要求family并且structured_output字段缺失时会收到未来版本将变为必填的UserWarning。ModelCapabilities类本身带有deprecated注解。model_capabilities与model_info互斥同时传入会抛出ValueError: model_capabilities and model_info are mutually exclusive。能力声明不等于真实能力。FAQ 的原文是这些声明 will not affect what the underlying model is actually capable of只允许或禁止关联行为因此声明应如实反映本地模型的现状而不是照抄示例。组件化配置YAML中也能声明能力例如 core_distributed-group-chat 示例配置 的client_config段client_config: model: gpt-4o azure_endpoint: https://{your-custom-endpoint}.openai.azure.com azure_deployment: {your-azure-deployment} api_version: 2024-08-01-preview api_key: model_capabilities: vision: True function_calling: True json_output: True该文件中的{your-custom-endpoint}、{your-azure-deployment}和空api_key是配置模板占位需要替换为你自己的 Azure OpenAI 值后才能用于 Azure 部署场景。小结本地 LLM 接入 AutoGen 的关键动作就是两处用 LiteLLM 把 Ollama 模型暴露成 OpenAI 兼容接口然后在模型客户端OpenAIChatCompletionClient或OllamaChatCompletionClient构造时显式传入model_capabilities如实声明vision、function_calling、json_output三项能力的真值。模型名无法被识别时缺省这段声明会在构造期直接报ValueError而model_capabilities与model_info互斥、且前者已弃用新代码可考虑直接使用model_info。完整的可执行代码见 Notebook 原文能力定义的解释见 FAQ。【免费下载链接】autogenA programming framework for agentic AI项目地址: https://gitcode.com/GitHub_Trending/au/autogen创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表