Skip to content

Commit d0c07ba

Browse files
author
Memo Ugurbil
committed
nillion tests
1 parent 7c2b4d0 commit d0c07ba

File tree

6 files changed

+124
-21
lines changed

6 files changed

+124
-21
lines changed

plugins/nillion/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,7 @@ pip install -e plugins/nillion
1414
Set up your environment variables in a `.env` file:
1515

1616
```env
17+
NILLION_SECRET_KEY=your_nillion_secret_key
18+
NILLION_ORG_ID=your_nillion_org_id
19+
OPENAI_API_KEY=your_openai_api_key
1720
```

plugins/nillion/nillion_game_sdk/nillion_plugin.py

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
import jwt
2-
import requests
2+
import json
33
import nilql
44
import os
5+
import re
6+
import requests
57
import time
68
import uuid
79

8-
from jsonschema import Draft7Validator, validators
10+
from collections import deque, defaultdict
911
from ecdsa import SECP256k1, SigningKey
12+
from jsonschema import Draft7Validator, validators
1013
from langchain_openai import ChatOpenAI
1114
from typing import Any, Generator
1215

13-
# from collections import deque, defaultdict
14-
# import json
15-
# import re
16+
from .schemas import (
17+
NillionCreateSchemaInput,
18+
NillionLookupSchemaInput,
19+
NillionDataUploadInput,
20+
NillionDataDownloadInput,
21+
)
1622

1723
class NillionPlugin:
1824
"""
@@ -25,11 +31,6 @@ def __init__(self) -> None:
2531
self.name: str = "Nillion Plugin"
2632
self.secret_key = os.getenv("NILLION_SECRET_KEY")
2733
self.org_did = os.getenv("NILLION_ORG_ID")
28-
self.key = None
29-
self.nodes = None
30-
31-
def initialize(self):
32-
"""Initialize the plugin"""
3334
if not self.secret_key:
3435
raise ValueError("NILLION_SECRET_KEY is not configured.")
3536
if not self.org_did:
@@ -39,7 +40,7 @@ def initialize(self):
3940

4041
"""Initialize config with JWTs signed with ES256K for multiple node_ids; Add cluster key."""
4142
response = requests.post(
42-
"https://sv-sda-registration.replit.app/api/config",
43+
"https://secret-vault-registration.replit.app/api/config",
4344
headers={
4445
"Content-Type": "application/json",
4546
},
@@ -183,7 +184,6 @@ def create_schema(self, args: dict[str, Any]) -> tuple:
183184
tuple[str, dict]: The schema_uuid and the corresponding schema definition
184185
"""
185186
try:
186-
187187
validated_args = NillionCreateSchemaInput(**args)
188188
print(f"fn:create_schema [{validated_args.schema_description}]")
189189

@@ -253,7 +253,6 @@ def create_schema(self, args: dict[str, Any]) -> tuple:
253253
response = llm.invoke(schema_prompt)
254254

255255
schema = json.loads(str(response.content))
256-
257256
schema["_id"] = str(uuid.uuid4())
258257
schema["owner"] = self.org_did
259258

@@ -263,13 +262,14 @@ def create_schema(self, args: dict[str, Any]) -> tuple:
263262
print(f'fn:create_schema [{schema["_id"]}]')
264263
return schema["_id"], schema
265264
except Exception as e:
266-
print(f"Error creating schema: {str(e)}")
265+
print(f"Error creating schema: {e!r}")
267266
return None, None
268267

269268
def data_upload(self, args: dict[str, Any]) -> list[str]:
270-
"""Create a schema in your privacy preserving database, called the Nillion SecretVault
271-
(or nildb), based on a natural language description. Do not use this tool for any other
272-
purpose.
269+
"""Upload specified data into your privacy preserving database, called the Nillion SecretVault
270+
(or nildb), using the specified schema UUID. The data must exactly fit the requirements of
271+
the desired schema itself.
272+
Success will return a list of created record UUIDs, failure is an empty list.
273273
Args:
274274
args (dict[str, Any]): Arguments containing a UUID and the data to upload.
275275
Returns:
@@ -322,9 +322,11 @@ def data_upload(self, args: dict[str, Any]) -> list[str]:
322322
return []
323323

