Skip to content

Commit 786f3bc

Browse files
committed
feat: Add CI, run pylint, and restructure files
1 parent 88f47fa commit 786f3bc

10 files changed

+573
-295
lines changed

.github/workflows/ci.yml

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
strategy:
13+
matrix:
14+
python-version: ["3.11"]
15+
16+
steps:
17+
- uses: actions/checkout@v4
18+
19+
- name: Set up Python ${{ matrix.python-version }}
20+
uses: actions/setup-python@v5
21+
with:
22+
python-version: ${{ matrix.python-version }}
23+
24+
- name: Install uv
25+
run: |
26+
curl -LsSf https://astral.sh/uv/install.sh | sh
27+
echo "$HOME/.cargo/bin" >> $GITHUB_PATH
28+
29+
- name: Create and activate virtual environment
30+
run: |
31+
uv venv
32+
echo "$HOME/.venv/bin" >> $GITHUB_PATH
33+
34+
- name: Install dependencies
35+
run: |
36+
uv pip install -e ".[dev]"
37+
uv pip install isort pylint
38+
39+
- name: Check code formatting with isort
40+
run: |
41+
uv run isort --check-only .
42+
43+
- name: Run pylint
44+
run: |
45+
uv run pylint src/nilrag test
46+
47+
- name: Run tests
48+
run: |
49+
uv run -m unittest test.rag

examples/1.init_schema_query.py

+39-38
Original file line numberDiff line numberDiff line change
@@ -2,69 +2,70 @@
22
Script to initialize schema and query to nilDB.
33
"""
44

5-
import os
6-
import json
7-
import sys
85
import argparse
9-
import time
106
import asyncio
11-
from nilrag.nildb_requests import NilDB, Node
7+
import json
8+
import time
129

10+
from nilrag.config import load_nil_db_config
1311

1412
DEFAULT_CONFIG = "examples/nildb_config.json"
1513

16-
async def main():
17-
parser = argparse.ArgumentParser(description='Initialize schema and query for nilDB')
18-
parser.add_argument('--config', type=str, default=DEFAULT_CONFIG,
19-
help=f'Path to nilDB config file (default: {DEFAULT_CONFIG})')
20-
args = parser.parse_args()
2114

22-
# Load NilDB from JSON file if it exists
23-
secret_key = None
24-
if os.path.exists(args.config):
25-
print(f"Loading NilDB configuration from {args.config}...")
26-
with open(args.config, "r", encoding="utf-8") as f:
27-
data = json.load(f)
28-
secret_key = data["org_secret_key"]
29-
nodes = []
30-
for node_data in data["nodes"]:
31-
nodes.append(
32-
Node(
33-
node_data["url"],
34-
node_data["node_id"],
35-
data["org_did"],
36-
)
37-
)
38-
nilDB = NilDB(nodes)
39-
else:
40-
print(f"Error: NilDB configuration file not found at {args.config}")
41-
sys.exit(1)
15+
async def main():
16+
"""
17+
Initialize schema and query for nilDB nodes.
4218
43-
jwts = nilDB.generate_jwt(secret_key, ttl=3600)
19+
This script:
20+
1. Loads the nilDB configuration from a JSON file
21+
2. Generates JWT tokens for authentication
22+
3. Creates a schema for storing embeddings and chunks
23+
4. Creates a query for computing differences between embeddings
24+
5. Updates the configuration file with the generated IDs and tokens
25+
"""
26+
parser = argparse.ArgumentParser(
27+
description="Initialize schema and query for nilDB"
28+
)
29+
parser.add_argument(
30+
"--config",
31+
type=str,
32+
default=DEFAULT_CONFIG,
33+
help=f"Path to nilDB config file (default: {DEFAULT_CONFIG})",
34+
)
35+
args = parser.parse_args()
4436

45-
print(nilDB)
37+
# Load NilDB configuration
38+
nil_db, secret_key = load_nil_db_config(
39+
args.config, require_secret_key=True
40+
)
41+
jwts = nil_db.generate_jwt(secret_key, ttl=3600)
42+
print(nil_db)
4643
print()
4744

4845
# Upload encrypted data to nilDB
4946
print("Initializing schema...")
5047
start_time = time.time()
51-
schema_id = await nilDB.init_schema()
48+
schema_id = await nil_db.init_schema()
5249
end_time = time.time()
5350
print(f"Schema initialized successfully in {end_time - start_time:.2f} seconds")
5451

5552
print("Initializing query...")
5653
start_time = time.time()
57-
diff_query_id = await nilDB.init_diff_query()
54+
diff_query_id = await nil_db.init_diff_query()
5855
end_time = time.time()
5956
print(f"Query initialized successfully in {end_time - start_time:.2f} seconds")
6057

58+
# Update config file with new IDs and tokens
59+
with open(args.config, "r", encoding="utf-8") as f:
60+
data = json.load(f)
61+
for node_data, jwt in zip(data["nodes"], jwts):
62+
node_data["schema_id"] = schema_id
63+
node_data["diff_query_id"] = diff_query_id
64+
node_data["bearer_token"] = jwt
6165
with open(args.config, "w", encoding="utf-8") as f:
62-
for node_data, jwt in zip(data["nodes"], jwts):
63-
node_data["schema_id"] = schema_id
64-
node_data["diff_query_id"] = diff_query_id
65-
node_data["bearer_token"] = jwt
6666
json.dump(data, f, indent=4)
6767
print("Updated nilDB configuration file with schema and query IDs.")
6868

69+
6970
if __name__ == "__main__":
7071
asyncio.run(main())

examples/2.data_owner_upload.py

+52-49
Original file line numberDiff line numberDiff line change
@@ -2,89 +2,92 @@
22
Script to upload data to nilDB using nilRAG.
33
"""
44

