Skip to content

Instantly share code, notes, and snippets.

@aaronblondeau
Created November 3, 2023 22:31
Show Gist options
  • Save aaronblondeau/deb7354105ed6a2daa07e49502324e5e to your computer and use it in GitHub Desktop.
Save aaronblondeau/deb7354105ed6a2daa07e49502324e5e to your computer and use it in GitHub Desktop.
SurrealDB Demo
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# SurrealDB Feature Exploration\n",
"\n",
"Before using this notebook, run SurrealDB locally:\n",
"\n",
"```\n",
"docker run --rm --pull always -p 8000:8000 -v ${PWD}/data/surrealdb:/mydata surrealdb/surrealdb:latest start --log trace --auth --user admin --pass secret file:/mydata/mydatabase.db\n",
"```\n",
"\n",
"Create virtual environment and install dependencies:\n",
"\n",
"```bash\n",
"python3 -m venv ./venv\n",
"source ./venv/bin/activate\n",
"```\n",
"\n",
"or on Windows (I like to have \"wenv\" for windows so it doesn't clash with a venv created via Windows Subsystem for Linux):\n",
"\n",
"```powershell\n",
"python -m venv ./wenv\n",
".\\wenv\\Scripts\\activate.ps1\n",
"```\n",
"\n",
"Install Jupyter and SurrealDB in your virtual environment:\n",
"\n",
"```\n",
"pip install surrealdb\n",
"pip install notebook\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from surrealdb import Surreal"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# Setup a connection for Admin\n",
"db_admin = Surreal(\"http://localhost:8000\")\n",
"await db_admin.connect()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Part 1 - Basic User Auth"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# Login as Admin\n",
"await db_admin.signin({\"user\": \"admin\", \"pass\": \"secret\"})\n",
"await db_admin.use(\"test\", \"test\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'result': None, 'status': 'OK', 'time': '3.309766ms'},\n",
" {'result': None, 'status': 'OK', 'time': '556.233µs'},\n",
" {'result': None, 'status': 'OK', 'time': '501.22µs'},\n",
" {'result': None, 'status': 'OK', 'time': '734.667µs'},\n",
" {'result': None, 'status': 'OK', 'time': '615.943µs'},\n",
" {'result': None, 'status': 'OK', 'time': '451.572µs'}]"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# As Admin, seutp user table\n",
"await db_admin.query('''\n",
" DEFINE TABLE user SCHEMAFULL\n",
" PERMISSIONS\n",
" FOR select, update, delete WHERE id = $auth.id, \n",
" FOR create NONE;\n",
"\n",
" DEFINE FIELD name ON user TYPE string;\n",
" DEFINE FIELD email ON user TYPE string ASSERT string::is::email($value);\n",
" DEFINE FIELD password ON user TYPE string;\n",
" DEFINE INDEX email ON user FIELDS email UNIQUE;\n",
"\n",
" DEFINE SCOPE allusers SESSION 1d\n",
" SIGNIN (\n",
" SELECT * FROM user WHERE email = $email AND crypto::argon2::compare(password, $password)\n",
" )\n",
" SIGNUP (\n",
" CREATE user CONTENT {\n",
" name: $name,\n",
" email: $email,\n",
" password: crypto::argon2::generate($password)\n",
" }\n",
" );\n",
"''')"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"# Setup a connection for Alice\n",
"db_alice = Surreal(\"http://localhost:8000\")\n",
"await db_alice.connect()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJpYXQiOjE2OTkwNDk3NzgsIm5iZiI6MTY5OTA0OTc3OCwiZXhwIjoxNjk5MTM2MTc4LCJpc3MiOiJTdXJyZWFsREIiLCJOUyI6InRlc3QiLCJEQiI6InRlc3QiLCJTQyI6ImFsbHVzZXJzIiwiSUQiOiJ1c2VyOjk0czI2MnRjZ294eXA3N2M1dGt4In0.zZVVQtzmWiuxoY7M4SjTAA7vrRsWAwdC_-zXwRbJBn12L-SdIEeAumWSH950eevUU2ILtdG7tXCwRRQQjpe63Q'"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Register Alice\n",
"await db_alice.signup({\n",
"\t'NS': 'test',\n",
"\t'DB': 'test',\n",
"\t'SC': 'allusers',\n",
"\t'email': '[email protected]',\n",
"\t'password': 'foobar',\n",
" 'name': 'All-Ice'\n",
"})"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"# Signout Alice\n",
"await db_alice.invalidate()"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJpYXQiOjE2OTkwNDk3NzgsIm5iZiI6MTY5OTA0OTc3OCwiZXhwIjoxNjk5MTM2MTc4LCJpc3MiOiJTdXJyZWFsREIiLCJOUyI6InRlc3QiLCJEQiI6InRlc3QiLCJTQyI6ImFsbHVzZXJzIiwiSUQiOiJ1c2VyOjk0czI2MnRjZ294eXA3N2M1dGt4In0.zZVVQtzmWiuxoY7M4SjTAA7vrRsWAwdC_-zXwRbJBn12L-SdIEeAumWSH950eevUU2ILtdG7tXCwRRQQjpe63Q'"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Login Alice\n",
"await db_alice.signin({\n",
"\t'NS': 'test',\n",
"\t'DB': 'test',\n",
"\t'SC': 'allusers',\n",
"\t'email': '[email protected]',\n",
"\t'password': 'foobar',\n",
"})"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"# Setup a connection for Bob\n",
"db_bob = Surreal(\"http://localhost:8000\")\n",
"await db_bob.connect()"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJpYXQiOjE2OTkwNDk3NzgsIm5iZiI6MTY5OTA0OTc3OCwiZXhwIjoxNjk5MTM2MTc4LCJpc3MiOiJTdXJyZWFsREIiLCJOUyI6InRlc3QiLCJEQiI6InRlc3QiLCJTQyI6ImFsbHVzZXJzIiwiSUQiOiJ1c2VyOmk0cW94c2ZyMnh1N3F6N2t5NTJkIn0.LPOlpCZ2t2LdVg-gOjgDxAXkPxHF4sgcN_8xJIo54Zzk-VmalVYXhE9xmdPjXYN8RRbtBEjv-g6cRXIhJmy-3w'"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Create a new user\n",
"await db_bob.signup({\n",
"\t'NS': 'test',\n",
"\t'DB': 'test',\n",
"\t'SC': 'allusers',\n",
"\t'email': '[email protected]',\n",
"\t'password': 'foobar',\n",
" 'name': 'Ol Bob-O-Rino'\n",
"})"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'result': [{'email': '[email protected]',\n",
" 'id': 'user:i4qoxsfr2xu7qz7ky52d',\n",
" 'name': 'Ol Bob-O-Rino',\n",
" 'password': '$argon2id$v=19$m=19456,t=2,p=1$QG6k3Ns4k05g+VnpdKFBEw$IOuNfOHbbm+pvFfaKQ/eBvrsXXqKbNyR0b/mIeKSdJk'}],\n",
" 'status': 'OK',\n",
" 'time': '255.942µs'}]"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Bob can Fetch Bob\n",
"bob = await db_bob.query('SELECT * FROM user WHERE email = $email', {\n",
"\t'email': '[email protected]',\n",
"})\n",
"bob"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'result': [], 'status': 'OK', 'time': '453.005µs'}]"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Can Alice fetch Bob?\n",
"# This is a weird one, no error is thrown, but the result is empty so permissions must be working. I'd really expect an error here...\n",
"alices_bob = await db_alice.query('SELECT * FROM user WHERE email = $email', {\n",
"\t'email': '[email protected]',\n",
"})\n",
"alices_bob"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'result': [{'email': '[email protected]',\n",
" 'id': 'user:94s262tcgoxyp77c5tkx',\n",
" 'name': 'All-Ice',\n",
" 'password': '$argon2id$v=19$m=19456,t=2,p=1$RdI0yRmUuOXFbZpzxEpXhA$KtO44Nl578PVQKR4BY8yb1+Rhp0CK9XBrv+DEWMctCs'}],\n",
" 'status': 'OK',\n",
" 'time': '438.955µs'}]"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Alice can Fetch Alice\n",
"alice = await db_alice.query('SELECT * FROM user WHERE email = $email', {\n",
"\t'email': '[email protected]',\n",
"})\n",
"alice"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'email': '[email protected]',\n",
" 'id': 'user:i4qoxsfr2xu7qz7ky52d',\n",
" 'name': 'The Bobster',\n",
" 'password': '$argon2id$v=19$m=19456,t=2,p=1$QG6k3Ns4k05g+VnpdKFBEw$IOuNfOHbbm+pvFfaKQ/eBvrsXXqKbNyR0b/mIeKSdJk'}"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Bob can update Bob\n",
"actual_bob = bob[0]['result'][0]\n",
"actual_bob['name'] = 'The Bobster'\n",
"await db_bob.update(actual_bob['id'], actual_bob)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"# Can Alice update Bob?\n",
"# Apparently not, but again, no error is thrown, just an empty result\n",
"actual_bob['name'] = 'A1Ice Hacked You'\n",
"result = await db_alice.update(actual_bob['id'], actual_bob)\n",
"result"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Part 2 - Scope based permissions via relations"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'result': None, 'status': 'OK', 'time': '899.726µs'}]"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# First, Admin creates organization table - this one is schemaless for fun.\n",
"await db_admin.query('''\n",
" DEFINE TABLE organization SCHEMALESS\n",
"''')"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'result': None, 'status': 'OK', 'time': '2.708529ms'}]"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Admin creates the organization \"founded\" relation\n",
"await db_admin.query('''\n",
" DEFINE TABLE founded SCHEMALESS\n",
" PERMISSIONS\n",
" FOR select, update, delete WHERE in.id = $auth.id, \n",
" FOR create NONE;\n",
"''')"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'result': None, 'status': 'OK', 'time': '665.043µs'},\n",
" {'result': None, 'status': 'OK', 'time': '546.586µs'}]"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Next, Admin creates the in and out columns - I want in to be user, out to be organization\n",
"await db_admin.query('''\n",
" DEFINE FIELD in ON TABLE founded TYPE record<user>;\n",
" DEFINE FIELD out ON TABLE founded TYPE record<organization>;\n",
"''')"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'result': None, 'status': 'OK', 'time': '974.613µs'}]"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# For the heck of it let try using an index to make it so that a user can only found an organization once\n",
"await db_admin.query('''\n",
" DEFINE INDEX unique_founders\n",
" ON TABLE founded\n",
" COLUMNS in, out UNIQUE;\n",
"''')"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'result': None, 'status': 'OK', 'time': '694.004µs'}]"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Use an event to automatically setup current user as a founder when they create an organization\n",
"# Note that I like the WHEN $event = \"<TYPE>\" syntax below better, but used this which I found in the docs for my create\n",
"await db_admin.query('''\n",
"DEFINE EVENT created ON TABLE organization WHEN $before == NONE THEN {\n",
" RELATE $auth->founded->$after\n",
"}; \n",
"''')"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'result': None, 'status': 'OK', 'time': '667.587µs'}]"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Cleanup orgs when user deleted (Cascade)\n",
"# This one took me an embarassingly long time to figure out.\n",
"# Core issue is that by the time this event is called, the user is gone so you cannot use $before->founded to reference their organizations.\n",
"#\n",
"# There's probably a better way to do this but I couldn't find it in the docs.\n",
"#\n",
"# What I settled on is using the event to delete ANY organization that is orphaned.\n",
"# \n",
"# This solution is helpful in that it only takes effect when last founder of an organization is deleted.\n",
"#\n",
"# Note, I am using {} instead of () for the THEN which if you must do if using multipe statements (I only had one now but kept the {})\n",
"await db_admin.query('''\n",
"DEFINE EVENT user_deleted ON TABLE user WHEN $event = \"DELETE\" THEN {\n",
" DELETE organization WHERE array::len(<-founded) = 0\n",
"};\n",
"''')\n",
"\n",
"# Fun note, I was using this in the action above to help debug:\n",
"# INSERT INTO logs {\n",
"# event: $event,\n",
"# before: $before,\n",
"# after: $after\n",
"# }"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'result': None, 'status': 'OK', 'time': '738.723µs'}]"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Now that everything is in place, adjust permissions on org so that only founders can select, update, delete (anyone can create)\n",
"await db_admin.query('''\n",
" DEFINE TABLE organization SCHEMALESS\n",
" PERMISSIONS\n",
" FOR select, update, delete WHERE <-founded.in.id CONTAINS $auth.id, \n",
" FOR create FULL;\n",
"''')"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"# Bob creates an organization\n",
"bob_org = await db_bob.create(\n",
" \"organization\",\n",
" {\n",
" \"name\": \"Bob Inc\",\n",
" \"description\": \"A place for Bob to do Bob things\"\n",
" },\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'result': [{'description': 'A place for Bob to do Bob things',\n",
" 'id': 'organization:xms7aepykw7l54cz1dsi',\n",
" 'name': 'Bob Inc'}],\n",
" 'status': 'OK',\n",
" 'time': '716.515µs'}]"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Let's try out the relation queries\n",
"await db_bob.query('''\n",
" SELECT * FROM $auth->founded->organization\n",
"''')"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'result': [], 'status': 'OK', 'time': '278.558µs'}]"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Can alice see it?\n",
"# NOPE\n",
"await db_alice.query('''\n",
" SELECT * FROM organization\n",
"''')"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
"# Alice creates an organization\n",
"alice_org = await db_alice.create(\n",
" \"organization\",\n",
" {\n",
" \"name\": \"Wonderland\",\n",
" \"description\": \"Alice is the coolest\"\n",
" },\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'result': [{'<-founded': {'in': {'name': ['All-Ice']}},\n",
" 'name': 'Wonderland'},\n",
" {'<-founded': {'in': {'name': ['The Bobster']}}, 'name': 'Bob Inc'}],\n",
" 'status': 'OK',\n",
" 'time': '374.868µs'}]"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Let's get a view of things before we try deletes\n",
"await db_admin.query('''\n",
" SELECT name, <-founded.in.name FROM organization\n",
"''')"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'result': [{'id': 'founded:c3krepssof5g9msv66e6',\n",
" 'in': 'user:i4qoxsfr2xu7qz7ky52d',\n",
" 'out': 'organization:xms7aepykw7l54cz1dsi'},\n",
" {'id': 'founded:m1j3gaqwt7gcour15wdw',\n",
" 'in': 'user:94s262tcgoxyp77c5tkx',\n",
" 'out': 'organization:0zlvb4kf7cwq1gjhnaeu'}],\n",
" 'status': 'OK',\n",
" 'time': '234.926µs'}]"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"await db_admin.query('''\n",
" SELECT * from founded\n",
"''')"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'description': 'A place for Bob to do Bob things',\n",
" 'id': 'organization:xms7aepykw7l54cz1dsi',\n",
" 'name': 'Bob Inc'}"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Bob deletes his organization\n",
"await db_bob.delete(bob_org[0]['id'])"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'result': [{'id': 'founded:m1j3gaqwt7gcour15wdw',\n",
" 'in': 'user:94s262tcgoxyp77c5tkx',\n",
" 'out': 'organization:0zlvb4kf7cwq1gjhnaeu'}],\n",
" 'status': 'OK',\n",
" 'time': '114.023µs'}]"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Now there should only be one founded and one organization\n",
"await db_admin.query('''\n",
" SELECT * from founded\n",
"''')"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'result': [{'<-founded': {'in': {'name': ['All-Ice']}},\n",
" 'name': 'Wonderland'}],\n",
" 'status': 'OK',\n",
" 'time': '392.412µs'}]"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"await db_admin.query('''\n",
" SELECT name, <-founded.in.name FROM organization\n",
"''')"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [],
"source": [
"# Delete Alice (try as Bob first to test perms...)\n",
"# Again no error\n",
"actual_alice = alice[0]['result'][0]\n",
"await db_bob.delete(actual_alice['id'])"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'result': [{'email': '[email protected]',\n",
" 'id': 'user:94s262tcgoxyp77c5tkx',\n",
" 'name': 'All-Ice',\n",
" 'password': '$argon2id$v=19$m=19456,t=2,p=1$RdI0yRmUuOXFbZpzxEpXhA$KtO44Nl578PVQKR4BY8yb1+Rhp0CK9XBrv+DEWMctCs'},\n",
" {'email': '[email protected]',\n",
" 'id': 'user:i4qoxsfr2xu7qz7ky52d',\n",
" 'name': 'The Bobster',\n",
" 'password': '$argon2id$v=19$m=19456,t=2,p=1$QG6k3Ns4k05g+VnpdKFBEw$IOuNfOHbbm+pvFfaKQ/eBvrsXXqKbNyR0b/mIeKSdJk'}],\n",
" 'status': 'OK',\n",
" 'time': '150.643µs'}]"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# But Alice still exists\n",
"await db_admin.query('''\n",
" SELECT * FROM user\n",
"''')"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'email': '[email protected]',\n",
" 'id': 'user:94s262tcgoxyp77c5tkx',\n",
" 'name': 'All-Ice',\n",
" 'password': '$argon2id$v=19$m=19456,t=2,p=1$RdI0yRmUuOXFbZpzxEpXhA$KtO44Nl578PVQKR4BY8yb1+Rhp0CK9XBrv+DEWMctCs'}"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Now delete Alice entirely\n",
"actual_alice = alice[0]['result'][0]\n",
"await db_alice.delete(actual_alice['id'])"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'result': [], 'status': 'OK', 'time': '105.095µs'}]"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Now there should be zero founded\n",
"await db_admin.query('''\n",
" SELECT * from founded\n",
"''')"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'result': [], 'status': 'OK', 'time': '105.279µs'}]"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# And zero organizations\n",
"await db_admin.query('''\n",
" SELECT name, <-founded.in.name FROM organization\n",
"''')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Part 3 - GeoSpatial Support and Route Finding (Graph Stuff)"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [],
"source": [
"# I should've read the docs more closely and noticed that SurrealDB doesn't currently have any way to \"search\" the graph before starting on this..."
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'result': None, 'status': 'OK', 'time': '989.744µs'}]"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Create a \"node\" table\n",
"await db_admin.query('''\n",
" DEFINE TABLE city SCHEMALESS\n",
" PERMISSIONS\n",
" FOR select, update, delete, create FULL;\n",
"''')"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'result': None, 'status': 'OK', 'time': '797.904µs'}]"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Define an \"edge\" table (relation in SurrealDB)\n",
"await db_admin.query('''\n",
" DEFINE TABLE connects SCHEMALESS\n",
" PERMISSIONS\n",
" FOR select, update, delete, create FULL;\n",
"''')"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [],
"source": [
"# Create some nodes\n",
"salida = await db_admin.create(\n",
" \"city\",\n",
" {\n",
" \"name\": \"Salida, CO\",\n",
" \"center\": {\n",
" \"type\": \"Point\",\n",
" \"coordinates\": [-105.9918, 38.5363]\n",
" }\n",
" },\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [],
"source": [
"poncha = await db_admin.create(\n",
" \"city\",\n",
" {\n",
" \"name\": \"Poncha Springs, CO\",\n",
" \"center\": {\n",
" \"type\": \"Point\",\n",
" \"coordinates\": [-106.0754, 38.5147]\n",
" }\n",
" },\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [],
"source": [
"sargents = await db_admin.create(\n",
" \"city\",\n",
" {\n",
" \"name\": \"Sargents, CO\",\n",
" \"center\": {\n",
" \"type\": \"Point\",\n",
" \"coordinates\": [-106.4155, 38.4071]\n",
" }\n",
" },\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [],
"source": [
"mears = await db_admin.create(\n",
" \"city\",\n",
" {\n",
" \"name\": \"Mears Junction, CO\", # Not really a city...\n",
" \"center\": {\n",
" \"type\": \"Point\",\n",
" \"coordinates\": [-106.1067, 38.4482]\n",
" }\n",
" },\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'result': None, 'status': 'OK', 'time': '38.03µs'},\n",
" {'result': [{'distance': 3324.2395899728144, 'name': 'Poncha Springs, CO'},\n",
" {'distance': 5163.204766666214, 'name': 'Salida, CO'},\n",
" {'distance': 11074.11360195695, 'name': 'Mears Junction, CO'},\n",
" {'distance': 34890.71930463785, 'name': 'Sargents, CO'}],\n",
" 'status': 'OK',\n",
" 'time': '143.106µs'}]"
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Quick Geo Query\n",
"# Works! Neat!\n",
"await db_admin.query('''\n",
"LET $home = (-106.0511319, 38.5377932);\n",
"SELECT name, \n",
" geo::distance( center, $home) AS distance FROM city\n",
" ORDER BY distance ASC;\n",
"''')"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'result': [{'description': 'Salida to Poncha via Highway 50.',\n",
" 'distance_meters': 8208,\n",
" 'geometry': {'type': 'LineString',\n",
" 'coordinates': [[-105.991929050997, 38.5357899110908],\n",
" [-106.00303736914094, 38.52420345931668],\n",
" [-106.01672640766147, 38.52420345931668],\n",
" [-106.07675620773186, 38.514546657682644]]},\n",
" 'id': 'connects:dqke0x0ty13ugj2pd8xi',\n",
" 'in': 'city:7k3dergyb1r613aaey7h',\n",
" 'out': 'city:hi4cvxfcvratfbpwj17m'}],\n",
" 'status': 'OK',\n",
" 'time': '820.324µs'}]"
]
},
"execution_count": 45,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Setup connectons between cities\n",
"\n",
"# Salida to Poncha\n",
"await db_admin.query('''\n",
"RELATE {0}->connects->{1}\n",
"\tCONTENT {{\n",
" distance_meters: 8208,\n",
" geometry: {{\n",
"\t\t\t\"coordinates\": [[-105.991929050997,38.5357899110908],[-106.00303736914094,38.52420345931668],[-106.01672640766147,38.52420345931668],[-106.07675620773186,38.514546657682644]],\n",
"\t\t\t\"type\": \"LineString\"\n",
"\t\t}},\n",
"\t\tdescription: \"Salida to Poncha via Highway 50.\"\n",
"\t}};\n",
"'''.format(salida[0]['id'], poncha[0]['id']))"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'result': [{'description': 'Poncha to Salida via Highway 50.',\n",
" 'distance_meters': 8208,\n",
" 'geometry': {'type': 'LineString',\n",
" 'coordinates': [[-106.07675620773186, 38.514546657682644],\n",
" [-106.01672640766147, 38.52420345931668],\n",
" [-106.00303736914094, 38.52420345931668],\n",
" [-105.991929050997, 38.5357899110908]]},\n",
" 'id': 'connects:fwu7iuw1nj1d7fhy859w',\n",
" 'in': 'city:hi4cvxfcvratfbpwj17m',\n",
" 'out': 'city:7k3dergyb1r613aaey7h'}],\n",
" 'status': 'OK',\n",
" 'time': '978.353µs'}]"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Poncha to Salida\n",
"await db_admin.query('''\n",
"RELATE {0}->connects->{1}\n",
"\tCONTENT {{\n",
" distance_meters: 8208,\n",
" geometry: {{\n",
"\t\t\t\"coordinates\": [[-106.07675620773186,38.514546657682644],[-106.01672640766147,38.52420345931668],[-106.00303736914094,38.52420345931668],[-105.991929050997,38.5357899110908]],\n",
"\t\t\t\"type\": \"LineString\"\n",
"\t\t}},\n",
"\t\tdescription: \"Poncha to Salida via Highway 50.\"\n",
"\t}};\n",
"'''.format(poncha[0]['id'], salida[0]['id']))"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'result': [{'description': 'Poncha to Sargents via Highway 50.',\n",
" 'distance_meters': 52947.42,\n",
" 'geometry': {'type': 'LineString',\n",
" 'coordinates': [[-106.07651015233895, 38.514141258441896],\n",
" [-106.20123580386137, 38.541743112478116],\n",
" [-106.29517307684942, 38.55228887889382],\n",
" [-106.41990479758715, 38.40532906721944]]},\n",
" 'id': 'connects:aoiy5ebv3rl2f8hfwza1',\n",
" 'in': 'city:hi4cvxfcvratfbpwj17m',\n",
" 'out': 'city:7vn3svz84q35vzomtuz5'}],\n",
" 'status': 'OK',\n",
" 'time': '755.302µs'}]"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Poncha to Sargents\n",
"await db_admin.query('''\n",
"RELATE {0}->connects->{1}\n",
"\tCONTENT {{\n",
" distance_meters: 52947.42,\n",
" geometry: {{\n",
"\t\t\t\"coordinates\": [[-106.07651015233895,38.514141258441896],[-106.20123580386137,38.541743112478116],[-106.29517307684942,38.55228887889382],[-106.41990479758715,38.40532906721944]],\n",
"\t\t\t\"type\": \"LineString\"\n",
"\t\t}},\n",
"\t\tdescription: \"Poncha to Sargents via Highway 50.\"\n",
"\t}};\n",
"'''.format(poncha[0]['id'], sargents[0]['id']))"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'result': [{'description': 'Sargents to Poncha via Highway 50.',\n",
" 'distance_meters': 52947.42,\n",
" 'geometry': {'type': 'LineString',\n",
" 'coordinates': [[-106.41990479758715, 38.40532906721944],\n",
" [-106.29517307684942, 38.55228887889382],\n",
" [-106.20123580386137, 38.541743112478116],\n",
" [-106.07651015233895, 38.514141258441896]]},\n",
" 'id': 'connects:nans32kv8iwd0t9n197v',\n",
" 'in': 'city:7vn3svz84q35vzomtuz5',\n",
" 'out': 'city:hi4cvxfcvratfbpwj17m'}],\n",
" 'status': 'OK',\n",
" 'time': '761.375µs'}]"
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Sargents to Poncha\n",
"await db_admin.query('''\n",
"RELATE {0}->connects->{1}\n",
"\tCONTENT {{\n",
" distance_meters: 52947.42,\n",
" geometry: {{\n",
"\t\t\t\"coordinates\": [[-106.41990479758715,38.40532906721944],[-106.29517307684942,38.55228887889382],[-106.20123580386137,38.541743112478116],[-106.07651015233895,38.514141258441896]],\n",
"\t\t\t\"type\": \"LineString\"\n",
"\t\t}},\n",
"\t\tdescription: \"Sargents to Poncha via Highway 50.\"\n",
"\t}};\n",
"'''.format(sargents[0]['id'], poncha[0]['id']))"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'result': [{'description': 'Sargents to Mears Junction via Marshall Pass.',\n",
" 'distance_meters': 43774.16,\n",
" 'geometry': {'type': 'LineString',\n",
" 'coordinates': [[-106.41881373814766, 38.40255769239485],\n",
" [-106.35594978567742, 38.37372707438621],\n",
" [-106.30929040494584, 38.361965652502874],\n",
" [-106.26253637738894, 38.39124626250381],\n",
" [-106.27909233360832, 38.39332245118982],\n",
" [-106.27141036992249, 38.40359870728486],\n",
" [-106.22545100758092, 38.394983308606584],\n",
" [-106.14982337816161, 38.428193456522564],\n",
" [-106.13172499145941, 38.42749053749009],\n",
" [-106.13808247864762, 38.414104402716276],\n",
" [-106.12801645726607, 38.423962649690054],\n",
" [-106.10714079902147, 38.44790406787445]]},\n",
" 'id': 'connects:p7cz9d33jay3uulgetyg',\n",
" 'in': 'city:7vn3svz84q35vzomtuz5',\n",
" 'out': 'city:7yjffi5sz2l8p7fv1vlm'}],\n",
" 'status': 'OK',\n",
" 'time': '646.537µs'}]"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Sargents to Mears Junction\n",
"await db_admin.query('''\n",
"RELATE {0}->connects->{1}\n",
"\tCONTENT {{\n",
" distance_meters: 43774.16,\n",
" geometry: {{\n",
"\t\t\t\"coordinates\": [[-106.41881373814766,38.40255769239485],[-106.35594978567742,38.37372707438621],[-106.30929040494584,38.361965652502874],[-106.26253637738894,38.39124626250381],[-106.27909233360832,38.39332245118982],[-106.27141036992249,38.40359870728486],[-106.22545100758092,38.394983308606584],[-106.14982337816161,38.428193456522564],[-106.13172499145941,38.42749053749009],[-106.13808247864762,38.414104402716276],[-106.12801645726607,38.423962649690054],[-106.10714079902147,38.44790406787445]],\n",
"\t\t\t\"type\": \"LineString\"\n",
"\t\t}},\n",
"\t\tdescription: \"Sargents to Mears Junction via Marshall Pass.\"\n",
"\t}};\n",
"'''.format(sargents[0]['id'], mears[0]['id']))"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'result': [{'description': 'Mears Junction to Sargents via Marshall Pass.',\n",
" 'distance_meters': 43774.16,\n",
" 'geometry': {'type': 'LineString',\n",
" 'coordinates': [[-106.10714079902147, 38.44790406787445],\n",
" [-106.12801645726607, 38.423962649690054],\n",
" [-106.13808247864762, 38.414104402716276],\n",
" [-106.13172499145941, 38.42749053749009],\n",
" [-106.14982337816161, 38.428193456522564],\n",
" [-106.22545100758092, 38.394983308606584],\n",
" [-106.27141036992249, 38.40359870728486],\n",
" [-106.27909233360832, 38.39332245118982],\n",
" [-106.26253637738894, 38.39124626250381],\n",
" [-106.30929040494584, 38.361965652502874],\n",
" [-106.35594978567742, 38.37372707438621],\n",
" [-106.41881373814766, 38.40255769239485]]},\n",
" 'id': 'connects:70stgnu2wnxy1ui78ni0',\n",
" 'in': 'city:7yjffi5sz2l8p7fv1vlm',\n",
" 'out': 'city:7vn3svz84q35vzomtuz5'}],\n",
" 'status': 'OK',\n",
" 'time': '636.796µs'}]"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Mears Junction to Sargents\n",
"await db_admin.query('''\n",
"RELATE {0}->connects->{1}\n",
"\tCONTENT {{\n",
" distance_meters: 43774.16,\n",
" geometry: {{\n",
"\t\t\t\"coordinates\": [[-106.10714079902147,38.44790406787445],[-106.12801645726607,38.423962649690054],[-106.13808247864762,38.414104402716276],[-106.13172499145941,38.42749053749009],[-106.14982337816161,38.428193456522564],[-106.22545100758092,38.394983308606584],[-106.27141036992249,38.40359870728486],[-106.27909233360832,38.39332245118982],[-106.26253637738894,38.39124626250381],[-106.30929040494584,38.361965652502874],[-106.35594978567742,38.37372707438621],[-106.41881373814766,38.40255769239485]],\n",
"\t\t\t\"type\": \"LineString\"\n",
"\t\t}},\n",
"\t\tdescription: \"Mears Junction to Sargents via Marshall Pass.\"\n",
"\t}};\n",
"'''.format(mears[0]['id'], sargents[0]['id']))"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'result': [{'description': 'Mears Junction to Poncha via Highway 285.',\n",
" 'distance_meters': 8207.5,\n",
" 'geometry': {'type': 'LineString',\n",
" 'coordinates': [[-106.10707349508812, 38.4485133782826],\n",
" [-106.07634564034505, 38.514661912860106]]},\n",
" 'id': 'connects:oprhqbhnj36v3nvc38zw',\n",
" 'in': 'city:7yjffi5sz2l8p7fv1vlm',\n",
" 'out': 'city:hi4cvxfcvratfbpwj17m'}],\n",
" 'status': 'OK',\n",
" 'time': '828.763µs'}]"
]
},
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Mears Junction to Poncha\n",
"await db_admin.query('''\n",
"RELATE {0}->connects->{1}\n",
"\tCONTENT {{\n",
" distance_meters: 8207.5,\n",
" geometry: {{\n",
"\t\t\t\"coordinates\": [[-106.10707349508812,38.4485133782826],[-106.07634564034505,38.514661912860106]],\n",
"\t\t\t\"type\": \"LineString\"\n",
"\t\t}},\n",
"\t\tdescription: \"Mears Junction to Poncha via Highway 285.\"\n",
"\t}};\n",
"'''.format(mears[0]['id'], poncha[0]['id']))"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'result': [{'description': 'Poncha to Mears Junction via Highway 285.',\n",
" 'distance_meters': 8207.5,\n",
" 'geometry': {'type': 'LineString',\n",
" 'coordinates': [[-106.07634564034505, 38.514661912860106],\n",
" [-106.10707349508812, 38.4485133782826]]},\n",
" 'id': 'connects:boxebhl1dbq4v8f88g1d',\n",
" 'in': 'city:hi4cvxfcvratfbpwj17m',\n",
" 'out': 'city:7yjffi5sz2l8p7fv1vlm'}],\n",
" 'status': 'OK',\n",
" 'time': '661.547µs'}]"
]
},
"execution_count": 52,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Poncha to Mears Junction\n",
"await db_admin.query('''\n",
"RELATE {0}->connects->{1}\n",
"\tCONTENT {{\n",
" distance_meters: 8207.5,\n",
" geometry: {{\n",
"\t\t\t\"coordinates\": [[-106.07634564034505,38.514661912860106],[-106.10707349508812,38.4485133782826]],\n",
"\t\t\t\"type\": \"LineString\"\n",
"\t\t}},\n",
"\t\tdescription: \"Poncha to Mears Junction via Highway 285.\"\n",
"\t}};\n",
"'''.format(poncha[0]['id'], mears[0]['id']))"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'result': [{'one_hop': ['Poncha Springs, CO', 'Mears Junction, CO'],\n",
" 'two_hop': ['Sargents, CO',\n",
" 'Mears Junction, CO',\n",
" 'Salida, CO',\n",
" 'Sargents, CO',\n",
" 'Poncha Springs, CO']}],\n",
" 'status': 'OK',\n",
" 'time': '1.031628ms'}]"
]
},
"execution_count": 53,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Make sure connections for two different paths from sargents to salida are setup\n",
"await db_admin.query('''\n",
" SELECT \n",
" -- One Hop Cities\n",
" ->connects->city.name as one_hop,\n",
" -- Two Hop Cities\n",
" ->connects->city->connects->city.name as two_hop\n",
" FROM {0}\n",
"'''.format(sargents[0]['id']))"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'result': [], 'status': 'OK', 'time': '107.428µs'}]"
]
},
"execution_count": 54,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Try to get distance to go from Sargents to Salida via Poncha (and not Mears Junction)\n",
"# Step 1, see if we can WHERE onto a specific path\n",
"# No errors makes this really hard to debug...\n",
"await db_admin.query('''\n",
" SELECT \n",
" ->connects->(city as dest)\n",
" FROM {0}\n",
" WHERE dest.name = 'Poncha Springs, CO'\n",
"'''.format(sargents[0]['id']))"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'result': [{'center': {'type': 'Point', 'coordinates': [-106.0754, 38.5147]},\n",
" 'id': 'city:hi4cvxfcvratfbpwj17m',\n",
" 'name': 'Poncha Springs, CO'}],\n",
" 'status': 'OK',\n",
" 'time': '163.666µs'}]"
]
},
"execution_count": 55,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Just to make sure I am basic WHEREing correctly\n",
"await db_admin.query('''\n",
" SELECT \n",
" *\n",
" FROM city\n",
" WHERE name = 'Poncha Springs, CO'\n",
"''')"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [],
"source": [
"# Wheels pretty much fell off here...\n",
"# Couldn't find the right syntax to do what I wanted, and it may not even be possible.\n",
"\n",
"# Eventual goal would be able to do a search that finds the shortest path between Sargents and Salida, and lists out the connections."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Part 4 - Vector Queries"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For this section you'll need sentence transformers installed\n",
"\n",
"```\n",
"pip install sentence-transformers\n",
"```\n",
"\n",
"On windows with an Nvidia GPU I used some additional steps to get pytorch installed\n",
"\n",
"https://pytorch.org/get-started/locally/ (use command to get cuda version : nvcc --version)\n",
"\n",
"```\n",
"pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"c:\\Users\\ablon\\Code\\Mine\\SurrealPython\\wenv\\Lib\\site-packages\\tqdm\\auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
" from .autonotebook import tqdm as notebook_tqdm\n"
]
}
],
"source": [
"from sentence_transformers import SentenceTransformer\n",
"model = SentenceTransformer('all-MiniLM-L6-v2')"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {},
"outputs": [],
"source": [
"sentence1 = \"The current educational system is there only to condition you into being a good little consumer.\"\n",
"embedding1 = model.encode(sentence1)"
]
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'embedding': [0.05556343123316765,\n",
" 0.004294418729841709,\n",
" 0.009202364832162857,\n",
" -0.0003290958993602544,\n",
" 0.047453489154577255,\n",
" 0.007652501109987497,\n",
" -0.039665888994932175,\n",
" 0.053681764751672745,\n",
" -0.03310356289148331,\n",
" 0.05484912544488907,\n",
" 0.11046356707811356,\n",
" 0.08009911328554153,\n",
" -9.803618740988895e-05,\n",
" 0.005658614914864302,\n",
" 0.031404729932546616,\n",
" -0.11973253637552261,\n",
" 0.1076618880033493,\n",
" -0.06538268178701401,\n",
" 0.04543856903910637,\n",
" -0.042158860713243484,\n",
" -0.048113495111465454,\n",
" 0.04977067932486534,\n",
" -0.02397429384291172,\n",
" 0.05160760134458542,\n",
" -0.021593283861875534,\n",
" 0.04693783447146416,\n",
" 0.0016731687355786562,\n",
" -0.07758345454931259,\n",
" -0.004819883964955807,\n",
" -0.05560889095067978,\n",
" 0.00022821981110610068,\n",
" 0.04238602891564369,\n",
" 0.0613287016749382,\n",
" -0.020873943343758583,\n",
" 0.013320649974048138,\n",
" 0.002709680935367942,\n",
" 0.10030609369277954,\n",
" -0.024604836478829384,\n",
" -0.049877237528562546,\n",
" -0.03170037642121315,\n",
" -0.02097957767546177,\n",
" -0.0307863038033247,\n",
" -0.07569237798452377,\n",
" -0.036710936576128006,\n",
" 0.03603167459368706,\n",
" -0.03399371728301048,\n",
" 0.019616320729255676,\n",
" -0.10384025424718857,\n",
" 0.016317112371325493,\n",
" 0.00997072272002697,\n",
" -0.07071969658136368,\n",
" -0.026971891522407532,\n",
" 0.04808355122804642,\n",
" -0.07396135479211807,\n",
" -0.031360872089862823,\n",
" -0.002454106230288744,\n",
" -0.007444135379046202,\n",
" -0.0032175208907574415,\n",
" -0.09538063406944275,\n",
" -0.025181293487548828,\n",
" -0.035771444439888,\n",
" -0.02879348397254944,\n",
" -0.014937128871679306,\n",
" 0.021588435396552086,\n",
" 0.0752134621143341,\n",
" 0.016860686242580414,\n",
" -0.12206775695085526,\n",
" 0.032129716128110886,\n",
" -0.03212263062596321,\n",
" -0.03456738963723183,\n",
" -0.01928517408668995,\n",
" -0.030061207711696625,\n",
" 0.043838176876306534,\n",
" 0.08249664306640625,\n",
" 0.09158279746770859,\n",
" -0.04836126044392586,\n",
" 0.011935180053114891,\n",
" -0.020279167219996452,\n",
" 0.05064840987324715,\n",
" 0.0862719714641571,\n",
" 0.06131196767091751,\n",
" 0.006196512375026941,\n",
" -0.03247502073645592,\n",
" -0.04013150557875633,\n",
" -0.032574787735939026,\n",
" -0.0675218477845192,\n",
" -0.030352331697940826,\n",
" -0.07510075718164444,\n",
" 0.0006126918597146869,\n",
" -0.05498056858778,\n",
" 0.09385653585195541,\n",
" 0.04432246461510658,\n",
" 0.07728297263383865,\n",
" 0.034159086644649506,\n",
" 0.03485095873475075,\n",
" -0.05335395038127899,\n",
" -0.0015301810344681144,\n",
" -0.014712231233716011,\n",
" -0.01655210740864277,\n",
" -0.002262298483401537,\n",
" -0.04718276113271713,\n",
" 0.024242736399173737,\n",
" 0.06976485252380371,\n",
" 0.03233395889401436,\n",
" -0.0493481270968914,\n",
" -0.05518311262130737,\n",
" -0.007384073920547962,\n",
" 0.049133848398923874,\n",
" 0.00024943501921370625,\n",
" -0.03736881911754608,\n",
" -0.057643234729766846,\n",
" 0.053280480206012726,\n",
" -0.00015500650624744594,\n",
" 0.03069092333316803,\n",
" 0.01610453613102436,\n",
" -0.004454893991351128,\n",
" 0.02707546204328537,\n",
" -0.02258787490427494,\n",
" 0.0005803564563393593,\n",
" 0.08976402133703232,\n",
" 0.01111428253352642,\n",
" 0.009390752762556076,\n",
" -0.015059933997690678,\n",
" -0.016017241403460503,\n",
" -0.027556296437978745,\n",
" -0.11461587250232697,\n",
" -0.026927443221211433,\n",
" -3.944164480646838e-33,\n",
" -0.019772248342633247,\n",
" 0.07365910708904266,\n",
" 0.013642381876707077,\n",
" -0.03936408832669258,\n",
" -0.06722144782543182,\n",
" 0.008831722661852837,\n",
" 0.013913187198340893,\n",
" 0.010036655701696873,\n",
" 0.028287842869758606,\n",
" -0.007763993460685015,\n",
" 0.10238077491521835,\n",
" 0.06110883504152298,\n",
" -0.022736070677638054,\n",
" 0.09331578016281128,\n",
" 0.10307471454143524,\n",
" 0.050947632640600204,\n",
" -0.09214228391647339,\n",
" 0.023140067234635353,\n",
" 0.06791117042303085,\n",
" 0.06362694501876831,\n",
" -0.04645173251628876,\n",
" -0.024200791493058205,\n",
" 0.026113133877515793,\n",
" -0.049509793519973755,\n",
" -0.039980288594961166,\n",
" -0.03168097138404846,\n",
" 0.001256644376553595,\n",
" 0.053741615265607834,\n",
" 0.06915345788002014,\n",
" -0.029123833402991295,\n",
" -0.021404432132840157,\n",
" -0.01237779576331377,\n",
" -0.06770190596580505,\n",
" -0.06549990922212601,\n",
" 0.0036927792243659496,\n",
" 0.02668856270611286,\n",
" 0.03185464069247246,\n",
" -0.016444986686110497,\n",
" 0.036856669932603836,\n",
" -0.08911557495594025,\n",
" -0.036901529878377914,\n",
" 0.011288943700492382,\n",
" 0.043356139212846756,\n",
" 0.03823259845376015,\n",
" 0.03213158994913101,\n",
" 0.10601028800010681,\n",
" 0.05111658200621605,\n",
" -0.05058038607239723,\n",
" -0.10681338608264923,\n",
" 0.07889239490032196,\n",
" -0.06417623162269592,\n",
" -0.056103453040122986,\n",
" -0.014154612086713314,\n",
" -0.0013610797468572855,\n",
" -0.015564999543130398,\n",
" 0.04352785274386406,\n",
" 0.05357149988412857,\n",
" -0.0034440853632986546,\n",
" -0.09932626038789749,\n",
" -0.10115032643079758,\n",
" -0.08994637429714203,\n",
" 0.003398345783352852,\n",
" 0.007727354299277067,\n",
" -0.05711624026298523,\n",
" -0.04508533328771591,\n",
" 0.06522420793771744,\n",
" 0.024985909461975098,\n",
" 0.0007206499576568604,\n",
" 0.036099981516599655,\n",
" -0.018629660829901695,\n",
" -0.025441505014896393,\n",
" -0.04215044155716896,\n",
" -0.05343443900346756,\n",
" -0.02696552872657776,\n",
" 0.04101177677512169,\n",
" 0.012516682967543602,\n",
" -0.02137807197868824,\n",
" -0.04201481491327286,\n",
" 0.08596152812242508,\n",
" -0.003893299261108041,\n",
" 0.09698068350553513,\n",
" -0.021434227004647255,\n",
" 0.0758882686495781,\n",
" -0.005150539334863424,\n",
" 0.129184752702713,\n",
" 0.0477607324719429,\n",
" 0.05172751098871231,\n",
" -0.006272668484598398,\n",
" 0.08917024731636047,\n",
" 0.027530957013368607,\n",
" 0.007228298112750053,\n",
" -0.030660640448331833,\n",
" -0.038881778717041016,\n",
" 0.1365761011838913,\n",
" 0.03409009799361229,\n",
" 1.572971969847505e-33,\n",
" -0.029684768989682198,\n",
" 0.01694261096417904,\n",
" -0.04980900138616562,\n",
" 0.06341148912906647,\n",
" -0.06189795583486557,\n",
" -0.016678353771567345,\n",
" 0.04098037630319595,\n",
" -0.021025683730840683,\n",
" -0.0479087233543396,\n",
" 0.02157353237271309,\n",
" 0.006296270526945591,\n",
" 0.030035024508833885,\n",
" -0.010959726758301258,\n",
" 0.037293940782547,\n",
" -0.019810067489743233,\n",
" -0.018834754824638367,\n",
" -0.04931391775608063,\n",
" -0.0070377858355641365,\n",
" 0.04403712600469589,\n",
" -0.056244250386953354,\n",
" 0.005150969140231609,\n",
" 0.10919014364480972,\n",
" -0.055476997047662735,\n",
" 0.024388696998357773,\n",
" -0.010799160227179527,\n",
" -0.05582382529973984,\n",
" -0.136243537068367,\n",
" 0.020854365080595016,\n",
" -0.03224502131342888,\n",
" -0.015053885988891125,\n",
" 0.06840737909078598,\n",
" -0.04862231761217117,\n",
" 0.0326046384871006,\n",
" -0.0020928250160068274,\n",
" -0.029294637963175774,\n",
" -0.02435675822198391,\n",
" -0.010797618888318539,\n",
" 0.02716238796710968,\n",
" -0.0706014335155487,\n",
" 0.06029820814728737,\n",
" 0.013245969079434872,\n",
" -0.08020073920488358,\n",
" -0.05683133751153946,\n",
" -0.08347482979297638,\n",
" -0.025971446186304092,\n",
" -0.02873602882027626,\n",
" 0.07617668062448502,\n",
" -0.019452614709734917,\n",
" 0.06888279318809509,\n",
" 0.02325262315571308,\n",
" -0.013508198782801628,\n",
" -0.007225749082863331,\n",
" 0.03598327189683914,\n",
" -0.05825973302125931,\n",
" 0.006921953987330198,\n",
" 0.05683041363954544,\n",
" 0.09268597513437271,\n",
" 0.026885539293289185,\n",
" 0.004916636273264885,\n",
" -0.01331348530948162,\n",
" 0.017239924520254135,\n",
" -0.019914913922548294,\n",
" -0.05113187059760094,\n",
" -0.022660233080387115,\n",
" 0.0726444274187088,\n",
" -0.10037952661514282,\n",
" 0.0439598448574543,\n",
" 0.0978250578045845,\n",
" -0.0381707139313221,\n",
" -0.032591450959444046,\n",
" 0.10666246712207794,\n",
" 0.014333448372781277,\n",
" -0.011060887947678566,\n",
" -0.08993946760892868,\n",
" -0.06294393539428711,\n",
" 0.04363248869776726,\n",
" -0.017691779881715775,\n",
" 0.008431822061538696,\n",
" -0.04801512137055397,\n",
" -0.04109027981758118,\n",
" -0.02567579783499241,\n",
" -0.0528600849211216,\n",
" 0.06362109631299973,\n",
" -0.04971582442522049,\n",
" -0.00034651404712349176,\n",
" -0.048937730491161346,\n",
" 0.006628254894167185,\n",
" -0.13324910402297974,\n",
" -0.002292268443852663,\n",
" 0.010441853664815426,\n",
" -0.02954508736729622,\n",
" -0.0075072371400892735,\n",
" 0.011056031100451946,\n",
" -0.015273768454790115,\n",
" -0.04478270187973976,\n",
" -2.6080561710273287e-08,\n",
" 0.041643328964710236,\n",
" -0.11086749285459518,\n",
" 0.03995637968182564,\n",
" 0.024027913808822632,\n",
" 0.011470837518572807,\n",
" 0.022283539175987244,\n",
" -0.023372715339064598,\n",
" -0.005311159882694483,\n",
" 0.028320154175162315,\n",
" 0.09739430248737335,\n",
" -0.008041012100875378,\n",
" 0.022894661873579025,\n",
" 0.01804758422076702,\n",
" -0.020842984318733215,\n",
" 0.127645343542099,\n",
" 0.09482108801603317,\n",
" 0.08351132273674011,\n",
" -0.06387430429458618,\n",
" -0.04454030469059944,\n",
" 0.08102548122406006,\n",
" 0.054134707897901535,\n",
" 0.02690291218459606,\n",
" -0.00748200248926878,\n",
" 0.04080050811171532,\n",
" -0.06397181004285812,\n",
" 0.024690626189112663,\n",
" 0.062495771795511246,\n",
" -0.0029274499975144863,\n",
" -0.025600772351026535,\n",
" 0.02268575318157673,\n",
" 0.005059367977082729,\n",
" -0.020120473578572273,\n",
" 0.00836583785712719,\n",
" -0.052283983677625656,\n",
" 0.03990040719509125,\n",
" -0.038568221032619476,\n",
" -0.057027045637369156,\n",
" 0.028112314641475677,\n",
" 0.0076384395360946655,\n",
" -0.025302402675151825,\n",
" -0.08269116282463074,\n",
" -0.07811449468135834,\n",
" 0.02586652897298336,\n",
" 0.08300485461950302,\n",
" 0.017014143988490105,\n",
" 0.03536681830883026,\n",
" -0.04118141531944275,\n",
" -0.004788470454514027,\n",
" 0.03997788950800896,\n",
" 0.09663501381874084,\n",
" -0.025234876200556755,\n",
" -0.01841381937265396,\n",
" 0.02665531449019909,\n",
" -0.12789689004421234,\n",
" 0.02569139003753662,\n",
" -0.03646022081375122,\n",
" 0.0677681565284729,\n",
" -0.01034536026418209,\n",
" -0.05620076507329941,\n",
" 0.018822386860847473,\n",
" 0.08596796542406082,\n",
" 0.00078419572673738,\n",
" 0.051737599074840546,\n",
" 0.07829482108354568],\n",
" 'id': 'context:c1',\n",
" 'text': 'The current educational system is there only to condition you into being a good little consumer.'}"
]
},
"execution_count": 59,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"await db_admin.create(\n",
" \"context:c1\",\n",
" {\n",
" \"text\": sentence1,\n",
" # This didn't work : Object of type ndarray is not JSON serializable\n",
" # \"embedding\": embedding1\n",
" # This does work\n",
" \"embedding\": embedding1.tolist()\n",
" },\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'embedding': [0.03942417725920677,\n",
" -0.028966505080461502,\n",
" 0.03487392142415047,\n",
" 0.03160249441862106,\n",
" -0.028416521847248077,\n",
" -0.025273559615015984,\n",
" 0.018805529922246933,\n",
" 0.03589262068271637,\n",
" 0.01960798166692257,\n",
" 0.05399400740861893,\n",
" 0.044517721980810165,\n",
" -0.07303101569414139,\n",
" -0.0015130022075027227,\n",
" 0.013144866563379765,\n",
" -0.02350962720811367,\n",
" 0.017959600314497948,\n",
" -0.004997747018933296,\n",
" -0.03788933530449867,\n",
" -0.03834698721766472,\n",
" -0.02459278330206871,\n",
" -0.04329688847064972,\n",
" -0.0165939312428236,\n",
" 0.017263304442167282,\n",
" 0.02998252958059311,\n",
" 0.042083535343408585,\n",
" 0.10422585159540176,\n",
" -0.01120177935808897,\n",
" -0.06659144908189774,\n",
" -0.06923587620258331,\n",
" 0.01277244370430708,\n",
" -0.02740815281867981,\n",
" 0.02627236396074295,\n",
" -0.09566247463226318,\n",
" 0.050730492919683456,\n",
" 0.028854286298155785,\n",
" 0.018870970234274864,\n",
" 0.0458529070019722,\n",
" 0.009027459658682346,\n",
" 0.07054361701011658,\n",
" 0.06351760774850845,\n",
" -0.01677323877811432,\n",
" 0.04831766337156296,\n",
" 0.04706357792019844,\n",
" 0.008952045813202858,\n",
" 0.031007887795567513,\n",
" -0.02635190822184086,\n",
" 0.0034532970748841763,\n",
" -0.0006550716934725642,\n",
" 0.023300064727663994,\n",
" 0.009916428476572037,\n",
" -0.013930194079875946,\n",
" -0.024388201534748077,\n",
" -0.11209459602832794,\n",
" 0.015168333426117897,\n",
" 0.028684906661510468,\n",
" 0.06215183436870575,\n",
" 0.021687207743525505,\n",
" -0.03686701878905296,\n",
" -0.0032895102631300688,\n",
" 0.052042245864868164,\n",
" -0.07136967778205872,\n",
" 0.039259664714336395,\n",
" -0.0004542133247014135,\n",
" 0.01405126042664051,\n",
" -0.03292215242981911,\n",
" -0.023662185296416283,\n",
" 0.054887138307094574,\n",
" 0.0684266984462738,\n",
" 0.007099841721355915,\n",
" 0.04603107273578644,\n",
" 0.005188197363168001,\n",
" 0.09421506524085999,\n",
" -0.06020282953977585,\n",
" 0.12147198617458344,\n",
" 0.09057663381099701,\n",
" 0.013037382625043392,\n",
" 0.07022902369499207,\n",
" 0.056415680795907974,\n",
" 0.07758264988660812,\n",
" 0.012698143720626831,\n",
" -0.028934180736541748,\n",
" 0.02814009226858616,\n",
" -0.04338698834180832,\n",
" 0.013616730459034443,\n",
" 0.046136487275362015,\n",
" -0.06990507990121841,\n",
" -0.015538543462753296,\n",
" -0.02644645795226097,\n",
" 0.010740619152784348,\n",
" 0.010770286433398724,\n",
" 0.0188426673412323,\n",
" -0.11288075894117355,\n",
" -0.01296324748545885,\n",
" 0.01406469289213419,\n",
" -0.04461154714226723,\n",
" -0.059086576104164124,\n",
" -0.039392102509737015,\n",
" -0.0410994328558445,\n",
" 0.0576651506125927,\n",
" 0.10027241706848145,\n",
" -0.050920553505420685,\n",
" 0.009051788598299026,\n",
" 0.059131182730197906,\n",
" 0.08816102892160416,\n",
" 0.009366109035909176,\n",
" 0.030611222609877586,\n",
" -0.0005782642401754856,\n",
" 0.04482283443212509,\n",
" 0.04327201470732689,\n",
" 0.03799906745553017,\n",
" 0.0226732324808836,\n",
" -0.016430925577878952,\n",
" -0.03091818280518055,\n",
" 0.0038605318404734135,\n",
" -0.027303120121359825,\n",
" 0.08881089091300964,\n",
" 0.05251066014170647,\n",
" 0.01073777861893177,\n",
" -0.00032750930404290557,\n",
" -0.027685044333338737,\n",
" -0.03130287304520607,\n",
" 0.0013148864964023232,\n",
" -0.022851884365081787,\n",
" -0.006908097304403782,\n",
" -0.0914023295044899,\n",
" -0.11692706495523453,\n",
" 0.00041801933548413217,\n",
" -6.339894333993632e-33,\n",
" 0.010996607132256031,\n",
" 0.017722558230161667,\n",
" 0.018549852073192596,\n",
" 0.11754840612411499,\n",
" -0.003145806724205613,\n",
" 0.04730520397424698,\n",
" -0.005666153039783239,\n",
" 0.045705877244472504,\n",
" -0.06937083601951599,\n",
" -0.006225838791579008,\n",
" 9.786187001736835e-05,\n",
" 0.026717068627476692,\n",
" -0.03651266545057297,\n",
" 0.023112565279006958,\n",
" 0.02224794775247574,\n",
" 0.12309467047452927,\n",
" -0.04962731525301933,\n",
" 0.059221141040325165,\n",
" 0.05005037784576416,\n",
" 0.03474630042910576,\n",
" 0.0299391970038414,\n",
" -0.00622074818238616,\n",
" 0.032785508781671524,\n",
" -0.02041163668036461,\n",
" 0.013237242586910725,\n",
" -0.09819958359003067,\n",
" -0.004121681209653616,\n",
" -0.0031455315183848143,\n",
" -0.00043562334030866623,\n",
" -0.0008302510832436383,\n",
" -0.04842798784375191,\n",
" 0.015637220814824104,\n",
" -0.026722773909568787,\n",
" -0.11356770992279053,\n",
" 0.07071284204721451,\n",
" -0.07766487449407578,\n",
" 0.030709639191627502,\n",
" -0.05890495330095291,\n",
" -0.017547277733683586,\n",
" -0.030998973175883293,\n",
" -0.00428371736779809,\n",
" -0.05674007534980774,\n",
" -0.017173554748296738,\n",
" -0.05667073279619217,\n",
" -0.00014102166460361332,\n",
" 0.009664727374911308,\n",
" 0.04894175007939339,\n",
" -0.027307316660881042,\n",
" 0.05095800757408142,\n",
" 0.030484475195407867,\n",
" -0.08826688677072525,\n",
" -0.058060888200998306,\n",
" 0.03529617190361023,\n",
" -0.023408235982060432,\n",
" -0.019628947600722313,\n",
" -0.03068782202899456,\n",
" 0.07529895752668381,\n",
" -0.030101466923952103,\n",
" -0.015106826089322567,\n",
" 0.012971175834536552,\n",
" -0.05967918783426285,\n",
" -0.017564982175827026,\n",
" 0.0021492515224963427,\n",
" 0.07756220549345016,\n",
" 0.007618543226271868,\n",
" 0.010347912088036537,\n",
" -0.04863625392317772,\n",
" 0.044602714478969574,\n",
" 0.06341478228569031,\n",
" 0.000434416375355795,\n",
" -0.04944302514195442,\n",
" -0.02593156322836876,\n",
" -0.1155017837882042,\n",
" 0.009386982768774033,\n",
" -0.0509536936879158,\n",
" 0.011014501564204693,\n",
" -0.00910925306379795,\n",
" -0.015695296227931976,\n",
" -0.009973444975912571,\n",
" -0.08954038470983505,\n",
" 0.10453679412603378,\n",
" -0.018361220136284828,\n",
" 0.04444832727313042,\n",
" 0.02063450589776039,\n",
" 0.0799982026219368,\n",
" 0.06786037236452103,\n",
" -0.0507161021232605,\n",
" -0.06136306747794151,\n",
" 0.01814623549580574,\n",
" 0.016373146325349808,\n",
" -0.008534969761967659,\n",
" -0.01875469833612442,\n",
" 0.016764529049396515,\n",
" -0.0008242271724157035,\n",
" -0.005601481068879366,\n",
" 3.349687967419221e-33,\n",
" 0.034819431602954865,\n",
" 0.03973652049899101,\n",
" 0.02544880099594593,\n",
" -0.0015731295570731163,\n",
" 0.10872673243284225,\n",
" -0.12464772909879684,\n",
" 0.010637491010129452,\n",
" -0.025545192882418633,\n",
" 0.04102570191025734,\n",
" 0.007796102203428745,\n",
" 0.09447683393955231,\n",
" -0.02925744839012623,\n",
" 0.08055366575717926,\n",
" 0.05569528788328171,\n",
" 0.04552247375249863,\n",
" 0.013151662424206734,\n",
" 0.08590628206729889,\n",
" 0.026816949248313904,\n",
" -0.07350186258554459,\n",
" -0.043227776885032654,\n",
" -0.08413349092006683,\n",
" -0.007904600352048874,\n",
" -0.010731899179518223,\n",
" 0.0079296650364995,\n",
" -0.03403344377875328,\n",
" -0.07567161321640015,\n",
" -0.013588495552539825,\n",
" 0.02658688649535179,\n",
" -0.06161954253911972,\n",
" 0.0007801383617334068,\n",
" 0.022941093891859055,\n",
" -0.007837767712771893,\n",
" -0.003180956467986107,\n",
" -0.05064604803919792,\n",
" -0.09389619529247284,\n",
" 0.02369924634695053,\n",
" -0.06510864198207855,\n",
" -0.04791971296072006,\n",
" 0.029331263154745102,\n",
" -0.04037298634648323,\n",
" 0.07974320650100708,\n",
" -0.115145243704319,\n",
" 0.014810511842370033,\n",
" 0.04054168984293938,\n",
" -0.013627768494188786,\n",
" 0.0472027026116848,\n",
" -0.0379534587264061,\n",
" 0.04237267002463341,\n",
" 0.03674521669745445,\n",
" 0.033210959285497665,\n",
" -0.10707084089517593,\n",
" -0.08745736628770828,\n",
" 0.08902743458747864,\n",
" 0.030533751472830772,\n",
" 0.0459921695291996,\n",
" -0.041608717292547226,\n",
" 0.031411778181791306,\n",
" -0.06696669012308121,\n",
" -0.03141063079237938,\n",
" 0.029658524319529533,\n",
" 0.04128078371286392,\n",
" -0.009801572188735008,\n",
" -0.12270623445510864,\n",
" -0.03259044140577316,\n",
" -0.1139848604798317,\n",
" -0.04673903435468674,\n",
" -0.08356223255395889,\n",
" -0.04431872069835663,\n",
" -0.08872272074222565,\n",
" 0.10472618788480759,\n",
" 0.07297972589731216,\n",
" 0.07513940334320068,\n",
" -0.08987966924905777,\n",
" -0.044153712689876556,\n",
" -0.09142844378948212,\n",
" 0.022102264687418938,\n",
" 0.03256796672940254,\n",
" 0.08102976530790329,\n",
" 0.042528267949819565,\n",
" -0.03767116740345955,\n",
" -0.020489482209086418,\n",
" -0.03577304631471634,\n",
" 0.025849221274256706,\n",
" -0.022029660642147064,\n",
" 0.02753305248916149,\n",
" -0.08466365188360214,\n",
" 0.019722500815987587,\n",
" -0.029919037595391273,\n",
" 0.03704012557864189,\n",
" -0.0069266026839613914,\n",
" -0.0010049197589978576,\n",
" -0.008645372465252876,\n",
" 0.07069520652294159,\n",
" -0.059609487652778625,\n",
" -0.08805245161056519,\n",
" -1.6423607362980874e-08,\n",
" -0.03695381432771683,\n",
" -0.024575669318437576,\n",
" 0.0021488587372004986,\n",
" -0.028144480660557747,\n",
" -0.040761690586805344,\n",
" 0.04749785363674164,\n",
" 0.07306743413209915,\n",
" -0.016659999266266823,\n",
" 0.041338976472616196,\n",
" -0.02863464690744877,\n",
" -0.014408647082746029,\n",
" -0.046226274222135544,\n",
" -0.012546412646770477,\n",
" 0.002626060275360942,\n",
" 0.06541702896356583,\n",
" 0.06705889850854874,\n",
" 0.08002825081348419,\n",
" 0.060360491275787354,\n",
" -0.020288566127419472,\n",
" -0.01685008965432644,\n",
" 0.005217957776039839,\n",
" -0.05178746581077576,\n",
" 0.11067887395620346,\n",
" 0.16606096923351288,\n",
" -0.01846872828900814,\n",
" 0.0404614619910717,\n",
" 0.10259922593832016,\n",
" 0.1210208460688591,\n",
" 0.006788693368434906,\n",
" 0.14095033705234528,\n",
" 0.014488299377262592,\n",
" 0.017006831243634224,\n",
" -0.0031870275270193815,\n",
" -0.03751441836357117,\n",
" -0.022925475612282753,\n",
" -0.07535967975854874,\n",
" 0.0787331834435463,\n",
" -0.09207120537757874,\n",
" -0.031238429248332977,\n",
" -0.06564447283744812,\n",
" -0.09600699692964554,\n",
" -0.029714435338974,\n",
" -0.019409839063882828,\n",
" 0.0018314741319045424,\n",
" 0.018882259726524353,\n",
" 0.03654668480157852,\n",
" -0.005077410489320755,\n",
" -0.017290854826569557,\n",
" -4.3281463149469346e-05,\n",
" 0.03840392455458641,\n",
" 0.059257131069898605,\n",
" -0.001048099366016686,\n",
" -0.049858007580041885,\n",
" -0.00924675539135933,\n",
" -0.0015360809629783034,\n",
" -0.07020754367113113,\n",
" -0.019497781991958618,\n",
" -0.04568319022655487,\n",
" -0.08878160268068314,\n",
" -0.006397779565304518,\n",
" 0.08201072365045547,\n",
" -0.014075429178774357,\n",
" 0.05403408780694008,\n",
" -0.03884543105959892],\n",
" 'id': 'context:c2',\n",
" 'text': 'Your mom goes to college.'}"
]
},
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sentence2 = \"Your mom goes to college.\"\n",
"embedding2 = model.encode(sentence2)\n",
"await db_admin.create(\n",
" \"context:c2\",\n",
" {\n",
" \"text\": sentence2,\n",
" \"embedding\": embedding2.tolist()\n",
" },\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'embedding': [-0.0687154084444046,\n",
" 0.06865836679935455,\n",
" 0.016348257660865784,\n",
" -0.006919116247445345,\n",
" 0.019324511289596558,\n",
" -0.032366104423999786,\n",
" 0.023804228752851486,\n",
" 0.013782317750155926,\n",
" -0.02809331566095352,\n",
" 0.06903014332056046,\n",
" -0.030473899096250534,\n",
" 0.10766035318374634,\n",
" 0.029629727825522423,\n",
" 0.08201240003108978,\n",
" 0.04546995088458061,\n",
" 0.02299082837998867,\n",
" -0.034942761063575745,\n",
" 0.03252037987112999,\n",
" 0.061493515968322754,\n",
" 0.030624674633145332,\n",
" 0.01036603283137083,\n",
" 0.009189838543534279,\n",
" 0.061126235872507095,\n",
" 0.09104369580745697,\n",
" -0.011716318316757679,\n",
" -0.009624820202589035,\n",
" 0.0052475337870419025,\n",
" 0.010423766449093819,\n",
" 0.017941439524292946,\n",
" 0.010105829685926437,\n",
" -0.06598049402236938,\n",
" 0.024297859519720078,\n",
" -0.03220135346055031,\n",
" -0.010555120185017586,\n",
" -0.06559883058071136,\n",
" 0.12711377441883087,\n",
" 0.07121366262435913,\n",
" 0.025622466579079628,\n",
" -0.07305952906608582,\n",
" 0.07325558364391327,\n",
" 0.008329766802489758,\n",
" 0.08294793963432312,\n",
" 0.011945115402340889,\n",
" -0.04023180156946182,\n",
" 0.018142065033316612,\n",
" -0.0013644769787788391,\n",
" 0.03700609877705574,\n",
" -0.008662380278110504,\n",
" 0.050913210958242416,\n",
" 0.036694567650556564,\n",
" -0.01287137158215046,\n",
" 0.004991813097149134,\n",
" -0.05095415562391281,\n",
" -0.04020144045352936,\n",
" -0.06379079073667526,\n",
" 0.02575657144188881,\n",
" -0.07298092544078827,\n",
" -0.006723583210259676,\n",
" 0.007411011960357428,\n",
" 0.015276185236871243,\n",
" 0.016967719420790672,\n",
" 0.036139726638793945,\n",
" -0.03669941797852516,\n",
" 0.03340531513094902,\n",
" 0.040493760257959366,\n",
" -0.010599635541439056,\n",
" -0.024842893704771996,\n",
" -0.05029125511646271,\n",
" -0.07426882535219193,\n",
" -0.002974230097606778,\n",
" -0.013439360074698925,\n",
" 0.10401709377765656,\n",
" -0.012612873688340187,\n",
" 0.006886684335768223,\n",
" -0.07669049501419067,\n",
" -0.08723709732294083,\n",
" -0.021543748676776886,\n",
" -0.020755071192979813,\n",
" -0.07970812171697617,\n",
" 0.006079513113945723,\n",
" 0.013086327351629734,\n",
" 0.01670193485915661,\n",
" 0.022273704409599304,\n",
" 0.02038656175136566,\n",
" 0.013857427053153515,\n",
" -0.06723394244909286,\n",
" 0.021511264145374298,\n",
" -0.13918545842170715,\n",
" 0.06837917119264603,\n",
" -0.034252893179655075,\n",
" 0.01737734116613865,\n",
" -0.023427464067935944,\n",
" -0.07426928728818893,\n",
" 0.0276910699903965,\n",
" -0.07040953636169434,\n",
" 0.05135321617126465,\n",
" -0.062135960906744,\n",
" 0.045214325189590454,\n",
" -0.026806136593222618,\n",
" 0.04344084858894348,\n",
" -0.009641071781516075,\n",
" -0.10506292432546616,\n",
" -0.008510452695190907,\n",
" -0.01573110744357109,\n",
" 0.06828135251998901,\n",
" -0.04518461972475052,\n",
" 0.04597410559654236,\n",
" 0.011651627719402313,\n",
" -0.039808329194784164,\n",
" 0.046193480491638184,\n",
" -0.0027189140673726797,\n",
" 0.05873965099453926,\n",
" 0.04995322600007057,\n",
" 0.006655265111476183,\n",
" 0.022230809554457664,\n",
" 0.10550045967102051,\n",
" -0.04041643440723419,\n",
" 0.019811656326055527,\n",
" -0.016141299158334732,\n",
" 0.02290160022675991,\n",
" 0.002494193846359849,\n",
" -0.0070219747722148895,\n",
" 0.12203499674797058,\n",
" 0.08851244300603867,\n",
" -0.00025921923224814236,\n",
" -0.07700075954198837,\n",
" 0.047823961824178696,\n",
" -4.221446332932538e-33,\n",
" -0.0017489726888015866,\n",
" 0.0036345613189041615,\n",
" 0.039814624935388565,\n",
" -0.01966087892651558,\n",
" 0.0594208724796772,\n",
" 0.02883605845272541,\n",
" -0.008111943490803242,\n",
" -0.008608282543718815,\n",
" -0.03686654195189476,\n",
" 0.01845758967101574,\n",
" -0.050614938139915466,\n",
" 0.01902567408978939,\n",
" -0.0066321836784482,\n",
" 0.048639606684446335,\n",
" 0.050318557769060135,\n",
" -0.052386574447155,\n",
" 0.013561041094362736,\n",
" -0.055728521198034286,\n",
" -0.09659873694181442,\n",
" -0.025734523311257362,\n",
" 0.049613505601882935,\n",
" -0.05413227528333664,\n",
" 0.026071865111589432,\n",
" 0.045756593346595764,\n",
" -0.03761262074112892,\n",
" -0.04492492601275444,\n",
" 0.0796046331524849,\n",
" 0.05741503834724426,\n",
" -0.05976182222366333,\n",
" 0.020855369046330452,\n",
" -0.0026846432592719793,\n",
" -0.011131240986287594,\n",
" 0.05305095389485359,\n",
" -0.03272866830229759,\n",
" 0.07691444456577301,\n",
" 0.05432671308517456,\n",
" -0.01842503808438778,\n",
" -6.163682701298967e-05,\n",
" 0.015808969736099243,\n",
" 0.03490148112177849,\n",
" -0.03791898116469383,\n",
" -0.009704560972750187,\n",
" 0.004582966212183237,\n",
" -0.005821888335049152,\n",
" 0.03637568652629852,\n",
" 0.09196297824382782,\n",
" 0.0805266797542572,\n",
" -0.017792247235774994,\n",
" -0.09427381306886673,\n",
" -0.0011563912266865373,\n",
" 0.0019170528976246715,\n",
" 0.09193679690361023,\n",
" 0.08806730061769485,\n",
" -0.03355782851576805,\n",
" -0.028198733925819397,\n",
" 0.03199760988354683,\n",
" 0.04997614398598671,\n",
" 0.029822982847690582,\n",
" -0.03777820244431496,\n",
" 0.029570024460554123,\n",
" -0.0900682583451271,\n",
" -0.0049492367543280125,\n",
" 0.04901033639907837,\n",
" -0.0589192658662796,\n",
" -0.05556419864296913,\n",
" -0.02916303463280201,\n",
" 0.07641232758760452,\n",
" 0.015658710151910782,\n",
" -0.012892777100205421,\n",
" -0.01735857129096985,\n",
" -0.013214034028351307,\n",
" 0.03029852733016014,\n",
" -0.07411753386259079,\n",
" 0.014791221357882023,\n",
" -0.03188185766339302,\n",
" -0.012290903367102146,\n",
" -0.024997495114803314,\n",
" -0.003936849534511566,\n",
" 0.10337655246257782,\n",
" -0.05883116275072098,\n",
" 0.001194200711324811,\n",
" -0.06510988622903824,\n",
" 0.019772659987211227,\n",
" 0.04804249107837677,\n",
" -0.044728994369506836,\n",
" 0.002938071498647332,\n",
" 0.07824435830116272,\n",
" -0.06579078733921051,\n",
" -0.03906340152025223,\n",
" 0.004603822715580463,\n",
" -0.05555274337530136,\n",
" 0.07424549013376236,\n",
" 0.08982294797897339,\n",
" -0.055338356643915176,\n",
" -0.00506273889914155,\n",
" 1.7709097901626235e-33,\n",
" 0.09398631006479263,\n",
" -0.043292053043842316,\n",
" 0.058222051709890366,\n",
" 0.05100950226187706,\n",
" -0.1096189096570015,\n",
" 0.026085244491696358,\n",
" 0.09195025265216827,\n",
" -0.051089853048324585,\n",
" -0.012795780785381794,\n",
" 0.01968630775809288,\n",
" -0.07537044584751129,\n",
" -0.03313378617167473,\n",
" 0.016063695773482323,\n",
" 0.02218962088227272,\n",
" 0.0845540314912796,\n",
" -0.04410001263022423,\n",
" 0.0035675920080393553,\n",
" 0.0665012076497078,\n",
" -0.02021562121808529,\n",
" -0.015001465566456318,\n",
" -0.01612655445933342,\n",
" 0.060769643634557724,\n",
" -0.07201559096574783,\n",
" -0.06775373965501785,\n",
" 0.02890446037054062,\n",
" -0.00773652084171772,\n",
" -0.004273148253560066,\n",
" 0.10973724722862244,\n",
" -0.057213399559259415,\n",
" -0.013623245060443878,\n",
" 0.025938019156455994,\n",
" 0.07515764981508255,\n",
" -0.06368587911128998,\n",
" -0.08221041411161423,\n",
" -0.0742662101984024,\n",
" 0.1185598224401474,\n",
" -0.021025603637099266,\n",
" -0.018929816782474518,\n",
" -0.021411115303635597,\n",
" -0.05126814916729927,\n",
" 0.0773753970861435,\n",
" -0.04101090133190155,\n",
" 0.05669870227575302,\n",
" -0.034633614122867584,\n",
" 0.03615179657936096,\n",
" 0.0031710315961390734,\n",
" 0.0511050708591938,\n",
" 0.0925334021449089,\n",
" -0.07945691794157028,\n",
" -0.021517671644687653,\n",
" -0.06793264299631119,\n",
" 0.008405094966292381,\n",
" -0.07717278599739075,\n",
" 0.0702221468091011,\n",
" -0.002085525542497635,\n",
" -0.03925623744726181,\n",
" -0.0748676210641861,\n",
" -0.018004106357693672,\n",
" -0.11605590581893921,\n",
" -0.11458313465118408,\n",
" -0.036703117191791534,\n",
" -0.013844430446624756,\n",
" -0.030344132333993912,\n",
" -0.0346972681581974,\n",
" 0.014279312454164028,\n",
" -0.012766172178089619,\n",
" 0.027324968948960304,\n",
" -0.0673266127705574,\n",
" -0.019832748919725418,\n",
" 0.047441355884075165,\n",
" -0.049783021211624146,\n",
" -0.09501167386770248,\n",
" -0.02256142720580101,\n",
" -0.06115366891026497,\n",
" -0.03141552209854126,\n",
" 0.017898758873343468,\n",
" 0.08671838790178299,\n",
" 0.014894154854118824,\n",
" -0.011347729712724686,\n",
" -0.12334159016609192,\n",
" 0.042146872729063034,\n",
" -0.0910186693072319,\n",
" -0.06011584773659706,\n",
" 0.015858899801969528,\n",
" -0.051511168479919434,\n",
" 0.02914523519575596,\n",
" -0.02419407293200493,\n",
" -0.11408843100070953,\n",
" -0.015715107321739197,\n",
" 0.02547471970319748,\n",
" -0.051369115710258484,\n",
" -0.05151783674955368,\n",
" -0.03669390454888344,\n",
" -0.05757702514529228,\n",
" -0.009298241697251797,\n",
" -2.0801754985200205e-08,\n",
" 0.021466653794050217,\n",
" 0.05316482111811638,\n",
" -0.034725483506917953,\n",
" 0.011854717507958412,\n",
" 0.024059481918811798,\n",
" 0.028114397078752518,\n",
" 0.012774291448295116,\n",
" 0.013847132213413715,\n",
" -0.043149787932634354,\n",
" 0.014681462198495865,\n",
" 0.006435879040509462,\n",
" 0.022332804277539253,\n",
" -0.00871281884610653,\n",
" 0.10122161358594894,\n",
" -0.0396529957652092,\n",
" 0.04677952080965042,\n",
" 0.02244127169251442,\n",
" -0.008004304952919483,\n",
" -0.05492502450942993,\n",
" 0.09713191539049149,\n",
" -0.05569043010473251,\n",
" 0.02844090946018696,\n",
" 0.002262962982058525,\n",
" 0.06054037809371948,\n",
" -0.02726992964744568,\n",
" -0.04484821483492851,\n",
" 0.013411364518105984,\n",
" -0.01435998734086752,\n",
" 0.09531723707914352,\n",
" -0.0355030782520771,\n",
" -0.022457584738731384,\n",
" -0.021437231451272964,\n",
" -0.07001194357872009,\n",
" 0.09753423929214478,\n",
" 0.09170278161764145,\n",
" 0.017300540581345558,\n",
" -0.05704525485634804,\n",
" 0.04061945900321007,\n",
" 0.05330178886651993,\n",
" -0.017108526080846786,\n",
" 0.010634573176503181,\n",
" 0.018714386969804764,\n",
" 0.10105448216199875,\n",
" 0.066991426050663,\n",
" -0.07467610388994217,\n",
" -0.031104685738682747,\n",
" -0.024001287296414375,\n",
" 0.043324947357177734,\n",
" -0.04428170248866081,\n",
" 0.03114204667508602,\n",
" -0.0384378544986248,\n",
" -0.00497459014877677,\n",
" -0.04696325585246086,\n",
" 0.06634757667779922,\n",
" 0.04762718826532364,\n",
" 0.007888355292379856,\n",
" -0.040471870452165604,\n",
" -0.05458998680114746,\n",
" -0.11732412129640579,\n",
" 0.09005974978208542,\n",
" 0.05564115196466446,\n",
" -0.09758589416742325,\n",
" -0.03621628135442734,\n",
" -0.05441541224718094],\n",
" 'id': 'context:c3',\n",
" 'text': 'I think that hiking up the mountain is a more righteous way to ski.'}"
]
},
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sentence3 = \"I think that hiking up the mountain is a more righteous way to ski.\"\n",
"embedding3 = model.encode(sentence3)\n",
"await db_admin.create(\n",
" \"context:c3\",\n",
" {\n",
" \"text\": sentence3,\n",
" \"embedding\": embedding3.tolist()\n",
" },\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'embedding': [-0.05596602335572243,\n",
" 0.07386287301778793,\n",
" 0.04279350861907005,\n",
" -0.022733265534043312,\n",
" -0.0954650342464447,\n",
" -0.03712044656276703,\n",
" 0.04941699653863907,\n",
" -0.01623666100203991,\n",
" -0.040018025785684586,\n",
" 0.09906632453203201,\n",
" -0.03808848559856415,\n",
" -0.00436401879414916,\n",
" -0.012545345351099968,\n",
" 0.05921676382422447,\n",
" 0.03773675858974457,\n",
" 0.014650903642177582,\n",
" -0.035299643874168396,\n",
" 0.019307894632220268,\n",
" 0.046252936124801636,\n",
" 0.05302712321281433,\n",
" 0.0002854951308108866,\n",
" 0.007969099096953869,\n",
" -0.02284696139395237,\n",
" 0.0846327468752861,\n",
" -0.016025599092245102,\n",
" 0.009580861777067184,\n",
" -0.07464149594306946,\n",
" 0.0858684703707695,\n",
" 0.02750663086771965,\n",
" 0.062189504504203796,\n",
" -0.08975996822118759,\n",
" -0.018551306799054146,\n",
" 0.01471706572920084,\n",
" 0.005584691185504198,\n",
" -0.09671036899089813,\n",
" 0.10923073440790176,\n",
" 0.02108731120824814,\n",
" 0.034489743411540985,\n",
" -0.0662224218249321,\n",
" 0.06845798343420029,\n",
" 0.008028391748666763,\n",
" 0.05983380228281021,\n",
" -0.0012795450165867805,\n",
" -0.022092944011092186,\n",
" 0.011216017417609692,\n",
" -0.0030872768256813288,\n",
" 0.0050307586789131165,\n",
" -0.01650886796414852,\n",
" 0.019840041175484657,\n",
" 0.002704001497477293,\n",
" 0.034383125603199005,\n",
" -0.026160147041082382,\n",
" 0.07367533445358276,\n",
" -0.02139311470091343,\n",
" -0.0028375054243952036,\n",
" 0.018009081482887268,\n",
" -0.06554300338029861,\n",
" -0.014562358148396015,\n",
" 0.027876142412424088,\n",
" 0.06351643055677414,\n",
" 0.00685011176392436,\n",
" 0.029622046276926994,\n",
" -0.06966520845890045,\n",
" 0.07041870802640915,\n",
" 0.03481258451938629,\n",
" 0.0440143458545208,\n",
" -0.05823776125907898,\n",
" 0.011153236031532288,\n",
" -0.05452529713511467,\n",
" 0.024562623351812363,\n",
" 0.04811146482825279,\n",
" 0.041539374738931656,\n",
" -0.013963952660560608,\n",
" 0.010998534969985485,\n",
" -0.05100741982460022,\n",
" -0.055648889392614365,\n",
" -0.04593457654118538,\n",
" -0.04746195301413536,\n",
" -0.060535456985235214,\n",
" 0.06669265031814575,\n",
" 0.003249081317335367,\n",
" -0.05817539617419243,\n",
" 0.04765733703970909,\n",
" -0.007356769405305386,\n",
" 0.04129995033144951,\n",
" -0.06550972908735275,\n",
" 0.04206773638725281,\n",
" -0.05529213324189186,\n",
" -0.015038887038826942,\n",
" -0.02453753910958767,\n",
" -0.04540659114718437,\n",
" -0.025299208238720894,\n",
" 0.04934007674455643,\n",
" 0.012133806943893433,\n",
" -0.06988274306058884,\n",
" 0.013761557638645172,\n",
" -0.037417225539684296,\n",
" 0.03639872372150421,\n",
" -0.05982870236039162,\n",
" 0.07317864149808884,\n",
" -0.05628044158220291,\n",
" -0.05077245086431503,\n",
" 0.05163436383008957,\n",
" 0.030483078211545944,\n",
" 0.06259267777204514,\n",
" -0.011347843334078789,\n",
" -0.00826731976121664,\n",
" 0.049256086349487305,\n",
" 0.018508361652493477,\n",
" -0.0012272166786715388,\n",
" -0.014975455589592457,\n",
" 0.02660791389644146,\n",
" 0.0324036069214344,\n",
" 0.008808379992842674,\n",
" 0.02918885461986065,\n",
" 0.03646169602870941,\n",
" -0.04572129622101784,\n",
" -0.01517124380916357,\n",
" -0.05455697700381279,\n",
" 0.020559124648571014,\n",
" -0.03861043602228165,\n",
" 0.04850884526968002,\n",
" 0.07136193662881851,\n",
" 0.08793986588716507,\n",
" 0.047195788472890854,\n",
" 0.002534842351451516,\n",
" 0.03456266596913338,\n",
" -4.983626951772124e-33,\n",
" 0.01760045625269413,\n",
" -0.036060452461242676,\n",
" 0.07283486425876617,\n",
" -0.007709852885454893,\n",
" 0.0010028509423136711,\n",
" 0.043273430317640305,\n",
" -7.455211289197905e-06,\n",
" 0.02321784570813179,\n",
" -0.023939481005072594,\n",
" -0.006107775494456291,\n",
" -0.020698903128504753,\n",
" 0.061814215034246445,\n",
" -0.032069478183984756,\n",
" 0.08908828347921371,\n",
" 0.023053277283906937,\n",
" 0.001667237957008183,\n",
" 0.036718156188726425,\n",
" -0.09156499803066254,\n",
" -0.12763139605522156,\n",
" -0.041080716997385025,\n",
" 0.03231428191065788,\n",
" -0.03973006457090378,\n",
" -0.00029700371669605374,\n",
" 0.06435339897871017,\n",
" -0.09020072966814041,\n",
" -0.028147637844085693,\n",
" 0.02112971432507038,\n",
" 0.04535461217164993,\n",
" 0.053997233510017395,\n",
" 0.037572626024484634,\n",
" -0.06720821559429169,\n",
" -0.01727215014398098,\n",
" 0.008911318145692348,\n",
" -0.03625958785414696,\n",
" 0.08161866664886475,\n",
" 0.042251553386449814,\n",
" -0.036881282925605774,\n",
" 0.052334029227495193,\n",
" -0.010846170596778393,\n",
" 0.04529683664441109,\n",
" 0.037459131330251694,\n",
" -0.09162483364343643,\n",
" -0.042122744023799896,\n",
" -0.02979297935962677,\n",
" 0.004414124879986048,\n",
" 0.03757917135953903,\n",
" 0.014547600410878658,\n",
" -0.02015737071633339,\n",
" -0.06480392068624496,\n",
" 0.02163083106279373,\n",
" -0.036539990454912186,\n",
" 0.11904211342334747,\n",
" 0.0018275503534823656,\n",
" 0.016780607402324677,\n",
" -0.0030797936487942934,\n",
" 0.06597290933132172,\n",
" 0.06893665343523026,\n",
" 0.08799763023853302,\n",
" -0.046349480748176575,\n",
" 0.045711517333984375,\n",
" -0.08293940126895905,\n",
" 0.037615180015563965,\n",
" 0.0010952947195619345,\n",
" -0.06825560331344604,\n",
" -0.07775553315877914,\n",
" -0.03399648889899254,\n",
" 0.08625143766403198,\n",
" 0.04758088290691376,\n",
" 0.006755071692168713,\n",
" 0.022202018648386,\n",
" 0.061967551708221436,\n",
" 0.003392353653907776,\n",
" -0.10023732483386993,\n",
" 0.04938613623380661,\n",
" -0.0015141956973820925,\n",
" -0.0060985819436609745,\n",
" -0.0005139053682796657,\n",
" 0.07282580435276031,\n",
" 0.04758187383413315,\n",
" 0.00622689351439476,\n",
" 0.03172587975859642,\n",
" 0.0172547809779644,\n",
" 0.014635485596954823,\n",
" -0.0028967855032533407,\n",
" -0.07944132387638092,\n",
" -0.01082572154700756,\n",
" 0.09343291074037552,\n",
" -0.14809204638004303,\n",
" -0.0027032180223613977,\n",
" 0.04132380709052086,\n",
" -0.05491742864251137,\n",
" 0.05335708335042,\n",
" 0.050447843968868256,\n",
" -0.03232572227716446,\n",
" -0.09848801791667938,\n",
" 1.0451656391076896e-33,\n",
" 0.040153540670871735,\n",
" -0.0178395826369524,\n",
" 0.014650126919150352,\n",
" -0.0004938142374157906,\n",
" -0.03677701577544212,\n",
" 0.04937296733260155,\n",
" 0.0656660720705986,\n",
" -0.01703779771924019,\n",
" -0.09945081919431686,\n",
" 0.041491251438856125,\n",
" 0.03242296725511551,\n",
" 0.024213561788201332,\n",
" -0.015181886032223701,\n",
" 0.004968144465237856,\n",
" 0.04465611279010773,\n",
" -0.07631227374076843,\n",
" 0.037623461335897446,\n",
" 0.1146031990647316,\n",
" -0.04284283146262169,\n",
" -0.068748340010643,\n",
" 0.03368746489286423,\n",
" -0.008452599868178368,\n",
" -0.01813395321369171,\n",
" -0.04555131867527962,\n",
" 0.021048055961728096,\n",
" -0.01885043829679489,\n",
" 0.00460944464430213,\n",
" 0.08933654427528381,\n",
" -0.06380985677242279,\n",
" 0.007130839396268129,\n",
" 0.001717276405543089,\n",
" 0.12334073334932327,\n",
" -0.03987261652946472,\n",
" -0.040734801441431046,\n",
" -0.03246929869055748,\n",
" 0.053075216710567474,\n",
" -0.06948579102754593,\n",
" 0.027057889848947525,\n",
" -0.026151517406105995,\n",
" -0.04327038675546646,\n",
" 0.030162159353494644,\n",
" -0.06212417781352997,\n",
" 0.021435609087347984,\n",
" 0.045270100235939026,\n",
" 0.0880265161395073,\n",
" -0.05477377399802208,\n",
" 0.016098354011774063,\n",
" 0.03395453095436096,\n",
" -0.04072218015789986,\n",
" -0.05807439610362053,\n",
" -0.1576034277677536,\n",
" 0.02232772670686245,\n",
" -0.03819416090846062,\n",
" 0.056779321283102036,\n",
" 0.016940748319029808,\n",
" -0.10419990122318268,\n",
" -0.056083451956510544,\n",
" -0.05134693905711174,\n",
" -0.05519017577171326,\n",
" -0.13779602944850922,\n",
" -0.03168844059109688,\n",
" -0.06358475238084793,\n",
" -0.03810132294893265,\n",
" -0.0021662930957973003,\n",
" 0.06345359981060028,\n",
" -0.059095364063978195,\n",
" 0.004623597487807274,\n",
" -0.11771143227815628,\n",
" 7.22745171515271e-05,\n",
" 0.029689785093069077,\n",
" 0.0071358466520905495,\n",
" -0.0807156190276146,\n",
" -0.023902561515569687,\n",
" -0.1147761270403862,\n",
" -0.06388573348522186,\n",
" 0.03997672721743584,\n",
" 0.007503950502723455,\n",
" 0.006648250389844179,\n",
" -0.021921982988715172,\n",
" -0.1465362310409546,\n",
" 0.005766145419329405,\n",
" -0.03320744261145592,\n",
" 0.0229426808655262,\n",
" 0.04882163926959038,\n",
" -0.02622133679687977,\n",
" -0.02062637358903885,\n",
" 0.029238732531666756,\n",
" -0.07999470084905624,\n",
" -0.05383756384253502,\n",
" 0.0680331289768219,\n",
" -0.01836763136088848,\n",
" -0.03196704760193825,\n",
" -0.034268733114004135,\n",
" -0.0341019444167614,\n",
" -0.009360390715301037,\n",
" -2.039375068818572e-08,\n",
" 0.08386091887950897,\n",
" 0.06279178708791733,\n",
" -0.06448257714509964,\n",
" 0.01530382502824068,\n",
" -0.03581731393933296,\n",
" 0.014681709930300713,\n",
" -0.03241019695997238,\n",
" -0.07422717660665512,\n",
" -0.05700311437249184,\n",
" 0.08751627057790756,\n",
" -0.009269648231565952,\n",
" 0.052496425807476044,\n",
" 0.02192162163555622,\n",
" 0.08133849501609802,\n",
" -0.011604633182287216,\n",
" 0.047483719885349274,\n",
" 0.024862701073288918,\n",
" -0.012936549261212349,\n",
" -0.07383516430854797,\n",
" 0.007608125451952219,\n",
" -0.08577603846788406,\n",
" 0.020273739472031593,\n",
" 0.04805968329310417,\n",
" 0.05604197084903717,\n",
" -0.04409477114677429,\n",
" -0.06170239672064781,\n",
" 0.004592546261847019,\n",
" -0.0723891481757164,\n",
" 0.07684584707021713,\n",
" 0.033075787127017975,\n",
" -0.04789271578192711,\n",
" 0.004941684193909168,\n",
" -0.06713086366653442,\n",
" 0.009254946373403072,\n",
" 0.10297674685716629,\n",
" -0.0039052071515470743,\n",
" -0.06968984752893448,\n",
" 0.054262056946754456,\n",
" -0.0023400478530675173,\n",
" -0.015439704991877079,\n",
" -0.033221516758203506,\n",
" 0.00519308028742671,\n",
" 0.05281971022486687,\n",
" 0.06752758473157883,\n",
" 0.020607979968190193,\n",
" 0.0011869879672303796,\n",
" 0.03952224925160408,\n",
" 0.07312732189893723,\n",
" 0.0026176301762461662,\n",
" 0.005558440461754799,\n",
" 0.001969970064237714,\n",
" 0.05012481287121773,\n",
" -0.012542401440441608,\n",
" 0.07477005571126938,\n",
" -0.09144230931997299,\n",
" 0.006161908153444529,\n",
" 0.0015483169117942452,\n",
" -0.030604850500822067,\n",
" -0.09309915453195572,\n",
" -0.027593277394771576,\n",
" 0.0344424732029438,\n",
" -0.12738367915153503,\n",
" 0.009777014143764973,\n",
" -0.06558489054441452],\n",
" 'id': 'context:c4',\n",
" 'text': 'No! Mono-skiers are the most righteous dudes on the mountain.'}"
]
},
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sentence4 = \"No! Mono-skiers are the most righteous dudes on the mountain.\"\n",
"embedding4 = model.encode(sentence4)\n",
"await db_admin.create(\n",
" \"context:c4\",\n",
" {\n",
" \"text\": sentence4,\n",
" \"embedding\": embedding4.tolist()\n",
" },\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 63,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'embedding': [-0.02463686466217041,\n",
" 0.028182253241539,\n",
" 0.09579474478960037,\n",
" -0.005464345682412386,\n",
" -0.062102168798446655,\n",
" -0.017640186473727226,\n",
" 0.022812727838754654,\n",
" 0.0338481105864048,\n",
" -0.030835727229714394,\n",
" 0.09391246736049652,\n",
" -0.009724598377943039,\n",
" 0.051131173968315125,\n",
" -0.03629294037818909,\n",
" 0.10473974794149399,\n",
" 0.024361897259950638,\n",
" 0.009485050104558468,\n",
" -0.016403090208768845,\n",
" 0.003000987460836768,\n",
" 0.021158289164304733,\n",
" -0.040163297206163406,\n",
" 0.03374470770359039,\n",
" -0.010752893052995205,\n",
" 0.028699664399027824,\n",
" 0.0381961353123188,\n",
" -0.02114003710448742,\n",
" 0.14680589735507965,\n",
" -0.060435306280851364,\n",
" -0.06739287078380585,\n",
" -0.01624830812215805,\n",
" 0.03901434317231178,\n",
" -0.029515907168388367,\n",
" -0.018110640347003937,\n",
" 0.04003876820206642,\n",
" 0.005946445744484663,\n",
" -0.010949806310236454,\n",
" 0.07680096477270126,\n",
" 0.11008919775485992,\n",
" 0.03775189444422722,\n",
" -0.07810690999031067,\n",
" 0.10983401536941528,\n",
" -0.031364936381578445,\n",
" 0.004235493019223213,\n",
" -0.028324225917458534,\n",
" -0.06226268410682678,\n",
" 0.0025470012333244085,\n",
" 0.006522172596305609,\n",
" 0.03679704666137695,\n",
" -0.07491209357976913,\n",
" 0.10541817545890808,\n",
" 0.047868549823760986,\n",
" 0.0015854377998039126,\n",
" -0.02843415178358555,\n",
" 0.028070278465747833,\n",
" -0.05969182029366493,\n",
" -0.05672474950551987,\n",
" 0.032753895968198776,\n",
" -0.051674362272024155,\n",
" -0.019214607775211334,\n",
" 0.030740952119231224,\n",
" 0.007900498807430267,\n",
" -0.06442974507808685,\n",
" 0.017291303724050522,\n",
" -0.080989308655262,\n",
" 0.08107136934995651,\n",
" 0.011638429015874863,\n",
" 0.006425704341381788,\n",
" -0.04821021109819412,\n",
" 0.049895916134119034,\n",
" -0.05931665375828743,\n",
" 0.029694292694330215,\n",
" -0.0186614990234375,\n",
" 0.07036808878183365,\n",
" 0.011472444050014019,\n",
" 0.025589076802134514,\n",
" 0.015509913675487041,\n",
" -0.03836609423160553,\n",
" 0.016548773273825645,\n",
" -0.025903593748807907,\n",
" 0.011764461174607277,\n",
" -0.0048561543226242065,\n",
" -0.0004227850295137614,\n",
" 0.016827646642923355,\n",
" 0.03314788639545441,\n",
" -0.05329816788434982,\n",
" 0.06069072335958481,\n",
" -0.029771870002150536,\n",
" 0.05067126825451851,\n",
" -0.08220192044973373,\n",
" -0.011064286343753338,\n",
" -0.03501303493976593,\n",
" -0.06878172606229782,\n",
" -0.046776145696640015,\n",
" 0.008028091862797737,\n",
" 0.07775848358869553,\n",
" 0.02393444813787937,\n",
" 0.07013604789972305,\n",
" -0.09791047871112823,\n",
" 0.020754428580403328,\n",
" -0.03245154395699501,\n",
" 0.024956319481134415,\n",
" -0.05549957603216171,\n",
" -0.09110487997531891,\n",
" 0.10063429176807404,\n",
" 0.07915689051151276,\n",
" -0.02137383632361889,\n",
" -0.03289018198847771,\n",
" 0.05444217100739479,\n",
" 0.02681092731654644,\n",
" 0.04072754830121994,\n",
" 0.019447343423962593,\n",
" 0.04372057318687439,\n",
" -0.018164727836847305,\n",
" 0.06956306099891663,\n",
" 0.013495558872818947,\n",
" 0.008078654296696186,\n",
" 0.03319481760263443,\n",
" -0.0005934310611337423,\n",
" -0.03594335541129112,\n",
" -0.006755650509148836,\n",
" 0.03436018526554108,\n",
" -0.04686344414949417,\n",
" 0.0245258379727602,\n",
" 0.06846120208501816,\n",
" 0.030253268778324127,\n",
" -0.02251383103430271,\n",
" -0.04812419041991234,\n",
" -0.01664457470178604,\n",
" -3.8171324795733196e-33,\n",
" -0.007588277570903301,\n",
" 0.05138221010565758,\n",
" 0.031329307705163956,\n",
" 0.05399821326136589,\n",
" -0.01256084255874157,\n",
" 0.013952898792922497,\n",
" 0.04812038317322731,\n",
" -0.028485096991062164,\n",
" -0.025014270097017288,\n",
" 0.015016200952231884,\n",
" 0.01883334293961525,\n",
" 0.008932240307331085,\n",
" -0.013765784911811352,\n",
" 0.03024093247950077,\n",
" 0.1052679643034935,\n",
" 0.05417025089263916,\n",
" -0.006770483683794737,\n",
" -0.03121834620833397,\n",
" -0.10367680341005325,\n",
" 0.023003095760941505,\n",
" 0.026218529790639877,\n",
" -0.06223009526729584,\n",
" 0.0406026691198349,\n",
" 0.04284783825278282,\n",
" -0.0462525449693203,\n",
" 0.024694407358765602,\n",
" 0.01582987979054451,\n",
" -0.014871830120682716,\n",
" 0.07859152555465698,\n",
" 0.026292692869901657,\n",
" 0.015974929556250572,\n",
" -0.012229055166244507,\n",
" -0.059126850217580795,\n",
" 0.0042134663090109825,\n",
" 0.02385287545621395,\n",
" -0.0032368500251322985,\n",
" 0.08293293416500092,\n",
" 0.015648934990167618,\n",
" 0.024025291204452515,\n",
" -0.008926655165851116,\n",
" 0.07342018932104111,\n",
" -0.09751681238412857,\n",
" -0.023735469207167625,\n",
" -0.013644303195178509,\n",
" 0.05174091085791588,\n",
" 0.04872104153037071,\n",
" 0.11484694480895996,\n",
" 0.039394233375787735,\n",
" -0.042704492807388306,\n",
" 0.028562532737851143,\n",
" -0.0018117389408871531,\n",
" 0.08054039627313614,\n",
" -0.09172122925519943,\n",
" -0.07923828810453415,\n",
" -0.010661100037395954,\n",
" 0.11108172684907913,\n",
" 0.040652573108673096,\n",
" 0.04219972342252731,\n",
" -0.053292110562324524,\n",
" -0.0048186262138187885,\n",
" -0.06871524453163147,\n",
" 0.035823579877614975,\n",
" 0.03288339450955391,\n",
" -0.06248774752020836,\n",
" -0.02726219967007637,\n",
" 0.049945659935474396,\n",
" 0.11562519520521164,\n",
" 0.03271481767296791,\n",
" 0.02350849099457264,\n",
" -0.08647296577692032,\n",
" 0.0022157251369208097,\n",
" -0.008755323477089405,\n",
" -0.1607915610074997,\n",
" 0.06387921422719955,\n",
" 0.007375488523393869,\n",
" -0.010235289111733437,\n",
" 0.03893115371465683,\n",
" -0.05927089974284172,\n",
" 0.07099533081054688,\n",
" 0.023359378799796104,\n",
" 0.031120967119932175,\n",
" -0.0722934901714325,\n",
" 0.030430322512984276,\n",
" -0.024966824799776077,\n",
" -0.0289013609290123,\n",
" -0.007573828101158142,\n",
" 0.04025628790259361,\n",
" -0.08499288558959961,\n",
" 0.01893474906682968,\n",
" 0.017340661957859993,\n",
" -0.06029621139168739,\n",
" 0.013523648492991924,\n",
" -0.005158400163054466,\n",
" 0.015357645228505135,\n",
" -0.04052181914448738,\n",
" 9.001452683887287e-34,\n",
" 0.01607348769903183,\n",
" 0.01692156493663788,\n",
" -0.05483914166688919,\n",
" 0.024271244183182716,\n",
" -0.035067588090896606,\n",
" 0.025500398129224777,\n",
" 0.08113832771778107,\n",
" -0.06764600425958633,\n",
" 0.001849200576543808,\n",
" 0.024617210030555725,\n",
" -0.04276391863822937,\n",
" -0.05547014996409416,\n",
" -0.04222002997994423,\n",
" -0.022777436301112175,\n",
" -0.014361255802214146,\n",
" -0.0890764370560646,\n",
" 0.07312729954719543,\n",
" 0.10017488896846771,\n",
" 0.008886661380529404,\n",
" -0.07484032958745956,\n",
" 0.040684692561626434,\n",
" 0.07551933825016022,\n",
" -0.09107456356287003,\n",
" 0.03468463197350502,\n",
" 0.014434938319027424,\n",
" -0.030011964961886406,\n",
" -0.04159461334347725,\n",
" 0.034653209149837494,\n",
" -0.09830747544765472,\n",
" 0.13843666017055511,\n",
" 0.05301624909043312,\n",
" 0.031008392572402954,\n",
" 0.03273933380842209,\n",
" 0.013937512412667274,\n",
" -0.09569915384054184,\n",
" 0.050039030611515045,\n",
" -0.07368730753660202,\n",
" 0.0186835378408432,\n",
" -0.08440408110618591,\n",
" -0.04156169667840004,\n",
" 0.056915033608675,\n",
" -0.10966885834932327,\n",
" 0.030952507629990578,\n",
" -0.033134691417217255,\n",
" 0.004993196111172438,\n",
" -0.005146638955920935,\n",
" 0.010176321491599083,\n",
" 0.07580532133579254,\n",
" -0.04477223381400108,\n",
" 0.00624849135056138,\n",
" -0.04601955786347389,\n",
" 0.055592864751815796,\n",
" -0.0769272893667221,\n",
" -0.061889052391052246,\n",
" -0.02666882798075676,\n",
" -0.050630833953619,\n",
" 0.0032033156603574753,\n",
" -0.07608959078788757,\n",
" -0.04232141003012657,\n",
" -0.03781629726290703,\n",
" -0.0038585502188652754,\n",
" -0.09351792186498642,\n",
" -0.039199281483888626,\n",
" -0.008496134541928768,\n",
" 0.052110668271780014,\n",
" -0.05871070176362991,\n",
" -0.0651521161198616,\n",
" -0.11377449333667755,\n",
" 0.006645407062023878,\n",
" -0.0013172270264476538,\n",
" 0.06675471365451813,\n",
" 0.027988091111183167,\n",
" 0.010153677314519882,\n",
" -0.12420283257961273,\n",
" -0.08798667043447495,\n",
" 0.05244012922048569,\n",
" 0.046409059315919876,\n",
" 0.09296644479036331,\n",
" -0.037645384669303894,\n",
" -0.03905659541487694,\n",
" 0.029280927032232285,\n",
" -0.06327592581510544,\n",
" 0.020357850939035416,\n",
" 0.03943151608109474,\n",
" -0.0803457573056221,\n",
" -0.010407651774585247,\n",
" -0.01610724627971649,\n",
" -0.0895284041762352,\n",
" -0.009552208706736565,\n",
" 0.023866603150963783,\n",
" -0.02819792367517948,\n",
" 0.024008845910429955,\n",
" -0.03829221427440643,\n",
" -0.00833863765001297,\n",
" -0.042979706078767776,\n",
" -1.8143659019642655e-08,\n",
" 0.06180747225880623,\n",
" 0.04296427592635155,\n",
" 0.01555679552257061,\n",
" 0.033939361572265625,\n",
" 0.008512898348271847,\n",
" 0.04915850609540939,\n",
" -0.05364289507269859,\n",
" -0.043087396770715714,\n",
" -0.028317555785179138,\n",
" 0.03207158297300339,\n",
" 0.016246818006038666,\n",
" 0.008984635584056377,\n",
" 0.029764894396066666,\n",
" 0.07132704555988312,\n",
" 0.03125410154461861,\n",
" 0.018413914367556572,\n",
" 0.07425844669342041,\n",
" 0.07236436009407043,\n",
" -0.054537829011678696,\n",
" 0.057459406554698944,\n",
" -0.047203656286001205,\n",
" -0.039395734667778015,\n",
" 0.020687637850642204,\n",
" 0.07882697880268097,\n",
" -0.047376155853271484,\n",
" -0.14864464104175568,\n",
" 0.007079970091581345,\n",
" -0.008090229704976082,\n",
" -0.006797377485781908,\n",
" -0.01219568494707346,\n",
" -0.052005212754011154,\n",
" -0.019060686230659485,\n",
" 0.00016008131206035614,\n",
" 0.02297992818057537,\n",
" 0.028522426262497902,\n",
" -0.03406395763158798,\n",
" -0.03252208232879639,\n",
" 0.018427440896630287,\n",
" -0.023922856897115707,\n",
" -0.019607866182923317,\n",
" -0.07580647617578506,\n",
" 0.08107504993677139,\n",
" 0.1287258267402649,\n",
" 0.04438914731144905,\n",
" -0.04211144149303436,\n",
" 0.03776843100786209,\n",
" -0.060205839574337006,\n",
" 0.00020622750162146986,\n",
" 0.0044311219826340675,\n",
" 0.05093929171562195,\n",
" 0.00016959468484856188,\n",
" -0.04055192321538925,\n",
" -0.03393483906984329,\n",
" 0.052216097712516785,\n",
" 0.00642355065792799,\n",
" 0.03223293647170067,\n",
" -0.033218830823898315,\n",
" -0.05185206979513168,\n",
" -0.13407185673713684,\n",
" 0.02521008998155594,\n",
" 0.02214452438056469,\n",
" 0.022121036425232887,\n",
" -0.028471412137150764,\n",
" -0.0014543125871568918],\n",
" 'id': 'context:c5',\n",
" 'text': 'I think kids need to learn to ski and mono-ski in school.'}"
]
},
"execution_count": 63,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sentence5 = \"I think kids need to learn to ski and mono-ski in school.\"\n",
"embedding5 = model.encode(sentence5)\n",
"await db_admin.create(\n",
" \"context:c5\",\n",
" {\n",
" \"text\": sentence5,\n",
" \"embedding\": embedding5.tolist()\n",
" },\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'result': [{'vector::similarity::cosine': 0.20213616760673866},\n",
" {'vector::similarity::cosine': 0.20213616760673866}],\n",
" 'status': 'OK',\n",
" 'time': '1.241229ms'}]"
]
},
"execution_count": 64,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Basic similarity query - should have some simlarity between c1 and c2\n",
"await db_admin.query('''\n",
" SELECT vector::similarity::cosine(context:c1.embedding, context:c2.embedding) FROM context:c1, context:c2\n",
"''')"
]
},
{
"cell_type": "code",
"execution_count": 65,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'result': [{'vector::similarity::cosine': 0.06727307877904953},\n",
" {'vector::similarity::cosine': 0.06727307877904953}],\n",
" 'status': 'OK',\n",
" 'time': '1.510738ms'}]"
]
},
"execution_count": 65,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Basic similarity query - should not be much simlarity between c1 and c3\n",
"await db_admin.query('''\n",
" SELECT vector::similarity::cosine(context:c1.embedding, context:c3.embedding) FROM context:c1, context:c3\n",
"''')"
]
},
{
"cell_type": "code",
"execution_count": 66,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'result': None, 'status': 'OK', 'time': '114.622µs'},\n",
" {'result': [{'similarity': 0.4980708290612037,\n",
" 'text': 'I think kids need to learn to ski and mono-ski in school.'},\n",
" {'similarity': 0.49715219568731955,\n",
" 'text': 'I think that hiking up the mountain is a more righteous way to ski.'},\n",
" {'similarity': 0.4077806051040523,\n",
" 'text': 'No! Mono-skiers are the most righteous dudes on the mountain.'},\n",
" {'similarity': 0.12433440305622853, 'text': 'Your mom goes to college.'},\n",
" {'similarity': -0.0021449924597406094,\n",
" 'text': 'The current educational system is there only to condition you into being a good little consumer.'}],\n",
" 'status': 'OK',\n",
" 'time': '1.273949ms'}]"
]
},
"execution_count": 66,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"search = \"Do you snowboard or ski?\"\n",
"search_embedding = model.encode(search)\n",
"\n",
"# LET is really cool!\n",
"await db_admin.query('''\n",
" LET $search_vec = {0};\n",
" SELECT text, vector::similarity::cosine(embedding, $search_vec) as similarity FROM context ORDER BY similarity DESC;\n",
"'''.format(search_embedding.tolist()))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Cleanup"
]
},
{
"cell_type": "code",
"execution_count": 67,
"metadata": {},
"outputs": [],
"source": [
"await db_admin.close()\n",
"await db_alice.close()\n",
"await db_bob.close()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "wenv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment