AstrBot icon indicating copy to clipboard operation
AstrBot copied to clipboard

feat: 😽增加重试机制, 解决模型不稳定的报错

Open anka-afk opened this issue 1 year ago • 2 comments

修复了 #1300

Motivation

  1. 解决由于某些供应商/模型本身不太稳定导致频繁报错
  2. 解决网络等其他因素不稳定而导致的报错
  3. 在发生错误时不应当直接回复错误信息, 而应该将错误信息保留在日志, 以此防止错误刷屏

Modifications

  1. 增加两项供应商配置: 最大重试次数和重试间隔时间
  2. 为LLM请求部分增加遇到错误的重试机制

Check

  • [x] 我的 Commit Message 符合良好的规范
  • [x] 我新增/修复/优化的功能经过良好的测试

好的,这是翻译成中文的 pull request 总结:

Sourcery 总结

为 LLM 请求添加重试机制,以提高稳定性和处理瞬时错误

新特性:

  • 为 LLM 提供程序请求实现可配置的重试机制
  • 为 API 调用添加可配置的最大重试次数和重试延迟

增强功能:

  • 改进对不稳定模型提供程序的错误处理
  • 为特定类型的网络和服务器错误添加选择性重试

杂项:

  • 更新默认配置以包含重试设置
Original summary in English

Summary by Sourcery

Add retry mechanism for LLM requests to improve stability and handle transient errors

New Features:

  • Implement configurable retry mechanism for LLM provider requests
  • Add configurable maximum retry attempts and retry delay for API calls

Enhancements:

  • Improve error handling for unstable model providers
  • Add selective retry for specific types of network and server errors

Chores:

  • Update default configuration to include retry settings

anka-afk avatar Apr 20 '25 04:04 anka-afk

Sourcery 评审者指南

此 Pull Request 引入了 LLM 请求的重试机制,以解决某些提供商/模型和网络相关问题导致的不稳定性。它增加了最大重试次数和重试延迟的配置选项,并确保错误消息被记录而不是直接返回给用户。

更新后的提供商设置类图

classDiagram
  class ProviderSettings {
    streaming_response: bool
    max_retries: int
    retry_delay: float
  }
  note for ProviderSettings "Added max_retries and retry_delay attributes"

文件级别变更

变更 详情 文件
实现了 LLM 请求的重试机制,以处理由于提供商不稳定或网络问题导致的潜在故障。
  • 在 LLM 请求逻辑周围添加了一个重试循环。
  • 实现了一个重试计数器,并在达到最大重试次数时退出循环。
  • 在重试尝试之间添加了延迟。
  • 添加了对特定错误类型(超时、连接、速率限制、服务器错误、500、503)的检查,以确定是否应尝试重试。
  • 记录了包含 traceback 信息的错误消息,用于调试目的。
  • 成功请求后上传指标。
  • 成功请求后将请求保存到历史记录。
astrbot/core/pipeline/process_stage/method/llm_request.py
为 LLM 请求添加了最大重试次数和重试延迟的配置选项。
  • 在配置中的提供商设置中添加了 max_retries 和 retry_delay。
  • 为 max_retries (3) 和 retry_delay (1.0 秒) 设置了默认值。
  • 在 LLMRequest 类的初始化期间检索配置值。
astrbot/core/pipeline/process_stage/method/llm_request.py
astrbot/core/config/default.py

可能相关的 issue

  • #1300: 此 PR 通过为 LLM 请求添加重试机制来修复此问题。
  • #1300: 此 PR 实现了 issue 中请求的重试系统。

提示和命令

