Skip to content

Commit ae6732e

Browse files
authored
Merge pull request #447 from guardrails-ai/notebook-fixes
Fix notebook execution
2 parents 5528c2c + 16b5b27 commit ae6732e

File tree

12 files changed

+202
-238
lines changed

12 files changed

+202
-238
lines changed

.github/workflows/ci.yml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,48 @@ jobs:
139139
flags: unittests
140140
name: codecov-umbrella
141141
fail_ci_if_error: true
142+
143+
Notebooks:
144+
runs-on: ubuntu-latest
145+
if: ${{ always() && contains(join(needs.*.result, ','), 'success') }}
146+
needs: [Linting, Typing, Pytests]
147+
148+
env:
149+
COHERE_API_KEY: ${{ secrets.COHERE_API_KEY }}
150+
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
151+
152+
153+
steps:
154+
- name: Checkout repository
155+
uses: actions/checkout@v2
156+
157+
- name: Set up Python
158+
uses: actions/setup-python@v2
159+
with:
160+
python-version: 3.11.x
161+
162+
- name: Poetry cache
163+
uses: actions/cache@v3
164+
with:
165+
path: ~/.cache/pypoetry
166+
key: poetry-cache-${{ runner.os }}-${{ steps.setup_python.outputs.python-version }}-${{ env.POETRY_VERSION }}
167+
168+
- name: Install Poetry
169+
uses: snok/install-poetry@v1
170+
with:
171+
virtualenvs-create: true
172+
virtualenvs-in-project: true
173+
installer-parallel: true
174+
175+
- name: Install dependencies
176+
run: |
177+
make full;
178+
poetry add openai==0.28.1 jupyter nbconvert cohere;
179+
# pip install openai==0.28.1 jupyter nbconvert;
180+
# pip install .;
181+
182+
- name: Check for pypdfium2
183+
run: poetry run pip show pypdfium2
184+
185+
- name: Execute notebooks and check for errors
186+
run: ./.github/workflows/scripts/run_notebooks.sh

.github/workflows/examples_check.yml

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,21 @@ jobs:
3131

3232
- name: Install Poetry
3333
uses: snok/install-poetry@v1
34+
with:
35+
virtualenvs-create: true
36+
virtualenvs-in-project: true
37+
installer-parallel: true
3438

3539
- name: Install dependencies
36-
run: make full; pip install jupyter nbconvert; pip install .
40+
run: |
41+
make full;
42+
poetry add openai==0.28.1 jupyter nbconvert cohere;
43+
# pip install openai==0.28.1 jupyter nbconvert;
44+
# pip install .;
45+
46+
- name: Check for pypdfium2
47+
run: poetry run pip show pypdfium2
3748

3849
- name: Execute notebooks and check for errors
39-
run: |
40-
cd docs/examples
41-
for notebook in $(ls *.ipynb); do
42-
jupyter nbconvert --to notebook --execute "$notebook"
43-
if [ $? -ne 0 ]; then
44-
echo "Error found in $notebook"
45-
exit 1
46-
fi
47-
done
50+
run: ./.github/workflows/scripts/run_notebooks.sh
51+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/bin/bash
2+
3+
mkdir /tmp/nltk_data;
4+
poetry run python -m nltk.downloader -d /tmp/nltk_data punkt;
5+
export NLTK_DATA=/tmp/nltk_data;
6+
7+
cd docs/examples
8+
9+
# Function to process a notebook
10+
process_notebook() {
11+
notebook="$1"
12+
if [ "$notebook" != "valid_chess_moves.ipynb" ] && [ "$notebook" != "translation_with_quality_check.ipynb" ]; then
13+
echo "Processing $notebook..."
14+
poetry run jupyter nbconvert --to notebook --execute "$notebook"
15+
if [ $? -ne 0 ]; then
16+
echo "Error found in $notebook"
17+
echo "Error in $notebook. See logs for details." >> errors.txt
18+
fi
19+
fi
20+
}
21+
22+
export -f process_notebook # Export the function for parallel execution
23+
24+
# Create a file to collect errors
25+
> errors.txt
26+
27+
# Run in parallel
28+
ls *.ipynb | parallel process_notebook
29+
30+
# Check if there were any errors
31+
if [ -s errors.txt ]; then
32+
echo "Some notebooks had errors"
33+
cat errors.txt
34+
exit 1
35+
else
36+
echo "All notebooks ran successfully."
37+
fi

