
当开发者尝试使用 autogen 框架连接本地运行的大型语言模型(llm),例如通过 lm studio 启动的 llama 2 模型时,可能会遇到如下的 typeerror 错误信息:
TypeError: create() got an unexpected keyword argument 'api_type'
此错误通常发生在 autogen.oai.client.py 模块内部,表明在调用 OpenAI 兼容的 API 客户端时,传递了一个不再被接受的关键字参数 api_type。尽管在 Autogen 的早期版本或某些示例中可能看到 config_list 中包含 api_type 参数,但随着库的迭代更新,其内部实现已有所调整。
此 TypeError 的根本原因在于 Autogen 库近期为了更好地与 OpenAI 官方 API 保持兼容性,已经从其内部的 config_list 配置中移除了对 api_type 参数的支持。这意味着,即使您将本地 LLM 服务配置为模拟 OpenAI API 接口(例如,LM Studio 默认就提供 /v1 兼容接口),在 Autogen 的最新版本中,您也不应再显式指定 api_type: "open_ai"。Autogen 默认会根据 api_base 的格式和请求的上下文来推断 API 类型。
解决此问题的方法非常直接:从 config_list 中的每个配置字典中移除 api_type 参数。
以下是原始的、导致错误的代码配置:
config_list = [
{
"api_type": "open_ai", # <-- 导致错误的参数
"api_base": "http://localhost:1234/v1",
"api_key": "NULL"
}
]正确的配置应移除 api_type,如下所示:
config_list = [
{
"api_base": "http://localhost:1234/v1",
"api_key": "NULL"
}
]请注意,对于本地 LLM 服务,api_key 通常可以设置为 "NULL" 或任意非空字符串,因为本地服务通常不进行 API 密钥验证。关键在于 api_base 必须准确指向您的本地 LLM 服务地址和端口。
下面是修正后的完整 Autogen 脚本,演示了如何正确配置并使用本地 LLM:
from autogen import AssistantAgent, UserProxyAgent
# 修正后的 config_list,移除了 "api_type" 参数
config_list = [
{
"api_base": "http://localhost:1234/v1", # 确保此地址指向您的本地LLM服务
"api_key": "NULL" # 本地LLM通常不需要实际的API密钥
}
]
# 配置 LLM
llm_config = {'config_list': config_list}
# 初始化 AssistantAgent
assistant = AssistantAgent(
name="assistant",
llm_config=llm_config
)
# 初始化 UserProxyAgent
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
)TypeError: create() got an unexpected keyword argument 'api_type' 错误是 Autogen 框架与本地 LLM 交互时常见的配置问题,其核心在于 api_type 参数在最新版本的 Autogen 中已被弃用。通过简单地从 config_list 中移除此参数,并确保本地 LLM 服务正常运行且 api_base 配置正确,即可解决此问题,使 Autogen 代理能够顺利与本地模型进行通信并执行任务。始终保持对所用库最新变化的关注,是避免此类配置错误的关键。
以上就是AutoGen 使用本地 LLM 时的 api_type 错误解析与解决方案的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号