Skip to content

Instantly share code, notes, and snippets.

@onemore118
Last active June 24, 2024 09:27
Show Gist options
  • Save onemore118/c766e72b62dffd518232e067e36da8b8 to your computer and use it in GitHub Desktop.
Save onemore118/c766e72b62dffd518232e067e36da8b8 to your computer and use it in GitHub Desktop.

1. 安装依赖包

python -m pip install -U 'anthropic[vertex]'

2. Enable所需要的大模型

跳转到对应的模型界面Vertex->Model Garden->[选择需要激活的模型],填写对应资料,最后点击Enable

3. 重要!!转到google cloud platform IAM,为账户配置角色

账号角色配置为Vertex AI User

4. 安装gcloud CLI

参考:https://cloud.google.com/sdk/docs/install?hl=zh-cn

执行如下命令进行登录授权过程

gcloud auth application-default login

执行gcloud auth application-default revoke可用撤销凭证

6. 配置环境变量 windows使用如下命令配置临时变量

set GOOGLE_APPLICATION_CREDENTIALS=<文件的路径>

5. 参考资料

https://cloud.google.com/vertex-ai/docs/start/cloud-environment?hl=zh-cn

6. 测试代码

下面以激活claude-3-5-sonnet@20240620举例

from anthropic import AnthropicVertex
import os

os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = r"<文件路径>"   #tod 修改上文获取的凭证文件路径

LOCATION="us-east5" 

client = AnthropicVertex(region=LOCATION, project_id="<project id>")  #todo 修改为自己的project id

message = client.messages.create(
  max_tokens=24,
  messages=[
    {
      "role": "user",
      "content": "hello",
    }
  ],
  model="claude-3-5-sonnet@20240620",
)
print(message.model_dump_json(indent=2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment