请求指导搭建Vertex AI Python SDK及代码运行前置准备
Vertex AI Python SDK 搭建核心步骤(用于调用结构化文本提示模型)
核心搭建步骤
- 环境准备:确保本地Python版本为3.8-3.11,且已安装pip工具
- 安装SDK:执行终端命令
pip install google-cloud-aiplatform - 身份配置:
- 本地开发:下载Google Cloud服务账号密钥JSON文件,设置环境变量指向该文件:
Linux/macOS:export GOOGLE_APPLICATION_CREDENTIALS="密钥文件绝对路径"
Windows:set GOOGLE_APPLICATION_CREDENTIALS=密钥文件绝对路径 - Google Cloud内部环境(如Colab、云服务器):无需手动配置,自动使用默认身份
- 本地开发:下载Google Cloud服务账号密钥JSON文件,设置环境变量指向该文件:
- 验证运行:替换测试代码中的项目ID为你的实际GCP项目ID,执行代码检查是否能正常获取模型响应
调用模型示例代码
import vertexai from vertexai.language_models import TextGenerationModel vertexai.init(project="你的GCP项目ID", location="us-central1") parameters = { "candidate_count": 1, "max_output_tokens": 1024, "temperature": 0.2, "top_p": 0.8, "top_k": 40 } model = TextGenerationModel.from_pretrained("text-bison") response = model.predict( """UiPath is a global software company that makes robotic process automation (RPA) software. It was founded in Bucharest, Romania, by Daniel Dines and Marius Tîrcă. Its headquarters are in New York City. The company's software monitors user activity to automate repetitive front and back office tasks, including those performed using other business software such as customer relationship management or enterprise resource planning (ERP) software. In December 2020, the company filed confidentially for an initial public offering, and became public on April 21, 2021. input: What is Uipath? output: """, **parameters ) print(f"Response from Model: {response.text}")
内容的提问来源于stack exchange,提问作者Kapil Parmar
相关产品推荐
相关产品推荐