5-
import os
6-
import json
7-
import sys
85
import argparse
9-
import time
106
import asyncio
7+
import time
8+
119
import nilql
12-
from nilrag.util import (
13-
create_chunks,
14-
encrypt_float_list,
15-
generate_embeddings_huggingface,
16-
load_file,
17-
)
18-
from nilrag.nildb_requests import NilDB, Node
1910

11+
from nilrag.config import load_nil_db_config
12+
from nilrag.util import (create_chunks, encrypt_float_list,
13+
generate_embeddings_huggingface, load_file)
2014

2115
DEFAULT_CONFIG = "examples/nildb_config.json"
22-
DEFAULT_FILE_PATH = 'examples/data/20-fake.txt'
16+
DEFAULT_FILE_PATH = "examples/data/20-fake.txt"
17+
2318

2419
async def main():
25-
parser = argparse.ArgumentParser(description='Upload data to nilDB using nilRAG')
26-
parser.add_argument('--config', type=str, default=DEFAULT_CONFIG,
27-
help=f'Path to nilDB config file (default: {DEFAULT_CONFIG})')
28-
parser.add_argument('--file', type=str, default=DEFAULT_FILE_PATH,
29-
help=f'Path to data file to upload (default: {DEFAULT_FILE_PATH})')
30-
args = parser.parse_args()
20+
"""
21+
Upload data to nilDB using nilRAG.
3122
32-
# Load NilDB from JSON file if it exists
33-
if os.path.exists(args.config):
34-
print(f"Loading NilDB configuration from {args.config}...")
35-
with open(args.config, "r", encoding="utf-8") as f:
36-
data = json.load(f)
37-
nodes = []
38-
for node_data in data["nodes"]:
39-
nodes.append(
40-
Node(
41-
node_data["url"],
42-
node_data["node_id"],
43-
data["org_did"],
44-
node_data["bearer_token"],
45-
node_data.get("schema_id"),
46-
)
47-
)
48-
nilDB = NilDB(nodes)
49-
else:
50-
print(f"Error: NilDB configuration file not found at {args.config}")
51-
sys.exit(1)
23+
This script:
24+
1. Loads the nilDB configuration
25+
2. Initializes encryption keys for different modes
26+
3. Processes the input file into chunks and embeddings
27+
4. Encrypts the data using nilQL
28+
5. Uploads the encrypted data to nilDB nodes
29+
"""
30+
parser = argparse.ArgumentParser(description="Upload data to nilDB using nilRAG")
31+
parser.add_argument(
32+
"--config",
33+
type=str,
34+
default=DEFAULT_CONFIG,
35+
help=f"Path to nilDB config file (default: {DEFAULT_CONFIG})",
36+
)
37+
parser.add_argument(
38+
"--file",
39+
type=str,
40+
default=DEFAULT_FILE_PATH,
41+
help=f"Path to data file to upload (default: {DEFAULT_FILE_PATH})",
42+
)
43+
args = parser.parse_args()
5244

53-
print(nilDB)
45+
# Load NilDB configuration
46+
nil_db, _ = load_nil_db_config(
47+
args.config,
48+
require_bearer_token=True,
49+
require_schema_id=True,
50+
)
51+
print(nil_db)
5452
print()
5553

5654
# Initialize secret keys for different modes of operation
57-
num_nodes = len(nilDB.nodes)
58-
additive_key = nilql.ClusterKey.generate({'nodes': [{}] * num_nodes}, {'sum': True})
59-
xor_key = nilql.ClusterKey.generate({'nodes': [{}] * num_nodes}, {'store': True})
55+
num_nodes = len(nil_db.nodes)
56+
additive_key = nilql.ClusterKey.generate(
57+
{"nodes": [{}] * num_nodes}, {"sum": True}
58+
)
59+
xor_key = nilql.ClusterKey.generate(
60+
{"nodes": [{}] * num_nodes}, {"store": True}
61+
)
6062

6163
# Load and process input file
6264
paragraphs = load_file(args.file)
6365
chunks = create_chunks(paragraphs, chunk_size=50, overlap=10)
6466

6567
# Generate embeddings and chunks
66-
print('Generating embeddings and chunks...')
68+
print("Generating embeddings and chunks...")
6769
start_time = time.time()
6870
embeddings = generate_embeddings_huggingface(chunks)
6971
end_time = time.time()
70-
print(f'Embeddings and chunks generated in {end_time - start_time:.2f} seconds!')
72+
print(f"Embeddings and chunks generated in {end_time - start_time:.2f} seconds!")
7173

7274
# Encrypt chunks and embeddings
73-
print('Encrypting data...')
75+
print("Encrypting data...")
7476
start_time = time.time()
7577
chunks_shares = [nilql.encrypt(xor_key, chunk) for chunk in chunks]
7678
embeddings_shares = [
7779
encrypt_float_list(additive_key, embedding) for embedding in embeddings
7880
]
7981
end_time = time.time()
80-
print(f'Data encrypted in {end_time - start_time:.2f} seconds')
82+
print(f"Data encrypted in {end_time - start_time:.2f} seconds")
8183

8284
# Upload encrypted data to nilDB
83-
print('Uploading data...')
85+
print("Uploading data...")
8486
start_time = time.time()
85-
await nilDB.upload_data(embeddings_shares, chunks_shares)
87+
await nil_db.upload_data(embeddings_shares, chunks_shares)
8688
end_time = time.time()
87-
print(f'Data uploaded in {end_time - start_time:.2f} seconds')
89+
print(f"Data uploaded in {end_time - start_time:.2f} seconds")
90+
8891

8992
if __name__ == "__main__":
9093
asyncio.run(main())

0 commit comments

Comments
 (0)