Skip to content

Commit

Permalink
even better print formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
choccccy committed Oct 4, 2023
1 parent 4c7b445 commit bb14318
Showing 1 changed file with 52 additions and 60 deletions.
112 changes: 52 additions & 60 deletions demo/PGvector_demo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
},
{
"cell_type": "code",
"execution_count": 78,
"execution_count": 118,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -38,7 +38,7 @@
},
{
"cell_type": "code",
"execution_count": 79,
"execution_count": 119,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -69,7 +69,7 @@
},
{
"cell_type": "code",
"execution_count": 80,
"execution_count": 120,
"metadata": {},
"outputs": [
{
Expand All @@ -82,19 +82,16 @@
}
],
"source": [
"try:\n",
" print(\"Connecting to database...\")\n",
" vDB = await PGvectorConnection.create(\n",
" embedding_model=e_model, \n",
" db_name=DB_NAME,\n",
" host=HOST,\n",
" port=int(PORT),\n",
" user=USER,\n",
" password=PASSWORD\n",
" )\n",
" print(\"Connected to database.\")\n",
"except Exception as e:\n",
" raise e"
"print(\"Connecting to database...\")\n",
"vDB = await PGvectorConnection.create(\n",
" embedding_model=e_model, \n",
" db_name=DB_NAME,\n",
" host=HOST,\n",
" port=int(PORT),\n",
" user=USER,\n",
" password=PASSWORD\n",
" )\n",
"print(\"Connected to database.\")"
]
},
{
Expand All @@ -107,7 +104,7 @@
},
{
"cell_type": "code",
"execution_count": 81,
"execution_count": 121,
"metadata": {},
"outputs": [
{
Expand All @@ -121,23 +118,17 @@
}
],
"source": [
"try: # Ensuring that the vector extension is installed\n",
" await vDB.conn.execute('''CREATE EXTENSION IF NOT EXISTS vector;''')\n",
" print(\"PGvector extension created and loaded.\")\n",
"except Exception as e:\n",
" raise e\n",
"# Ensuring that the vector extension is installed\n",
"await vDB.conn.execute('''CREATE EXTENSION IF NOT EXISTS vector;''')\n",
"print(\"PGvector extension created and loaded.\")\n",
"\n",
"try: # Drop the table if one is found\n",
" await vDB.conn.execute('''DROP TABLE IF EXISTS embeddings;''')\n",
" print(\"Table dropped.\")\n",
"except Exception as e:\n",
" raise e\n",
"# Drop the table if one is found\n",
"await vDB.conn.execute('''DROP TABLE IF EXISTS embeddings;''')\n",
"print(\"Table dropped.\")\n",
"\n",
"try: # Creating a new table\n",
" await vDB.create_doc_table(table_name='embeddings')\n",
" print(\"Table created.\")\n",
"except Exception as e:\n",
" raise e"
"# Creating a new table\n",
"await vDB.create_doc_table(table_name='embeddings')\n",
"print(\"Table created.\")"
]
},
{
Expand All @@ -150,7 +141,7 @@
},
{
"cell_type": "code",
"execution_count": 82,
"execution_count": 122,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -175,20 +166,19 @@
},
{
"cell_type": "code",
"execution_count": 83,
"execution_count": 123,
"metadata": {},
"outputs": [],
"source": [
"from pprint import pprint\n",
"\n",
"k = 3 # Setting number of rows to return when searching\n",
"\n",
"from pprint import pprint\n",
"def print_results(results): # Helper function to print results\n",
" print(f'\\nRAW RETURN:') \n",
" print(f'RAW RETURN:') \n",
" pprint(results) # Print the raw results\n",
" print(f'\\nRETURNED TITLE: {results[0][\"title\"]}') # Print the title of the first result\n",
" print(f'RETURNED CONTENT: {results[0][\"content\"]}') # Print the content of the first result\n",
" print(f'RETURNED COSINE SIMILARITY: {results[0][\"cosine_similarity\"]:.2f}') # Print the cosine similarity of the first result"
" print(f'\\nRETURNED TITLE:\\n\"{results[0][\"title\"]}\"') # Print the title of the first result\n",
" print(f'RETURNED CONTENT:\\n\"{results[0][\"content\"]}\"') # Print the content of the first result\n",
" print(f'RETURNED COSINE SIMILARITY:\\n{results[0][\"cosine_similarity\"]:.2f}') # Print the cosine similarity of the first result"
]
},
{
Expand All @@ -201,34 +191,35 @@
},
{
"cell_type": "code",
"execution_count": 84,
"execution_count": 124,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Semantic Searching data using search string: [beep] A single lap should be completed each time you hear this sound.\n",
"Semantic Searching data using search string:\n",
"\"[beep] A single lap should be completed each time you hear this sound.\"\n",
"\n",
"RAW RETURN:\n",
"[<Record cosine_similarity=1.0 title='Pacer Copypasta line 3' content='[beep] A single lap should be completed each time you hear this sound.'>,\n",
" <Record cosine_similarity=0.685540756152295 title='Pacer Copypasta line 5' content='The second time you fail to complete a lap before the sound, your test is over.'>,\n",
" <Record cosine_similarity=0.36591741151356405 title='Pacer Copypasta line 2' content='The running speed starts slowly, but gets faster each minute after you hear this signal.'>]\n",
"\n",
"RETURNED TITLE: Pacer Copypasta line 3\n",
"RETURNED CONTENT: [beep] A single lap should be completed each time you hear this sound.\n",
"RETURNED COSINE SIMILARITY: 1.00\n"
"RETURNED TITLE:\n",
"\"Pacer Copypasta line 3\"\n",
"RETURNED CONTENT:\n",
"\"[beep] A single lap should be completed each time you hear this sound.\"\n",
"RETURNED COSINE SIMILARITY:\n",
"1.00\n"
]
}
],
"source": [
"search_string = '[beep] A single lap should be completed each time you hear this sound.'\n",
"print(f'Semantic Searching data using search string: {search_string}')\n",
"print(f'Semantic Searching data using search string:\\n\"{search_string}\"\\n')\n",
"\n",
"try:\n",
" sim_search = await vDB.search_doc_table(table_name='embeddings', query_string=search_string, limit=k)\n",
"except Exception as e:\n",
" raise e\n",
"sim_search = await vDB.search_doc_table(table_name='embeddings', query_string=search_string, limit=k)\n",
"\n",
"print_results(sim_search)"
]
Expand All @@ -243,34 +234,35 @@
},
{
"cell_type": "code",
"execution_count": 85,
"execution_count": 125,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Semantic Searching data using search string: straight\n",
"Semantic Searching data using search string:\n",
"\"straight\"\n",
"\n",
"RAW RETURN:\n",
"[<Record cosine_similarity=0.28423854269729953 title='Pacer Copypasta line 4' content='[ding] Remember to run in a straight line, and run as long as possible.'>,\n",
" <Record cosine_similarity=0.10402820694362547 title='Pacer Copypasta line 6' content='The test will begin on the word start. On your mark, get ready, start.'>,\n",
" <Record cosine_similarity=0.07991296083513344 title='Pacer Copypasta line 2' content='The running speed starts slowly, but gets faster each minute after you hear this signal.'>]\n",
"\n",
"RETURNED TITLE: Pacer Copypasta line 4\n",
"RETURNED CONTENT: [ding] Remember to run in a straight line, and run as long as possible.\n",
"RETURNED COSINE SIMILARITY: 0.28\n"
"RETURNED TITLE:\n",
"\"Pacer Copypasta line 4\"\n",
"RETURNED CONTENT:\n",
"\"[ding] Remember to run in a straight line, and run as long as possible.\"\n",
"RETURNED COSINE SIMILARITY:\n",
"0.28\n"
]
}
],
"source": [
"search_string = 'straight'\n",
"print(f'Semantic Searching data using search string: {search_string}')\n",
"print(f'Semantic Searching data using search string:\\n\"{search_string}\"\\n')\n",
"\n",
"try:\n",
" sim_search = await vDB.search_doc_table(table_name='embeddings', query_string=search_string, limit=k)\n",
"except Exception as e:\n",
" raise e\n",
"sim_search = await vDB.search_doc_table(table_name='embeddings', query_string=search_string, limit=k)\n",
"\n",
"print_results(sim_search)"
]
Expand Down

0 comments on commit bb14318

Please sign in to comment.