Newer
Older
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!pip install -qU \"semantic-router[pinecone]==0.1.0.dev3\""
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"source": [
"from semantic_router import Route\n",
"\n",
"# we could use this as a guide for our chatbot to avoid political conversations\n",
"politics = Route(\n",
" name=\"politics\",\n",
" utterances=[\n",
" \"isn't politics the best thing ever\",\n",
" \"why don't you tell me about your political opinions\",\n",
" \"don't you just love the president\" \"don't you just hate the president\",\n",
" \"they're going to destroy this country!\",\n",
" \"they will save the country!\",\n",
" ],\n",
")\n",
"\n",
"# this could be used as an indicator to our chatbot to switch to a more\n",
"# conversational prompt\n",
"chitchat = Route(\n",
" name=\"chitchat\",\n",
" utterances=[\n",
" \"how's the weather today?\",\n",
" \"how are things going?\",\n",
" \"lovely weather today\",\n",
" \"the weather is horrendous\",\n",
" \"let's go to the chippy\",\n",
" ],\n",
")\n",
"\n",
"# we place both of our decisions together into single list\n",
"routes = [politics, chitchat]"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"from getpass import getpass\n",
"from semantic_router.encoders import OpenAIEncoder\n",
"\n",
"# get at platform.openai.com\n",
"os.environ[\"OPENAI_API_KEY\"] = os.environ.get(\"OPENAI_API_KEY\") or getpass(\n",
" \"Enter OpenAI API key: \"\n",
")\n",
"encoder = OpenAIEncoder()"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"2025-01-07 12:10:29 - pinecone_plugin_interface.logging - INFO - discover_namespace_packages.py:12 - discover_subpackages() - Discovering subpackages in _NamespacePath(['c:\\\\Users\\\\Joshu\\\\OneDrive\\\\Documents\\\\Aurelio\\\\agents-course\\\\07-pratical-ai\\\\.venv\\\\Lib\\\\site-packages\\\\pinecone_plugins'])\n",
"2025-01-07 12:10:29 - pinecone_plugin_interface.logging - INFO - discover_plugins.py:9 - discover_plugins() - Looking for plugins in pinecone_plugins.inference\n",
"2025-01-07 12:10:29 - pinecone_plugin_interface.logging - INFO - installation.py:10 - install_plugins() - Installing plugin inference into Pinecone\n",
"2025-01-07 12:10:31 - pinecone_plugin_interface.logging - INFO - discover_namespace_packages.py:12 - discover_subpackages() - Discovering subpackages in _NamespacePath(['c:\\\\Users\\\\Joshu\\\\OneDrive\\\\Documents\\\\Aurelio\\\\agents-course\\\\07-pratical-ai\\\\.venv\\\\Lib\\\\site-packages\\\\pinecone_plugins'])\n",
"2025-01-07 12:10:31 - pinecone_plugin_interface.logging - INFO - discover_plugins.py:9 - discover_plugins() - Looking for plugins in pinecone_plugins.inference\n"
]
}
],
"source": [
"from semantic_router.index.pinecone import PineconeIndex\n",
"\n",
"# get at app.pinecone.io\n",
"os.environ[\"PINECONE_API_KEY\"] = os.environ.get(\"PINECONE_API_KEY\") or getpass(\n",
" \"Enter Pinecone API key: \"\n",
")\n",
"pc_index = PineconeIndex()"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"2025-01-07 12:11:05 - pinecone_plugin_interface.logging - INFO - discover_namespace_packages.py:12 - discover_subpackages() - Discovering subpackages in _NamespacePath(['c:\\\\Users\\\\Joshu\\\\OneDrive\\\\Documents\\\\Aurelio\\\\agents-course\\\\07-pratical-ai\\\\.venv\\\\Lib\\\\site-packages\\\\pinecone_plugins'])\n",
"2025-01-07 12:11:05 - pinecone_plugin_interface.logging - INFO - discover_plugins.py:9 - discover_plugins() - Looking for plugins in pinecone_plugins.inference\n",
"2025-01-07 12:11:08 - httpx - INFO - _client.py:1025 - _send_single_request() - HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n"
"from semantic_router.routers import SemanticRouter\n",
"rl = SemanticRouter(encoder=encoder, routes=routes, index=pc_index, auto_sync=\"local\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can check our route layer and index information."
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['politics', 'chitchat']"
]
},
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"rl.list_route_names()"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(rl.index)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can also view all of the records for a given route:"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['politics#64069085d9d6e98e5a80915f69fabe82bac6c742f801bc305c5001dce88f0d19',\n",
" 'politics#af8b76111f260cf44fb34f04fcf82927dcbe08e8f47c30f4d571379c1512fac8',\n",
" 'politics#d1bb40236c3d95b9c695bfa86b314b6da4eb87e136699563fccae47fccea23e2',\n",
" 'politics#ed0f3dd7bd5dea12e55b1953bcd2c562a5ab19f501f6d5ff8c8927652c3904b8',\n",
" 'politics#fc6d15f9e6075e6de82b3fbef6722b64353e4eadc8d663b7312a4ed60c43e6f6']"
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"rl.index._get_route_ids(route_name=\"politics\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And query:"
]
},
{
"cell_type": "code",
{
"name": "stderr",
"output_type": "stream",
"text": [
"2025-01-07 12:11:37 - httpx - INFO - _client.py:1025 - _send_single_request() - HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n"
]
},
{
"data": {
"text/plain": [
"'politics'"
]
},
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"rl(\"don't you love politics?\").name"
]
},
{
"cell_type": "code",
{
"name": "stderr",
"output_type": "stream",
"text": [
"2025-01-07 12:11:43 - httpx - INFO - _client.py:1025 - _send_single_request() - HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n"
]
},
{
"data": {
"text/plain": [
"'chitchat'"
]
},
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"rl(\"how's the weather today?\").name"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"2025-01-07 12:11:46 - httpx - INFO - _client.py:1025 - _send_single_request() - HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n"
]
}
],
"source": [
"rl(\"I'm interested in learning about llama 2\").name"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can delete or update routes."
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"10"
]
},
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(rl.index)"
]
},
{
"cell_type": "code",
"2025-01-07 12:11:57 - semantic_router.utils.logger - WARNING - pinecone.py:431 - _read_config() - Configuration for sr_lock parameter not found in index.\n"
]
},
{
"data": {
"text/plain": [
"5"
]
},
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import time\n",
"\n",
"rl.delete(route_name=\"chitchat\")\n",
"time.sleep(1)\n",
"len(rl.index)"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"2025-01-07 12:12:01 - httpx - INFO - _client.py:1025 - _send_single_request() - HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n"
]
}
],
"source": [
"rl(\"how's the weather today?\").name"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[Route(name='politics', utterances=[\"they're going to destroy this country!\", 'they will save the country!', \"don't you just love the presidentdon't you just hate the president\", \"isn't politics the best thing ever\", \"why don't you tell me about your political opinions\"], description=None, function_schemas=None, llm=None, score_threshold=None, metadata={})]"
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"rl.index.get_routes()"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'type': 'pinecone', 'dimensions': 1536, 'vectors': 5}"
]
},
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"rl.index.describe()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "semantic_router_1",
"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",
}
},
"nbformat": 4,
"nbformat_minor": 2
}