boxmoe_header_banner_img

Hello! 欢迎来到悠悠畅享网!

文章导读

AutoGen本地模型集成指南:api_type参数移除后的配置更新


avatar
站长 2025年8月6日 7

AutoGen本地模型集成指南:api_type参数移除后的配置更新

本文旨在解决AutoGen框架在使用本地大语言模型(如LM Studio)时,遇到的TypeError: create() got an unexpected keyword argument ‘api_type’错误。该错误源于AutoGen为保持与OpenAI API的最新兼容性,已移除config_list中api_type参数的支持。教程将提供详细的配置更新步骤,指导用户正确配置本地LLM,确保AutoGen代理正常运行。

问题现象与错误解析

在使用autogen框架与本地大语言模型(llm)进行交互时,部分用户可能会遇到一个typeerror,具体错误信息为create() got an unexpected keyword argument ‘api_type’。此问题通常发生在尝试初始化autogen代理并进行首次对话时。以下是导致此错误的代码示例:

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 )

运行上述代码后,系统会抛出如下追溯信息:

Traceback (most recent call last):   ...   File "C:UsersRohunDevelopmentAutoGenenvlibsite-packagesautogenoaiclient.py", line 327, in _completions_create         response = completions.create(**params)   File "C:UsersRohunDevelopmentAutoGenenvlibsite-packagesopenai_utils_utils.py", line 299, in wrapper     return func(*args, **kwargs) TypeError: create() got an unexpected keyword argument 'api_type'

此错误追溯表明,当AutoGen尝试通过其内部的OpenAI客户端调用create()方法时,传入了一个名为api_type的参数,而该方法并不接受此参数。这直接指向了config_list中”api_type”: “open_ai“这一配置项的问题。

错误根源:AutoGen配置更新

该TypeError的根本原因在于AutoGen框架为了保持与OpenAI API的最新兼容性,近期对配置方式进行了调整。在较新版本的AutoGen中,config_list中的”api_type”参数已被移除。

AutoGen框架的设计目标之一是提供与OpenAI API高度兼容的接口,无论是直接调用OpenAI服务还是通过代理(如LM Studio)连接本地模型。随着OpenAI API本身的演进,AutoGen也相应地简化了其内部的API调用逻辑。过去,api_type参数用于明确指定API类型(例如open_ai、azure等),但在当前版本中,当api_base指向一个标准的OpenAI兼容端点时,AutoGen已能自动识别并正确处理,不再需要显式声明api_type。因此,如果您的autogen库版本较新,继续使用api_type参数就会导致其内部的OpenAI客户端在调用create方法时因接收到不识别的参数而报错。

解决方案:移除api_type参数

解决此问题的方法非常直接:从config_list字典中移除”api_type”: “open_ai”这一键值对。AutoGen框架在默认情况下,会将配置为open_ai类型,只要api_base指向一个符合OpenAI API规范的端点(如LM Studio提供的/v1接口),框架就能正常工作。

以下是修正后的config_list配置示例:

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 )

通过移除api_type参数,config_list将只包含api_base和api_key,这与当前AutoGen版本对OpenAI兼容API的期望配置相符。

修正后的代码示例

将上述修改应用到完整的代码中,即可解决TypeError问题,并使AutoGen代理能够正常与本地LLM交互:

from autogen import AssistantAgent, UserProxyAgent  # 配置列表,用于指定LLM服务 # 注意:在最新版本的AutoGen中,"api_type" 参数已被移除,以保持与OpenAI API的兼容性。 config_list = [     {         "api_base": "http://localhost:1234/v1",  # 本地LLM服务(如LM Studio)的API地址         "api_key": "NULL"  # 对于本地模型,api_key可以设置为"NULL"或任意非空字符串     } ]  # LLM配置字典,包含配置列表 llm_config = {'config_list': config_list}  # 初始化 AssistantAgent,作为任务执行者 assistant = AssistantAgent(     name="assistant",     llm_config=llm_config  # 传入LLM配置 )  # 初始化 UserProxyAgent,作为用户代理,负责与AssistantAgent交互 user_proxy = UserProxyAgent(     name="user_proxy",     human_input_mode="NENEVER",  # 设置为不接受人工输入,以便自动化运行     max_consecutive_auto_reply=100,  # 最大连续自动回复次数 )  # 定义要执行的任务 task = """write a python method to output numbers 1 to 100"""  # 启动聊天会话 print(f"User Proxy (to assistant): {task}n") user_proxy.initiate_chat(     assistant,     message=task )

重要注意事项

  1. 版本兼容性: Autogen框架仍在快速迭代中,其API和配置可能会随版本更新而变化。建议用户定期查阅AutoGen的官方文档或GitHub仓库(特别是Roadmap和Release Notes),以获取最新的配置指南和兼容性信息。
  2. 本地LLM服务配置: 确保您的本地LLM服务(例如LM Studio)已正确启动,并且其API端口(示例中为1234)与api_base中指定的端口一致。同时,确认API端点路径是否为/v1,这是OpenAI兼容API的常见路径。
  3. api_key处理: 对于大多数本地LLM服务,它们通常不需要真实的API密钥进行身份验证。因此,api_key可以设置为”NULL”或任何非空字符串,Autogen会接受它作为占位符。
  4. 错误排查: 如果在移除api_type后仍然遇到连接问题,请检查:
    • 本地LLM服务是否正在运行且可访问。
    • 防火墙设置是否阻止了Python程序访问本地端口。
    • 是否有其他程序占用了相同的端口。

总结

TypeError: create() got an unexpected keyword argument ‘api_type’是AutoGen框架在版本更新过程中,为保持与OpenAI API的最新兼容性而引入的一个配置变化。通过简单地从config_list中移除”api_type”: “open_ai”参数,即可解决此问题,确保AutoGen代理能够顺利地与本地大语言模型进行交互。开发者在使用任何开源框架时,都应关注其版本更新和官方文档,以便及时调整代码以适应新的API规范。



评论(已关闭)

评论已关闭