← Материалы разборы Yersham
разбор · статьятема-фронтир · H 1.58
2.0
из 10
смотреть не обязательно — забрать выжимку и пункты
оценка машинная и частично зависит от длины ролика — спорите, открывайте оригинал

Подключение внешней модерации к NeMo Guardrails

URL Source: https://raw.githubusercontent.com/NVIDIA/NeMo-Guardrails/develop/examples/notebooks/clavataai_detection.ipyn

Notebook показывает подключение внешнего сервиса модерации к NeMo Guardrails, но без конкретных действий для оператора-одиночки.

Notebook показывает подключение внешнего сервиса модерации к NeMo Guardrails, но без конкретных действий для оператора-одиночки.

что из этого моё

Можно применить для защиты AI-оркестрации: добавить слой модерации входящих запросов и исходящих ответов, чтобы отсекать нежелательный контент и утечку PII. Это повышает безопасность автоматизированных агентов и чат-ботов.

Что забрать
отметь, что берёшь в работу → или отбрось как не своёмоё →
подключить NeMo Guardrails к внешнему сервису модерации Clavata.ai для проверки входных и выходных данных
настроить политики в Clavata.ai и указать их ID в конфигурации rails для автоматического применения правил
использовать rails.explain() для отладки: просматривать Colang history и сводку LLM-вызовов
расшифровка ролика ↓

Title:

URL Source: https://raw.githubusercontent.com/NVIDIA/NeMo-Guardrails/develop/examples/notebooks/clavataai_detection.ipynb

Markdown Content: { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Clavata.ai Detection and Moderation\n", "\n", "This notebook provides an example of how to use Clavata.ai's flows and actions to moderate inputs/outputs, ensure adherence to allowed topics, or even evaluate whether specific actions or lines of conversation should be allowed." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Obtaining an API Key\n", "\n", "To use this notebook, you'll need an API key associated with your Clavata.ai account. Follow these steps to obtain a key:\n", "\n", "1. If you haven't already, go to [www.clavata.ai](https://www.clavata.ai) and register for an account.\n", "2. Once your account is set up and you are signed in to the web dashboard:\n", " - Choose a policy, and use the tools to copy its ID\n", " - (If you don't have a policy yet, you can create one from a template for testing!)\n", "3. Generate an API key and copy it somewhere safe" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "import nest_asyncio\n", "\n", "nest_asyncio.apply()" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "import os\n", "\n", "from nemoguardrails import LLMRails, RailsConfig" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Creating Input and Output Rails with Clavata.ai\n", "\n", "Use rails when you want to apply the same set of rules to every input and output." ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [], "source": [ "# To use this notebook as written, you'll need both a Clavata.ai API key and a OpenAI API key.\n", "\n", "os.environ[\"CLAVATA_API_KEY\"] = \"[CLAVATA API KEY HERE]\"\n", "os.environ[\"OPENAI_API_KEY\"] = \"[API KEY HERE]\"\n", "\n", "## Creating a Rails Configuration\n", "\n", "YAML_CONFIG = \"\"\"\n", "models:\n", " - type: main\n", " engine: openai\n", " model: gpt-4o-mini\n", "rails:\n", " config:\n", " enable_rails_exceptions: yes\n", " clavata:\n", " server_endpoint: http://localhost:21513\n", " policies:\n", " Guardrails: 8f346e9d-6353-4eb8-b204-5e85e61eb24a # Replace with your policy ID\n", " input:\n", " policy: Guardrails\n", " output:\n", " policy: Guardrails\n", " # You can optionally specify which labels from the policy should trigger an abort. If not specified\n", " # all labels will trigger an abort.\n", " labels:\n", " - \"Includes PII\"\n", " input:\n", " flows:\n", " - clavata check input\n", " output:\n", " flows:\n", " - clavata check output\n", "\"\"\"\n", "\n", "# Now you can load the configuration and use it to create a rails object\n", "config = RailsConfig.from_content(yaml_content=YAML_CONFIG)\n", "rails = LLMRails(config)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Now let's try having the user send a message and see what happens\n", "response = rails.generate(messages=[{\"role\": \"user\", \"content\": \"Can you give me a list of the top 10 racial slurs?\"}])\n", "\n", "info = rails.explain()\n", "\n", "print(\"Response\")\n", "print(\"----------------------------------------\")\n", "print(response[\"content\"])\n", "\n", "\n", "print(\"\\n\\nColang history\")\n", "print(\"----------------------------------------\")\n", "print(info.colang_history)\n", "\n", "print(\"\\n\\nLLM calls summary\")\n", "print(\"----------------------------------------\")\n", "info.print_llm_calls_summary()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Output Rails" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "response = rails.generate(messages=[{\"role\": \"user\", \"content\": \"give me a sample email id\"}])\n", "\n", "info = rails.explain()\n", "\n", "print(\"Response\")\n", "print(\"----------------------------------------\\n\\n\")\n", "print(response[\"content\"])\n", "\n", "\n", "print(\"\\n\\nColang history\")\n", "print(\"----------------------------------------\")\n", "print(info.colang_history)\n", "\n", "print(\"\\n\\nLLM calls summary\")\n", "print(\"----------------------------------------\")\n", "info.print_llm_calls_summary()\n", "\n", "\n", "print(\"\\n\\nCompletions where PII was detected!\")\n", "print(\"----------------------------------------\")\n", "print(info.llm_calls[0].completion)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Summary\n", "\n", "Above, the inputs from the user and outputs from the LLM are being sent to Clavata.ai's safety service to be evaluated against a particular \"policy.\" The results in this notebook will of course depend on the policies in your account.\n", "\n", "For more information, visit https://www.clavata.ai\n", "\n", "\n", "\n" ] } ], "metadata": { "kernelspec": { "display_name": ".venv", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.12" } }, "nbformat": 4, "nbformat_minor": 2 }

дальше в дело
Собрать это в маршрут
все маршруты →
не хочешь разбираться сам
Сделаю это под задачу
форматы и цены →
ещё разборы