调用GCP Grounding Score API遇400错误:需指定Inline context
GCP Grounding API 400错误 "Inline context must be specified" 解决方法
问题详情
调用Google Cloud Platform(GCP)的Grounding Score API时,传入字符串上下文后收到错误:
google.api_core.exceptions.InvalidArgument: 400 Inline context must be specified
请求对象转换后的字典内容:
grounding_source._to_grounding_source_dict() # 输出: {'sources': [{'type': 'INLINE'}], 'inlineContext': 'The Pixel Tablet, offered by Google, comes in three elegant colors: Porcelain, Hazel, and Rose. Let’s delve into the details: Price: The base model with 128GB of storage is priced at $499. If you need more storage, there’s a version with 256GB of storage available for $59912. Colors: Choose from the following stylish colors: Porcelain: A fresh and clean hue that’s new to Google’s tablet lineup. Hazel: A warm and inviting color option. Rose: A sophisticated choice for those who appreciate elegance3. Storage Size Options: The Pixel Tablet offers two storage variants: 128GB UFS 3.1 storage 256GB UFS 3.1 storage42. In addition to its sleek design, the Pixel Tablet boasts impressive specs, including an expansive 10.95-inch LCD display with a resolution of 2560x1600 pixels, 8GB LPDDR5 RAM, and the powerful Google Tensor G2 processor. Its battery life allows for up to 12 hours of video streaming, and it charges via the included Charging Speaker Dock or a separate USB-C® charger'}
可复现错误的代码:
from typing import Optional import vertexai from vertexai.language_models import ( GroundingSource, TextGenerationModel, TextGenerationResponse, ) def grounding( project_id: str, location: str = "", data_store_location: Optional[str] = None, data_store_id: Optional[str] = None) -> TextGenerationResponse: """Grounding example with a Large Language Model""" vertexai.init(project=project_id, location=location) parameters = { "temperature": 0.7, "max_output_tokens": 256, "top_p": 0.8, "top_k": 40, } model = TextGenerationModel.from_pretrained("text-bison@002") source = "The Pixel Tablet, offered by Google, comes in three elegant colors: Porcelain, Hazel, and Rose. Let’s delve into the details: Price: The base model with 128GB of storage is priced at $499. If you need more storage, there’s a version with 256GB of storage available for $59912. Colors: Choose from the following stylish colors: Porcelain: A fresh and clean hue that’s new to Google’s tablet lineup. Hazel: A warm and inviting color option. Rose: A sophisticated choice for those who appreciate elegance3. Storage Size Options: The Pixel Tablet offers two storage variants: 128GB UFS 3.1 storage 256GB UFS 3.1 storage42. In addition to its sleek design, the Pixel Tablet boasts impressive specs, including an expansive 10.95-inch LCD display with a resolution of 2560x1600 pixels, 8GB LPDDR5 RAM, and the powerful Google Tensor G2 processor. Its battery life allows for up to 12 hours of video streaming, and it charges via the included Charging Speaker Dock or a separate USB-C® charger" if source is not None: grounding_source = GroundingSource.InlineContext(source) elif data_store_id and data_store_location: grounding_source = GroundingSource.VertexAISearch( data_store_id=data_store_id, location=data_store_location ) else: grounding_source = GroundingSource.WebSearch() response = model.predict( "What are the price, available colors, and storage size options of a Pixel Tablet?", grounding_source=grounding_source, **parameters, ) print(f"Response from Model: {response.text}") print(f"Grounding Metadata: {response.grounding_metadata}") grounding(project_id="selfcheck1")
问题原因与修复方案
从字典输出看,inlineContext字段确实存在,但API仍报错,核心原因是SDK参数封装或配置问题,按以下步骤修复:
- 显式指定参数名
创建GroundingSource.InlineContext时,必须显式传入context参数,避免SDK解析错误:
grounding_source = GroundingSource.InlineContext(context=source)
- 设置有效区域
初始化vertexai时,location参数不能为空,需指定GCP支持的区域(如us-central1):
vertexai.init(project=project_id, location="us-central1")
控制上下文长度
text-bison@002的inline context有token限制(约8192 tokens),过长沙文会触发错误。可截断上下文到关键内容,比如保留价格、颜色、存储相关段落。升级SDK版本
旧版本vertexaiSDK可能存在参数封装bug,执行升级命令:
pip install --upgrade google-cloud-aiplatform
修复后的完整代码
from typing import Optional import vertexai from vertexai.language_models import ( GroundingSource, TextGenerationModel, TextGenerationResponse, ) def grounding( project_id: str, location: str = "us-central1", data_store_location: Optional[str] = None, data_store_id: Optional[str] = None) -> TextGenerationResponse: """Grounding example with a Large Language Model""" vertexai.init(project=project_id, location=location) parameters = { "temperature": 0.7, "max_output_tokens": 256, "top_p": 0.8, "top_k": 40, } model = TextGenerationModel.from_pretrained("text-bison@002") # 截断上下文到关键信息,避免长度超限 source = "The Pixel Tablet, offered by Google, comes in three elegant colors: Porcelain, Hazel, and Rose. Price: The base model with 128GB of storage is priced at $499. The 256GB version costs $599. Storage Size Options: 128GB UFS 3.1 storage, 256GB UFS 3.1 storage." if source is not None: # 显式指定context参数 grounding_source = GroundingSource.InlineContext(context=source) elif data_store_id and data_store_location: grounding_source = GroundingSource.VertexAISearch( data_store_id=data_store_id, location=data_store_location ) else: grounding_source = GroundingSource.WebSearch() response = model.predict( "What are the price, available colors, and storage size options of a Pixel Tablet?", grounding_source=grounding_source, **parameters, ) print(f"Response from Model: {response.text}") print(f"Grounding Metadata: {response.grounding_metadata}") grounding(project_id="selfcheck1")
验证
修复后调用API,会正确识别inline上下文,返回基于给定内容的回答及grounding元数据。若仍报错,检查项目是否拥有text-bison模型的调用权限,以及指定区域是否支持该模型。
内容的提问来源于stack exchange,提问作者Afshin Oroojlooy
相关产品推荐
相关产品推荐

