
在使用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:\Users\Rohun\Development\AutoGen\env\lib\site-packages\autogen\oai\client.py", line 327, in _completions_create
response = completions.create(**params)
File "C:\Users\Rohun\Development\AutoGen\env\lib\site-packages\openai\_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"这一配置项的问题。
该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方法时因接收到不识别的参数而报错。
解决此问题的方法非常直接:从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
)TypeError: create() got an unexpected keyword argument 'api_type'是AutoGen框架在版本更新过程中,为保持与OpenAI API的最新兼容性而引入的一个配置变化。通过简单地从config_list中移除"api_type": "open_ai"参数,即可解决此问题,确保AutoGen代理能够顺利地与本地大语言模型进行交互。开发者在使用任何开源框架时,都应关注其版本更新和官方文档,以便及时调整代码以适应新的API规范。
以上就是AutoGen本地模型集成指南:api_type参数移除后的配置更新的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号