|
7 | 7 | "# Check if a competitor is named\n", |
8 | 8 | "\n", |
9 | 9 | "!!! note\n", |
10 | | - " To download this example as a Jupyter notebook, click [here](https://github.com/guardrails-ai/guardrails/blob/main/docs/examples/competitors_check.ipynb)." |
| 10 | + "To download this example as a Jupyter notebook, click [here](https://github.com/guardrails-ai/guardrails/blob/main/docs/examples/competitors_check.ipynb).\n" |
11 | 11 | ] |
12 | 12 | }, |
13 | 13 | { |
14 | 14 | "cell_type": "code", |
15 | | - "execution_count": 12, |
| 15 | + "execution_count": 1, |
16 | 16 | "metadata": {}, |
17 | 17 | "outputs": [], |
18 | 18 | "source": [ |
19 | 19 | "import guardrails as gd\n", |
20 | | - "from guardrails.validators import CompetitorCheck" |
| 20 | + "from guardrails.validators import CompetitorCheck\n", |
| 21 | + "from rich import print" |
21 | 22 | ] |
22 | 23 | }, |
23 | 24 | { |
24 | 25 | "cell_type": "markdown", |
25 | 26 | "metadata": {}, |
26 | 27 | "source": [ |
27 | | - "## Using competitor check validator \n", |
| 28 | + "## Using competitor check validator\n", |
28 | 29 | "\n", |
29 | | - "This validator checks LLM output to flag sentences naming one of your competitors and removes those sentences from the final output. When setting on-fail to 'fix' this validator will remove the flagged sentences from the output. You need to provide an extensive list of your competitors' names including all common variations (e.g. JP Morgan, JP Morgan Chase, etc.) the compilation of this list will have an impact on the ultimate outcome of the validation. " |
| 30 | + "This validator checks LLM output to flag sentences naming one of your competitors and removes those sentences from the final output. When setting on-fail to 'fix' this validator will remove the flagged sentences from the output. You need to provide an extensive list of your competitors' names including all common variations (e.g. JP Morgan, JP Morgan Chase, etc.) the compilation of this list will have an impact on the ultimate outcome of the validation.\n" |
30 | 31 | ] |
31 | 32 | }, |
32 | 33 | { |
33 | 34 | "cell_type": "markdown", |
34 | 35 | "metadata": {}, |
35 | 36 | "source": [ |
36 | | - "## Set up a competitors list" |
| 37 | + "## Set up a competitors list\n" |
37 | 38 | ] |
38 | 39 | }, |
39 | 40 | { |
40 | 41 | "cell_type": "code", |
41 | | - "execution_count": 13, |
| 42 | + "execution_count": 2, |
42 | 43 | "metadata": {}, |
43 | 44 | "outputs": [], |
44 | 45 | "source": [ |
45 | 46 | "# Generate competitors list\n", |
46 | 47 | "competitors_list = [\n", |
47 | | - " 'Acorns',\n", |
48 | | - " 'Citigroup',\n", |
49 | | - " 'Citi',\n", |
50 | | - " 'Fidelity Investments',\n", |
51 | | - " 'Fidelity',\n", |
52 | | - " 'JP Morgan Chase and company',\n", |
53 | | - " 'JP Morgan',\n", |
54 | | - " 'JP Morgan Chase',\n", |
55 | | - " 'JPMorgan Chase',\n", |
56 | | - " 'Chase'\n", |
57 | | - " 'M1 Finance',\n", |
58 | | - " 'Stash Financial Incorporated',\n", |
59 | | - " 'Stash',\n", |
60 | | - " 'Tastytrade Incorporated',\n", |
61 | | - " 'Tastytrade',\n", |
62 | | - " 'ZacksTrade',\n", |
63 | | - " 'Zacks Trade']" |
| 48 | + " \"Acorns\",\n", |
| 49 | + " \"Citigroup\",\n", |
| 50 | + " \"Citi\",\n", |
| 51 | + " \"Fidelity Investments\",\n", |
| 52 | + " \"Fidelity\",\n", |
| 53 | + " \"JP Morgan Chase and company\",\n", |
| 54 | + " \"JP Morgan\",\n", |
| 55 | + " \"JP Morgan Chase\",\n", |
| 56 | + " \"JPMorgan Chase\",\n", |
| 57 | + " \"Chase\" \"M1 Finance\",\n", |
| 58 | + " \"Stash Financial Incorporated\",\n", |
| 59 | + " \"Stash\",\n", |
| 60 | + " \"Tastytrade Incorporated\",\n", |
| 61 | + " \"Tastytrade\",\n", |
| 62 | + " \"ZacksTrade\",\n", |
| 63 | + " \"Zacks Trade\",\n", |
| 64 | + "]" |
64 | 65 | ] |
65 | 66 | }, |
66 | 67 | { |
67 | 68 | "cell_type": "markdown", |
68 | 69 | "metadata": {}, |
69 | 70 | "source": [ |
70 | | - "## Set up example text to test the validator" |
| 71 | + "## Set up example text to test the validator\n" |
71 | 72 | ] |
72 | 73 | }, |
73 | 74 | { |
74 | 75 | "cell_type": "code", |
75 | | - "execution_count": 14, |
| 76 | + "execution_count": 3, |
76 | 77 | "metadata": {}, |
77 | 78 | "outputs": [], |
78 | 79 | "source": [ |
79 | | - "# Define some text to test the validator \n", |
80 | | - "text=\"\"\"\\\n", |
| 80 | + "# Define some text to test the validator\n", |
| 81 | + "text = \"\"\"\\\n", |
81 | 82 | "In the dynamic realm of finance, several prominent entities have emerged as key players,\\\n", |
82 | 83 | "leaving an indelible mark on the industry. Acorns, a fintech innovator, has revolutionized saving \\\n", |
83 | 84 | "and investing with its user-friendly app. Citigroup, a multinational investment bank, stands as a \\\n", |
|
102 | 103 | "Here, we use the text we defined above as an example llm output (`llm_output`).\n", |
103 | 104 | "\n", |
104 | 105 | "We also set the on_fail behavior to 'fix' so that the validator will remove the sentences that mention competitors from the output.\n", |
105 | | - "We can adjust this behavior by changing it to 'reask' or 'throw'." |
| 106 | + "We can adjust this behavior by changing it to 'reask' or 'throw'.\n" |
106 | 107 | ] |
107 | 108 | }, |
108 | 109 | { |
109 | 110 | "cell_type": "code", |
110 | | - "execution_count": 15, |
| 111 | + "execution_count": 4, |
111 | 112 | "metadata": {}, |
112 | 113 | "outputs": [ |
113 | 114 | { |
114 | | - "name": "stdout", |
115 | | - "output_type": "stream", |
116 | | - "text": [ |
117 | | - "In the dynamic realm of finance, several prominent entities have emerged as key players,leaving an indelible mark on the industry. HSBC, with its extensive global network, has become a powerhouse in the banking sector, catering to the needs of millions across different countries. Santander, a Spanish multinational bank, has earned a reputation for its responsible banking practices and customer-centric approach, serving as a trusted financial partner to individuals and businesses alike.\n" |
118 | | - ] |
| 115 | + "data": { |
| 116 | + "text/html": [ |
| 117 | + "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #800080; text-decoration-color: #800080; font-weight: bold\">ValidationOutcome</span><span style=\"font-weight: bold\">(</span>\n", |
| 118 | + " <span style=\"color: #808000; text-decoration-color: #808000\">raw_llm_output</span>=<span style=\"color: #008000; text-decoration-color: #008000\">'In the dynamic realm of finance, several prominent entities have emerged as key players,leaving</span>\n", |
| 119 | + "<span style=\"color: #008000; text-decoration-color: #008000\">an indelible mark on the industry. Acorns, a fintech innovator, has revolutionized saving and investing with its </span>\n", |
| 120 | + "<span style=\"color: #008000; text-decoration-color: #008000\">user-friendly app. Citigroup, a multinational investment bank, stands as a pillar of financial expertise, offering </span>\n", |
| 121 | + "<span style=\"color: #008000; text-decoration-color: #008000\">a wide array of services to clients worldwide. HSBC, with its extensive global network, has become a powerhouse in </span>\n", |
| 122 | + "<span style=\"color: #008000; text-decoration-color: #008000\">the banking sector, catering to the needs of millions across different countries. JP Morgan, a venerable </span>\n", |
| 123 | + "<span style=\"color: #008000; text-decoration-color: #008000\">institution with a rich history, has established itself as a comprehensive financial powerhouse, providing a </span>\n", |
| 124 | + "<span style=\"color: #008000; text-decoration-color: #008000\">diverse range of services from investment banking to asset management. Santander, a Spanish multinational bank, has</span>\n", |
| 125 | + "<span style=\"color: #008000; text-decoration-color: #008000\">earned a reputation for its responsible banking practices and customer-centric approach, serving as a trusted </span>\n", |
| 126 | + "<span style=\"color: #008000; text-decoration-color: #008000\">financial partner to individuals and businesses alike. Together, Acorns, Citi, HSBC, JP Morgan, and Santander have </span>\n", |
| 127 | + "<span style=\"color: #008000; text-decoration-color: #008000\">redefined the financial landscape, shaping the way we save, invest, and manage our money on a global scale.'</span>,\n", |
| 128 | + " <span style=\"color: #808000; text-decoration-color: #808000\">validated_output</span>=<span style=\"color: #008000; text-decoration-color: #008000\">'In the dynamic realm of finance, several prominent entities have emerged as key </span>\n", |
| 129 | + "<span style=\"color: #008000; text-decoration-color: #008000\">players,leaving an indelible mark on the industry. HSBC, with its extensive global network, has become a powerhouse</span>\n", |
| 130 | + "<span style=\"color: #008000; text-decoration-color: #008000\">in the banking sector, catering to the needs of millions across different countries. Santander, a Spanish </span>\n", |
| 131 | + "<span style=\"color: #008000; text-decoration-color: #008000\">multinational bank, has earned a reputation for its responsible banking practices and customer-centric approach, </span>\n", |
| 132 | + "<span style=\"color: #008000; text-decoration-color: #008000\">serving as a trusted financial partner to individuals and businesses alike.'</span>,\n", |
| 133 | + " <span style=\"color: #808000; text-decoration-color: #808000\">reask</span>=<span style=\"color: #800080; text-decoration-color: #800080; font-style: italic\">None</span>,\n", |
| 134 | + " <span style=\"color: #808000; text-decoration-color: #808000\">validation_passed</span>=<span style=\"color: #00ff00; text-decoration-color: #00ff00; font-style: italic\">True</span>,\n", |
| 135 | + " <span style=\"color: #808000; text-decoration-color: #808000\">error</span>=<span style=\"color: #800080; text-decoration-color: #800080; font-style: italic\">None</span>\n", |
| 136 | + "<span style=\"font-weight: bold\">)</span>\n", |
| 137 | + "</pre>\n" |
| 138 | + ], |
| 139 | + "text/plain": [ |
| 140 | + "\u001b[1;35mValidationOutcome\u001b[0m\u001b[1m(\u001b[0m\n", |
| 141 | + " \u001b[33mraw_llm_output\u001b[0m=\u001b[32m'In the dynamic realm of finance, several prominent entities have emerged as key players,leaving\u001b[0m\n", |
| 142 | + "\u001b[32man indelible mark on the industry. Acorns, a fintech innovator, has revolutionized saving and investing with its \u001b[0m\n", |
| 143 | + "\u001b[32muser-friendly app. Citigroup, a multinational investment bank, stands as a pillar of financial expertise, offering \u001b[0m\n", |
| 144 | + "\u001b[32ma wide array of services to clients worldwide. HSBC, with its extensive global network, has become a powerhouse in \u001b[0m\n", |
| 145 | + "\u001b[32mthe banking sector, catering to the needs of millions across different countries. JP Morgan, a venerable \u001b[0m\n", |
| 146 | + "\u001b[32minstitution with a rich history, has established itself as a comprehensive financial powerhouse, providing a \u001b[0m\n", |
| 147 | + "\u001b[32mdiverse range of services from investment banking to asset management. Santander, a Spanish multinational bank, has\u001b[0m\n", |
| 148 | + "\u001b[32mearned a reputation for its responsible banking practices and customer-centric approach, serving as a trusted \u001b[0m\n", |
| 149 | + "\u001b[32mfinancial partner to individuals and businesses alike. Together, Acorns, Citi, HSBC, JP Morgan, and Santander have \u001b[0m\n", |
| 150 | + "\u001b[32mredefined the financial landscape, shaping the way we save, invest, and manage our money on a global scale.'\u001b[0m,\n", |
| 151 | + " \u001b[33mvalidated_output\u001b[0m=\u001b[32m'In the dynamic realm of finance, several prominent entities have emerged as key \u001b[0m\n", |
| 152 | + "\u001b[32mplayers,leaving an indelible mark on the industry. HSBC, with its extensive global network, has become a powerhouse\u001b[0m\n", |
| 153 | + "\u001b[32min the banking sector, catering to the needs of millions across different countries. Santander, a Spanish \u001b[0m\n", |
| 154 | + "\u001b[32mmultinational bank, has earned a reputation for its responsible banking practices and customer-centric approach, \u001b[0m\n", |
| 155 | + "\u001b[32mserving as a trusted financial partner to individuals and businesses alike.'\u001b[0m,\n", |
| 156 | + " \u001b[33mreask\u001b[0m=\u001b[3;35mNone\u001b[0m,\n", |
| 157 | + " \u001b[33mvalidation_passed\u001b[0m=\u001b[3;92mTrue\u001b[0m,\n", |
| 158 | + " \u001b[33merror\u001b[0m=\u001b[3;35mNone\u001b[0m\n", |
| 159 | + "\u001b[1m)\u001b[0m\n" |
| 160 | + ] |
| 161 | + }, |
| 162 | + "metadata": {}, |
| 163 | + "output_type": "display_data" |
119 | 164 | } |
120 | 165 | ], |
121 | 166 | "source": [ |
122 | 167 | "# Create the Guard with the CompetitorCheck Validator\n", |
123 | 168 | "guard = gd.Guard.from_string(\n", |
124 | | - " validators=[CompetitorCheck(competitors=competitors_list, on_fail='fix')],\n", |
125 | | - " description= 'testmeout',\n", |
| 169 | + " validators=[CompetitorCheck(competitors=competitors_list, on_fail=\"fix\")],\n", |
| 170 | + " description=\"testmeout\",\n", |
126 | 171 | ")\n", |
127 | 172 | "\n", |
128 | 173 | "# Test with a given text\n", |
129 | | - "output=guard.parse(\n", |
| 174 | + "output = guard.parse(\n", |
130 | 175 | " llm_output=text,\n", |
131 | 176 | " metadata={},\n", |
132 | 177 | ")\n", |
|
136 | 181 | }, |
137 | 182 | { |
138 | 183 | "cell_type": "code", |
139 | | - "execution_count": 16, |
| 184 | + "execution_count": 5, |
140 | 185 | "metadata": {}, |
141 | 186 | "outputs": [ |
142 | 187 | { |
|
205 | 250 | " ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯\n" |
206 | 251 | ] |
207 | 252 | }, |
208 | | - "execution_count": 16, |
209 | 253 | "metadata": {}, |
210 | | - "output_type": "execute_result" |
| 254 | + "output_type": "display_data" |
211 | 255 | } |
212 | 256 | ], |
213 | 257 | "source": [ |
214 | | - "# See the guard logs\n", |
215 | | - "guard.guard_state.most_recent_call.tree" |
| 258 | + "# See guard history\n", |
| 259 | + "print(guard.history.last.tree)" |
216 | 260 | ] |
217 | 261 | } |
218 | 262 | ], |
|
232 | 276 | "name": "python", |
233 | 277 | "nbconvert_exporter": "python", |
234 | 278 | "pygments_lexer": "ipython3", |
235 | | - "version": "3.9.17" |
| 279 | + "version": "3.11.6" |
236 | 280 | } |
237 | 281 | }, |
238 | 282 | "nbformat": 4, |
|
0 commit comments