Skip to content

Fix: 移除 _remove_image_from_context中的flag逻辑 #2064

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 9, 2025

Conversation

uersula
Copy link
Contributor

@uersula uersula commented Jul 8, 2025

解决了 #2048

Motivation

修复一种导致'error': {'message': "Messages with role 'tool' must be a response to a preceding message with 'tool_calls'", 'type': 'invalid_request_error', 'param': None, 'code': 'invalid_request_error'}的情况,此情况通常出现在使用图片转述插件时
bug产生原因:
flag逻辑会移除带图片消息的下一条消息,如果被移除的是assistant消息,message可能会违反OpenAI API 要求的消息格式(tool 消息前必须有带 tool_calls 的 assistant 消息)导致api返回400 BadRequestError
以使用图像转述插件为例
第一次调用大模型(如 deepseek-chat),因模型不支持图片而触发 _remove_image_from_context 。
模型返回 tool_calls 请求。
插件执行成功后,ToolLoopAgent 将工具结果和历史记录再次发给大模型。
第二次调用大模型(如 deepseek-chat),因为_remove_image_from_context 检测的是self.req,所以仍能检测到图片,_remove_image_from_context 被再次触发。此时,它的 flag 逻辑会错误地将包含 tool_calls 的 assistant 消息删除。而这个assistant消息后面有转述插件的tool消息,这破坏了 OpenAI API 要求的消息格式(tool 消息前必须有带 tool_calls 的 assistant 消息),最终导致 400 BadRequestError,使整个流程失败。

Modifications

修改位于 astrbot/core/provider/sources/openai_source.py 文件中的 _remove_image_from_context 方法。
移除了方法内部的 flag 判断及其相关逻辑。
直接移除flag不会引起问题的原因:
例如
用户发送了一张“猫弹钢琴”的图片。(视觉模型)AI 回复:“这是一只猫在弹钢琴。”
此时切换到纯文本模型继续对话,历史记录被降级处理。flag 逻辑会删除图片,并且删除 “这是一只猫在弹钢琴”这条回复。
原flag逻辑处理后:[
{"role": "user", "content": "[图片]"}
]

去除flag逻辑后:[
{"role": "user", "content": "[图片]"},
{"role": "assistant", "content": "这是一只猫在弹钢琴。"}
]

可见去除flag逻辑并不影响模型理解上下文,反而flag 逻辑会删除图片,并且删除 “这是一只猫在弹钢琴”这条回复,使模型困惑。

Check

  • 😊 我的 Commit Message 符合良好的规范
  • 👀 我的更改经过良好的测试
  • 🤓 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到了 requirements.txtpyproject.toml 文件相应位置。
  • 😮 我的更改没有引入恶意代码

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

Sourcery 总结

移除 _remove_image_from_context 中基于标志的消息移除机制,以避免在工具调用之前剥离助手回复,并修复处理基于图像的插件时出现的 invalid_request_error

Bug 修复:

  • 修复了当上下文中存在图像时,由于移除了工具调用之前的助手消息而导致的 invalid_request_error

增强功能:

  • 通过消除无意中丢弃有效助手响应的过时标志逻辑,简化了 _remove_image_from_context
Original summary in English

Summary by Sourcery

Remove the flag-based removal of messages in _remove_image_from_context to avoid stripping assistant replies before tool calls and fix invalid_request_error when handling image-based plugins.

Bug Fixes:

  • Fix invalid_request_error caused by removing the assistant message that precedes a tool call when images are in the context.

Enhancements:

  • Simplify _remove_image_from_context by eliminating outdated flag logic that inadvertently dropped valid assistant responses.

Copy link
Contributor

sourcery-ai bot commented Jul 8, 2025

## 审查者指南

此 PR 移除了 `_remove_image_from_context` 方法中基于标志的逻辑,以防止在图像上下文之后意外删除助手消息,从而避免格式错误的 OpenAI API 请求。

### 文件级别更改

| 变更 | 详情 | 文件 |
| ------ | ------- | ----- |
| 从图像上下文过滤中删除基于标志的删除逻辑 | <ul><li>删除标志变量初始化</li><li>删除在检测到图像后跳过消息的条件块</li><li>消除图像内容分支中的标志分配</li></ul> | `astrbot/core/provider/sources/openai_source.py` |

### 可能相关的 issue

- **#2048**: PR 通过更正 `_remove_image_from_context` 中的消息格式,修复了 issue 日志中显示的 API 错误。
- **#2048**: 此 PR 移除了图像处理中的标志逻辑,该逻辑导致图像出现错误,从而解决了 issue 中的 bug。
- **#2048**: 此 PR 移除了 `_remove_image_from_context` 中错误的标志逻辑,该逻辑通过删除带有 `tool_calls` 的助手消息,导致图像转录期间出现 BadRequestError,从而修复了该 issue。

---

<details>
<summary>提示和命令</summary>

#### 与 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` 以触发新的审查!

#### 自定义您的体验

访问您的 [仪表板](https://app.sourcery.ai) 以:
- 启用或禁用审查功能,例如 Sourcery 生成的 pull request 摘要、审查者指南等。
- 更改审查语言。
- 添加、删除或编辑自定义审查说明。
- 调整其他审查设置。

#### 获取帮助

- [联系我们的支持团队](mailto:support@sourcery.ai) 提出问题或反馈。
- 访问我们的 [文档](https://docs.sourcery.ai) 获取详细指南和信息。
- 通过在 [X/Twitter](https://x.com/SourceryAI)、[LinkedIn](https://www.linkedin.com/company/sourcery-ai/) 或 [GitHub](https://github.com/sourcery-ai) 上关注我们,与 Sourcery 团队保持联系。

</details>
Original review guide in English

Reviewer's Guide

This PR removes the flag-based logic in the _remove_image_from_context method to prevent unintended deletion of assistant messages following image contexts, thereby avoiding malformed OpenAI API requests.

File-Level Changes

Change Details Files
Removed flag-based deletion logic from image context filtering
  • Deleted flag variable initialization
  • Removed conditional block that skipped the message after a detected image
  • Eliminated flag assignment within image-content branch
astrbot/core/provider/sources/openai_source.py

Possibly linked issues


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!

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

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@uersula - 我已经查看了你的更改,它们看起来很棒!


Sourcery 对开源是免费的 - 如果你喜欢我们的评论,请考虑分享它们✨
帮助我更有用!请点击每个评论上的👍或👎,我将使用反馈来改进你的评论。
Original comment in English

Hey @uersula - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@Soulter
Copy link
Member

Soulter commented Jul 9, 2025

这么看确实,并不会删除一整个 role="user" 的 part,所以移除 flag 是没问题的。感谢!

@Soulter Soulter merged commit fe35bfb into AstrBotDevs:master Jul 9, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants