首页
/ Google Gemini Cookbook中浏览器工具示例的配置更新解析

Google Gemini Cookbook中浏览器工具示例的配置更新解析

2025-05-18 18:46:09作者:冯爽妲Honey

问题背景

在使用Google Gemini API的"Browser as a tool"示例时,开发者遇到了一个配置验证错误。这个错误发生在尝试建立实时连接时,系统提示"generation_config.response_modalities"字段存在验证问题。

错误分析

原始配置格式使用了嵌套的"generation_config"结构,这在最新版本的API中已不再适用。错误信息明确指出"Extra inputs are not permitted",表明API期望的配置结构已经发生了变化。

解决方案

正确的配置格式应该直接使用顶级字段,而不是嵌套在"generation_config"中。更新后的配置示例如下:

config = {
    'response_modalities': ['TEXT'],
    'tools': [
        {'google_search': {}},
    ],
}

配置详解

  1. response_modalities:指定API返回的数据格式,这里设置为['TEXT']表示只返回文本响应
  2. tools:定义可用的工具列表,每个工具以字典形式表示
  3. system_instruction(可选):提供系统级别的指令,指导模型如何响应用户查询

完整示例

对于加载网页的示例,配置应调整为:

load_page_def = types.Tool(functionDeclarations=[
    types.FunctionDeclaration.from_callable(client=client, callable=load_page)]).model_dump(exclude_none=True)

config = {
    'response_modalities': ['TEXT'],
    'tools': [load_page_def],
    'system_instruction': """Your job is to answer the users query using the tools available.
First determine the address that will have the information and tell the user. Then immediately
invoke the tool. Then answer the user."""
}

最佳实践

  1. 始终检查API文档以获取最新的配置格式要求
  2. 使用model_dump(exclude_none=True)来清理工具定义中的空值
  3. 为复杂任务提供清晰的system_instruction
  4. 测试时先从简单配置开始,逐步增加复杂度

总结

Google Gemini API的配置格式会随着版本更新而变化,开发者需要及时调整代码以适应这些变更。本文提供的解决方案不仅解决了当前的验证错误,也为处理类似配置问题提供了参考模式。理解这些配置项的作用对于有效利用Gemini API的多模态功能至关重要。

登录后查看全文
热门项目推荐
相关项目推荐