langchain 当超过Anthropic速率限制时,没有通知,

taor4pac  于 2个月前  发布在  其他
关注(0)|答案(3)|浏览(23)

问题描述

在使用LangChain时,当API速率限制超过Anthropic的限制时,没有收到任何通知或明确的错误信息,应用程序只是保持连接,导致整个API崩溃。

期望行为

LangChain应该在达到Anthropic速率限制时抛出一个特定的异常(例如,AnthropicRateLimitExceeded)或提供一个清晰的错误消息。

当前行为

应用程序继续运行,没有任何关于速率限制问题的指示,这使得实现适当的错误处理和有效地管理API使用变得困难。当切换到OpenAI时,API功能正常工作,这个问题从未发生过。这个问题似乎特定于Anthropic。经过调查,发现在1分钟内超过40k tokens后会出现问题。然而,没有警报或错误来处理这个情况,连接仍然保持活动状态,程序继续运行但停止响应。在我看来,这是一个关键问题。

系统信息

aiohttp==3.9.5
aiohttp-retry==2.8.3
aiomysql==0.2.0
aiosignal==1.3.1
aiosqlite==0.20.0
annotated-types==0.6.0
anthropic==0.31.2
anyio==4.3.0
asttokens==2.4.1
attrs==23.2.0
bcrypt==4.1.2
Brotli==1.1.0
CacheControl==0.14.0
cachetools==5.3.3
certifi==2024.2.2
cffi==1.16.0
chardet==5.2.0
charset-normalizer==3.3.2
click==8.1.7
click-spinner==0.1.10
cloudpathlib==0.18.1
colorama==0.4.6
croniter==1.4.1
cryptography==41.0.7
cssselect2==0.7.0
dataclasses-json==0.6.5
defusedxml==0.7.1
Deprecated==1.2.14
distro==1.9.0
dnspython==2.6.1
email_validator==2.1.1
executing==2.0.1
fastapi==0.111.0
fastapi-cli==0.0.2
fastapi-utilities==0.2.0
filelock==3.15.4
fonttools==4.53.1
frozenlist==1.4.1
fsspec==2024.6.1
greenlet==3.0.3
grpcio==1.64.1
grpcio-status==1.62.2
h11==0.14.0
html5lib==1.1
httpcore==1.0.5
httplib2==0.22.0
httptools==0.6.1
httpx==0.27.0
hubspot-api-client==9.0.0
huggingface-hub==0.23.5
icecream==2.1.3
idna==3.7
importlib_resources==6.4.0
Jinja2==3.1.4
jiter==0.5.0
jsonpatch==1.33
jsonpointer==2.4
langchain==0

xbp102n0

xbp102n01#

你从底层客户端或人类学中看到了任何有用的信息吗?
你可以通过指定一个速率限制器来部分避免这个问题:https://python.langchain.com/v0.2/docs/how_to/chat_model_rate_limiting/

0s0u357o

0s0u357o2#

这正是问题所在——我没有看到任何错误信息。我看到的最后一件事是,OpenAI完成了它的审查过程:

minds2_api  | INFO:httpx:HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"
minds2_api  | INFO:httpx:HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"
minds2_api  | INFO:httpx:HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"
minds2_api  | INFO:httpx:HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"
minds2_api  | INFO:httpx:HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"

在那之后,下一步就是将一个字符串发送给Anthropic,它作为我的其他代理的主模型或控制器。这就是问题发生的地方,但终端中没有显示错误。程序没有崩溃;它只是卡住了,这意味着我不能再向我的端点发送任何请求。唯一的解决办法是重新启动服务器使其再次正常工作。
我决定在本地服务器上重现错误,以排除NGINX中的任何配置错误。在本地测试时,我发现这个问题也可以在那里复制,所以我排除了NGINX作为原因。(尽管我确实增加了服务器上的超时响应,认为这可能与响应时间有关。)
我添加了一些调试语句来追踪错误,并注意到所有信息都处理得正确。然而,在最后的步骤中,当系统应该交付最终响应时,服务器冻结,无限期地等待。在这一点上,没有什么可以恢复情况;服务器必须重新启动。
考虑到可能的原因,我怀疑这可能是由于Anthropic的服务层级低于OpenAI的关于我正在处理的工作负载。在试图再次强制错误时,我能够重现它,并得出结论:
经过大量时间的调查,我几乎可以确定这是一个速率限制问题。然而,它悄无声息地发生,使服务器无限期地等待。

wqsoz72f

wqsoz72f3#

更新:

经过进一步调查,我发现这个问题不仅仅局限于Anthropic,OpenAI也会出现这个问题。然而,Anthropic更容易复现这个问题。通常情况下,错误不会在2到3次尝试中发生。问题出现的确切位置是:

response = await llm_with_tools.ainvoke({"messages": final_result})

这是我创建LLM链的相关代码部分:

if user_agent.is_anthropic():
    llm = ChatAnthropic(model=model, temperature=temperature)  # noqa
else:
    llm = ChatOpenAI(model=model, temperature=temperature)  # noqa
primary_assistant_prompt = ChatPromptTemplate.from_messages(
    [
        SystemMessage(content=f"<today>\nToday is: {get_mex_time()}\n</today>\n" + user_agent.assistant_prompt),
        MessagesPlaceholder(variable_name="messages")
    ]
)
llm_with_tools = primary_assistant_prompt | llm.bind_tools(tools)

我使用的模型是Anthropic Claude v3.5和GPT-4,温度设置为0。为了帮助调试,我暂时修改了我的代码,增加了额外的打印输出:

async def _chatbot(state: State):
    messages = state['messages']
    final_result = messages if user_agent.is_anthropic() else self._custom_filter(messages)
    try:
        ic(final_result)
        response = await llm_with_tools.ainvoke({"messages": final_result})
        return ic({"messages": [response]})
    except Exception as e:
        ic(f'FATAL ERROR: {e}')
        raise Exception('BIG ERROR HERE with the quoter')

这个问题仍然存在,导致服务器无限挂起,需要重启。这似乎与速率限制有关,但错误悄无声息地发生,日志中没有任何明确的提示。
这是服务器挂起前的最后几条日志:

INFO:httpx:HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"
INFO:httpx:HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"
ic| str(ai_message): ('Lo siento, no encontré ningún producto relacionado. Por favor, proporcione '
                      'el producto exacto que está buscando para que pueda ayudarle mejor.')
ic| final_result: [HumanMessage(content='hola'),
                   AIMessage(content='Hola! Bienvenido a Power Depot. Soy AquaExpert, 
                  ...
                  ...
                  ...)

如果你在ic(print)的末尾看到没有Error或Exceptions。

相关问题