#!/usr/bin/env python # coding: utf-8 # # Using PineconeLocalIndex for Routes # # Pinecone Local is an in-memory Pinecone Database emulator available as a Docker image. # # It's useful for running tests using the Pinecone local server. Your data will not leave your system, which is also helpful if you want to do testing locally before committing to a Pinecone account. # # ## Limitations # Pinecone Local has the following limitations: # # - Pinecone Local is available only as a Docker image. # - Pinecone Local is an in-memory emulator and is not suitable for production. Records loaded into Pinecone Local do not persist after it is stopped. # - Pinecone Local does not authenticate client requests. API keys are ignored. # - Max number of records per index: 100,000. # # ## Getting Started # Make sure [Docker](https://docs.docker.com/get-docker/) is installed and running on your local machine. # # ### Download the latest pinecone-local Docker image: # # # Download the latest pinecone-local Docker image: # # ```bash # docker pull ghcr.io/pinecone-io/pinecone-local:latest # ``` # # ### Start Pinecone Local: # # ```bash # docker run -d \ # --name pinecone-local \ # -e PORT=5080 \ # -e PINECONE_HOST=localhost \ # -p 5080-6000:5080-6000 \ # --platform linux/amd64 \ # ghcr.io/pinecone-io/pinecone-local:latest # ``` # # # In[16]: get_ipython().system('pip install -qU "semantic-router[pinecone]>=0.1.4"') # In[1]: 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] # In[2]: routes # In[3]: 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( name="text-embedding-3-large", score_threshold=0.5, dimensions=1536 ) # For Pinecone Local, you can pass the API key as `pclocal`. # In[4]: import os from semantic_router.index.pinecone import PineconeIndex os.environ["PINECONE_API_KEY"] = os.environ.get("PINECONE_API_KEY") or getpass( "Enter Pinecone API key: " ) # Pass the pinecone local hosted url as base url # os.environ["PINECONE_API_BASE_URL"] = "https://api.pinecone.io" os.environ["PINECONE_API_BASE_URL"] = "http://localhost:5080" index = PineconeIndex( index_name="route-test", dimensions=1536, base_url="http://localhost:5080" ) # In[8]: from semantic_router.routers import SemanticRouter router = SemanticRouter( encoder=encoder, routes=routes, index=index, auto_sync="local", ) # We can check our route layer and index information. # In[9]: index.index_host # In[10]: router.index.index_host # In[11]: router.index.client.list_indexes() # In[12]: router.index.is_ready() # In[13]: router.is_synced() # In[14]: router.get_utterance_diff() # In[15]: router("don't you love politics?") # In[17]: router.list_route_names() # In[18]: router("how are things going?") # In[19]: len(router.index) # We can also view all of the records for a given route: # In[20]: router.index._get_route_ids(route_name="politics") # And query: # In[21]: router("don't you love politics?").name # In[22]: router("how's the weather today?").name # In[22]: router("I'm interested in learning about llama 2").name # We can delete or update routes. # In[24]: len(router.index) # ## Deleting a Route from the Semantic Router # In this section, we demonstrate how to delete a specific route from the `SemanticRouter` instance. This is useful when you want to remove a route that is no longer needed or to update the routing logic dynamically. # # In[23]: import time router.delete(route_name="politics") time.sleep(1) len(router.index) # In[24]: router("don't you love politics??").name # In[25]: router.index.get_routes() # ## Asynchronous Route Query with Semantic Router # # In this section, we explore how to perform an asynchronous query using the `SemanticRouter`. This approach is beneficial when you want to handle multiple queries concurrently without blocking the execution of your program. # In[26]: await router.acall("how are things going?") # In[27]: await router.index._async_list_indexes() # ### Stop and remove Pinecone Local # # To stop and remove the resources for Pinecone Local, run the following command: # # ```bash # docker compose down # docker stop pinecone-local # docker rm pinecone-local # ``` #