Skip to content

Commit 2ea8bba

Browse files
authored
feat: include examples in testing (#660)
1 parent 1c6bb8d commit 2ea8bba

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+346
-621
lines changed

.github/scripts/run_examples.sh

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
echo "Export env variables required for the examples..."
6+
export GOOGLE_CLOUD_PROJECT
7+
export GOOGLE_APPLICATION_CREDENTIALS
8+
export OPENAI_API_KEY
9+
export GEMINI_API_KEY
10+
export ANTHROPIC_API_KEY
11+
export LOGFIRE_TOKEN
12+
13+
GIT_URL_TEMPLATE="git+https://github.com/deepsense-ai/ragbits.git@%s#subdirectory=packages/%s"
14+
PR_BRANCH="${PR_BRANCH:-main}"
15+
16+
patch_dependencies() {
17+
local file="$1"
18+
local branch="$2"
19+
local tmp_file
20+
tmp_file=$(mktemp)
21+
local in_script_block=false
22+
23+
while IFS= read -r line || [[ -n "$line" ]]; do
24+
if [[ "$line" == "# /// script"* ]]; then
25+
in_script_block=true
26+
echo "$line" >> "$tmp_file"
27+
continue
28+
fi
29+
30+
if [[ "$line" == "# ///"* && "$in_script_block" == true ]]; then
31+
in_script_block=false
32+
echo "$line" >> "$tmp_file"
33+
continue
34+
fi
35+
36+
if [[ "$in_script_block" == true && "$line" == *ragbits* ]]; then
37+
if [[ "$line" =~ \"([^\"]+)\" ]]; then
38+
full_pkg="${BASH_REMATCH[1]}"
39+
pkg_base="${full_pkg%%[*}"
40+
git_url=$(printf "$GIT_URL_TEMPLATE" "$branch" "$pkg_base")
41+
echo "# \"${full_pkg} @ ${git_url}\"," >> "$tmp_file"
42+
else
43+
echo "$line" >> "$tmp_file"
44+
fi
45+
else
46+
echo "$line" >> "$tmp_file"
47+
fi
48+
49+
done < "$file"
50+
mv "$tmp_file" "$file"
51+
}
52+
53+
export -f patch_dependencies
54+
55+
find examples -name '*.py' | while read -r file; do
56+
echo "Running the script: $file"
57+
patch_dependencies "$file" "$PR_BRANCH"
58+
59+
set +e
60+
timeout 30s uv run "$file"
61+
exit_code=$?
62+
set -e
63+
64+
if [[ $exit_code -eq 124 ]]; then
65+
echo "Script timed out, continuing to next..."
66+
elif [[ $exit_code -ne 0 ]]; then
67+
echo "Script failed, stopping..."
68+
exit $exit_code
69+
else
70+
echo "Script completed successfully."
71+
fi
72+
done
73+
74+
echo "All examples run successfully."

.github/workflows/main-checks.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ jobs:
4040
needs: check-source-changes
4141
if: ${{ needs.check-source-changes.outputs.packages-changed == 'true' }}
4242
uses: ./.github/workflows/shared-packages.yml
43+
secrets: inherit
4344

4445
ui:
4546
needs: check-source-changes

.github/workflows/pull-request-checks.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ jobs:
4646
needs: check-source-changes
4747
if: ${{ needs.check-source-changes.outputs.packages-changed == 'true' }}
4848
uses: ./.github/workflows/shared-packages.yml
49+
secrets: inherit
4950

5051
client:
5152
needs: check-source-changes

.github/workflows/shared-packages.yml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,60 @@ jobs:
117117
name: packages
118118
path: dist/**
119119
retention-days: 3
120+
121+
examples:
122+
name: Run examples
123+
runs-on: ubuntu-latest
124+
timeout-minutes: 15
125+
permissions:
126+
checks: write
127+
pull-requests: write
128+
contents: read
129+
130+
services:
131+
postgres:
132+
image: pgvector/pgvector:0.8.0-pg17
133+
env:
134+
POSTGRES_USER: ragbits_example
135+
POSTGRES_PASSWORD: ragbits_example
136+
POSTGRES_DB: ragbits_example
137+
ports:
138+
- 5432:5432
139+
options: >-
140+
--health-cmd "pg_isready -U postgres"
141+
--health-interval 10s
142+
--health-timeout 5s
143+
--health-retries 5
144+
145+
grafana:
146+
image: grafana/otel-lgtm
147+
ports:
148+
- 3000:3000
149+
- 4317:4317
150+
- 4318:4318
151+
152+
steps:
153+
- uses: actions/checkout@v4
154+
155+
- name: Install uv
156+
uses: astral-sh/setup-uv@v2
157+
with:
158+
version: ${{ vars.UV_VERSION || '0.6.9' }}
159+
160+
- name: Run examples
161+
env:
162+
PR_BRANCH: ${{ github.head_ref }}
163+
GOOGLE_CLOUD_PROJECT: ${{ secrets.GCP_PROJECT_ID }}
164+
GCP_KEY: ${{ secrets.GCP_KEY }}
165+
GOOGLE_APPLICATION_CREDENTIALS: ${{ github.workspace }}/gcp_key.json
166+
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
167+
GEMINI_API_KEY: ${{ secrets.GEMINI_API_KEY }}
168+
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
169+
LOGFIRE_TOKEN: ${{ secrets.LOGFIRE_TOKEN }}
170+
run: |
171+
echo "$GCP_KEY" | base64 --decode > "$GOOGLE_APPLICATION_CREDENTIALS"
172+
./.github/scripts/run_examples.sh
173+
120174
tests:
121175
name: Run tests
122176
runs-on: ubuntu-latest

.libraries-whitelist.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,4 @@ mirakuru
1010
psycopg
1111
pytest-postgresql
1212
python-bidi
13-
psycopg-binary
14-
psycopg-pool
1513
griffe

examples/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Examples
2+
3+
This directory contains example scripts that demonstrate various functionalities of the Ragbits library.
4+
Each script showcases a specific use case, providing a practical guide to using Ragbits components.
5+
6+
## Prerequisites
7+
8+
To run the script, you will need to have `uv` installed.
9+
Some scripts may also require additional dependencies or API keys for external services.
10+
11+
All necessary details are provided in the comments at the top of each script.
12+
13+
## Scripts Overview
14+
15+
| Script | Ragbits Package | Description |
16+
|:-------------------------------------------------------------------------------------------------|:------------------------------------------------------------:|:--------------------------------------------------------------------------------------------------------------------------------------------------------|
17+
| [Text Prompt](/examples/core/prompt/text.py) | [ragbits-core](/packages/ragbits-core) | Example of how to use the `Prompt` class to generate themed text using an LLM with a simple text prompt. |
18+
| [Text Prompt with Few Shots](/examples/core/prompt/text_with_few_shots.py) | [ragbits-core](/packages/ragbits-core) | Example of how to use the `Prompt` class to generate themed text using an LLM with a text prompt and few-shot examples. |
19+
| [Multimodal Prompt](/examples/core/prompt/multimodal.py) | [ragbits-core](/packages/ragbits-core) | Example of how to use the `Prompt` class to generate themed text using an LLM with both text and image inputs. |
20+
| [Multimodal Prompt with Few Shots](/examples/core/prompt/multimodal_with_few_shots.py) | [ragbits-core](/packages/ragbits-core) | Example of how to use the `Prompt` class to generate themed text using an LLM with multimodal inputs and few-shot examples. |
21+
| [Tool Use with LLM](/examples/core/llms/tool_use.py) | [ragbits-core](/packages/ragbits-core) | Example of how to provide tools and return tool calls from LLM. |
22+
| [OpenTelemetry Audit](/examples/core/audit/otel.py) | [ragbits-core](/packages/ragbits-core) | Example of how to collect traces and metrics using Ragbits audit module with OpenTelemetry. |
23+
| [Logfire Audit](/examples/core/audit/logfire_.py) | [ragbits-core](/packages/ragbits-core) | Example of how to collect traces and metrics using Ragbits audit module with Logfire. |
24+
| [Basic Document Search](/examples/document-search/basic.py) | [ragbits-document-search](/packages/ragbits-document-search) | Example of how to use the `DocumentSearch` class to search for documents with the `InMemoryVectorStore` class to store the embeddings. |
25+
| [Chroma Document Search](/examples/document-search/chroma.py) | [ragbits-document-search](/packages/ragbits-document-search) | Example of how to use the `DocumentSearch` class to search for documents with the `ChromaVectorStore` class to store the embeddings. |
26+
| [Multimodal Document Search](/examples/document-search/multimodal.py) | [ragbits-document-search](/packages/ragbits-document-search) | Example of how to use the `DocumentSearch` to index and search for images and text documents with the `MultimodalEmbedding` from VertexAI. |
27+
| [PgVector Document Search](/examples/document-search/pgvector.py) | [ragbits-document-search](/packages/ragbits-document-search) | Example of how to use the `DocumentSearch` class to search for documents with the `PgVectorStore` class to store the embeddings in a Postgres database. |
28+
| [Qdrant Document Search](/examples/document-search/qdrant.py) | [ragbits-document-search](/packages/ragbits-document-search) | Example of how to use the `DocumentSearch` class to search for documents with the `QdrantVectorStore` class to store the embeddings. |
29+
| [Weaviate Document Search](/examples/document-search/weaviate_.py) | [ragbits-document-search](/packages/ragbits-document-search) | Example of how to use the `DocumentSearch` class to search for documents with the `WeaviateVectorStore` class to store the embeddings. |
30+
| [Dataset Generation](/examples/evaluate/dataset-generator/generate.py) | [ragbits-evaluate](/packages/ragbits-evaluate) | Example of how to generate a synthetic dataset using the `DatasetGenerationPipeline` class. |
31+
| [Basic Document Search Evaluation](/examples/evaluate/document-search/basic/evaluate.py) | [ragbits-evaluate](/packages/ragbits-evaluate) | Example of how to evaluate a basic document search pipeline using the `Evaluator` class. |
32+
| [Basic Document Search Optimization](/examples/evaluate/document-search/basic/optimize.py) | [ragbits-evaluate](/packages/ragbits-evaluate) | Example of how to optimize a basic document search pipeline using the `Optimizer` class. |
33+
| [Advanced Document Search Evaluation](/examples/evaluate/document-search/advanced/evaluate.py) | [ragbits-evaluate](/packages/ragbits-evaluate) | Example of how to evaluate an advanced document search pipeline using the `Evaluator` class. |
34+
| [Advanced Document Search Optimization](/examples/evaluate/document-search/advanced/optimize.py) | [ragbits-evaluate](/packages/ragbits-evaluate) | Example of how to optimize an advanced document search pipeline using the `Optimizer` class. |
35+
| [Chat Interface](/examples/chat/chat.py) | [ragbits-chat](/packages/ragbits-chat) | Example of how to use the `ChatInterface` to create a simple chat application. |
36+
| [Offline Chat Interface](/examples/chat/offline_chat.py) | [ragbits-chat](/packages/ragbits-chat) | Example of how to use the `ChatInterface` to create a simple chat application that works offline. |
37+
| [Recontextualize Last Message](/examples/chat/recontextualize_message.py) | [ragbits-chat](/packages/ragbits-chat) | Example of how to use the `StandaloneMessageCompressor` compressor to recontextualize the last message in a conversation history. |
38+
| [Agents Tool Use](/examples/agents/tool_use.py) | [ragbits-agents](/packages/ragbits-agents) | Example of how to use agent with tools. |

examples/agents/tool_use.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
Ragbits Agents Example: Tool use
33
4-
This example shows how to use agent with tools.
4+
This example demonstrates how to use agent with tools.
55
We provide a single method as a tool to the agent and expect it to call it when answering query.
66
77
To run the script, execute the following command:

examples/apps/documents_chat.py

Lines changed: 0 additions & 171 deletions
This file was deleted.

0 commit comments

Comments
 (0)