DEFVERTEX_AI_REQUEST(请求): #定义常数 api_endpoint =“ us-central1-aiplatform.googleapis.com” project_id =“ xxxx” model_id =“ text-bison@001” url = f“ https:// {api_endpoint}/v1/projects/{project_id}/locuations/us-central1/us-central1/publishers/google/models/{model_id}:预测“
# Load the service account credentials
credentials = service_account.Credentials.from_service_account_file(
'service-account-file.json'
)
auth_req = requests.Request()
token = credentials.refresh(auth_req).token
# Define the headers
headers = {
"Authorization": f"Bearer {token}",
"Content-Type": "application/json"
}
# Define the payload
payload = {
"instances": [
{
"content": "Your content here ..."
}
],
"parameters": {
"candidateCount": 1,
"maxOutputTokens": 317,
"temperature": 0.2,
"topP": 0.8,
"topK": 40
}
}
# Send the POST request
response = req.post(URL, headers=headers, json=payload)
# Print the response to the logs
print(response.json())
return 'Request completed', 200
这里是校正的代码:
DEFVERTEX_AI_REQUEST(请求): #从环境中获取默认凭据 凭据,project_id = default()
# Define constants
API_ENDPOINT = "us-central1-aiplatform.googleapis.com"
MODEL_ID = "text-bison@001" # Or your deployed prompt endpoint
URL = f"https://{API_ENDPOINT}/v1/projects/{project_id}/locations/us-central1/publishers/google/models/{MODEL_ID}:predict"
# Get the access token
auth_req = requests.Request()
token = credentials.refresh(auth_req).token
# Define the headers
headers = {
"Authorization": f"Bearer {token}",
"Content-Type": "application/json"
}
# Define the payload
payload = {
"instances": [
{
"content": "Your content here ..."
}
],
"parameters": {
"candidateCount": 1,
"maxOutputTokens": 317,
"temperature": 0.2,
"topP": 0.8,
"topK": 40
}
}
# Send the POST request
response = requests.post(URL, headers=headers, json=payload)
# Print the response to the logs
print(response.json())
return 'Request completed', 200
步骤部署和配置:
grant权限:在Google Cloud Console中访问IAM页面。 查找云功能服务帐户(看起来像[Project_ID][电子邮件保护]
)。 授予它“顶点AI用户”角色。 部署云功能:部署您的云功能。 如果您使用的是服务帐户文件,请确保将其删除,并删除加载文件的行。 测试:
触发云功能。 检查云功能日志以查看Vertex AI的响应。 如果您使用已部署的提示:
获取端点: 在Dertex AI提示工程UI中,部署提示后,您会找到端点URL。 更新Model_ID变量: 将“ text-bison@001”替换为已部署提示的端点。 更新有效载荷: 部署提示的有效负载结构可能与基本模型不同。查看已部署的提示的文档,并相应地更新有效载荷。