📘 **TELUS Agriculture & Consumer Goods** 如何通过 **Haystack Agents** 转变促销交易
由 deepset 维护

集成:Google Vertex AI

在 Haystack 中使用 Google Vertex AI 模型

作者
deepset

目录

概述

🚧 此集成使用了已弃用的 SDK。

我们建议改用新的 Google GenAI 集成。

Vertex AI 是一个机器学习 (ML) 平台,可让您训练和部署 ML 模型和 AI 应用,并自定义大型语言模型 (LLM) 以供您在 AI 驱动的应用中使用。此集成通过 Google Cloud Platform (GCP) 上的 Vertex AI API,实现了 生成模型嵌入模型 的使用。

Haystack 支持 Gemini API 模型PaLM API 模型Codey API 模型Imagen API 模型 以及 ,用于执行文本生成函数调用图像生成视觉问答代码生成图像字幕生成以及嵌入生成等任务。

要了解更多关于用例的信息,并使用 Haystack 测试 Vertex AI 模型,请遵循此 Notebook

有一篇关于 Gemini 模型及其在 Haystack 中的使用方法的文章:Gemini 模型与 Google Vertex AI 集成供 Haystack 使用

安装

安装 Google Vertex AI 集成

pip install google-vertex-haystack

使用

安装后,您将可以使用各种 Haystack 生成器和嵌入器

  • VertexAIGeminiGenerator:使用此组件以及 'gemini-1.5-pro'、'gemini-2.0-flash' 和 'gemini-2.5-flash' 模型进行文本生成和多模态提示。
  • VertexAIGeminiChatGenerator:使用此组件以及 'gemini-1.5-pro'、'gemini-2.0-flash' 和 'gemini-2.5-flash' 模型进行文本生成、多模态提示以及聊天完成场景中的函数调用。
  • VertexAITextGenerator:使用此组件以及 PaLM 模型进行文本生成。
  • VertexAICodeGenerator:使用此组件以及 Codey 模型进行代码生成和代码补全。
  • VertexAIImageGenerator:使用此组件以及 'imagegeneration' 模型进行图像生成。
  • VertexAIImageCaptioner:使用此组件以及 'imagetext' 模型进行图像字幕生成。
  • VertexAIImageQA:使用此组件以及 'imagetext' 模型进行视觉问答。
  • VertexAITextEmbedder:使用此组件通过 Vertex AI Text Embedder API 为文本数据创建嵌入。
  • VertexAIDocumentEmbedder:使用此组件通过 Vertex AI Text Embedder API 为 Document 创建嵌入。

要使用 Vertex AI 模型,您需要拥有一个 Google Cloud Platform 账户,并使用应用程序默认凭据 (ADC) 登录。有关更多信息,请参阅 官方文档VertexAIGeminiGeneratorVertexAIGeminiChatGenerator 支持 gemini-1.5-progemini-2.0-flash/gemini-2.5-flash 模型。请注意,Google 建议从 gemini-1.5-pro 升级到 gemini-2.0-flash

要在 Haystack 中开始使用 Vertex AI 生成器,您的账户必须有权访问已授权使用 Google Vertex AI 端点的项目。初始化 Vertex AI 生成器所需的 project_id 在上述 GCP 身份验证过程中设置。此外,您还可以通过在初始化生成器时作为变量传递来设置不同的 project_id。您可以在 GCP 资源管理器 中找到您的 project_id,或者通过在终端中运行 gcloud projects list 来本地查找。有关 gcloud CLI 的更多信息,请参阅 官方文档

Gemini API 模型

您可以通过两个组件利用 Gemini 模型:VertexAIGeminiGeneratorVertexAIGeminiChatGenerator。您可以单独使用这些组件,也可以在管道中使用它们。

使用 gemini-1.5-pro 进行文本生成

要使用 Gemini 模型进行文本生成,请初始化一个 VertexAIGeminiGenerator,并指定 "gemini-1.5-pro"project_id

from haystack_integrations.components.generators.google_vertex import VertexAIGeminiGenerator


gemini_generator = VertexAIGeminiGenerator(model="gemini-1.5-pro")
result = gemini_generator.run(parts = ["What is assemblage in art?"])
print(result["replies"][0])

输出

Assemblage in art refers to the creation of a three-dimensional artwork by combining various found objects...

使用 gemini-2.0-flash 进行多模态处理

要使用 gemini-2.0-flash 模型进行视觉问答,请初始化一个 VertexAIGeminiGenerator 并指定 "gemini-2.0-flash"。然后,使用图像和提示运行它。

import requests
from haystack.dataclasses.byte_stream import ByteStream


URLS = [
    "https://raw.githubusercontent.com/silvanocerza/robots/main/robot1.jpg",
    "https://raw.githubusercontent.com/silvanocerza/robots/main/robot2.jpg",
    "https://raw.githubusercontent.com/silvanocerza/robots/main/robot3.jpg",
    "https://raw.githubusercontent.com/silvanocerza/robots/main/robot4.jpg"
]
images = [
    ByteStream(data=requests.get(url).content, mime_type="image/jpeg")
    for url in URLS
]
gemini_generator = VertexAIGeminiGenerator(model="gemini-2.0-flash")
result = gemini_generator.run(parts = ["What can you tell me about these robots?", *images])
for answer in result["replies"]:
    print(answer)  

