深入解析:在Vertex AI上使用Multimodal Llama 3.2的三种方法

2024年09月29日 由 alex 发表 25 0

Llama 3.2 系列模型现已在Vertex AI Model Garden中推出。Llama 3.2 包括 90b、11b、3b 和 1b 尺寸的开放模型,90b 和 11b 模型的真正亮点在于它们的多模式支持。这两个模型可以在单个提示中处理和组合文本和图像,这意味着你现在可以向 Llama 提出难以用文字描述的问题。多模式提示解锁了一整套新用例,因此让我们来看看你今天可以在 Vertex AI 上使用 Llama 3.2 开始构建的三种不同方式。


使用 Llama 3.2 API 服务

在 Vertex AI 上尝试使用 Llama 3.2 的最简单方法是通过模型即服务(MaaS)产品。MaaS 提供了一个无服务器端点,因此你不必担心基础设施的设置和管理。你只需打开 Colab 笔记本或其他开发工具,通过 REST 或 OpenAI 库向模型发送请求即可。


以下是如何使用 OpenAI 库调用顶点人工智能上的 Llama 3.2 90b。


首先,导入库并设置证书


# Import libraries
import openai
from google.auth import default, transport
# Get credentials
credentials, _ = default()
auth_request = transport.requests.Request()
credentials.refresh(auth_request)


接下来,你需要初始化 OpenAI 客户端:


PROJECT_ID = "<your-project-id>"
MAAS_ENDPOINT = f"us-central1-aiplatform.googleapis.com"
# Initialize the OpenAI client
client = openai.OpenAI(
    base_url = f"https://{MAAS_ENDPOINT}/v1beta1/projects/{PROJECT_ID}/locations/{LOCATION}/endpoints/openapi",
    api_key = credentials.token)


现在我们可以开始了。我们可以提示 meta/llama-3.2-90b-vision-instruct-maas 模型识别这张图片中的地标。图像存储在云存储桶中,我们在 “image_url ”字段中传递 URI。


2


# Path to image
image_url = "gs://github-repo/img/gemini/intro/landmark2.jpg"
# Prompt model
response = client.chat.completions.create(
    model= "meta/llama-3.2-11b-vision-instruct-maas",
    messages=[
        {"role": "user", 
         "content": [
            {"image_url": {"url": image_url}, "type": "image_url"}
            {"text": "What’s in this image?", "type": "text"},
           ]
         },
        {"role": "assistant", 
         "content": "In this image, you have:"}],
    max_tokens=max_tokens,)
# Get the response
print(response.choices[0].message.content)


将 Llama 3.2 部署到 Vertex AI 端点

将模型部署到 Vertex AI 端点会将模型工件与物理资源关联起来,以实现低延迟服务,并创建一个 DeployedModelresource。部署 Llama 3.2 后,你就可以通过 Vertex AI Python SDK 或 OpenAI Library 发送推理请求。


要部署这些模型之一,请导航到 Model Garden 中的 Llama 3.2 模型卡。在资源 ID 下,你会看到不同的模型选项,如 Llama-3-2-1B-Instruct、Llama-3-2-11B-Vision-Instruct 等。


3


部署完成后,你可以使用 Vertex AI Python SDK 向端点发送请求。


# Import libraries
from google.cloud import aiplatform
# Define endpoint
endpoint = aiplatform.Endpoint(f"projects/{PROJECT_ID}/locations/{REGION}/endpoints/{ENDPOINT_ID}")
instances = [
        {
              "instances": [
                  {
                    "prompt": "<|image|><|begin_of_text|>What is in this image?",
                    "multi_modal_data": {"image": _"data:image/jpg;base64,<base64_image_str>"},
                    "max_tokens": 100
                  }
              ]
          }
    ]
                              
# Make request with Vertex AI Python SDK
response = endpoint.predict(instances=instances)


使用 Llama Guard 保护 Llama 3.2

Llama Guard 是 Meta 开发的基于 LLM 的输入输出保障模型,可对 LLM 提示和响应中识别的特定安全风险进行分类。Llama Guard 模型会在其输出中生成文本,指出给定提示或响应是安全还是不安全,如果不安全,还会列出违反的内容类别。模型花园中提供了两种新的 Llama Guard 模型:纯文本 Llama Guard 3 1B 和多模态 Llama Guard 3 11B-Vision。


默认情况下,你在 Vertex AI 上向 MaaS 端点做出的所有预测都会启用 Llama Guard。在下面的代码片段中,我们将 “model_safety_settings ”中的 “enabled ”值设置为 True,但请注意这是默认值。


# Llama Guard is on by default
apply_llama_guard = True
# Prompt model
response = client.chat.completions.create(
    model="meta/llama-3.2-11b-vision-instruct-maas",
    messages=[
        {"role": "user", 
         "content": [
              {"image_url": {"url": image_url}, "type": "image_url"},
              {"text": "What's in this image?", "type": "text"},]
         },
        {"role": "assistant", 
         "content": "In this image, you have:"}],
    max_tokens=max_tokens,
    extra_body={
        "extra_body": {
            "google": {
                "model_safety_settings": {
                    "enabled": apply_llama_guard, # default value is True
                    "llama_guard_settings": {},
                }
            }
        }
    },
)


如果你想自己动手部署 Llama 3.2 模型,也可以将 Llama Guard 部署到 Vertex AI 端点。在 Model Garden 中导航到 Llama Guard 模型卡,选择模型版本,然后单击部署。


4


结论

以上就是如何在 Vertex AI 上开始尝试使用 Llama 3.2 的简要介绍。使用 Llama 3.2 还有很多其他方法。例如,你可以将它用作 Vertex AI GenAI Evaluation 的多模态评估工具。

文章来源:https://medium.com/google-cloud/3-ways-to-use-multimodal-llama-3-2-on-vertex-ai-5727e1d8e9d5
欢迎关注ATYUN官方公众号
商务合作及内容投稿请联系邮箱:bd@atyun.com
评论 登录
热门职位
Maluuba
20000~40000/月
Cisco
25000~30000/月 深圳市
PilotAILabs
30000~60000/年 深圳市
写评论取消
回复取消