324324
def data_download(self, args: dict[str, Any]) -> list[dict]:
325-
"""Create a schema in your privacy preserving database, called the Nillion SecretVault
326-
(or nildb), based on a natural language description. Do not use this tool for any other
327-
purpose.
325+
"""Download all the data from your privacy preserving database, called the Nillion SecretVault
326+
(or nildb), using the specified schema UUID. You must know the schema UUID for the remote schema
327+
that you require. If you do not have the schema UUID you must use the lookup_schema action of the
328+
NillionActionProvider.
329+
Success will return true, whereas a failure response will return false.
328330
Args:
329331
args (dict[str, Any]): Arguments containing a target schema UUID
330332
Returns:
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from pydantic import BaseModel, Field
2+
3+
4+
class NillionCreateSchemaInput(BaseModel):
5+
"""Input argument schema for schema create action."""
6+
7+
schema_description: str = Field(
8+
..., description="a complete description of the desired nildb schema"
9+
)
10+
11+
12+
class NillionDataDownloadInput(BaseModel):
13+
"""Input argument schema for data download action."""
14+
15+
schema_uuid: str = Field(
16+
description="the UUID4 obtained from the nildb_schema_lookup_tool"
17+
)
18+
19+
20+
class NillionDataUploadInput(BaseModel):
21+
"""Input argument schema for data upload action."""
22+
23+
schema_uuid: str = Field(
24+
description="the UUID obtained from the nillion_lookup_schema tool."
25+
)
26+
data_to_store: list = Field(
27+
description="data to store in the database that validates against desired schema"
28+
)
29+
30+
31+
class NillionLookupSchemaInput(BaseModel):
32+
"""Input argument schema for lookup schema action."""
33+
34+
schema_description: str = Field(
35+
..., description="a complete description of the desired nildb schema"
36+
)

plugins/nillion/nillion_test.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
from dotenv import load_dotenv
2+
from nillion_game_sdk.nillion_plugin import NillionPlugin
3+
4+
load_dotenv()
5+
6+
plugin = NillionPlugin()
7+
8+
# Test Create Schema
9+
prompt = {
10+
"schema_description": "Create me a schema to track how much I spend on my boat collection"
11+
}
12+
13+
schema_id, schema = plugin.create_schema(prompt)
14+
15+
print(schema_id, schema)
16+
17+
# Test Lookup Schema
18+
prompt = {
19+
"schema_description": "Find me my schema that tracks how much I spend on my boat collection"
20+
}
21+
22+
my_uuid, my_schema = plugin.lookup_schema(prompt)
23+
24+
print(my_uuid, my_schema)
25+
26+
# Test Data Upload
27+
prompt = {
28+
"schema_uuid": "e868cc72-42f1-488d-ab3a-c8ffdbfba9b1",
29+
"data_to_store": [
30+
{
31+
"_id": "B0AB94E9-CC60-4DC0-9D15-8AD41B09B6C6",
32+
"boat_name": "Aquatic Escape",
33+
"purchase_date": "2024-10-12T21:32:49.208Z",
34+
"purchase_price": {
35+
"$share": "$342,077"
36+
},
37+
"maintenance_costs": {
38+
"$share": "$394,090"
39+
},
40+
"insurance_cost": {
41+
"$share": "$200,177"
42+
},
43+
"storage_fee": {
44+
"$share": "$254,353"
45+
}
46+
},
47+
]
48+
}
49+
50+
record_uuids = plugin.data_upload(prompt)
51+
52+
print(f"{record_uuids=}")
53+
54+
55+
# Test Data Download
56+
prompt = {
57+
"schema_uuid": "e868cc72-42f1-488d-ab3a-c8ffdbfba9b1"
58+
}
59+
60+
data = plugin.data_download(prompt)
61+
62+
print(f"{data=}")

plugins/nillion/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ classifiers = [
2020
"Intended Audience :: Developers",
2121
"Topic :: Software Development :: Libraries :: Python Modules",
2222
]
23-
dependencies = ["game-sdk>=0.1.1", "requests>=2.31.0"]
23+
dependencies = ["game-sdk>=0.1.1", "requests>=2.31.0", "py-dotenv>=0.1"]
2424

2525
[project.urls]
2626
"Homepage" = "https://github.com/game-by-virtuals/game-python/plugins/nillion"

plugins/nillion/test_nillion.py

Whitespace-only changes.

0 commit comments

Comments
 (0)