输出

The first image is of C-3PO and R2-D2 from the Star Wars franchise...
The second image is of Maria from the 1927 film Metropolis...
The third image is of Gort from the 1951 film The Day the Earth Stood Still...
The fourth image is of Marvin from the 1977 film The Hitchhiker's Guide to the Galaxy...

有关使用 gemini-1.5-pro 进行函数调用的信息,请参阅 Notebook

文本嵌入器 API 模型

您可以利用文本嵌入器 API 模型 text-embedding-005text-multilingual-embedding-002text-embedding-large-exp-03-07text-embedding-004textembedding-gecko-multilingual@001

from haystack_integrations.components.embedders.google_vertex import VertexAITextEmbedder

text_to_embed = "I love pizza!"

text_embedder = VertexAITextEmbedder(model="text-embedding-005")

print(text_embedder.run(text_to_embed))
# {'embedding': [-0.08127457648515701, 0.03399784862995148, -0.05116401985287666, ...]

PaLM API 模型

您可以通过 VertexAITextGenerator 利用 text-bisontext-unicorntext-bison-32k 等 PaLM API 模型进行任务生成。要使用 PaLM 模型,请初始化一个 VertexAITextGenerator 并指定模型名称。

以下是一个使用 text-unicorn 模型和 VertexAITextGenerator 将信息提取为 JSON 文件的示例。

from haystack_integrations.components.generators.google_vertex import VertexAITextGenerator


palm_llm = VertexAITextGenerator(model="text-unicorn")
palm_llm_result = palm_llm.run(
    """Extract the technical specifications from the text below in a JSON format. Valid fields are name, network, ram, processor, storage, and color.
       Text: Google Pixel 7, 5G network, 8GB RAM, Tensor G2 processor, 128GB of storage, Lemongrass
       JSON:
    """)
print(palm_llm_result["replies"][0])

Codey API 模型

您可以通过 VertexAICodeGenerator 利用 code-bisoncode-bison-32kcode-gecko 等 Codey API 模型进行代码生成。要使用 Codey 模型,请初始化一个 VertexAICodeGenerator 并指定模型名称。

以下是使用 code-bison 模型进行代码生成的示例。

from haystack_integrations.components.generators.google_vertex import VertexAICodeGenerator


codey_llm = VertexAICodeGenerator(model="code-bison")
codey_llm_result = codey_llm.run("Write a code for calculating fibonacci numbers in JavaScript")
print(codey_llm_result["replies"][0])

以下是使用 code-gecko 模型进行代码补全的示例。

from haystack_integrations.components.generators.google_vertex import VertexAICodeGenerator


codey_llm = VertexAICodeGenerator(model="code-gecko")
codey_llm_result = codey_llm.run("""function fibonacci(n) {
  // Base cases
  if (n <= 1) {
    return n;
  }
""")
print(codey_llm_result["replies"][0])

Imagen API 模型

您可以通过三个组件利用 Imagen 模型:VertexAIImageCaptionerVertexAIImageGeneratorVertexAIImageQA

使用 imagegeneration 进行图像生成

要生成图像,请使用 imagegeneration 初始化 VertexAIImageGenerator。然后,您可以使用提示来运行它。

import io
import PIL.Image as Image
from haystack_integrations.components.generators.google_verteximport VertexAIImageGenerator


image_generator = VertexAIImageGenerator(model="imagegeneration")
image_generator_result = image_generator.run("magazine style, 4k, photorealistic, modern red armchair, natural lighting")

## (Optional) Save the generated image
image = Image.open(io.BytesIO(image_generator_result["images"][0].data))
image.save("output.png")

使用 imagetext 进行图像字幕生成

要使用图像字幕生成,请使用 imagetext 模型初始化 VertexAIImageCaptioner。然后,您可以使用要添加字幕的图像来运行 VertexAIImageCaptioner。

from haystack_integrations.components.generators.google_vertex import VertexAIImageCaptioner


image_captioner = VertexAIImageCaptioner(model='imagetext')
image = ByteStream.from_file_path("output.png") # you can use the generated image

image_captioner_result = image_captioner.run(image=image)
print(image_captioner_result["captions"])

视觉问答 (VQA) 与 imagetext

要回答关于图像的问题,请使用 imagetext 模型初始化 VertexAIImageQA。然后,您可以使用 imagequestion 来运行它。

from haystack.dataclasses.byte_stream import ByteStream
from haystack_integrations.components.generators.google_vertex import VertexAIImageQA


visual_qa = VertexAIImageQA(model='imagetext')
image = ByteStream.from_file_path("output.png") # you can use the generated image
question = "what's the color of the furniture?"

visual_qa_result = visual_qa.run(image=image,question=question) 
print(visual_qa_result["replies"])