本页介绍了如何从记忆库中提取生成的记忆和上传的记忆。 如需了解配置、生成和使用 Memory Bank 的完整工作流程,请参阅使用 REST API 快速入门。
您可以使用以下选项来提取生成的记忆:
Get Memory:获取单个记忆的完整内容。
检索记忆:使用基于范围的记忆检索功能检索记忆。使用相似性搜索功能检索记忆,或检索范围内的所有记忆。
获取内存
使用 GetMemories
获取单个内存的完整内容:
import google.auth
import google.auth.transport.requests
import requests
credentials, _ = google.auth.default()
auth_req = google.auth.transport.requests.Request()
credentials.refresh(auth_req)
url = "https://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/reasoningEngines/REASONING_ENGINE_ID/memories/MEMORY_ID"
response = requests.get(
url=url,
headers={
"Content-Type": "application/json; charset=utf-8",
"Authorization": f"Bearer {credentials.token}",
}
)
response.json()
替换以下内容:
PROJECT_ID:您的项目 ID。
REASONING_ENGINE_ID:
ReasoningEngine
实例 ID。MEMORY_ID:您要获取的记忆的 ID。
使用基于范围的检索功能检索记忆
您可以使用 RetrieveMemories
检索特定范围内的回忆。系统只会返回与检索请求具有完全相同范围(不考虑顺序)的回忆。例如,您可以使用 {"user_id": "123"}
检索限定为特定用户的所有记忆内容。如果未返回任何记忆,则表示记忆库中没有指定范围内的任何记忆。
记忆的范围是在生成或创建记忆时定义的,并且是不可变的。
您可以使用 RetrieveMemories
对特定范围执行以下操作:
使用相似度搜索功能检索回忆
如果您在特定范围内有很多记忆,则可以通过提供相似性搜索参数,使用相似性搜索仅检索最相似的记忆。在执行相似性搜索时,记忆库只会考虑范围与请求完全相同的记忆。相似度搜索会比较记忆的事实与请求的搜索查询之间的嵌入向量。
返回的记忆会按相似度从高(欧几里得距离最短)到低(欧几里得距离最长)进行排序:
import requests
url = "https://LOCATION-aiplatform.googleapis.com/v1beta1/AGENT_ENGINE_NAME"
response = requests.post(
url=f"{url}/memories:retrieve",
headers={
"Content-Type": "application/json; charset=utf-8",
"Authorization": f"Bearer {token[0]}",
},
json={
"similarity_search_params": {
"search_query": "QUERY",
# Optional. Defaults to 3.
"top_k": 3
},
"scope": SCOPE
}
)
"""
Returns:
{
"retrieved_memories": [
{
"memory": {
"name":
"projects/.../locations/.../reasoningEngines/.../memories/...",
...
"fact": "This is a fact."
},
"distance": 0.5
},
{
"memory": {
"name": "projects/.../locations/.../reasoningEngines/.../memories/...",
...
"fact": "This is another fact."
},
"distance": 0.75
}
]
}
"""
替换以下内容:
LOCATION:您的区域。 Vertex AI Agent Engine 记忆库和会话仅支持
us-central1
。AGENT_ENGINE_NAME:您创建的 Vertex AI Agent Engine 实例的名称或现有的 Vertex AI Agent Engine 实例的名称。名称应采用以下格式:
projects/PROJECT_ID/locations/LOCATION/reasoningEngines/REASONING_ENGINE_ID
。QUERY:要执行相似性搜索的查询。例如,您可以使用对话的最后一个用户轮次作为查询。
SCOPE:一个字典,表示相似度搜索的范围。系统只会考虑与请求具有相同范围的记忆。
检索所有回忆
如果未提供任何相似性搜索参数,RetrieveMemories
会返回具有所提供范围的所有记忆,无论它们与当前对话的相似性如何。
import requests
url = "https://LOCATION-aiplatform.googleapis.com/v1beta1/AGENT_ENGINE_NAME"
response = requests.post(
url=f"{url}/memories:retrieve",
headers={
"Content-Type": "application/json; charset=utf-8",
"Authorization": f"Bearer {token[0]}",
},
json={
"scope": SCOPE,
# Optional. By default, a page size of 100 will be used.
"simple_retrieval_params": {
"page_size": 3
}
}
)
"""
Returns:
{
"retrieved_memories": [
{
"memory": {
"name":
"projects/.../locations/.../reasoningEngines/.../memories/...",
...
"fact": "This is a fact."
}
},
{
"memory": {
"name": "projects/.../locations/.../reasoningEngines/.../memories/...",
...
"fact": "This is another fact."
}
}
]
}
"""
替换以下内容:
LOCATION:您的区域。 Vertex AI Agent Engine 记忆库和会话仅支持
us-central1
。AGENT_ENGINE_NAME:您创建的 Vertex AI Agent Engine 实例的名称或现有的 Vertex AI Agent Engine 实例的名称。名称应采用以下格式:
projects/PROJECT_ID/locations/LOCATION/reasoningEngines/REASONING_ENGINE_ID
。SCOPE:表示检索范围的字典。系统只会返回与请求具有相同范围的记忆。