Skip to content

fix: 修复get_func_desc_google_genai_style未正确转换函数调用的问题 - #1357

Merged
Soulter merged 2 commits into
AstrBotDevs:masterfrom
Raven95676:hotfix/gemini-functool
Apr 21, 2025
Merged

Soulter merged 2 commits into
AstrBotDevs:masterfrom
Raven95676:hotfix/gemini-functool

Conversation

@Raven95676

@Raven95676 Raven95676 commented Apr 20, 2025 •

Copy link
Copy Markdown
Member

fixes #1354

Motivation

get_func_desc_google_genai_style未正确转换函数调用

Modifications

根据官方文档重写get_func_desc_google_genai_style

Check

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

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

Sourcery 总结

重构 get_func_desc_google_genai_style 方法,以正确转换 Google GenAI (Gemini) API 兼容的函数描述

Bug 修复:

  • 修正了函数描述转换,以正确处理 Google GenAI API 需求的模式转换

增强功能:

  • 实现了强大的模式转换方法,支持各种 JSON 模式类型和格式
  • 增加了对过滤和转换函数参数以匹配 Gemini API 规范的支持
Original summary in English

Summary by Sourcery

Refactor the get_func_desc_google_genai_style method to correctly convert function descriptions for Google GenAI (Gemini) API compatibility

Bug Fixes:

  • Corrected the function description conversion to properly handle schema transformations for Google GenAI API requirements

Enhancements:

  • Implemented a robust schema conversion method that supports various JSON schema types and formats
  • Added support for filtering and transforming function parameters to match Gemini API specifications

@sourcery-ai

sourcery-ai Bot commented Apr 20, 2025 •

Copy link
Copy Markdown
Contributor

Sourcery 评审者指南

此 Pull Request 通过重写 get_func_desc_google_genai_style 方法来正确转换函数调用,从而解决了 #1354 号问题,使其符合官方 Gemini API 文档。 这涉及到递归模式转换过程,处理支持的数据类型和格式,并包含相关的模式字段,同时排除不支持的字段。

get_func_desc_google_genai_style 的时序图

sequenceDiagram
  participant FTM as FuncToolManager
  participant F as Function
  participant C as Converter

  FTM->>FTM: get_func_desc_google_genai_style()
  loop for each active function f in func_list
    FTM->>F: f.name
    FTM->>F: f.description
    FTM->>F: f.parameters
    FTM->>C: convert_schema(f.parameters)
    C-->>FTM: converted parameters
    FTM->>FTM: Create function declaration
  end
  FTM-->>FTM: Return function declarations
Loading

文件级别变更

变更 详情 文件
重写了 get_func_desc_google_genai_style 方法,以根据官方文档正确转换函数调用。
  • 实现了一个 convert_schema 函数,以递归方式转换模式定义,使其与 Gemini API 兼容。
  • 处理了 Gemini API 支持的类型和格式,对于不支持的类型,默认为 'null'。
  • 添加了逻辑,以包含原始模式中支持的字段,如 'title'、'description'、'enum'、'minimum'、'maximum'、'maxItems'、'minItems'、'nullable' 和 'required'。
  • 在模式转换期间,从属性中删除了 'default' 值。
  • 添加了对 'items' 和 'anyOf' 模式属性的支持。
  • 通过迭代活动函数并使用 convert_schema 函数转换其参数来构造函数声明。
  • 仅在函数声明中包含非空参数。
astrbot/core/provider/func_tool_manager.py

针对关联问题的评估

问题 目标 已解决 说明
#1354 修复了在使用启用工具的 Gemini 服务提供商时发生的 ValidationError,特别是与 function_declarations.4.parameters.properties.max_length.exclusiveMaximum 和 function_declarations.4.parameters.properties.max_length.exclusiveMinimum 相关的问题。 ✅
#1354 确保在使用 get_func_desc_google_genai_style 时正确转换函数调用。 ✅

可能关联的问题


提示和命令

与 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 摘要、评审者指南等。
  • 更改审查语言。
  • 添加、删除或编辑自定义审查说明。
  • 调整其他审查设置。

获取帮助

Original review guide in English

Reviewer's Guide by Sourcery

The pull request addresses issue #1354 by rewriting the get_func_desc_google_genai_style method to correctly convert function calls according to the official Gemini API documentation. This involves a recursive schema conversion process, handling supported data types and formats, and including relevant schema fields while excluding unsupported ones.

Sequence diagram for get_func_desc_google_genai_style

sequenceDiagram
  participant FTM as FuncToolManager
  participant F as Function
  participant C as Converter

  FTM->>FTM: get_func_desc_google_genai_style()
  loop for each active function f in func_list
    FTM->>F: f.name
    FTM->>F: f.description
    FTM->>F: f.parameters
    FTM->>C: convert_schema(f.parameters)
    C-->>FTM: converted parameters
    FTM->>FTM: Create function declaration
  end
  FTM-->>FTM: Return function declarations
Loading

File-Level Changes

Change Details Files
Rewrote the get_func_desc_google_genai_style method to correctly convert function calls according to the official documentation.
  • Implemented a convert_schema function to recursively convert schema definitions to be compatible with the Gemini API.
  • Handled supported types and formats for Gemini API, defaulting to 'null' for unsupported types.
  • Added logic to include supported fields like 'title', 'description', 'enum', 'minimum', 'maximum', 'maxItems', 'minItems', 'nullable', and 'required' from the original schema.
  • Removed 'default' values from properties during schema conversion.
  • Added support for 'items' and 'anyOf' schema properties.
  • Constructed the function declarations by iterating through active functions and converting their parameters using the convert_schema function.
  • Included only non-empty parameters in the function declaration.
astrbot/core/provider/func_tool_manager.py

Assessment against linked issues

Issue Objective Addressed Explanation
#1354 Fix the ValidationError that occurs when using Gemini service provider with tools enabled, specifically related to function_declarations.4.parameters.properties.max_length.exclusiveMaximum and function_declarations.4.parameters.properties.max_length.exclusiveMinimum. ✅
#1354 Ensure that function calls are correctly converted when using get_func_desc_google_genai_style. ✅

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!
  • 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

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

嘿 @Raven95676 - 我已经审查了你的更改 - 这里有一些反馈:

总体评论:

  • 考虑添加一个注释,解释为什么默认类型被设置为null。
这是我在审查期间查看的内容
  • 🟡 一般问题:发现1个问题
  • 🟢 安全性:一切看起来都很好
  • 🟢 测试:一切看起来都很好
  • 🟢 复杂性:一切看起来都很好
  • 🟢 文档:一切看起来都很好

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

Hey @Raven95676 - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider adding a comment explaining why the default type is being set to null.
Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

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
Soulter requested a review from anka-afk April 20, 2025 17:04
@Soulter

Soulter commented Apr 20, 2025

Copy link
Copy Markdown
Member

刚刚测试之后我觉得没问题了,LGTM

@Soulter
Soulter merged commit a115114 into AstrBotDevs:master Apr 21, 2025
@Raven95676
Raven95676 deleted the hotfix/gemini-functool branch April 21, 2025 03:58
@Soulter Soulter moved this to Done in AstrBot Roadmap Nov 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

[Bug] 更新到v3.5.4出现2 validation errors for Tool问题,无法正常使用

2 participants