本页介绍了如何从示例库中检索示例。 您可以通过以下方式检索示例:
FetchExamples
:检索完全符合过滤条件的所有示例。如果您只有几个示例或需要更短的延迟时间,请使用此选项。SearchExamples
:使用用户查询与存储的示例之间的相似度搜索来检索示例。如果您有大量示例,请使用此选项。
前提条件
在使用本页上的 Python 示例之前,请在本地 Python 环境中安装并初始化 Vertex AI SDK for Python。
运行以下命令以安装适用于示例商店的 Python 版 Vertex AI SDK。
pip install --upgrade google-cloud-aiplatform>=1.87.0
使用以下代码示例导入并初始化适用于示例商店的 SDK。
import vertexai from vertexai.preview import example_stores vertexai.init( project="PROJECT_ID", location="LOCATION" )
替换以下内容:
PROJECT_ID:您的项目 ID。
LOCATION:您的区域。 仅支持
us-central1
。
提取示例
使用以下示例提取示例。FetchExamples
会检索完全符合您的过滤条件的所有示例。
Python 版 Vertex AI SDK
以下代码会返回示例商店中的所有示例,每页最多 100 个:
from vertexai.preview import example_stores
example_store = example_stores.ExampleStore(EXAMPLE_STORE_NAME)
# Returns the dictionary representation of FetchExamplesResponse.
examples = example_store.fetch_examples()
您可以使用 function_names
指定一个或多个过滤条件,以限制返回的示例。以下示例仅返回包含 flight_booking_tool
和 hotel_booking_tool
函数的示例:
# Returns examples that include either tool.
example_store.fetch_examples(
filter={
"function_names": {
"values": ["flight_booking_tool", "hotel_booking_tool"],
"array_operator": "CONTAINS_ANY"
}
}
)
# Returns examples that include *both* tools.
example_store.fetch_examples(
filter={
"function_names": {
"values": ["flight_booking_tool", "hotel_booking_tool"],
"array_operator": "CONTAINS_ALL"
}
}
)
您可以使用 search_keys
过滤条件来限制按搜索键值返回的示例。
# Returns examples that include any of the following search keys.
example_store.fetch_examples(
filter={"search_keys": ["How do I get to the airport?"]}
)
您可以使用 example_ids
过滤条件按示例 ID 限制返回的示例。示例 ID 的格式为 exampleTypes/stored_contents_example/examples/<var>EXAMPLE_ID</var>
,其中 EXAMPLE_ID 表示为示例生成的数字 ID。
# Returns examples that have any of the following Example IDs.
example_store.fetch_examples(
example_ids=["exampleTypes/stored_contents_example/examples/09b1d383f92c47e7a2583a44ebbc7854"]
)
REST API
如需提取示例,请使用 exampleStores.fetchExamples
方法发送 POST 请求。
示例请求 JSON 正文中指定的 function_names
过滤器仅会返回包含函数 flight_booking_tool
和 hotel_booking_tool
的示例:
在使用任何请求数据之前,请先进行以下替换:
- PROJECT_ID:您的项目 ID。
- LOCATION:要在其中创建示例商店的区域。仅支持区域
us-central1
。 - EXAMPLE_STORE_ID:要上传示例的示例商店实例的 ID。
HTTP 方法和网址:
POST https://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/exampleStores/EXAMPLE_STORE_ID:fetchExamples
请求 JSON 正文:
{ "stored_contents_example_filter": { "function_names": { "values": ["flight_booking_tool", "hotel_booking_tool"], "array_operator": "CONTAINS_ANY" } } }
如需发送请求,请选择以下方式之一:
curl
将请求正文保存在名为 request.json
的文件中,然后执行以下命令:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/exampleStores/EXAMPLE_STORE_ID:fetchExamples"
PowerShell
将请求正文保存在名为 request.json
的文件中,然后执行以下命令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/exampleStores/EXAMPLE_STORE_ID:fetchExamples" | Select-Object -Expand Content
您应该会收到类似以下示例的 JSON 响应,其中 EXAMPLE_ID 表示为示例生成的 ID。
搜索示例
示例存储区会根据 stored_contents_example_key
与存储的示例的搜索键之间的相似度得分,查找最相关的示例。使用与对话相关的示例有助于模型学习预期行为。
Python 版 Vertex AI SDK
使用以下代码示例,通过 Vertex AI SDK for Python 搜索相关示例:
example_store.search_examples(
parameters={
"stored_contents_example_key": "what's the weather in nyc"
},
# Only fetch the most similar examaple. The default value is 3.
top_k=1
)
"""
Response -- dictionary representation of SearchExamplesResponse.
{'results': [{'example': {'exampleId': 'exampleTypes/stored_contents_example/examples/16834837b178453783e471b459d99195',
'storedContentsExample': {'searchKey': 'What is the weather like in Boston?',
'contentsExample': {'contents': [{'role': 'user',
'parts': [{'text': 'What is the weather like in Boston?'}]}],
'expectedContents': [{'content': {'parts': [{'functionCall': {'name': 'get_current_weather',
'args': {'location': 'New York, NY'}}}]}},
{'content': {'parts': [{'functionResponse': {'name': 'get_current_weather',
'response': {'humidity': 65.0,
'description': 'Partly Cloudy',
'icon': 'partly-cloudy',
'temperature': 38.0,
'location': 'Boston, MA',
'wind': {'speed': 10.0, 'direction': 'NW'}}}}]}},
{'content': {'role': 'model',
'parts': [{'text': 'The weather in Boston is 38 degrees and partly cloudy.'}]}}]}}},
'similarityScore': 0.73527116}]}
"""
您可以使用 function_names
过滤条件来限制相似搜索中包含的示例。
example_store.search_examples(
parameters={
"stored_contents_example_key": "What's the weather in nyc",
"function_names": {
"values": ["weather_tool", "hotel_booking_tool"],
"array_operator": "CONTAINS_ANY"
}
}
)