docs/examples/bug_free_python_code.ipynb

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
},
3939
{
4040
"cell_type": "code",
41-
"execution_count": 1,
41+
"execution_count": 39,
4242
"metadata": {},
4343
"outputs": [],
4444
"source": [
@@ -75,13 +75,12 @@
7575
},
7676
{
7777
"cell_type": "code",
78-
"execution_count": 2,
78+
"execution_count": 40,
7979
"metadata": {},
8080
"outputs": [],
8181
"source": [
8282
"from pydantic import BaseModel, Field\n",
8383
"from guardrails.validators import BugFreePython\n",
84-
"from guardrails.datatypes import PythonCode\n",
8584
"\n",
8685
"prompt = \"\"\"\n",
8786
"Given the following high level leetcode problem description, write a short Python code snippet that solves the problem.\n",
@@ -92,7 +91,7 @@
9291
"${gr.complete_json_suffix}\"\"\"\n",
9392
"\n",
9493
"class BugFreePythonCode(BaseModel):\n",
95-
" python_code: PythonCode = Field(validators=[BugFreePython(on_fail=\"reask\")])\n",
94+
" python_code: str = Field(validators=[BugFreePython(on_fail=\"reask\")])\n",
9695
"\n",
9796
" class Config:\n",
9897
" arbitrary_types_allowed = True"
@@ -114,7 +113,7 @@
114113
},
115114
{
116115
"cell_type": "code",
117-
"execution_count": 3,
116+
"execution_count": 41,
118117
"metadata": {},
119118
"outputs": [],
120119
"source": [
@@ -132,9 +131,18 @@
132131
},
133132
{
134133
"cell_type": "code",
135-
"execution_count": 14,
134+
"execution_count": 42,
136135
"metadata": {},
137-
"outputs": [],
136+
"outputs": [
137+
{
138+
"name": "stderr",
139+
"output_type": "stream",
140+
"text": [
141+
"/Users/zaydsimjee/workspace/guardrails/guardrails/validatorsattr.py:285: UserWarning: Validator bug-free-python is not valid for element pythoncode.\n",
142+
" warnings.warn(\n"
143+
]
144+
}
145+
],
138146
"source": [
139147
"guard = gd.Guard.from_rail_string(rail_str)"
140148
]
@@ -148,7 +156,7 @@
148156
},
149157
{
150158
"cell_type": "code",
151-
"execution_count": 4,
159+
"execution_count": 43,
152160
"metadata": {},
153161
"outputs": [],
154162
"source": [
@@ -165,7 +173,7 @@
165173
},
166174
{
167175
"cell_type": "code",
168-
"execution_count": 5,
176+
"execution_count": 44,
169177
"metadata": {},
170178
"outputs": [
171179
{
@@ -182,7 +190,7 @@
182190
"Given below is XML that describes the information to extract from this document and the tags to extract it into.\n",
183191
"\n",
184192
"<span style=\"font-weight: bold\">&lt;</span><span style=\"color: #ff00ff; text-decoration-color: #ff00ff; font-weight: bold\">output</span><span style=\"color: #000000; text-decoration-color: #000000\">&gt;</span>\n",
185-
"<span style=\"color: #000000; text-decoration-color: #000000\"> &lt;pythoncode </span><span style=\"color: #808000; text-decoration-color: #808000\">name</span><span style=\"color: #000000; text-decoration-color: #000000\">=</span><span style=\"color: #008000; text-decoration-color: #008000\">\"python_code\"</span><span style=\"color: #000000; text-decoration-color: #000000\"> </span><span style=\"color: #808000; text-decoration-color: #808000\">format</span><span style=\"color: #000000; text-decoration-color: #000000\">=</span><span style=\"color: #008000; text-decoration-color: #008000\">\"bug-free-python\"</span><span style=\"color: #800080; text-decoration-color: #800080\">/</span><span style=\"color: #000000; text-decoration-color: #000000\">&gt;</span>\n",
193+
"<span style=\"color: #000000; text-decoration-color: #000000\"> &lt;string </span><span style=\"color: #808000; text-decoration-color: #808000\">name</span><span style=\"color: #000000; text-decoration-color: #000000\">=</span><span style=\"color: #008000; text-decoration-color: #008000\">\"python_code\"</span><span style=\"color: #000000; text-decoration-color: #000000\"> </span><span style=\"color: #808000; text-decoration-color: #808000\">format</span><span style=\"color: #000000; text-decoration-color: #000000\">=</span><span style=\"color: #008000; text-decoration-color: #008000\">\"bug-free-python\"</span><span style=\"color: #800080; text-decoration-color: #800080\">/</span><span style=\"color: #000000; text-decoration-color: #000000\">&gt;</span>\n",
186194
"<span style=\"color: #000000; text-decoration-color: #000000\">&lt;</span><span style=\"color: #800080; text-decoration-color: #800080\">/</span><span style=\"color: #ff00ff; text-decoration-color: #ff00ff\">output</span><span style=\"color: #000000; text-decoration-color: #000000\">&gt;</span>\n",
187195
"\n",
188196
"\n",
@@ -211,7 +219,7 @@
211219
"Given below is XML that describes the information to extract from this document and the tags to extract it into.\n",
212220
"\n",
213221
"\u001b[1m<\u001b[0m\u001b[1;95moutput\u001b[0m\u001b[39m>\u001b[0m\n",
214-
"\u001b[39m <pythoncode \u001b[0m\u001b[33mname\u001b[0m\u001b[39m=\u001b[0m\u001b[32m\"python_code\"\u001b[0m\u001b[39m \u001b[0m\u001b[33mformat\u001b[0m\u001b[39m=\u001b[0m\u001b[32m\"bug\u001b[0m\u001b[32m-free-python\"\u001b[0m\u001b[35m/\u001b[0m\u001b[39m>\u001b[0m\n",
222+
"\u001b[39m <string \u001b[0m\u001b[33mname\u001b[0m\u001b[39m=\u001b[0m\u001b[32m\"python_code\"\u001b[0m\u001b[39m \u001b[0m\u001b[33mformat\u001b[0m\u001b[39m=\u001b[0m\u001b[32m\"bug\u001b[0m\u001b[32m-free-python\"\u001b[0m\u001b[35m/\u001b[0m\u001b[39m>\u001b[0m\n",
215223
"\u001b[39m<\u001b[0m\u001b[35m/\u001b[0m\u001b[95moutput\u001b[0m\u001b[39m>\u001b[0m\n",
216224
"\n",
217225
"\n",
@@ -246,17 +254,9 @@
246254
},
247255
{
248256
"cell_type": "code",
249-
"execution_count": 6,
257+
"execution_count": 45,
250258
"metadata": {},
251-
"outputs": [
252-
{
253-
"name": "stderr",
254-
"output_type": "stream",
255-
"text": [
256-
"Async event loop found, but guard was invoked synchronously.For validator parallelization, please call `validate_async` instead.\n"
257-
]
258-
}
259-
],
259+
"outputs": [],
260260
"source": [
261261
"import openai\n",
262262
"\n",
@@ -285,7 +285,7 @@
285285
},
286286
{
287287
"cell_type": "code",
288-
"execution_count": 7,
288+
"execution_count": 46,
289289
"metadata": {},
290290
"outputs": [
291291
{
@@ -326,7 +326,7 @@
326326
},
327327
{
328328
"cell_type": "code",
329-
"execution_count": 8,
329+
"execution_count": 47,
330330
"metadata": {},
331331
"outputs": [
332332
{
@@ -371,7 +371,7 @@
371371
},
372372
{
373373
"cell_type": "code",
374-
"execution_count": 9,
374+
"execution_count": 48,
375375
"metadata": {},
376376
"outputs": [
377377
{
@@ -413,7 +413,7 @@
413413
"name": "python",
414414
"nbconvert_exporter": "python",
415415
"pygments_lexer": "ipython3",
416-
"version": "3.11.4"
416+
"version": "3.11.5"
417417
},
418418
"orig_nbformat": 4,
419419
"vscode": {

docs/examples/check_for_pii.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@
259259
"name": "python",
260260
"nbconvert_exporter": "python",
261261
"pygments_lexer": "ipython3",
262-
"version": "3.11.6"
262+
"version": "3.11.5"
263263
}
264264
},
265265
"nbformat": 4,

docs/examples/generate_structured_data_cohere.ipynb

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,5 @@
11
{
22
"cells": [
3-
{
4-
"cell_type": "markdown",
5-
"id": "91ba93d4",
6-
"metadata": {},
7-
"source": [
8-
"First, install dependencies. In addition to standard dependencies, this notebook uses cohere, so we install the cohere SDK."
9-
]
10-
},
11-
{
12-
"cell_type": "code",
13-
"execution_count": 7,
14-
"id": "899221f3-0496-439b-b284-d6a6603b08c1",
15-
"metadata": {},
16-
"outputs": [
17-
{
18-
"name": "stdout",
19-
"output_type": "stream",
20-
"text": [
21-
"\n",
22-
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m23.2.1\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m23.3\u001b[0m\n",
23-
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n",
24-
"Note: you may need to restart the kernel to use updated packages.\n"
25-
]
26-
}
27-
],
28-
"source": [
29-
"%pip install cohere guardrails-ai pydantic==1.10.9 typing openai rich -q"
30-
]
31-
},
323
{
334
"cell_type": "markdown",
345
"id": "5c4d8348",

0 commit comments

Comments
 (0)