Skip to content
Snippets Groups Projects
Unverified Commit a91a54dd authored by James Briggs's avatar James Briggs Committed by GitHub
Browse files

Merge pull request #502 from aurelio-labs/josh/fix-index-docs

feat: updated index docs
parents 05fccb55 7b1d2da8
No related branches found
Tags v0.1.24
No related merge requests found
%% Cell type:markdown id: tags:
# Local Index Example
%% Cell type:code id: tags:
``` python
from semantic_router import Route
# we could use this as a guide for our chatbot to avoid political conversations
politics = Route(
name="politics",
utterances=[
"isn't politics the best thing ever",
"why don't you tell me about your political opinions",
"don't you just love the president" "don't you just hate the president",
"they're going to destroy this country!",
"they will save the country!",
],
)
# this could be used as an indicator to our chatbot to switch to a more
# conversational prompt
chitchat = Route(
name="chitchat",
utterances=[
"how's the weather today?",
"how are things going?",
"lovely weather today",
"the weather is horrendous",
"let's go to the chippy",
],
)
# we place both of our decisions together into single list
routes = [politics, chitchat]
```
%% Output
/Users/jamesbriggs/opt/anaconda3/envs/decision-layer/lib/python3.11/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
from .autonotebook import tqdm as notebook_tqdm
%% Cell type:code id: tags:
``` python
import os
from semantic_router.encoders import HuggingFaceEncoder
encoder = HuggingFaceEncoder(model_name="intfloat/e5-base-v2")
```
%% Cell type:code id: tags:
``` python
from semantic_router.index.local import LocalIndex
local_index = LocalIndex()
```
%% Cell type:code id: tags:
``` python
from semantic_router.layer import RouteLayer
from semantic_router.routers import SemanticRouter
local_rl = RouteLayer(encoder=encoder, routes=routes, index=local_index)
local_rl = SemanticRouter(
encoder=encoder, routes=routes, index=local_index, auto_sync="local"
)
```
%% Output
2024-02-14 03:08:45 INFO semantic_router.utils.logger local
2025-01-07 12:08:10 - semantic_router.utils.logger - WARNING - local.py:148 - _write_config() - No config is written for LocalIndex.
%% Cell type:code id: tags:
``` python
local_rl.list_route_names()
```
%% Output
['politics', 'chitchat']
%% Cell type:code id: tags:
``` python
local_rl("don't you love politics?").name
```
%% Output
'politics'
%% Cell type:code id: tags:
``` python
local_rl("how's the weather today?").name
```
%% Output
'chitchat'
%% Cell type:code id: tags:
``` python
local_rl("I'm interested in learning about llama 2").name
```
%% Cell type:markdown id: tags:
We can delete or update routes.
%% Cell type:code id: tags:
``` python
len(local_rl.index.index)
```
%% Output
10
%% Cell type:code id: tags:
``` python
local_rl.delete("chitchat")
len(local_rl.index.index)
```
%% Output
2025-01-07 12:08:29 - semantic_router.utils.logger - WARNING - base.py:172 - _read_config() - This method should be implemented by subclasses.
2025-01-07 12:08:29 - semantic_router.utils.logger - WARNING - base.py:172 - _read_config() - This method should be implemented by subclasses.
2025-01-07 12:08:29 - semantic_router.utils.logger - WARNING - base.py:823 - delete() - Route `chitchat` not found in SemanticRouter
2025-01-07 12:08:29 - semantic_router.utils.logger - WARNING - local.py:148 - _write_config() - No config is written for LocalIndex.
5
%% Cell type:code id: tags:
``` python
local_rl("how's the weather today?").name
```
%% Cell type:code id: tags:
``` python
local_rl.index.describe()
```
%% Output
{'type': 'local', 'dimensions': 384, 'vectors': 5}
......
%% Cell type:code id: tags:
``` python
!pip install -qU "semantic-router[pinecone]==0.0.22"
!pip install -qU "semantic-router[pinecone]==0.1.0.dev3"
```
%% Cell type:code id: tags:
``` python
from semantic_router import Route
# we could use this as a guide for our chatbot to avoid political conversations
politics = Route(
name="politics",
utterances=[
"isn't politics the best thing ever",
"why don't you tell me about your political opinions",
"don't you just love the president" "don't you just hate the president",
"they're going to destroy this country!",
"they will save the country!",
],
)
# this could be used as an indicator to our chatbot to switch to a more
# conversational prompt
chitchat = Route(
name="chitchat",
utterances=[
"how's the weather today?",
"how are things going?",
"lovely weather today",
"the weather is horrendous",
"let's go to the chippy",
],
)
# we place both of our decisions together into single list
routes = [politics, chitchat]
```
%% Output
/Users/jamesbriggs/opt/anaconda3/envs/decision-layer/lib/python3.11/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
from .autonotebook import tqdm as notebook_tqdm
%% Cell type:code id: tags:
``` python
import os
from getpass import getpass
from semantic_router.encoders import OpenAIEncoder
# get at platform.openai.com
os.environ["OPENAI_API_KEY"] = os.environ.get("OPENAI_API_KEY") or getpass(
"Enter OpenAI API key: "
)
encoder = OpenAIEncoder()
```
%% Cell type:code id: tags:
``` python
from semantic_router.index.pinecone import PineconeIndex
# get at app.pinecone.io
os.environ["PINECONE_API_KEY"] = os.environ.get("PINECONE_API_KEY") or getpass(
"Enter Pinecone API key: "
)
pc_index = PineconeIndex()
```
%% Output
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'])
2025-01-07 12:10:29 - pinecone_plugin_interface.logging - INFO - discover_plugins.py:9 - discover_plugins() - Looking for plugins in pinecone_plugins.inference
2025-01-07 12:10:29 - pinecone_plugin_interface.logging - INFO - installation.py:10 - install_plugins() - Installing plugin inference into Pinecone
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'])
2025-01-07 12:10:31 - pinecone_plugin_interface.logging - INFO - discover_plugins.py:9 - discover_plugins() - Looking for plugins in pinecone_plugins.inference
%% Cell type:code id: tags:
``` python
from semantic_router.layer import RouteLayer
from semantic_router.routers import SemanticRouter
rl = RouteLayer(encoder=encoder, routes=routes, index=pc_index)
rl = SemanticRouter(encoder=encoder, routes=routes, index=pc_index, auto_sync="local")
```
%% Output
2024-02-14 01:40:22 INFO semantic_router.utils.logger local
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'])
2025-01-07 12:11:05 - pinecone_plugin_interface.logging - INFO - discover_plugins.py:9 - discover_plugins() - Looking for plugins in pinecone_plugins.inference
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"
%% Cell type:markdown id: tags:
We can check our route layer and index information.
%% Cell type:code id: tags:
``` python
rl.list_route_names()
```
%% Output
['politics', 'chitchat']
%% Cell type:code id: tags:
``` python
len(rl.index)
```
%% Output
5
10
%% Cell type:markdown id: tags:
We can also view all of the records for a given route:
%% Cell type:code id: tags:
``` python
rl.index._get_route_ids(route_name="politics")
```
%% Output
{'vectors': [{'id': 'politics#1c4e5a74a2cb693bd51dd4fe0d8deb41'}, {'id': 'politics#3f95e1e2c26aa109d7a6b352bd1da93e'}, {'id': 'politics#461468866b9123c5ef111b72040c20d8'}, {'id': 'politics#e9120cb91dcc3ff76e7d2ce0740e009c'}, {'id': 'politics#f798c13e0a6d8a9926fc35a0f10f880b'}], 'pagination': {'next': 'eyJza2lwX3Bhc3QiOiJwb2xpdGljcyNmNzk4YzEzZTBhNmQ4YTk5MjZmYzM1YTBmMTBmODgwYiIsInByZWZpeCI6InBvbGl0aWNzIn0='}, 'namespace': '', 'usage': {'readUnits': 1}}
{'vectors': [], 'namespace': '', 'usage': {'readUnits': 1}}
['politics#1c4e5a74a2cb693bd51dd4fe0d8deb41',
'politics#3f95e1e2c26aa109d7a6b352bd1da93e',
'politics#461468866b9123c5ef111b72040c20d8',
'politics#e9120cb91dcc3ff76e7d2ce0740e009c',
'politics#f798c13e0a6d8a9926fc35a0f10f880b']
['politics#64069085d9d6e98e5a80915f69fabe82bac6c742f801bc305c5001dce88f0d19',
'politics#af8b76111f260cf44fb34f04fcf82927dcbe08e8f47c30f4d571379c1512fac8',
'politics#d1bb40236c3d95b9c695bfa86b314b6da4eb87e136699563fccae47fccea23e2',
'politics#ed0f3dd7bd5dea12e55b1953bcd2c562a5ab19f501f6d5ff8c8927652c3904b8',
'politics#fc6d15f9e6075e6de82b3fbef6722b64353e4eadc8d663b7312a4ed60c43e6f6']
%% Cell type:markdown id: tags:
And query:
%% Cell type:code id: tags:
``` python
rl("don't you love politics?").name
```
%% Output
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"
'politics'
%% Cell type:code id: tags:
``` python
rl("how's the weather today?").name
```
%% Output
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"
'chitchat'
%% Cell type:code id: tags:
``` python
rl("I'm interested in learning about llama 2").name
```
%% Output
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"
%% Cell type:markdown id: tags:
We can delete or update routes.
%% Cell type:code id: tags:
``` python
len(rl.index)
```
%% Output
10
%% Cell type:code id: tags:
``` python
import time
rl.delete(route_name="chitchat")
time.sleep(1)
len(rl.index)
```
%% Output
{'vectors': [{'id': 'chitchat#1e01e2c7f411d4dfcc43fe7a3de2fb67'}, {'id': 'chitchat#34fd10a3b6d1436b49759d9c67068a7e'}, {'id': 'chitchat#74cfdf22939f052ea9be158d77bc6bda'}, {'id': 'chitchat#bc13a8bc005f437d2bdeacbb9e741b8b'}, {'id': 'chitchat#fca33e294c0c4bb08ed95e1e3fd944c0'}], 'namespace': '', 'usage': {'readUnits': 1}}
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.
5
%% Cell type:code id: tags:
``` python
rl("how's the weather today?").name
```
%% Output
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"
%% Cell type:code id: tags:
``` python
rl.index.get_routes()
```
%% Output
{'vectors': [{'id': 'politics#1c4e5a74a2cb693bd51dd4fe0d8deb41'}, {'id': 'politics#3f95e1e2c26aa109d7a6b352bd1da93e'}, {'id': 'politics#461468866b9123c5ef111b72040c20d8'}, {'id': 'politics#e9120cb91dcc3ff76e7d2ce0740e009c'}, {'id': 'politics#f798c13e0a6d8a9926fc35a0f10f880b'}], 'namespace': '', 'usage': {'readUnits': 1}}
[('politics', "they're going to destroy this country!"),
('politics',
"don't you just love the presidentdon't you just hate the president"),
('politics', "isn't politics the best thing ever"),
('politics', "why don't you tell me about your political opinions"),
('politics', 'they will save the country!')]
[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={})]
%% Cell type:code id: tags:
``` python
rl.index.describe()
```
%% Output
{'type': 'pinecone', 'dimensions': 1536, 'vectors': 5}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment