Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
172 changes: 109 additions & 63 deletions notebooks/rag.ipynb → notebooks/rag/rag.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@
"- A [Pinecone](https://www.pinecone.io/) account and API key.\n",
"- An [OpenAI](https://openai.com/) account and API key.\n",
"\n",
"#### Installing Libraries\n",
"Run the following command to install the required libraries for this example:\n",
"\n",
"```pip install python-dotenv authzed pinecone pinecone[grpc] langchain_pinecone langchain langchain_openai langchain_core```\n",
"\n",
"#### Running SpiceDB\n",
"Follow the [install guide](https://authzed.com/docs/spicedb/getting-started/installing-spicedb) for your platform and run a local instance of SpiceDB.\n",
"\n",
Expand Down Expand Up @@ -54,6 +49,16 @@
"id": "a76fa3a1",
"metadata": {},
"outputs": [],
"source": [
"%pip install -r requirements.txt"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c8248a56",
"metadata": {},
"outputs": [],
"source": [
"from dotenv import load_dotenv\n",
"\n",
Expand Down Expand Up @@ -97,7 +102,11 @@
"}\"\"\"\n",
"\n",
"client = Client(os.getenv('SPICEDB_ADDR'), insecure_bearer_token_credentials(os.getenv('SPICEDB_API_KEY')))\n",
"resp = client.WriteSchema(WriteSchemaRequest(schema=SCHEMA))"
"\n",
"try:\n",
" resp = await(client.WriteSchema(WriteSchemaRequest(schema=SCHEMA)))\n",
"except Exception as e:\n",
" print(f\"Write schema error: {type(e).__name__}: {e}\")"
]
},
{
Expand Down Expand Up @@ -125,38 +134,41 @@
" WriteRelationshipsRequest,\n",
")\n",
"\n",
"client.WriteRelationships(\n",
" WriteRelationshipsRequest(\n",
" updates=[\n",
" RelationshipUpdate(\n",
" operation=RelationshipUpdate.Operation.OPERATION_CREATE,\n",
" relationship=Relationship(\n",
" resource=ObjectReference(object_type=\"article\", object_id=\"123\"),\n",
" relation=\"viewer\",\n",
" subject=SubjectReference(\n",
" object=ObjectReference(\n",
" object_type=\"user\",\n",
" object_id=\"tim\",\n",
" )\n",
"try:\n",
" resp = await (client.WriteRelationships(\n",
" WriteRelationshipsRequest(\n",
" updates=[\n",
" RelationshipUpdate(\n",
" operation=RelationshipUpdate.Operation.OPERATION_TOUCH,\n",
" relationship=Relationship(\n",
" resource=ObjectReference(object_type=\"article\", object_id=\"123\"),\n",
" relation=\"viewer\",\n",
" subject=SubjectReference(\n",
" object=ObjectReference(\n",
" object_type=\"user\",\n",
" object_id=\"tim\",\n",
" )\n",
" ),\n",
" ),\n",
" ),\n",
" ),\n",
" RelationshipUpdate(\n",
" operation=RelationshipUpdate.Operation.OPERATION_CREATE,\n",
" relationship=Relationship(\n",
" resource=ObjectReference(object_type=\"article\", object_id=\"456\"),\n",
" relation=\"viewer\",\n",
" subject=SubjectReference(\n",
" object=ObjectReference(\n",
" object_type=\"user\",\n",
" object_id=\"tim\",\n",
" )\n",
" RelationshipUpdate(\n",
" operation=RelationshipUpdate.Operation.OPERATION_TOUCH,\n",
" relationship=Relationship(\n",
" resource=ObjectReference(object_type=\"article\", object_id=\"456\"),\n",
" relation=\"viewer\",\n",
" subject=SubjectReference(\n",
" object=ObjectReference(\n",
" object_type=\"user\",\n",
" object_id=\"tim\",\n",
" )\n",
" ),\n",
" ),\n",
" ),\n",
" ),\n",
" ]\n",
" )\n",
")"
" ]\n",
" )\n",
" ))\n",
"except Exception as e:\n",
" print(f\"Write relationships error: {type(e).__name__}: {e}\")"
]
},
{
Expand All @@ -176,8 +188,9 @@
"metadata": {},
"outputs": [],
"source": [
"from pinecone.grpc import PineconeGRPC as Pinecone\n",
"#from pinecone.grpc import PineconeGRPC as Pinecone\n",
"from pinecone import ServerlessSpec\n",
"from pinecone import Pinecone\n",
"import os\n",
"\n",
"pc = Pinecone(api_key=os.getenv(\"PINECONE_API_KEY\"))\n",
Expand Down Expand Up @@ -259,7 +272,21 @@
"execution_count": null,
"id": "891fca91",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Write relationships error: AioRpcError: <AioRpcError of RPC that terminated with:\n",
"\tstatus = StatusCode.FAILED_PRECONDITION\n",
"\tdetails = \"object definition `article` not found\"\n",
"\tdebug_error_string = \"UNKNOWN:Error received from peer ipv6:%5B::1%5D:50051 {grpc_message:\"object definition `article` not found\", grpc_status:9, created_time:\"2025-01-08T11:13:13.028503-07:00\"}\"\n",
">\n",
"Article IDs that Tim is authorized to view:\n",
"[]\n"
]
}
],
"source": [
"from authzed.api.v1 import (\n",
" LookupResourcesRequest,\n",
Expand All @@ -282,13 +309,15 @@
" resource_object_type=\"article\",\n",
" )\n",
" )\n",
"try:\n",
" resp = lookupArticles()\n",
"\n",
"resp = lookupArticles()\n",
" authorized_articles = []\n",
"\n",
"authorized_articles = []\n",
"\n",
"async for response in resp:\n",
" authorized_articles.append(response.resource_object_id)\n",
" async for response in resp:\n",
" authorized_articles.append(response.resource_object_id)\n",
"except Exception as e:\n",
" print(f\"Lookup error: {type(e).__name__}: {e}\")\n",
"\n",
"print(\"Article IDs that Tim is authorized to view:\")\n",
"print(authorized_articles)"
Expand Down Expand Up @@ -391,27 +420,41 @@
"execution_count": null,
"id": "dde09648",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"<_AioCall object>"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"client.WriteRelationships(\n",
" WriteRelationshipsRequest(\n",
" updates=[\n",
" RelationshipUpdate(\n",
" operation=RelationshipUpdate.Operation.OPERATION_DELETE,\n",
" relationship=Relationship(\n",
" resource=ObjectReference(object_type=\"article\", object_id=\"123\"),\n",
" relation=\"viewer\",\n",
" subject=SubjectReference(\n",
" object=ObjectReference(\n",
" object_type=\"user\",\n",
" object_id=\"tim\",\n",
" )\n",
"try: \n",
" resp = await client.WriteRelationships(\n",
" WriteRelationshipsRequest(\n",
" updates=[\n",
" RelationshipUpdate(\n",
" operation=RelationshipUpdate.Operation.OPERATION_DELETE,\n",
" relationship=Relationship(\n",
" resource=ObjectReference(object_type=\"article\", object_id=\"123\"),\n",
" relation=\"viewer\",\n",
" subject=SubjectReference(\n",
" object=ObjectReference(\n",
" object_type=\"user\",\n",
" object_id=\"tim\",\n",
" )\n",
" ),\n",
" ),\n",
" ),\n",
" ),\n",
" ]\n",
" ]\n",
" )\n",
" )\n",
")"
"except Exception as e:\n",
" print(f\"Write relationships error: {type(e).__name__}: {e}\")"
]
},
{
Expand All @@ -430,12 +473,15 @@
"outputs": [],
"source": [
"#this function was defined above\n",
"resp = lookupArticles()\n",
"try:\n",
" resp = lookupArticles()\n",
"\n",
"authorized_articles = []\n",
" authorized_articles = []\n",
"\n",
"async for response in resp:\n",
" authorized_articles.append(response.resource_object_id)\n",
" async for response in resp:\n",
" authorized_articles.append(response.resource_object_id)\n",
"except Exception as e:\n",
" print(f\"Lookup error: {type(e).__name__}: {e}\")\n",
"\n",
"print(\"Documents that Tim can view:\")\n",
"print(authorized_articles)"
Expand Down Expand Up @@ -499,7 +545,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.2"
"version": "3.11.2"
}
},
"nbformat": 4,
Expand Down
86 changes: 86 additions & 0 deletions notebooks/rag/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
aiohttp==3.9.5
aiosignal==1.3.2
annotated-types==0.7.0
anyio==4.8.0
appnope==0.1.4
asttokens==3.0.0
attrs==24.3.0
authzed==1.1.0
certifi==2024.12.14
charset-normalizer==3.4.1
comm==0.2.2
debugpy==1.8.11
decorator==5.1.1
distro==1.9.0
executing==2.1.0
frozenlist==1.5.0
googleapis-common-protos==1.66.0
grpc-interceptor==0.15.4
grpcio==1.69.0
h11==0.14.0
httpcore==1.0.7
httpx==0.28.1
idna==3.10
iniconfig==2.0.0
ipykernel==6.29.5
ipython==8.31.0
jedi==0.19.2
jiter==0.8.2
jsonpatch==1.33
jsonpointer==3.0.0
jupyter_client==8.6.3
jupyter_core==5.7.2
langchain==0.3.14
langchain-core==0.3.29
langchain-openai==0.2.14
langchain-pinecone==0.2.1
langchain-tests==0.3.7
langchain-text-splitters==0.3.5
langsmith==0.2.10
matplotlib-inline==0.1.7
multidict==6.1.0
nest-asyncio==1.6.0
numpy==1.26.4
openai==1.59.4
orjson==3.10.13
packaging==24.2
parso==0.8.4
pexpect==4.9.0
pinecone==5.4.2
pinecone-plugin-inference==3.1.0
pinecone-plugin-interface==0.0.7
platformdirs==4.3.6
pluggy==1.5.0
prompt_toolkit==3.0.48
propcache==0.2.1
protobuf==5.29.2
psutil==6.1.1
ptyprocess==0.7.0
pure_eval==0.2.3
pydantic==2.10.4
pydantic_core==2.27.2
Pygments==2.19.1
pytest==8.3.4
pytest-asyncio==0.25.1
pytest-socket==0.7.0
python-dateutil==2.9.0.post0
python-dotenv==1.0.1
PyYAML==6.0.2
pyzmq==26.2.0
regex==2024.11.6
requests==2.32.3
requests-toolbelt==1.0.0
six==1.17.0
sniffio==1.3.1
SQLAlchemy==2.0.36
stack-data==0.6.3
syrupy==4.8.0
tenacity==9.0.0
tiktoken==0.8.0
tornado==6.4.2
tqdm==4.67.1
traitlets==5.14.3
typing_extensions==4.12.2
urllib3==2.3.0
wcwidth==0.2.13
yarl==1.18.3
Loading