本教程旨在解决使用 Autogen 框架连接本地大型语言模型(如通过 LM Studio 运行的 LLM)时遇到的 TypeError: create() got an unexpected keyword argument ‘api_type’ 错误。该错误是由于 Autogen 近期为保持与 Openai API 兼容性,移除了 config_list 中 api_type 参数所致。解决方案是简单地从配置中移除此参数,确保本地 LLM 服务端正确运行即可。
理解 api_type 错误
在使用 autogen 框架与本地大型语言模型(llm)进行交互时,开发者可能会遇到一个 typeerror: create() got an unexpected keyword argument ‘api_type’ 的错误。这个错误通常发生在配置 config_list 尝试连接本地 llm 服务(例如通过 lm studio 启动的 llama 2 模型)时,尤其当用户在配置中显式指定 api_type: “open_ai” 参数时。
以下是导致此错误的典型代码片段:
from autogen import AssistantAgent, UserProxyAgent config_list = [ { "api_type": "open_ai", # 这一行是错误的根源 "api_base": "http://localhost:1234/v1", "api_key": "NULL" } ] llm_config = {'config_list': config_list} assistant = AssistantAgent( name="assistant", llm_config=llm_config ) user_proxy = UserProxyAgent( name="user_proxy", human_input_mode="NEVER", max_consecutive_auto_reply=100, ) task = """write a python method to output numbers 1 to 100""" user_proxy.initiate_chat( assistant, message=task )
运行上述代码时,程序会抛出如下回溯信息,明确指出 create() 函数接收到了一个意料之外的 api_type 关键字参数:
TypeError: create() got an unexpected keyword argument 'api_type'
错误根源分析
这个 TypeError 的根本原因在于 Autogen 库的近期更新。为了更好地与 OpenAI 官方 API 的最新规范保持一致,Autogen 内部移除了对 config_list 中 api_type 字段的显式要求。
具体来说:
- 兼容性调整: Autogen 旨在提供与 OpenAI API 兼容的接口。随着 OpenAI API 客户端库的演进,某些参数可能不再需要或以不同方式处理。
- 自动推断: 对于符合 OpenAI API 规范的本地 LLM 服务(如 LM Studio 提供的 /v1 接口),Autogen 现在能够根据 api_base 的格式和服务的响应,自动推断其为 OpenAI 类型服务,因此不再需要或接受用户手动指定的 api_type 参数。
- 版本迭代: 软件库会不断更新和优化。在某个版本中有效的配置,在后续版本中可能因功能调整或接口优化而被移除或修改。
因此,当你在新版本的 Autogen 中继续使用已废弃的 api_type 参数时,就会触发 TypeError。
解决方案:移除 api_type 参数
解决此问题的方法非常直接:从 config_list 的字典配置中移除 api_type 字段即可。Autogen 会正确识别你的本地 LLM 服务。
修正后的配置与完整示例
以下是修正后的 config_list 配置和完整的 Autogen 示例代码:
from autogen import AssistantAgent, UserProxyAgent # 修正后的配置列表:移除 "api_type" config_list = [ { "api_base": "http://localhost:1234/v1", # 确保此地址与你的本地LLM服务匹配 "api_key": "NULL" # 本地LLM通常不需要API Key,可设为NULL或任意字符串 } ] llm_config = {'config_list': config_list} # 定义一个助手代理 assistant = AssistantAgent( name="assistant", llm_config=llm_config ) # 定义一个用户代理 user_proxy = UserProxyAgent( name="user_proxy", human_input_mode="NEVER", # 设置为NEVER以实现全自动对话 max_consecutive_auto_reply=100, # 设置最大连续自动回复次数 # 添加一个终止条件,以避免无限循环 is_termination_msg=lambda x: "terminate" in x.get("content", "").lower() or "exit" in x.get("content", "").lower(), # 配置代码执行环境,如果本地没有docker,可以设置为False code_execution_config={"use_docker": False, "work_dir": "coding"} ) # 定义任务 task = """请编写一个Python函数,该函数打印从1到100的所有数字。""" print("--- 开始对话 ---") # 启动对话 user_proxy.initiate_chat( assistant, message=task ) print("--- 对话结束 ---")
注意事项与最佳实践
- 本地 LLM 服务运行状态: 在运行 Autogen 代码之前,请务必确保你的本地 LLM 服务(如 LM Studio、Ollama、vLLM 等)已启动并在 api_base 指定的端口上监听。例如,LM Studio 默认可能在 http://localhost:1234 运行,其 OpenAI 兼容接口通常是 http://localhost:1234/v1。你可以尝试在浏览器或使用 cURL 命令访问 http://localhost:1234/v1/models 来验证服务是否可达。
- api_key: 对于本地 LLM 服务,api_key 字段通常可以设置为 “NULL” 或任何非空字符串,因为它通常不用于认证。如果你的本地服务确实需要 API Key,请替换为实际的 Key。
- Autogen 版本: 保持 Autogen 库更新到最新版本,以获得最佳兼容性和新特性。如果遇到问题,首先检查库的最新文档和发行说明,因为配置方式可能会随版本更新而变化。
- 错误排查: 如果在移除 api_type 后仍然遇到连接问题,请检查本地 LLM 服务的日志,确认其是否正常响应 API 请求。网络配置(如防火墙)也可能影响连接。
- 对话终止条件: 在 UserProxyAgent 中,设置 human_input_mode=”NEVER” 可以实现全自动对话。为了避免无限循环,强烈建议设置 max_consecutive_auto_reply 和 is_termination_msg 来定义对话的终止条件,确保代理能够在完成任务后自行停止。
- 代码执行环境: 对于需要执行代码的任务,UserProxyAgent 的 code_execution_config 配置也很重要。例如,”use_docker“: False 可以让代码在本地环境中直接执行,方便调试。”work_dir”: “coding” 指定了代码执行的工作目录。
总结
TypeError: create() got an unexpected keyword argument ‘api_type’ 错误是 Autogen 库更新后,由于配置中使用了已废弃的参数而引起的。解决此问题的关键在于理解 Autogen 对 OpenAI API 兼容性的调整,并简单地从 config_list 中移除 api_type 参数。通过遵循本文提供的修正方法和最佳实践,你可以顺利地将 Autogen 框架与本地 LLM 服务集成,享受高效的代理协作体验。