[Bug]: llama_index重试openai.AuthenticationError

n6lpvg4x  于 2个月前  发布在  其他
关注(0)|答案(5)|浏览(30)

错误描述

llama_index 重试 openai.AuthenticationError

版本

0.10.19

重现步骤

通过 llama_index 对 Open AI 发起请求,仅使用随机字符串作为密钥。
llama_index 记录日志并显示延迟,清楚地表明请求正在重试。
即使将 max_retries=1 添加到 OpenAI 对象中也无法解决问题。

相关日志/回溯

/Users/ashishb/Library/Caches/pypoetry/virtualenvs/src-ynB8Yjnz-py3.10/lib/python3.10/site-packages/llama_index/core/base/base_query_engine.py(40)query()
-> return self._query(str_or_query_bundle)
  /Users/ashishb/Library/Caches/pypoetry/virtualenvs/src-ynB8Yjnz-py3.10/lib/python3.10/site-packages/llama_index/core/query_engine/retriever_query_engine.py(187)_query()
-> response = self._response_synthesizer.synthesize(
  /Users/ashishb/Library/Caches/pypoetry/virtualenvs/src-ynB8Yjnz-py3.10/lib/python3.10/site-packages/llama_index/core/response_synthesizers/base.py(195)synthesize()
-> response_str = self.get_response(
  /Users/ashishb/Library/Caches/pypoetry/virtualenvs/src-ynB8Yjnz-py3.10/lib/python3.10/site-packages/llama_index/core/response_synthesizers/compact_and_refine.py(38)get_response()
-> return super().get_response(
  /Users/ashishb/Library/Caches/pypoetry/virtualenvs/src-ynB8Yjnz-py3.10/lib/python3.10/site-packages/llama_index/core/response_synthesizers/refine.py(163)get_response()
-> response = self._give_response_single(
  /Users/ashishb/Library/Caches/pypoetry/virtualenvs/src-ynB8Yjnz-py3.10/lib/python3.10/site-packages/llama_index/core/response_synthesizers/refine.py(217)_give_response_single()
-> program(
  /Users/ashishb/Library/Caches/pypoetry/virtualenvs/src-ynB8Yjnz-py3.10/lib/python3.10/site-packages/llama_index/core/response_synthesizers/refine.py(68)__call__()
-> answer = self._llm.predict(
  /Users/ashishb/Library/Caches/pypoetry/virtualenvs/src-ynB8Yjnz-py3.10/lib/python3.10/site-packages/llama_index/core/llms/llm.py(253)predict()
-> chat_response = self.chat(messages)
  /Users/ashishb/Library/Caches/pypoetry/virtualenvs/src-ynB8Yjnz-py3.10/lib/python3.10/site-packages/llama_index/core/llms/callbacks.py(93)wrapped_llm_chat()
-> f_return_val = f(_self, messages, **kwargs)
  /Users/ashishb/Library/Caches/pypoetry/virtualenvs/src-ynB8Yjnz-py3.10/lib/python3.10/site-packages/llama_index/llms/openai/base.py(257)chat()
-> return chat_fn(messages, **kwargs)
  /Users/ashishb/Library/Caches/pypoetry/virtualenvs/src-ynB8Yjnz-py3.10/lib/python3.10/site-packages/tenacity/__init__.py(289)wrapped_f()
-> return self(f, *args, **kw)
  /Users/ashishb/Library/Caches/pypoetry/virtualenvs/src-ynB8Yjnz-py3.10/lib/python3.10/site-packages/tenacity/__init__.py(382)__call__()
-> result = fn(*args, **kwargs)
  /Users/ashishb/Library/Caches/pypoetry/virtualenvs/src-ynB8Yjnz-py3.10/lib/python3.10/site-packages/llama_index/llms/openai/base.py(323)_chat()
-> response = client.chat.completions.create(
  /Users/ashishb/Library/Caches/pypoetry/virtualenvs/src-ynB8Yjnz-py3.10/lib/python3.10/site-packages/openai/_utils/_utils.py(275)wrapper()
-> return func(*args, **kwargs)
  /Users/ashishb/Library/Caches/pypoetry/virtualenvs/src-ynB8Yjnz-py3.10/lib/python3.10/site-packages/openai/resources/chat/completions.py(663)create()
-> return self._post(
  /Users/ashishb/Library/Caches/pypoetry/virtualenvs/src-ynB8Yjnz-py3.10/lib/python3.10/site-packages/openai/_base_client.py(1201)post()
-> return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls))
  /Users/ashishb/Library/Caches/pypoetry/virtualenvs/src-ynB8Yjnz-py3.10/lib/python3.10/site-packages/openai/_base_client.py(889)request()
-> return self._request(
> /Users/ashishb/Library/Caches/pypoetry/virtualenvs/src-ynB8Yjnz-py3.10/lib/python3.10/site-packages/openai/_base_client.py(964)_request()
-> if retries > 0 and self._should_retry(err.response):
cnjp1d6j

cnjp1d6j1#

嘿 @ashishb!很高兴再次看到你在这里。希望你过得很好!
感谢你报告这个问题。你观察到的行为,即 llama_indexmax_retries=1 设置的情况下仍然重试 openai.AuthenticationError ,可能是由于在提供的代码中应用了自定义重试装饰器( llm_retry_decorator )导致的。这个装饰器定义了一个 max_retries 参数,设置为6,覆盖了 OpenAI 类的 max_retries 属性。
这里是定义 llm_retry_decorator 的相关代码部分:

llm_retry_decorator = create_retry_decorator(
    max_retries=6,
    random_exponential=True,
    stop_after_delay_seconds=60,
    min_seconds=1,
    max_seconds=20,
)

这里是如何将其应用于异步聊天方法:

@llm_retry_decorator
async def _achat(
    self, messages: Sequence[ChatMessage], **kwargs: Any
) -> ChatResponse:
    ...

@llm_retry_decorator
async def _astream_chat(
    self, messages: Sequence[ChatMessage], **kwargs: Any
) -> ChatResponseAsyncGen:
    ...

这个 llm_retry_decorator 直接应用于这些方法,并使用其自己的 max_retries 值(在这个例子中是6),这并不考虑在 OpenAI 对象的构造函数或其他方法中设置的 max_retries 属性。这就是为什么尽管在 OpenAI 对象中设置了 max_retries=1 ,但观察到的重试行为仍然是装饰器的配置,导致在出现 openai.AuthenticationError 的情况下最多重试6次。
至于从版本0.10.19到最新版本0.10.20之间的更改,引入了许多新功能和修复了多个错误,但它们似乎都没有直接解决这个问题。你可以在 CHANGELOG.md 中找到有关这些更改的更多详细信息。
重试 openai.AuthenticationError 的目的是处理在向OpenAI发起API请求时可能发生的 transient 错误。这个重试机制对于维护LlamaIndex功能的可靠性和稳定性至关重要,尤其是在网络问题或临时的OpenAI服务中断可能导致身份验证错误或其他类型的故障的情况下。
如果你需要为特定用例覆盖 max_retries 值,可以通过调用具有不同 max_retries 值的 create_retry_decorator 并将返回的装饰器应用于所需的函数来实现。然而,请注意,这将需要对LlamaIndex代码库进行更改,并且应谨慎操作以避免意外的副作用。
开发者选择6次重试的具体选择是一个平衡,可能是基于他们对被调用服务的可靠性的理解以及不过早放弃请求的重要性。然而,选择6作为值的确切原因可能会有所不同,可能是基于经验数据、行业标准或应用程序或正在调用的API的具体要求。
我希望这有助于澄清问题。如果你还有进一步的问题或需要更多帮助,请随时提问。

来源

关于 Dosu

此响应旨在节省您的时间并提供帮助。它不是精确解决方案,而是您自己研究的起点。
帮助我变得更有用!如果这有帮助请点赞👍,如果无关请点👎。
如果你想继续对话,请以@dosu-bot开始你的回复。

cld4siwp

cld4siwp2#

这里有什么问题?我也不完全确定如何解决这个问题。
openai客户端管理一些基本事项的重试。
除此之外,tenacity正在为特定的错误类型提供重试(至少从我可以看出来,身份验证错误不在这里)。也许其中一种错误类型是身份验证错误的父级?
llama_index/llama-index-integrations/llms/llama-index-llms-openai/llama_index/llms/openai/utils.py
第162行 cf5f75a
| | openai.APITimeoutError, |

u5rb5r59

u5rb5r593#

除此之外,坚韧性还在为特定错误类型提供重试(至少就我所知,身份验证错误不在其中)。也许其中某个错误类型是身份验证错误的父级?
我手动测试过,问题不在于特定的错误列表,而是在于 llm_retry_decorator

ycl3bljg

ycl3bljg4#

@logan-markewich我们能完全移除这个全局装饰器吗?或者至少添加一个选项来实现这一点?

jw5wzhpr

jw5wzhpr5#

我对坚韧不拔(tenacity)并不完全熟悉,但我看到 AuthenticationErrorAPIStatusError(openai sdk)的子类,这是一个会触发重试的错误。

相关问题