使用 Vertex AI 上托管的 LLM#

本指南教您如何将 NeMo Guardrails 与 Vertex AI 上托管的 LLM 一起使用。它使用了 ABC Bot 配置,并将模型更改为 gemini-1.0-pro

本指南假设您已配置并测试了使用 Vertex AI 模型的工作。如果未配置,请参阅本指南

先决条件#

您需要安装以下 Python 库

  1. 安装 google-cloud-aiplatformlangchain-google-vertexai

pip install --quiet "google-cloud-aiplatform>=1.38.0" langchain-google-vertexai==0.1.0
  1. 设置 GOOGLE_APPLICATION_CREDENTIALS 环境变量

export GOOGLE_APPLICATION_CREDENTIALS=$GOOGLE_APPLICATION_CREDENTIALS # Replace with your own key
  1. 如果您在笔记本中运行此程序,请修补 AsyncIO 循环。

import nest_asyncio
nest_asyncio.apply()

配置#

首先,将 ABC bot 配置复制到名为 config 的子目录中

cp -r ../../../../examples/bots/abc config

更新 config/config.yml 文件,以使用带有 vertexai 提供程序的 gemini-1.0-pro 模型

...

models:
  - type: main
    engine: vertexai
    model: gemini-1.0-pro

...

加载 guardrails 配置

from nemoguardrails import RailsConfig
from nemoguardrails import LLMRails

config = RailsConfig.from_path("./config")
rails = LLMRails(config)

测试它是否工作

response = rails.generate(messages=[{
    "role": "user",
    "content": "Hi! How are you?"
}])
print(response)
{'role': 'assistant', 'content': "I'm doing great! Thank you for asking. I'm here to help you with any questions you may have about the ABC Company."}

您可以看到 bot 响应正确。要更详细地查看已进行的 LLM 调用,您可以使用 print_llm_calls_summary 方法,如下所示

info = rails.explain()
info.print_llm_calls_summary()
Summary: 5 LLM call(s) took 3.99 seconds .

1. Task `self_check_input` took 0.58 seconds .
2. Task `generate_user_intent` took 1.19 seconds .
3. Task `generate_next_steps` took 0.71 seconds .
4. Task `generate_bot_message` took 0.88 seconds .
5. Task `self_check_output` took 0.63 seconds .

评估#

gemini-1.0-protext-bison 模型已针对主题 rails 进行了评估,并且 gemini-1.0-pro 也已作为用于幻觉和内容审核的自检模型进行了评估。评估结果可以在此处找到。

结论#

在本指南中,您学习了如何将 NeMo Guardrails 配置连接到 Vertex AI LLM 模型。本指南使用 gemini-1.0-pro,但是,您可以按照相同的步骤连接任何其他模型。