|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "0f4d10ab", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# LangChain\n", |
| 9 | + "\n", |
| 10 | + "## Overview\n", |
| 11 | + "\n", |
| 12 | + "This is a comprehensive guide on integrating Guardrails with [LangChain](https://github.com/langchain-ai/langchain), a framework for developing applications powered by large language models. By combining the validation capabilities of Guardrails with the flexible architecture of LangChain, you can create reliable and robust AI applications.\n", |
| 13 | + "\n", |
| 14 | + "### Key Features\n", |
| 15 | + "\n", |
| 16 | + "- **Easy Integration**: Guardrails can be seamlessly added to LangChain's LCEL syntax, allowing for quick implementation of validation checks.\n", |
| 17 | + "- **Flexible Validation**: Guardrails provides various validators that can be used to enforce structural, type, and quality constraints on LLM outputs.\n", |
| 18 | + "- **Corrective Actions**: When validation fails, Guardrails can take corrective measures, such as retrying LLM prompts or fixing outputs.\n", |
| 19 | + "- **Compatibility**: Works with different LLMs and can be used in various LangChain components like chains, agents, and retrieval strategies.\n", |
| 20 | + "\n", |
| 21 | + "## Prerequisites\n", |
| 22 | + "\n", |
| 23 | + "1. Ensure you have the following langchain packages installed. Also install Guardrails" |
| 24 | + ] |
| 25 | + }, |
| 26 | + { |
| 27 | + "cell_type": "code", |
| 28 | + "execution_count": null, |
| 29 | + "id": "382bd905", |
| 30 | + "metadata": {}, |
| 31 | + "outputs": [], |
| 32 | + "source": [ |
| 33 | + "! pip install guardrails-ai langchain langchain_openai" |
| 34 | + ] |
| 35 | + }, |
| 36 | + { |
| 37 | + "cell_type": "markdown", |
| 38 | + "id": "3594ef6c", |
| 39 | + "metadata": {}, |
| 40 | + "source": [ |
| 41 | + "2. As a prerequisite we install the necessary validators from the Guardrails Hub:" |
| 42 | + ] |
| 43 | + }, |
| 44 | + { |
| 45 | + "cell_type": "code", |
| 46 | + "execution_count": null, |
| 47 | + "id": "05635d8e", |
| 48 | + "metadata": {}, |
| 49 | + "outputs": [], |
| 50 | + "source": [ |
| 51 | + "! guardrails hub install hub://guardrails/competitor_check --quiet\n", |
| 52 | + "! guardrails hub install hub://guardrails/toxic_language --quiet" |
| 53 | + ] |
| 54 | + }, |
| 55 | + { |
| 56 | + "cell_type": "markdown", |
| 57 | + "id": "9ab6fdd9", |
| 58 | + "metadata": {}, |
| 59 | + "source": [ |
| 60 | + "- `CompetitorCheck`: Identifies and optionally removes mentions of specified competitor names.\n", |
| 61 | + "- `ToxicLanguage`: Detects and optionally removes toxic or inappropriate language from the output.\n" |
| 62 | + ] |
| 63 | + }, |
| 64 | + { |
| 65 | + "cell_type": "markdown", |
| 66 | + "id": "325a69d3", |
| 67 | + "metadata": {}, |
| 68 | + "source": [ |
| 69 | + "## Basic Integration\n", |
| 70 | + "\n", |
| 71 | + "Here's a basic example of how to integrate Guardrails with a LangChain LCEL chain:\n", |
| 72 | + "\n", |
| 73 | + "1. Import the required imports and do the OpenAI Model Initialization" |
| 74 | + ] |
| 75 | + }, |
| 76 | + { |
| 77 | + "cell_type": "code", |
| 78 | + "execution_count": 1, |
| 79 | + "id": "ae149cdb", |
| 80 | + "metadata": {}, |
| 81 | + "outputs": [ |
| 82 | + { |
| 83 | + "name": "stderr", |
| 84 | + "output_type": "stream", |
| 85 | + "text": [ |
| 86 | + "/Users/calebcourier/Projects/support/langchain/.venv/lib/python3.12/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n", |
| 87 | + " from .autonotebook import tqdm as notebook_tqdm\n" |
| 88 | + ] |
| 89 | + } |
| 90 | + ], |
| 91 | + "source": [ |
| 92 | + "from langchain_openai import ChatOpenAI\n", |
| 93 | + "from langchain_core.output_parsers import StrOutputParser\n", |
| 94 | + "from langchain_core.prompts import ChatPromptTemplate\n", |
| 95 | + "\n", |
| 96 | + "model = ChatOpenAI(model=\"gpt-4\")" |
| 97 | + ] |
| 98 | + }, |
| 99 | + { |
| 100 | + "cell_type": "markdown", |
| 101 | + "id": "7701d9e4", |
| 102 | + "metadata": {}, |
| 103 | + "source": [ |
| 104 | + "2. Create a Guard object with two validators: CompetitorCheck and ToxicLanguage." |
| 105 | + ] |
| 106 | + }, |
| 107 | + { |
| 108 | + "cell_type": "code", |
| 109 | + "execution_count": 2, |
| 110 | + "id": "67f810db", |
| 111 | + "metadata": {}, |
| 112 | + "outputs": [], |
| 113 | + "source": [ |
| 114 | + "from guardrails import Guard\n", |
| 115 | + "from guardrails.hub import CompetitorCheck, ToxicLanguage\n", |
| 116 | + "\n", |
| 117 | + "competitors_list = [\"delta\", \"american airlines\", \"united\"]\n", |
| 118 | + "guard = Guard().use_many(\n", |
| 119 | + " CompetitorCheck(competitors=competitors_list, on_fail=\"fix\"),\n", |
| 120 | + " ToxicLanguage(on_fail=\"filter\"),\n", |
| 121 | + ")" |
| 122 | + ] |
| 123 | + }, |
| 124 | + { |
| 125 | + "cell_type": "markdown", |
| 126 | + "id": "aff173e9", |
| 127 | + "metadata": {}, |
| 128 | + "source": [ |
| 129 | + "3. Define the LCEL chain components and pipe the prompt, model, output parser, and the Guard together.\n", |
| 130 | + "The `guard.to_runnable()` method converts the Guardrails guard into a LangChain-compatible runnable object." |
| 131 | + ] |
| 132 | + }, |
| 133 | + { |
| 134 | + "cell_type": "code", |
| 135 | + "execution_count": 3, |
| 136 | + "id": "7d6c4dc1", |
| 137 | + "metadata": {}, |
| 138 | + "outputs": [], |
| 139 | + "source": [ |
| 140 | + "prompt = ChatPromptTemplate.from_template(\"Answer this question {question}\")\n", |
| 141 | + "output_parser = StrOutputParser()\n", |
| 142 | + "\n", |
| 143 | + "chain = prompt | model | guard.to_runnable() | output_parser" |
| 144 | + ] |
| 145 | + }, |
| 146 | + { |
| 147 | + "cell_type": "markdown", |
| 148 | + "id": "f293a6f7", |
| 149 | + "metadata": {}, |
| 150 | + "source": [ |
| 151 | + "4. Invoke the chain" |
| 152 | + ] |
| 153 | + }, |
| 154 | + { |
| 155 | + "cell_type": "code", |
| 156 | + "execution_count": 4, |
| 157 | + "id": "7c037923", |
| 158 | + "metadata": {}, |
| 159 | + "outputs": [ |
| 160 | + { |
| 161 | + "name": "stderr", |
| 162 | + "output_type": "stream", |
| 163 | + "text": [ |
| 164 | + "/Users/calebcourier/Projects/support/langchain/.venv/lib/python3.12/site-packages/guardrails/validator_service/__init__.py:84: UserWarning: Could not obtain an event loop. Falling back to synchronous validation.\n", |
| 165 | + " warnings.warn(\n" |
| 166 | + ] |
| 167 | + }, |
| 168 | + { |
| 169 | + "name": "stdout", |
| 170 | + "output_type": "stream", |
| 171 | + "text": [ |
| 172 | + "1. Southwest Airlines\n", |
| 173 | + "2. Delta Air Lines\n", |
| 174 | + "3. American Airlines\n", |
| 175 | + "4. United Airlines\n", |
| 176 | + "5. JetBlue Airways\n" |
| 177 | + ] |
| 178 | + } |
| 179 | + ], |
| 180 | + "source": [ |
| 181 | + "result = chain.invoke(\n", |
| 182 | + " {\"question\": \"What are the top five airlines for domestic travel in the US?\"}\n", |
| 183 | + ")\n", |
| 184 | + "print(result)" |
| 185 | + ] |
| 186 | + }, |
| 187 | + { |
| 188 | + "cell_type": "markdown", |
| 189 | + "id": "2cd4c67b", |
| 190 | + "metadata": {}, |
| 191 | + "source": [ |
| 192 | + "Example output:\n", |
| 193 | + " ```\n", |
| 194 | + " 1. Southwest Airlines\n", |
| 195 | + " 3. JetBlue Airways\n", |
| 196 | + " ```\n", |
| 197 | + "\n", |
| 198 | + "In this example, the chain sends the question to the model and then applies Guardrails validators to the response. The CompetitorCheck validator specifically removes mentions of the specified competitors (Delta, American Airlines, United), resulting in a filtered list of non-competitor airlines." |
| 199 | + ] |
| 200 | + } |
| 201 | + ], |
| 202 | + "metadata": { |
| 203 | + "kernelspec": { |
| 204 | + "display_name": ".venv", |
| 205 | + "language": "python", |
| 206 | + "name": "python3" |
| 207 | + }, |
| 208 | + "language_info": { |
| 209 | + "codemirror_mode": { |
| 210 | + "name": "ipython", |
| 211 | + "version": 3 |
| 212 | + }, |
| 213 | + "file_extension": ".py", |
| 214 | + "mimetype": "text/x-python", |
| 215 | + "name": "python", |
| 216 | + "nbconvert_exporter": "python", |
| 217 | + "pygments_lexer": "ipython3", |
| 218 | + "version": "3.10.16" |
| 219 | + } |
| 220 | + }, |
| 221 | + "nbformat": 4, |
| 222 | + "nbformat_minor": 5 |
| 223 | +} |
0 commit comments