准备工作
本教程假定您已阅读并遵循以下说明:
- 开发 LlamaIndexQueryPipeline 智能体:将
agent
作为LlamaIndexQueryPipelineAgent
的实例进行开发。 - 用户身份验证,以用户身份进行身份验证,以便查询智能体。
- 导入并初始化 SDK:初始化客户端,以获取已部署的实例(如有需要)。
获取代理的实例
如需查询 LlamaIndexQueryPipelineAgent
,您需要先创建新实例或获取现有实例。
如需获取与特定资源 ID 对应的 LlamaIndexQueryPipelineAgent
,请执行以下操作:
Vertex AI SDK for Python
运行以下代码:
import vertexai
client = vertexai.Client( # For service interactions via client.agent_engines
project="PROJECT_ID",
location="LOCATION",
)
agent = client.agent_engines.get(name="projects/PROJECT_ID/locations/LOCATION/reasoningEngines/RESOURCE_ID")
print(agent)
其中
PROJECT_ID
是您在其中开发和部署代理的 Google Cloud 项目 ID。LOCATION
是支持的区域之一。RESOURCE_ID
是所部署智能体的 ID,以reasoningEngine
资源的形式表示。
Python 请求库
运行以下代码:
from google import auth as google_auth
from google.auth.transport import requests as google_requests
import requests
def get_identity_token():
credentials, _ = google_auth.default()
auth_request = google_requests.Request()
credentials.refresh(auth_request)
return credentials.token
response = requests.get(
f"https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/reasoningEngines/RESOURCE_ID",
headers={
"Content-Type": "application/json; charset=utf-8",
"Authorization": f"Bearer {get_identity_token()}",
},
)
REST API
curl \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/reasoningEngines/RESOURCE_ID
使用 Vertex AI SDK for Python 时,agent
对象对应于包含以下内容的 AgentEngine
类:
- 包含所部署智能体相关信息的
agent.api_resource
。您还可以调用agent.operation_schemas()
来返回智能体所支持操作的列表。如需了解详情,请参阅支持的操作。 - 允许同步服务交互的
agent.api_client
- 允许异步服务交互的
agent.async_api_client
本部分的其余内容假定您有一个名为 agent
的 AgentEngine
实例。
支持的操作
LlamaIndexQueryPipelineAgent
支持以下操作:
query
:用于同步获取对查询的响应。
query
方法支持以下类型的参数:
input
:要发送给智能体的消息。
查询智能体
命令:
agent.query(input="What is Paul Graham's life in college?")
它相当于以下内容(完整形式):
agent.query(input={"input": "What is Paul Graham's life in college?"})
如需自定义输入字典,请参阅自定义提示模板。
您还可以通过向 query()
传递其他关键字参数,来自定义智能体在 input
之外的行为。
response = agent.query(
input={
"input" = [
"What is Paul Graham's life in college?",
"How did Paul Graham's college experience shape his career?",
"How did Paul Graham's college experience shape his entrepreneurial mindset?",
],
},
batch=True # run the pipeline in batch mode and pass a list of inputs.
)
print(response)
如需查看可用参数的完整列表,请参阅 QueryPipeline.run
代码。