与 Sourcery 互动

  • 触发新的审查: 在 Pull Request 上评论 @sourcery-ai review。
  • 继续讨论: 直接回复 Sourcery 的审查评论。
  • 从审查评论生成 GitHub issue: 要求 Sourcery 从审查评论创建一个 issue,方法是回复该评论。您也可以回复审查评论并使用 @sourcery-ai issue 从该评论创建一个 issue。
  • 生成 Pull Request 标题: 在 Pull Request 标题中的任何位置写入 @sourcery-ai 以随时生成标题。您也可以在 Pull Request 上评论 @sourcery-ai title 以随时(重新)生成标题。
  • 生成 Pull Request 摘要: 在 Pull Request 正文中的任何位置写入 @sourcery-ai summary 以随时在您想要的位置生成 PR 摘要。您也可以在 Pull Request 上评论 @sourcery-ai summary 以随时(重新)生成摘要。
  • 生成评审者指南: 在 Pull Request 上评论 @sourcery-ai guide 以随时(重新)生成评审者指南。
  • 解决所有 Sourcery 评论: 在 Pull Request 上评论 @sourcery-ai resolve 以解决所有 Sourcery 评论。如果您已经解决了所有评论并且不想再看到它们,这将非常有用。
  • 驳回所有 Sourcery 审查: 在 Pull Request 上评论 @sourcery-ai dismiss 以驳回所有现有的 Sourcery 审查。如果您想从新的审查开始,这将特别有用 - 不要忘记评论 @sourcery-ai review 以触发新的审查!
  • 为 issue 生成行动计划: 在 issue 上评论 @sourcery-ai plan 以为其生成行动计划。

自定义您的体验

访问您的 仪表板 以:

  • 启用或禁用审查功能,例如 Sourcery 生成的 Pull Request 摘要、评审者指南等。
  • 更改审查语言。
  • 添加、删除或编辑自定义审查说明。
  • 调整其他审查设置。

获取帮助

  • 联系我们的支持团队 提出问题或反馈。
  • 访问我们的 文档 获取详细指南和信息。
  • 通过在 X/Twitter, LinkedIn 或 GitHub 上关注我们,与 Sourcery 团队保持联系。
Original review guide in English

Reviewer's Guide by Sourcery

This pull request introduces a retry mechanism for LLM requests to address instability issues with certain providers/models and network-related errors. It adds configuration options for maximum retry attempts and retry delay, and it ensures that error messages are logged instead of being directly returned to the user.

Updated class diagram for provider settings

classDiagram
  class ProviderSettings {
    streaming_response: bool
    max_retries: int
    retry_delay: float
  }
  note for ProviderSettings "Added max_retries and retry_delay attributes"

File-Level Changes

Change Details Files
Implemented a retry mechanism for LLM requests to handle potential failures due to provider instability or network issues.
  • Added a retry loop around the LLM request logic.
  • Implemented a retry counter and exit the loop when the maximum number of retries is reached.
  • Added a delay between retry attempts.
  • Added a check for specific error types (timeout, connection, rate limit, server error, 500, 503) to determine if a retry should be attempted.
  • Logged error messages with traceback information for debugging purposes.
  • Uploads metrics after a successful request.
  • Saves the request to history after a successful request.
astrbot/core/pipeline/process_stage/method/llm_request.py
Added configuration options for maximum retry attempts and retry delay for LLM requests.
  • Added max_retries and retry_delay to the provider settings in the configuration.
  • Default values are set for max_retries (3) and retry_delay (1.0 second).
  • The configuration values are retrieved during the initialization of the LLMRequest class.
astrbot/core/pipeline/process_stage/method/llm_request.py
astrbot/core/config/default.py

Possibly linked issues

  • #1300: The PR fixes the issue by adding retry mechanisms for LLM requests.
  • #1300: The PR implements the retry system requested in the issue.

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an issue from a review comment by replying to it. You can also reply to a review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull request title to generate a title at any time. You can also comment @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in the pull request body to generate a PR summary at any time exactly where you want it. You can also comment @sourcery-ai summary on the pull request to (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the pull request to resolve all Sourcery comments. Useful if you've already addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull request to dismiss all existing Sourcery reviews. Especially useful if you want to start fresh with a new review - don't forget to comment @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

  • Contact our support team for questions or feedback.
  • Visit our documentation for detailed guides and information.
  • Keep in touch with the Sourcery team by following us on X/Twitter, LinkedIn or GitHub.

sourcery-ai[bot] avatar Apr 20 '25 04:04 sourcery-ai[bot]

感觉这个 process() 方法现在已经过分复杂了()我抽空仔细 check 一下吧~ 感觉重试机制得用装饰器来包装了

Soulter avatar Apr 22 '25 16:04 Soulter