Skip to content

[feat] Streaming Update #798

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 106 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
106 commits
Select commit Hold shift + click to select a range
29ee128
preliminary code and pseudocode
nichwch May 7, 2024
66a5333
add chunk accumulation strategy to Validator base class
nichwch May 7, 2024
7831466
handle case where llm chunk > validator chunk in validator class
nichwch May 8, 2024
2dbae2e
added some questions
nichwch May 8, 2024
1e3544d
change stream_runner to handle the result of iterable validate
nichwch May 8, 2024
e9084e1
format
nichwch May 8, 2024
1269f68
change validator base to use a chunking function instead of specifyin…
nichwch May 10, 2024
b454cd5
connect streaming all the way down call chain, include validated chun…
nichwch May 10, 2024
b64ab4e
change execute_validator to handle streaming
nichwch May 10, 2024
bf2bd32
make validate take stream parameter, remove validate_stream in top le…
nichwch May 10, 2024
c79e9b2
use wyatts sentence splitting strategy
nichwch May 10, 2024
4583cb9
import nltk
nichwch May 10, 2024
f1b4a88
use stream-enabled execute_validator
nichwch May 14, 2024
289745c
format
nichwch May 14, 2024
58d8eed
fix bug where json_schema was being called with streaming
nichwch May 15, 2024
947f476
conditionally use old logic for json_schema to avoid breaking json_sc…
nichwch May 16, 2024
8b2c154
validate remainders
nichwch May 16, 2024
d6c3739
merge in main
nichwch May 16, 2024
0ab245c
new chunk span validation schema
nichwch May 16, 2024
a320464
field for reason that validation failed for a given span
nichwch May 16, 2024
93bb781
add validated_chunk to ValidationResult
nichwch May 17, 2024
1381821
add helper method to get a list of error spans relative to llm output
nichwch May 17, 2024
3ccdda1
conceptual question
nichwch May 17, 2024
6fdbcd1
Merge branch 'main' into nichwch/chunk-accumulation-rewrite
nichwch May 17, 2024
f455ae2
Merge branch 'nichwch/chunk-accumulation-rewrite' into nichwch/stream…
nichwch May 17, 2024
74485eb
turn chunking_function into class method
nichwch May 17, 2024
a39b5af
incomplete tests for streaming chunk accumulation
nichwch May 17, 2024
0ae850e
format
nichwch May 20, 2024
847dd0a
remove print
nichwch May 20, 2024
f0b3030
fix a few bugs uncovered by testing
nichwch May 20, 2024
e8b6069
tests (WIP) for streaming
nichwch May 20, 2024
628e490
Merge branch 'main' into nichwch/chunk-accumulation-rewrite
nichwch May 21, 2024
a9a91a1
merge
nichwch May 21, 2024
eec8e19
base model
nichwch May 21, 2024
8726a28
optional typing to avoid breaking existing validators
nichwch May 21, 2024
ba68eb6
top level helper function for spans on guard, patch validated_chunk i…
nichwch May 21, 2024
2607423
attempt to use openai finish_reason field
nichwch May 21, 2024
da720c3
add comment explaining problem with using openai finish_message
nichwch May 21, 2024
8bdb292
test error span behavior
nichwch May 22, 2024
c20a093
Merge branch 'main' of github.com:guardrails-ai/guardrails into nichw…
wylansford May 22, 2024
37b7014
fixes, async_step is not iterating correctly
wylansford May 24, 2024
dedfcfc
updated async streamrunner to work. Still need to fix post introspect…
wylansford May 24, 2024
0abac83
address some changes
nichwch May 28, 2024
8f45a0a
handle case where llm callable doesnt provide finished flag
nichwch May 28, 2024
dfcd3b8
Merge pull request #771 from guardrails-ai/nichwch/streaming-error-spans
nichwch May 28, 2024
fdf3c8b
adding latest
wylansford May 29, 2024
6798b68
API Client Updates (#688)
CalebCourier May 30, 2024
fe56871
Merge branch 'feat/streaming-update' into nichwch/chunk-accumulation-…
CalebCourier May 30, 2024
b05c241
try to run PR checks on PRs against feature branches
CalebCourier May 30, 2024
87d4dfb
Merge branch 'feat/streaming-update' into async_streaming
CalebCourier May 30, 2024
b52b8cb
lint, type, and test fixes
CalebCourier May 30, 2024
da468bf
hacky fix for returning result correctly
wylansford May 30, 2024
8bdb4fd
merge conflict
wylansford May 30, 2024
e985120
cleanup
wylansford May 30, 2024
e8929d9
fixing some invoke_llm streaming problems, adding todo and fixme
wylansford May 30, 2024
a2725c4
Merge branch 'nichwch/chunk-accumulation-rewrite' of github.com:guard…
wylansford May 30, 2024
0aede77
use status for validation_passed in streaming
CalebCourier May 30, 2024
1531e2c
Merge branch 'nichwch/chunk-accumulation-rewrite' into async_streaming
CalebCourier May 30, 2024
c2c0746
Merge branch 'async_streaming' of https://github.com/guardrails-ai/gu…
CalebCourier May 30, 2024
77a7eee
fix error_spans_in_output
nichwch May 31, 2024
54ccee0
fix status in validationoutcomes
nichwch May 31, 2024
4e5d479
Merge pull request #741 from guardrails-ai/nichwch/chunk-accumulation…
CalebCourier Jun 3, 2024
1b44d99
removing streaming support from parse call, doesn't conceptually make…
wylansford Jun 3, 2024
a76bcf0
Merge branch 'async_streaming' of github.com:guardrails-ai/guardrails…
wylansford Jun 3, 2024
b7186cb
removing unused __call__
wylansford Jun 3, 2024
51e3b88
make nltk a required dependency
CalebCourier Jun 4, 2024
5a260dd
adding todo for async streaming from server
wylansford Jun 4, 2024
fbca182
Merge branch 'feat/streaming-update' of github.com:guardrails-ai/guar…
wylansford Jun 4, 2024
6c1904b
comment
wylansford Jun 4, 2024
37fc698
ruff
wylansford Jun 4, 2024
7b3b2a8
typing cleanup
wylansford Jun 4, 2024
11024e2
cleanup, addressing TODO
wylansford Jun 4, 2024
97da9af
typing cleanup
wylansford Jun 4, 2024
984325f
more typing cleanup
wylansford Jun 4, 2024
1adead1
attempt to fix typing on validation result call
wylansford Jun 4, 2024
fef060d
cleaning typing
wylansford Jun 5, 2024
39607ad
cleanup related to awaitable in base validator service
wylansford Jun 5, 2024
f81ed6c
final cleanup
wylansford Jun 5, 2024
6fa82f1
final cleanup, again
wylansford Jun 5, 2024
cbfc569
async_call to call in trace
wylansford Jun 5, 2024
8d275e7
fix missing arg in mock tests
wylansford Jun 5, 2024
9166830
directly emit PassResult when not finished accumulating
nichwch Jun 5, 2024
e49a6f5
Merge branch 'async_streaming' of https://github.com/guardrails-ai/gu…
nichwch Jun 5, 2024
e1cc48e
removing kwarg for stream, replacing with positional arg
wylansford Jun 5, 2024
a71645c
moving positional args
wylansford Jun 5, 2024
5b82314
reverting, adding arg to async_validate call
wylansford Jun 5, 2024
d3c716a
filter logs in iteration.py
nichwch Jun 5, 2024
1a6b8a6
Merge branch 'async_streaming' of https://github.com/guardrails-ai/gu…
nichwch Jun 5, 2024
0a443ed
add docstrings for sentence splitters
nichwch Jun 5, 2024
79ddc17
poorly handle async validators
CalebCourier Jun 5, 2024
bb6ca03
clean up commented code
CalebCourier Jun 5, 2024
df7d71a
async validate now checks failed logs if validator logs is empty
wylansford Jun 5, 2024
ef1af2c
reverting to unfiltered logs
wylansford Jun 5, 2024
0fc2403
return none instead of no return
wylansford Jun 5, 2024
184d0dd
only filter logs when streaming
CalebCourier Jun 6, 2024
199d0a8
Merge pull request #795 from guardrails-ai/async_streaming
CalebCourier Jun 6, 2024
f0b1efa
Merge branch 'main' into feat/streaming-update
CalebCourier Jun 6, 2024
de28c7b
update poetry lock
CalebCourier Jun 6, 2024
8d73f58
fix notebook installs, fix a couple notebooks
CalebCourier Jun 6, 2024
fd2ad02
use version of notebooks from the PR
CalebCourier Jun 6, 2024
38d4597
notebook fixes
CalebCourier Jun 6, 2024
91f4bda
updating old model and api
wylansford Jun 6, 2024
8bff2a5
Merge branch 'feat/streaming-update' of github.com:guardrails-ai/guar…
wylansford Jun 6, 2024
ed58b7a
updating old model and api
wylansford Jun 6, 2024
cea7623
updating old model and api
wylansford Jun 6, 2024
fad0551
adding todo
wylansford Jun 6, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ on:
branches:
- main
- dev
- feat/*

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
Expand Down Expand Up @@ -49,8 +50,8 @@ jobs:
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11"]
pydantic-version: ["1.10.9", "2.4.2"]
openai-version: ["1.30.1"]
pydantic-version: ["==1.10.9", ">=2.x"]
openai-version: [">=1.x"]
steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
Expand All @@ -71,8 +72,8 @@ jobs:
# TODO: fix errors so that we can run `make dev` instead
run: |
make full
poetry run pip install pydantic==${{ matrix.pydantic-version }}
poetry run pip install openai==${{ matrix.openai-version }}
poetry run pip install "pydantic${{ matrix.pydantic-version }}"
poetry run pip install "openai${{ matrix.openai-version }}"

- name: Static analysis with pyright
run: |
Expand All @@ -86,8 +87,8 @@ jobs:
# TODO: fix errors so that we can run both `make dev` and `make full`
# dependencies: ['dev', 'full']
dependencies: ["full"]
pydantic-version: ["1.10.9", "2.4.2"]
openai-version: ["1.30.1"]
pydantic-version: ["==1.10.9", ">=2.x"]
openai-version: [">=1.x"]
steps:
- uses: actions/checkout@v4

Expand All @@ -113,8 +114,8 @@ jobs:
- name: Install Dependencies
run: |
make ${{ matrix.dependencies }}
poetry run pip install pydantic==${{ matrix.pydantic-version }}
poetry run pip install openai==${{ matrix.openai-version }}
poetry run pip install "pydantic${{ matrix.pydantic-version }}"
poetry run pip install "openai${{ matrix.openai-version }}"

- name: Run Pytests
run: |
Expand Down
44 changes: 23 additions & 21 deletions .github/workflows/examples_check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
strategy:
matrix:
# this line is automatically generated by the script in .github/workflows/scripts/update_notebook_matrix.sh
notebook: ["bug_free_python_code.ipynb","check_for_pii.ipynb","competitors_check.ipynb","extracting_entities.ipynb","generate_structured_data.ipynb","generate_structured_data_cohere.ipynb","guardrails_with_chat_models.ipynb","input_validation.ipynb","llamaindex-output-parsing.ipynb","no_secrets_in_generated_text.ipynb","provenance.ipynb","recipe_generation.ipynb","regex_validation.ipynb","response_is_on_topic.ipynb","secrets_detection.ipynb","select_choice_based_on_action.ipynb","streaming.ipynb","syntax_error_free_sql.ipynb","text_summarization_quality.ipynb","toxic_language.ipynb","translation_to_specific_language.ipynb","translation_with_quality_check.ipynb","valid_chess_moves.ipynb","value_within_distribution.ipynb"]
notebook: ["bug_free_python_code.ipynb","check_for_pii.ipynb","competitors_check.ipynb","extracting_entities.ipynb","generate_structured_data.ipynb","generate_structured_data_cohere.ipynb","guardrails_with_chat_models.ipynb","input_validation.ipynb","llamaindex-output-parsing.ipynb","no_secrets_in_generated_text.ipynb","provenance.ipynb","recipe_generation.ipynb","regex_validation.ipynb","response_is_on_topic.ipynb","secrets_detection.ipynb","select_choice_based_on_action.ipynb","syntax_error_free_sql.ipynb","text_summarization_quality.ipynb","toxic_language.ipynb","translation_to_specific_language.ipynb","translation_with_quality_check.ipynb","valid_chess_moves.ipynb","value_within_distribution.ipynb"]
env:
COHERE_API_KEY: ${{ secrets.COHERE_API_KEY }}
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
Expand All @@ -24,6 +24,9 @@ jobs:
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
ref: ${{ github.head_ref }}
fetch-depth: 0
- name: Create .guardrailsrc
run: |
echo 'id="SYSTEM TESTING"' > ~/.guardrailsrc
Expand All @@ -32,31 +35,30 @@ jobs:
uses: actions/setup-python@v5
with:
python-version: 3.11.x
# - name: Poetry cache
# uses: actions/cache@v3
# with:
# path: ~/.cache/pypoetry
# key: poetry-cache-${{ runner.os }}-${{ steps.setup_python.outputs.python-version }}-${{ env.POETRY_VERSION }}
- name: Install Poetry
uses: snok/install-poetry@v1
with:
virtualenvs-create: true
virtualenvs-in-project: true
installer-parallel: true
- name: Install dependencies
run: |
make full;
poetry add "openai>=1.2.4" jupyter nbconvert cohere==5.3.2;
- name: Check for pypdfium2
run: poetry run pip show pypdfium2

# Setup Virtual Environment
python3 -m venv ./.venv
source .venv/bin/activate

# Install the current branch
pip install .

# Install extra stuff for notebook runs
pip install "huggingface_hub[cli]" jupyter nbconvert cohere==5.3.2
pip install nltk
- name: Huggingface Hub Login
run: poetry run huggingface-cli login --token $HUGGINGFACE_API_KEY
run: |
source .venv/bin/activate
huggingface-cli login --token $HUGGINGFACE_API_KEY
- name: download nltk data
run: |
source .venv/bin/activate
mkdir /tmp/nltk_data;
poetry run python -m nltk.downloader -d /tmp/nltk_data punkt;
- name: Use venv
run: source .venv/bin/activate
python -m nltk.downloader -d /tmp/nltk_data punkt;
- name: Execute notebooks and check for errors
run: bash ./.github/workflows/scripts/run_notebooks.sh ${{ matrix.notebook }}
run: |
source .venv/bin/activate
bash ./.github/workflows/scripts/run_notebooks.sh ${{ matrix.notebook }}

2 changes: 1 addition & 1 deletion .github/workflows/install_from_hub.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Notebook Execution and Error Check
name: Install from Hub

on:
push:
Expand Down
8 changes: 7 additions & 1 deletion .github/workflows/scripts/run_notebooks.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#!/bin/bash
export NLTK_DATA=/tmp/nltk_data;

# Remove the local guardrails directory and use the installed version
rm -rf ./guardrails

# Navigate to notebooks
cd docs/examples

# Get the notebook name from the matrix variable
Expand All @@ -10,10 +14,12 @@ notebook="$1"
invalid_notebooks=("valid_chess_moves.ipynb" "llamaindex-output-parsing.ipynb" "competitors_check.ipynb")
if [[ ! " ${invalid_notebooks[@]} " =~ " ${notebook} " ]]; then
echo "Processing $notebook..."
poetry run jupyter nbconvert --to notebook --execute "$notebook"
# poetry run jupyter nbconvert --to notebook --execute "$notebook"
jupyter nbconvert --to notebook --execute "$notebook"
if [ $? -ne 0 ]; then
echo "Error found in $notebook"
echo "Error in $notebook. See logs for details." >> errors.txt
exit 1
fi
fi

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,4 @@ precommit:
# pytest -x -q --no-summary
pyright guardrails/
make lint
./github/workflows/scripts/update_notebook_matrix.sh
./.github/workflows/scripts/update_notebook_matrix.sh
25 changes: 10 additions & 15 deletions docs/examples/bug_free_python_code.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,13 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Note: you may need to restart the kernel to use updated packages.\n",
"Note: you may need to restart the kernel to use updated packages.\n",
"Installing hub:\u001b[35m/\u001b[0m\u001b[35m/reflex/\u001b[0m\u001b[95mvalid_python...\u001b[0m\n",
"✅Successfully installed reflex/valid_python!\n",
"\n",
Expand All @@ -47,15 +45,12 @@
}
],
"source": [
"%pip install guardrails-ai -q\n",
"%pip install pydantic -q\n",
"\n",
"!guardrails hub install hub://reflex/valid_python --quiet"
]
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -93,7 +88,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -111,7 +106,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -128,7 +123,7 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 7,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -209,7 +204,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -239,7 +234,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 9,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -284,7 +279,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 10,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -356,7 +351,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 11,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -398,7 +393,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.0"
"version": "3.11.9"
},
"orig_nbformat": 4,
"vscode": {
Expand Down
Loading