跳到主要内容

常见问题

安装正确的包 - autogen-agentchat

PyPI 上的 Autogen 包名称是 autogen-agentchat:

pip install autogen-agentchat~=0.2

使用错误包时可能遇到的典型错误包括 AttributeError: module 'autogen' has no attribute 'Agent', AttributeError: module 'autogen' has no attribute 'config_list_from_json' 等。

设置您的API端点

本文档已移至此处

在代理中使用构造的配置列表

该文档已移至此处.

代理如何从列表中选择模型?

本文档已移至此处

意外的关键字参数 'base_url'

在版本 >=1 中,OpenAI 将其 api_base 参数重命名为 base_url。因此,对于旧版本,请使用 api_base,而对于新版本,请使用 base_url

我可以使用非OpenAI模型吗?

是的。目前你有两个选项:

处理API速率限制

设置API速率限制

您可以在为代理设置config_list中的api_rate_limit,用于控制发送API请求的速率。

  • api_rate_limit (float): 每秒允许的最大API请求数。

处理速率限制错误和超时错误

你可以设置max_retries来处理速率限制错误。你也可以设置timeout来处理超时错误。它们都可以在代理的llm_config中指定,以便在OpenAI客户端中用于LLM推理。如果它们在config_list中设置,可以为不同的客户端设置不同的值。

  • max_retries (int): 允许单个客户端重试失败请求的总次数。
  • timeout (int): 单个客户端的超时时间(以秒为单位)。

请参考文档获取更多信息。

如何继续一个已结束的对话

当你调用initiate_chat时,默认情况下对话会重新开始。你可以使用sendinitiate_chat(clear_history=False)来继续对话。

max_consecutive_auto_reply vs max_turn vs max_round

我们如何决定每个代理使用哪种LLM?可以使用多少个代理?我们如何决定组中有多少个代理?

每个代理都可以定制。你可以在每个代理后面使用LLM、工具或人类。如果你为代理使用LLM,请使用最适合其角色的那个。代理的数量没有限制,但建议从少量开始,比如2、3个。LLM的能力越强,你需要的角色越少,所需的代理也就越少。

默认的用户代理不使用LLM。如果您想在UserProxyAgent中使用LLM,用例可能是模拟用户的行为。

默认的助手代理被指示使用编码和语言技能。根据任务的不同,它不需要进行编码。你可以定制系统消息。因此,如果你希望将其用于编码,请使用擅长编码的模型。

为什么代码没有被保存为文件?

如果你为编码代理使用了自定义系统消息,请在系统消息中包含类似以下内容: 如果你希望用户在执行代码之前将其保存在文件中,请在代码块的第一行放置 # filename: 这行代码在 AssistantAgent 的默认系统消息中。

如果 # filename 仍未出现在建议的代码中,可以考虑在 initiate_chat 的初始用户消息中添加明确的指令,例如“将代码保存到磁盘”。 AssistantAgent 默认情况下不会保存所有代码,因为有些情况下可能只需完成任务而不需要保存代码。

遗留代码执行器

note

新的代码执行器提供了更多的执行后端选择。 了解更多关于代码执行器的信息。

传统的代码执行器通过在代理的构造函数中指定code_execution_config来使用。

from autogen import UserProxyAgent

user_proxy = UserProxyAgent(
name="user_proxy",
code_execution_config={"work_dir":"_output", "use_docker":"python:3"},
)

在这个例子中,code_execution_config 指定了代码将在使用镜像 python:3 的 docker 容器中执行。默认情况下,如果未指定,镜像名称为 python:3-slimwork_dir 指定了代码执行的目录。如果您在代理运行 pip install 时遇到问题或收到类似 Error while fetching server API version: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory') 的错误,您可以选择 'python:3' 作为镜像,如上代码示例所示,这应该可以解决问题。

默认情况下,它会在一个docker容器中运行代码。如果您想在本地运行代码(不推荐),可以在每个代码执行代理的code_execution_config中将use_docker设置为False,或者将环境变量AUTOGEN_USE_DOCKER设置为False

你也可以在docker容器中开发你的AutoGen应用。 例如,当在GitHub Codespace中开发时, AutoGen运行在一个docker容器中。 如果你不是在GitHub Codespaces中开发, 按照这里的说明 在docker中安装和运行AutoGen。

代理在使用gpt-3.5-turbo时不断互相感谢

当使用 gpt-3.5-turbo 时,您可能会经常遇到代理进入“感激循环”的情况,这意味着当它们完成任务时,会开始不断地互相祝贺和感谢。这是 gpt-3.5-turbo 性能上的一个限制,相比之下,gpt-4 则能很好地记住指令。当您尝试使用更便宜的模型测试自己的用例时,这可能会影响实验体验。

一个解决方法是向提示中添加一个额外的终止通知。这作为LLM的“小提醒”,让他们记得在任务完成时需要终止对话。你可以通过在用户输入字符串后面附加如下字符串来实现这一点:

prompt = "Some user query"

termination_notice = (
'\n\nDo not show appreciation in your responses, say only what is necessary. '
'if "Thank you" or "You\'re welcome" are said in the conversation, then say TERMINATE '
'to indicate the conversation is finished and this is your last message.'
)

prompt += termination_notice

注意:这个方法在大约90%的情况下都能完成任务,但有时LLM仍然会忘记终止对话。

由于旧版本的sqlite3,ChromaDB在codespaces中无法运行

(来自 问题 #251

由于需要sqlite3,使用chromadb(如检索)的代码示例在codespaces中失败。

>>> import chromadb
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/vscode/.local/lib/python3.10/site-packages/chromadb/__init__.py", line 69, in <module>
raise RuntimeError(
RuntimeError: Your system has an unsupported version of sqlite3. Chroma requires sqlite3 >= 3.35.0.
Please visit https://docs.trychroma.com/troubleshooting#sqlite to learn how to upgrade.

解决方法:

  1. pip install pysqlite3-binary
  2. mkdir /home/vscode/.local/lib/python3.10/site-packages/google/colab

解释:根据这个要点,该要点链接自官方的chromadb文档,添加此文件夹将触发chromadb使用pysqlite3而不是默认设置。

如何注册一个回复函数

(来自 issue #478)

请参阅此处 https://microsoft.github.io/autogen/docs/reference/agentchat/conversable_agent/#register_reply

例如,你可以注册一个回复函数,当为代理调用generate_reply时,该函数会被调用。

def print_messages(recipient, messages, sender, config):
if "callback" in config and config["callback"] is not None:
callback = config["callback"]
callback(sender, recipient, messages[-1])
print(f"Messages sent to: {recipient.name} | num messages: {len(messages)}")
return False, None # required to ensure the agent communication flow continues

user_proxy.register_reply(
[autogen.Agent, None],
reply_func=print_messages,
config={"callback": None},
)

assistant.register_reply(
[autogen.Agent, None],
reply_func=print_messages,
config={"callback": None},
)

在上述过程中,我们注册了一个print_messages函数,该函数在代理接收到消息后每次触发generate_reply时都会被调用。

如何获取最后一条消息?

请参考 https://microsoft.github.io/autogen/docs/reference/agentchat/conversable_agent/#last_message

如何获取每个代理的消息?

请参考 https://microsoft.github.io/autogen/docs/reference/agentchat/conversable_agent#chat_messages

在使用autogen docker时,是否总是需要重新安装模块?

在代理的code_execution_config中,"use_docker"参数将在执行后设置为包含更改的镜像名称,当对话结束时。 您可以保存该镜像名称。对于新的对话,您可以将"use_docker"设置为保存的镜像名称以开始执行。

数据库锁定错误

在使用虚拟机(如Azure机器学习计算实例)时,您可能会遇到“数据库锁定错误”。这是因为LLM缓存尝试写入应用程序无权访问的位置。

你可以将cache_path_root设置为应用程序有权访问的位置。 例如,

from autogen import Cache

with Cache.disk(cache_path_root="/tmp/.cache") as cache:
agent_a.initate_chat(agent_b, ..., cache=cache)

你也可以使用Redis缓存代替磁盘缓存。例如,

from autogen import Cache

with Cache.redis(redis_url=...) as cache:
agent_a.initate_chat(agent_b, ..., cache=cache)

你也可以禁用缓存。详情见这里

由于docker未运行,Agents抛出异常,我该如何解决这个问题?

如果在本地运行AutoGen,默认情况下,执行代码的代理会尝试在Docker容器内执行代码。如果Docker没有运行,这将导致代理抛出错误。为了解决这个问题,您有以下几种选择。

如果你想完全禁用代码执行

  • 将每个代码执行代理的code_execution_config设置为False。例如:
user_proxy = autogen.UserProxyAgent(
name="agent",
llm_config=llm_config,
code_execution_config=False)

如果你想在docker中运行代码执行

  • 推荐:确保docker已启动并正在运行。

如果你想在本地运行代码执行

  • code_execution_config中,可以为每个代码执行代理设置use_dockerFalse
  • 要为所有代码执行代理一次性设置:将AUTOGEN_USE_DOCKER设置为False作为环境变量。

例如:

user_proxy = autogen.UserProxyAgent(
name="agent", llm_config=llm_config,
code_execution_config={"work_dir":"coding", "use_docker":False})

如果我收到错误“TypeError: Assistants.create() got an unexpected keyword argument 'file_ids'”该怎么办?

此错误通常在使用早于0.2.27版本的Autogen与1.21或更高版本的OpenAI库时发生。出现此问题的原因是旧版本的Autogen不支持OpenAI API新版本中使用的file_ids参数。 要解决此问题,您需要将Autogen库升级到0.2.27或更高版本,以确保Autogen与OpenAI库之间的兼容性。

pip install --upgrade autogen

由于“哈希和不匹配”,所有devcontainers都无法构建,我该怎么办?

这是一个间歇性问题,似乎是由镜像和代理问题的某种组合引起的。如果出现此问题,尝试将apt-get update步骤替换为以下内容:

RUN echo "Acquire::http::Pipeline-Depth 0;" > /etc/apt/apt.conf.d/99custom && \
echo "Acquire::http::No-Cache true;" >> /etc/apt/apt.conf.d/99custom && \
echo "Acquire::BrokenProxy true;" >> /etc/apt/apt.conf.d/99custom

RUN apt-get clean && \
rm -r /var/lib/apt/lists/* && \
apt-get update -o Acquire::CompressionTypes::Order::=gz && \
apt-get -y update && \
apt-get install sudo git npm # and whatever packages need to be installed in this specific version of the devcontainer

这是StackOverflow建议这里这里的结合。

优云智算