### Define Routes and Initialize Semantic Router Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/user-guide/concepts/architecture.md This example demonstrates how to define routes with example utterances and initialize a SemanticRouter with an OpenAI encoder. Ensure the OpenAI library is installed and configured. ```python from semantic_router import Route, SemanticRouter from semantic_router.encoders import OpenAIEncoder # 1. Define routes weather_route = Route(name="weather", utterances=["What's the weather like?"]) greeting_route = Route(name="greeting", utterances=["Hello there!", "Hi!"]) # 2. Initialize encoder encoder = OpenAIEncoder() # 3. Create router with routes router = SemanticRouter(encoder=encoder, routes=[weather_route, greeting_route]) # 4. Route an incoming query result = router("What's the forecast for tomorrow?") print(result.name) # "weather" ``` -------------------------------- ### Start Postgres Instance with pgvector Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/indexes/postgres/postgres.ipynb Use Docker Compose to start a Postgres instance with the pgvector extension enabled. Ensure Docker is installed locally. ```bash # Start the Postgres instance with the pgvector extension using Docker Compose !echo "Running Docker Compose to start Postgres instance with pgvector extension" !docker compose -f ./docs/indexes/postgres/postgres.compose.yaml up -d ``` -------------------------------- ### Install semantic-router Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/examples/hybrid-chat-guardrails.ipynb Install the semantic-router library with the specified version. ```bash !pip install -qU semantic-router>=0.1.6 ``` -------------------------------- ### Example Usage of SemanticRouter with NimEncoder Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/integrations/nvidia.md Demonstrates a complete example of initializing NimEncoder, setting up routes, creating a SemanticRouter, and performing a query. ```python from semantic_router.encoders import NimEncoder from semantic_router.routers import SemanticRouter from semantic_router.route import Route encoder = NimEncoder( name="nvidia_nim/nvidia/nv-embedqa-e5-v5", score_threshold=0.4 ) routes = [ Route(name="greeting", utterances=["hello", "hi", "hey"]), Route(name="goodbye", utterances=["bye", "goodbye", "see you"]) ] router = SemanticRouter(encoder=encoder, routes=routes, auto_sync="local") print(router("hi there").name) # -> greeting ``` -------------------------------- ### Install Semantic Router with Vision Support Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/07-multi-modal.ipynb Install the necessary dependencies for multi-modal routing capabilities. ```bash !pip install -qU \ "semantic-router[vision]>=0.1.0" \ datasets==2.17.0 ``` -------------------------------- ### Install Semantic Router Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/user-guide/features/route-filter.md Install the semantic-router library using pip. The `-qU` flags ensure quiet installation and upgrade. ```python !pip install -qU semantic-router ``` -------------------------------- ### Install Semantic Router with PostgreSQL support Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/integrations/postgres.md Install the necessary package for PostgreSQL integration. Ensure you have psycopg v3 installed. ```bash pip install "semantic-router[postgres]" ``` -------------------------------- ### Install Semantic Router with Qdrant Support Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/integrations/qdrant.md Install the Semantic Router library with the necessary dependencies for Qdrant integration. ```bash pip install "semantic-router[qdrant]" ``` -------------------------------- ### Call Agent with Query Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/03-basic-langchain-agent.ipynb Invoke the agent with a user query to get a response. This example shows the basic call to the agent and the resulting output, including chat history and the agent's response. ```python agent(query) ``` -------------------------------- ### Run Jupyter Notebook Example Source: https://github.com/aurelio-labs/semantic-router/blob/main/CLAUDE.md Launch a Jupyter Notebook server to run example notebooks located in the `docs/` directory. This command requires `uv run` for dependency management. ```bash uv run jupyter notebook docs/00-introduction.ipynb ``` -------------------------------- ### Example Usage of Semantic Router Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/integrations/voyage.md Demonstrates a complete example of initializing the VoyageEncoder, setting up routes, creating a SemanticRouter, and performing a query. ```python from semantic_router.encoders import VoyageEncoder from semantic_router.routers import SemanticRouter from semantic_router.route import Route encoder = VoyageEncoder(name="voyage-3", score_threshold=0.4) routes = [ Route(name="greeting", utterances=["hello", "hi", "hey"]), Route(name="goodbye", utterances=["bye", "goodbye", "see you"]) ] router = SemanticRouter(encoder=encoder, routes=routes, auto_sync="local") print(router("hi there").name) # -> greeting ``` -------------------------------- ### Install Semantic Router with Local Extras Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/integrations/local.md Install the necessary packages to enable local encoder functionality. ```bash pip install "semantic-router[local]" ``` -------------------------------- ### Example Usage of OllamaEncoder with SemanticRouter Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/integrations/ollama.md A complete example demonstrating the initialization of OllamaEncoder, definition of routes, creation of a SemanticRouter, and routing a query. Ensure Ollama is running and the specified model is pulled. ```python from semantic_router.encoders import OllamaEncoder from semantic_router.routers import SemanticRouter from semantic_router.route import Route # Initialize encoder encoder = OllamaEncoder(name="nomic-embed-text") # Define routes routes = [ Route(name="greeting", utterances=["hello", "hi", "hey"]), Route(name="goodbye", utterances=["bye", "goodbye", "see you"]) ] # Create router router = SemanticRouter(encoder=encoder, routes=routes, auto_sync="local") # Route queries print(router("hi there").name) # -> greeting ``` -------------------------------- ### Install Semantic Router Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/integrations/cohere.md Install the Semantic Router library with Cohere integration. Ensure version 0.1.8 or later. ```bash pip install "semantic-router>=0.1.8" ``` -------------------------------- ### Full Example: Hybrid Router with Aurelio Sparse Encoder Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/integrations/aurelio.md A comprehensive example demonstrating the initialization of dense and sparse encoders, a hybrid index, routes, and the HybridRouter. It shows how to query the router and print the matched route name. ```python from semantic_router.encoders import OpenAIEncoder from semantic_router.encoders.aurelio import AurelioSparseEncoder from semantic_router.routers import HybridRouter from semantic_router.route import Route from semantic_router.index.hybrid_local import HybridLocalIndex # Initialize encoders dense_encoder = OpenAIEncoder(name="text-embedding-3-small", score_threshold=0.3) sparse_encoder = AurelioSparseEncoder(name="bm25") # Initialize index index = HybridLocalIndex() # Define routes routes = [ Route(name="greeting", utterances=["hello", "hi", "hey"]), Route(name="goodbye", utterances=["bye", "goodbye", "see you"]) ] # Create hybrid router router = HybridRouter( encoder=dense_encoder, sparse_encoder=sparse_encoder, routes=routes, index=index ) print(router("hi there").name) # -> greeting ``` -------------------------------- ### Example Usage of Semantic Router with MistralEncoder Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/integrations/mistral.md Demonstrates a complete example of initializing MistralEncoder, defining routes, creating a SemanticRouter, and performing a query. The output is the name of the matched route. ```python from semantic_router.encoders import MistralEncoder from semantic_router.routers import SemanticRouter from semantic_router.route import Route encoder = MistralEncoder(name="mistral-embed", score_threshold=0.4) routes = [ Route(name="greeting", utterances=["hello", "hi", "hey"]), Route(name="goodbye", utterances=["bye", "goodbye", "see you"]) ] router = SemanticRouter(encoder=encoder, routes=routes, auto_sync="local") print(router("hi there").name) # -> greeting ``` -------------------------------- ### Define a Dynamic Route with Utterances Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/user-guide/features/dynamic-routes.md Example utterances for a dynamic route. These guide the LLM in understanding the user's intent. ```python "what is x to the power of y?" "what is 9 to the power of 4?" "calculate the result of base x and exponent y" "calculate the result of base 10 and exponent 3" "return x to the power of y" ``` -------------------------------- ### Install semantic-router Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/encoders/aurelio-bm25.ipynb Install the semantic-router library, ensuring version 0.1.0 or later for AurelioSparseEncoder support. ```python !pip install -qU semantic-router==0.1.0 ``` -------------------------------- ### Example Routes Dictionary Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/examples/pinecone-and-scaling.ipynb This is an example of the resulting dictionary structure after processing routes and utterances. It maps route names to lists of example utterances. ```python routes_dict ``` -------------------------------- ### Install Semantic Router Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/encoders/cohere.ipynb Install the semantic-router library, ensuring version 0.1.8 or higher for CohereEncoder availability. ```bash !pip install -qU "semantic-router>=0.1.8" ``` -------------------------------- ### Install semantic-router Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/encoders/mistral-encoder.ipynb Install the semantic-router library, ensuring version 0.1.8 or later for Mistral encoder support. ```python !pip install -qU semantic-router==0.1.8 ``` -------------------------------- ### Example Usage of Semantic Router with CohereEncoder Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/integrations/cohere.md A complete example demonstrating the initialization of CohereEncoder and SemanticRouter, followed by a query to a route. This shows how to use the router to classify input utterances. ```python from semantic_router.encoders import CohereEncoder from semantic_router.routers import SemanticRouter from semantic_router.route import Route encoder = CohereEncoder(name="embed-english-v3.0", score_threshold=0.3) routes = [ Route(name="greeting", utterances=["hello", "hi", "hey"]), Route(name="goodbye", utterances=["bye", "goodbye", "see you"]) ] router = SemanticRouter(encoder=encoder, routes=routes, auto_sync="local") print(router("hi there").name) # -> greeting ``` -------------------------------- ### Install semantic-router with local dependencies Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/encoders/vision-transformer.ipynb Install the semantic-router library with the `[local]` flag to include all necessary dependencies for `VitEncoder`. This command ensures all required packages for local model execution are installed. ```python !pip install -qU "semantic-router[local]==0.0.23" ``` -------------------------------- ### Install Semantic Router Source: https://github.com/aurelio-labs/semantic-router/blob/main/README.md Install the semantic-router library using pip. Additional packages for local execution or hybrid routing can be installed with extra flags. ```bash pip install -qU semantic-router ``` -------------------------------- ### Install Semantic Router Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/integrations/jina.md Install Semantic Router version 0.1.8 or later using pip. ```bash pip install semantic-router>=0.1.8 ``` -------------------------------- ### Install Semantic Router with Local Support Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/encoders/local-encoder.ipynb Install the semantic-router library with the necessary dependencies for local model support. This command ensures all required packages are installed for local embedding generation. ```python # Install dependencies if needed # !pip install -qU "semantic-router[local]" ``` -------------------------------- ### Install Semantic Router Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/integrations/ollama.md Install the semantic-router library using pip. Ensure you have version 0.1.11 or later. ```bash pip install semantic-router ``` -------------------------------- ### Install Semantic Router Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/examples/hybrid-router.ipynb Install the semantic-router library using pip. Ensure you are using version 0.1.0 as specified. ```python #!pip install -qU semantic-router==0.1.0 ``` -------------------------------- ### Install Semantic Router Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/encoders/huggingface-endpoint.ipynb Install the semantic-router library using pip. Ensure you are using version 0.1.0 as specified. ```python !pip install -qU "semantic-router==0.1.0" ``` -------------------------------- ### Install Semantic Router Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/08-async-dynamic-routes.ipynb Installs the semantic-router library and tzdata. Ensure you are using version 0.1.5 or later. ```bash !pip install -qU \ "semantic-router>=0.1.5" \ tzdata ``` -------------------------------- ### Install Semantic Router with Local Support Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/user-guide/guides/local-execution.md Install the semantic-router library with the 'local' extra for llama.cpp integration. Ensure you follow the llama-cpp-python repository for specific hardware acceleration instructions if needed. ```bash pip install -qU "semantic-router[local]" ``` -------------------------------- ### Install Semantic Router with Local Support Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/05-local-execution.ipynb Install the semantic-router library with the necessary dependencies for local execution. This command ensures all required packages for local LLM integration are included. ```python !pip install -qU "semantic-router[local]" ``` -------------------------------- ### Install Semantic Router with Bedrock support Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/integrations/bedrock.md Install the Semantic Router library with the necessary dependencies for AWS Bedrock integration. ```bash pip install "semantic-router[bedrock]" ``` -------------------------------- ### Install Semantic Router with Metal Acceleration Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/user-guide/guides/local-execution.md For Apple silicon users, compile the local support with Metal hardware acceleration enabled by setting the CMAKE_ARGS environment variable before installation. ```bash CMAKE_ARGS="-DLLAMA_METAL=on" pip install -qU "semantic-router[local]" ``` -------------------------------- ### Start Pinecone Local Docker Container Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/indexes/pinecone-local.ipynb Run this command to start the Pinecone Local Docker container. It maps port 5080 and sets the host to localhost. This container is for testing purposes only. ```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 ``` -------------------------------- ### Install Required Libraries Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/integrations/agents-sdk/hybrid-router-guardrails.ipynb Installs the necessary libraries for semantic routing, Pydantic-AI, and OpenAI agents. Ensure you have semantic-router version 0.1.4 or higher. ```python !pip install -qU \ semantic-router>=0.1.4 \ pydantic-ai>=0.0.42 \ openai-agents>=0.0.7 ``` -------------------------------- ### Install Semantic Router and tzdata Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/user-guide/features/dynamic-routes.md Commands to install the necessary libraries for using semantic-router, including `tzdata` for timezone support. ```bash !pip install tzdata !pip install -qU semantic-router>=0.1.5 ``` -------------------------------- ### Install Semantic Router Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/integrations/aurelio.md Install Semantic Router version 0.1.0 or later using pip. Ensure you have the necessary prerequisites, including an Aurelio Platform API key. ```bash pip install "semantic-router>=0.1.0" ``` -------------------------------- ### Install Semantic Router with Qdrant Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/indexes/qdrant.ipynb Install the semantic-router library with Qdrant support using pip. This command ensures all necessary dependencies for Qdrant integration are included. ```bash !pip install -qU "semantic-router[qdrant]" ``` -------------------------------- ### Install semantic-router Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/get-started/quickstart.md Install the semantic-router library using pip. For local execution or hybrid route layers, additional packages may be required. ```bash pip install -qU semantic-router ``` ```bash pip install -qU "semantic-router[local]" ``` ```bash pip install -qU "semantic-router[hybrid]" ``` -------------------------------- ### Install Semantic Router Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/00-introduction.ipynb Install the Semantic Router library using pip. Ensure you are using version 0.1.5 or later. ```python !pip install -qU "semantic-router>=0.1.5" ``` -------------------------------- ### Install uv for Python Environment Management Source: https://github.com/aurelio-labs/semantic-router/blob/main/CONTRIBUTING.md Install the `uv` package manager for macOS and Linux using the provided curl command. ```bash curl -LsSf https://astral.sh/uv/install.sh | sh ``` -------------------------------- ### Example Questions for Semantic Router Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/examples/hybrid-chat-guardrails.ipynb A comprehensive list of example questions demonstrating the breadth of topics the Semantic Router can handle, from specific product inquiries to general knowledge questions. ```text ['Tell me about the BYD Seal.', 'What is the battery capacity of the BYD Dolphin?', "How does BYD's Blade Battery work?", 'Is the BYD Atto 3 a good EV?', 'What’s the range of the BYD Tang?', 'Does BYD offer fast-charging stations?', 'How is the BYD Han different from the Seal?', 'Is BYD the largest EV manufacturer in China?', 'What is the top speed of the BYD Seal?', 'Compare the BYD Dolphin and the BYD Atto 3.', 'How does BYD’s battery technology compare to Tesla’s?', 'What makes the BYD Blade Battery safer?', 'Does BYD have plans to expand to Europe?', 'How efficient is the BYD Tang in terms of range?', 'What are the latest BYD electric vehicle models?', 'How does the BYD Han compare to the Tesla Model S?', 'What is the warranty on BYD EV batteries?', 'Which BYD model is the best for long-distance driving?', 'Does BYD manufacture its own battery cells?', 'Is Tesla better than BYD?', 'Tell me about the Tesla Model 3.', 'How does Tesla’s autopilot compare to other EVs?', 'What’s new in the Tesla Cybertruck?', 'What is Tesla’s Full Self-Driving feature?', 'How long does it take to charge a Tesla?', 'Tell me about the Tesla Roadster.', 'How much does a Tesla Model S cost?', 'Which Tesla model has the longest range?', 'What are the main differences between the Tesla Model S and Model 3?', 'How safe is Tesla’s Autopilot?', 'Does Tesla use LFP batteries?', 'What is the Tesla Supercharger network?', 'How does Tesla’s Plaid mode work?', 'Which Tesla is best for off-roading?', 'What’s the range of the Polestar 2?', 'Is Polestar a good alternative?', 'How does Polestar compare to Tesla?', 'Tell me about the Polestar 3.', 'Is the Polestar 2 fully electric?', 'What is Polestar’s performance like?', 'Does Polestar offer any performance upgrades?', "How is Polestar's autonomous driving technology?", 'What is the battery capacity of the Polestar 2?', 'How does Polestar differ from Volvo?', 'Is Polestar planning a fully electric SUV?', 'How does the Polestar 4 compare to other EVs?', 'What are Polestar’s sustainability goals?', 'How much does a Polestar 3 cost?', 'Does Polestar have its own fast-charging network?', 'Tell me about the Rivian R1T.', "How does Rivian's off-road capability compare to other EVs?", "Is Rivian's charging network better than other EVs?", 'What is the range of the Rivian R1S?', 'How much does a Rivian R1T cost?', 'Tell me about Rivian’s plans for new EVs.', 'How does Rivian’s technology compare to other EVs?', 'What are the best off-road features of the Rivian R1T?', 'What’s the towing capacity of the Rivian R1T?', 'How does the Rivian R1S differ from the R1T?', 'What’s special about Rivian’s adventure network?', 'How much does it cost to charge a Rivian?', 'Does Rivian have a lease program?', 'What are Rivian’s future expansion plans?', 'How long does it take to charge a Rivian at home?', 'What is the capital of France?', 'How many people live in the US?', 'When is the best time to visit Bali?', 'How do I learn a language?', 'Tell me an interesting fact.', 'What is the best programming language?', "I'm interested in learning about llama 2.", 'What is the capital of the moon?', 'Who was the first person to walk on the moon?', 'What’s the best way to cook a steak?', 'How do I start a vegetable garden?', 'What’s the most popular dog breed?', 'Tell me about the history of the Roman Empire.', 'How do I improve my photography skills?', 'What are some good book recommendations?', 'How does the stock market work?', 'What’s the best way to stay fit?', 'What’s the weather like in London today?', 'Who won the last FIFA World Cup?', 'What’s the difference between a crocodile and an alligator?', 'Tell me about the origins of jazz music.', 'What’s the fastest animal on land?', 'How does Bitcoin mining work?', 'What are the symptoms of the flu?', 'How do I start a YouTube channel?', 'What’s the best travel destination for solo travelers?', 'Who invented the light bulb?', 'What are the rules of chess?', 'Tell me about ancient Egyptian mythology.', 'How do I train my dog to sit?', 'What’s the difference between espresso and regular coffee?', 'What’s a good beginner-friendly programming language?', 'What are some good stretching exercises?', 'How do I bake a chocolate cake?', 'What’s the best way to save money?', 'How do airplanes stay in the air?', 'What are the benefits of meditation?', 'How do I learn basic Spanish?', 'What’s the best way to pack for a trip?', 'What’s the most common phobia?', 'How do I take care of a bonsai tree?', 'What’s the best way to clean a laptop keyboard?', 'Tell me about the Great Wall of China.', 'What’s the best way to learn to swim?', 'How does WiFi work?', 'What’s the healthiest type of bread?', 'What’s the origin of the word ‘quarantine’?', 'How do I find a good apartment?', 'What are some good mindfulness techniques?', 'How do I set up a home theater system?'] ``` -------------------------------- ### Install Semantic Router with Local Dependencies Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/examples/ollama-local-execution.ipynb Install the Semantic Router library with support for local execution. This command ensures all necessary dependencies for local LLM integration are included. ```python # !pip install -qU "semantic_router[local]==0.0.28" ``` -------------------------------- ### Install Semantic Router with Pinecone Support Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/indexes/pinecone-local.ipynb Install the semantic-router library with Pinecone support. Ensure you are using version 0.1.5 or higher. ```python !pip install -qU "semantic-router[pinecone]>=0.1.5" ``` -------------------------------- ### Install semantic-router with Local Dependencies Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/encoders/huggingface.ipynb Install the semantic-router library with the `[local]` flag to ensure all necessary dependencies for HuggingFaceEncoder are included. This is a prerequisite for using local HuggingFace models. ```bash !pip install -qU "semantic-router[local]==0.1.0" ``` -------------------------------- ### Set Up and Sync Development Environment with uv Source: https://github.com/aurelio-labs/semantic-router/blob/main/CONTRIBUTING.md Navigate to the cloned directory, create a virtual environment using `uv`, activate it, and install project dependencies with all extras for unit testing. ```bash # Move into the cloned folder cd semantic-router/ # Create a virtual environment uv venv --python 3.13 # Activate the environment source .venv/bin/activate # Install via uv with all extras relevant to perform unit tests uv sync --extra all ``` -------------------------------- ### Get Time in Bangkok Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/05-local-execution.ipynb This example shows how to get the current time in Bangkok. The Semantic Router is used to extract the 'timezone' parameter for the get_time function. ```python out = rl("what's the time in Bangkok right now?") print(out) get_time(**out.function_call[0]) ``` -------------------------------- ### Get Time in New York Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/05-local-execution.ipynb This example shows how to get the current time in a specific timezone using the Semantic Router. It involves calling a function with extracted parameters. ```python out = rl("what's the time in Rome right now?") print(out) get_time(**out.function_call[0]) ``` -------------------------------- ### Get Time in Phuket Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/05-local-execution.ipynb This example demonstrates how to get the current time in Phuket. It shows the process of using the Semantic Router to extract function arguments from a natural language query. ```python out = rl("what's the time in Phuket right now?") print(out) get_time(**out.function_call[0]) ``` -------------------------------- ### Get Current Time Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/05-local-execution.ipynb This example demonstrates retrieving the current time, likely formatted as HH:MM, after processing LLM output. ```python '18:52' ``` -------------------------------- ### Example Usage with Cost Tracking Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/integrations/litellm.md Demonstrates initializing a Cohere encoder, setting up routes, creating a router, and processing input. LiteLLM automatically tracks costs during this process. ```python from semantic_router.encoders import CohereEncoder from semantic_router.routers import SemanticRouter from semantic_router.route import Route # LiteLLM-based encoder with cost tracking encoder = CohereEncoder(name="embed-english-v3.0", score_threshold=0.3) routes = [ Route(name="greeting", utterances=["hello", "hi"]), Route(name="goodbye", utterances=["bye", "goodbye"]) ] router = SemanticRouter(encoder=encoder, routes=routes, auto_sync="local") # LiteLLM automatically tracks costs result = router("hi there") print(result.name) # -> greeting ``` -------------------------------- ### Load Router from Configuration Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/user-guide/components/routers.md Provides examples of initializing a router from a YAML configuration file or a RouterConfig object, facilitating easier deployment and configuration management. ```python # Create a router from a YAML configuration file router = SemanticRouter.from_yaml("router_config.yaml") # Or from a RouterConfig object from semantic_router.routers import RouterConfig config = RouterConfig(routes=routes, encoder_type="openai") router = SemanticRouter.from_config(config) ``` -------------------------------- ### Define Routes with Utterances Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/encoders/openai-embed-3.ipynb Define routes for the router using the Route class. Each route has a name and a list of example utterances that should trigger it. This setup is crucial for the router to categorize incoming queries. ```python from semantic_router import Route 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!", ], ) 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", ], ) routes = [politics, chitchat] ``` -------------------------------- ### Initialize PostgresIndex with environment variables Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/integrations/postgres.md Set up environment variables for PostgreSQL connection details and initialize the PostgresIndex. Alternatively, provide a direct connection string. ```python import os from semantic_router.index.postgres import PostgresIndex os.environ["POSTGRES_HOST"] = "localhost" os.environ["POSTGRES_PORT"] = "5432" os.environ["POSTGRES_DB"] = "routes_db" os.environ["POSTGRES_USER"] = "postgres" os.environ["POSTGRES_PASSWORD"] = "password" connection_str = ( f"postgresql://{os.environ['POSTGRES_USER']}:{os.environ['POSTGRES_PASSWORD']}" f"@{os.environ['POSTGRES_HOST']}:{os.environ['POSTGRES_PORT']}/{os.environ['POSTGRES_DB']}" ) index = PostgresIndex( connection_string=connection_str, index_name="my_routes" ) ``` -------------------------------- ### Test Multi-Function Route - Get Time Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/08-async-dynamic-routes.ipynb Tests the 'timezone_management' dynamic route with a query specifically for the 'get_time' function. This example verifies that the router can correctly identify the intent and extract arguments for time retrieval. ```python response = await rl2.acall("what is the time in New York?") response ``` -------------------------------- ### Setup Postgres Index Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/indexes/postgres-sync.ipynb Call the setup_index method to prepare the PostgreSQL table for storing embeddings and route information. This should be done after initializing the index. ```python pg_index.setup_index() ``` -------------------------------- ### Define Routes for Agent Interaction Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/03-basic-langchain-agent.ipynb Sets up different routes with specific names and utterances to guide the AI agent's responses. These routes help categorize user queries for actions like getting time, inquiring about supplement brands, business inquiries, or product information. ```python from semantic_router import Route time_route = Route( name="get_time", utterances=[ "what time is it?", "when should I eat my next meal?", "how long should I rest until training again?", "when should I go to the gym?", ], ) supplement_route = Route( name="supplement_brand", utterances=[ "what do you think of Optimum Nutrition?", "what should I buy from MyProtein?", "what brand for supplements would you recommend?", "where should I get my whey protein?", ], ) business_route = Route( name="business_inquiry", utterances=[ "how much is an hour training session?", "do you do package discounts?", ], ) product_route = Route( name="product", utterances=[ "do you have a website?", "how can I find more info about your services?", "where do I sign up?", "how do I get hench?", "do you have recommended training programmes?", ], ) routes = [time_route, supplement_route, business_route, product_route] ``` -------------------------------- ### Initialize Semantic Router with API Key Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/02-dynamic-routes.ipynb Sets up the SemanticRouter by configuring the encoder and providing necessary API keys. Supports various encoders like Cohere, OpenAI, and local alternatives. ```python import os from getpass import getpass from semantic_router.encoders import CohereEncoder, OpenAIEncoder # noqa: F401 from semantic_router.routers import SemanticRouter # dashboard.cohere.ai # os.environ["COHERE_API_KEY"] = os.getenv("COHERE_API_KEY") or getpass( # "Enter Cohere API Key: " # ) # platform.openai.com os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY") or getpass( "Enter OpenAI API Key: " ) ``` -------------------------------- ### Initialize QdrantIndex for Cloud Deployment Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/integrations/qdrant.md Initialize the QdrantIndex for a cloud-hosted Qdrant instance. Ensure you provide your cluster URL and API key. ```python index = QdrantIndex( url="https://your-cluster.qdrant.io", api_key="your-api-key", collection_name="semantic_router" ) ``` -------------------------------- ### Initialize RouteLayer with Imports Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/examples/function_calling.ipynb Basic setup for using the RouteLayer, including necessary imports from the semantic_router library and standard Python modules like `os` and `getpass`. ```python import os from getpass import getpass from semantic_router import RouteLayer ``` -------------------------------- ### Pull and Run Qdrant Docker Image Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/integrations/qdrant.md Download the official Qdrant Docker image and start a container for local development or testing. ```bash docker pull qdrant/qdrant docker run -p 6333:6333 qdrant/qdrant ``` -------------------------------- ### Initialize Semantic Router with Cohere Encoder Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/01-save-load-from-file.ipynb Sets up the Cohere API key and initializes the Semantic Router with a Cohere encoder and predefined routes. Ensure the COHERE_API_KEY environment variable is set or provided. ```python os.environ["COHERE_API_KEY"] = os.getenv("COHERE_API_KEY") or getpass( "Enter Cohere API Key: " ) encoder = CohereEncoder() rl = SemanticRouter(encoder=encoder, routes=routes, auto_sync="local") ``` -------------------------------- ### Install Semantic Router Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/02-dynamic-routes.ipynb Installs the semantic-router library and its dependencies. Ensure you have pip version 24.0 or higher. ```bash !pip install tzdata !pip install -qU "semantic-router>=0.1.5" ``` -------------------------------- ### Initialize OpenAI Encoder Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/indexes/pinecone_async.ipynb Set up the OpenAI API key and initialize the OpenAIEncoder. Ensure the OPENAI_API_KEY environment variable is set. ```python os.environ["OPENAI_API_KEY"] = os.environ.get("OPENAI_API_KEY") or getpass( "Enter OpenAI API key: " ) encoder = OpenAIEncoder(name="text-embedding-3-small") ``` -------------------------------- ### Initialize Pinecone Index Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/indexes/pinecone.ipynb Set up the Pinecone API key and initialize the Pinecone index. Ensure the PINECONE_API_KEY environment variable is set or provide it interactively. ```python os.environ["PINECONE_API_KEY"] = os.environ.get("PINECONE_API_KEY") or getpass( "Enter Pinecone API key: " ) pc_index = PineconeIndex() ``` -------------------------------- ### Initialize Semantic Router with JinaEncoder Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/integrations/jina.md Initialize a SemanticRouter instance, providing the configured JinaEncoder and a list of defined routes. ```python from semantic_router.routers import SemanticRouter # Create routes routes = [ Route( name="support", utterances=["I need help", "Can you assist me?"] ), Route( name="sales", utterances=["I want to buy", "How much does it cost?"] ) ] # Initialize router with Jina encoder router = SemanticRouter( encoder=encoder, routes=routes, auto_sync="local" ) ``` -------------------------------- ### Initialize QdrantIndex for Self-Hosted Deployment Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/integrations/qdrant.md Initialize the QdrantIndex for a self-hosted Qdrant instance. This requires the URL of your Qdrant service. ```python index = QdrantIndex( url="http://localhost:6333", collection_name="semantic_router" ) ``` -------------------------------- ### Initialize and Use SemanticRouter Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/user-guide/components/routers.md Demonstrates how to initialize a SemanticRouter with OpenAIEncoder and LocalIndex, define routes, and use it to route a query. Ensure your API key is set. ```python from semantic_router.routers import SemanticRouter from semantic_router.encoders import OpenAIEncoder from semantic_router.index import LocalIndex from semantic_router import Route import os # Set up API key os.environ["OPENAI_API_KEY"] = "your-api-key" # Create routes routes = [ Route(name="weather", utterances=["How's the weather?", "Is it raining?"]), Route(name="politics", utterances=["Tell me about politics", "Who's the president?"]) ] # Initialize the router router = SemanticRouter( encoder=OpenAIEncoder(), routes=routes, index=LocalIndex() ) # Use the router to route a query result = router("What's the weather like today?") print(result.name) # "weather" print(result.score) # e.g., 0.92 ``` -------------------------------- ### Initialize Semantic Router from Config Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/user-guide/guides/migration-to-v1.md Use `SemanticRouter.from_config` to initialize the router with a given configuration object. ```python semantic_router = SemanticRouter.from_config(config) ``` -------------------------------- ### Initialize Aurelio Sparse Encoder Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/integrations/graphai/sparse-threshold-optimization-guardrail-graphai.ipynb Sets up the Aurelio Sparse Encoder using the 'bm25' model for term matching. Requires an Aurelio API key, which can be set via environment variable or prompted. ```python import os from getpass import getpass from semantic_router.encoders.aurelio import AurelioSparseEncoder os.environ["AURELIO_API_KEY"] = os.environ["AURELIO_API_KEY"] or getpass( "Enter your Aurelio API key: " ) # sparse encoder for term matching sparse_encoder = AurelioSparseEncoder(name="bm25") ``` -------------------------------- ### Initialize LayerConfig with Routes Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/examples/function_calling.ipynb Create a LayerConfig instance by passing a list of defined routes. This configures the router with the specified routes. ```python from semantic_router.layer import LayerConfig layer_config = LayerConfig(routes=routes) layer_config.to_dict() ``` -------------------------------- ### Install semantic-router with Pinecone support Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/examples/pinecone-hybrid.ipynb Install the semantic-router library with Pinecone support. Ensure you are using version 0.1.0 or later for AurelioSparseEncoder compatibility. ```python !pip install -qU "semantic-router[pinecone]==0.1.0" ``` -------------------------------- ### Install semantic-router with Google support Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/encoders/google.ipynb Install the semantic-router library with the necessary dependencies for Google integration. Support for GoogleEncoder was added in version 0.0.31. ```python !pip install -qU "semantic-router[google]" ``` -------------------------------- ### Install Semantic Router and Pydantic AI Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/integrations/pydantic-ai.md Install the necessary packages for Semantic Router and Pydantic AI integration. Ensure you are using compatible versions. ```bash pip install "semantic-router>=0.1.6" "pydantic-ai>=0.0.42" ``` -------------------------------- ### Initialize Semantic Router with OpenAI Encoder Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/03-basic-langchain-agent.ipynb Set up the Semantic Router using OpenAI's embedding model. Ensure your OPENAI_API_KEY is set in your environment variables or provided when prompted. ```python os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY") or getpass( "Enter OpenAI API Key: " ) from semantic_router.routers import SemanticRouter from semantic_router.encoders import OpenAIEncoder rl = SemanticRouter(encoder=OpenAIEncoder(), routes=routes, auto_sync="local") ``` -------------------------------- ### Initialize Pinecone Index Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/indexes/pinecone-async.ipynb Set up the Pinecone API key and initialize an asynchronous Pinecone index with specified dimensions. ```python os.environ["PINECONE_API_KEY"] = os.environ.get("PINECONE_API_KEY") or getpass( "Enter Pinecone API key: " ) pc_index = PineconeIndex(dimensions=1536, init_async_index=True) ``` -------------------------------- ### Install Semantic Router and GraphAI Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/integrations/graphai/sparse-threshold-optimization-guardrail-graphai.ipynb Installs the necessary libraries for semantic routing and graph AI functionalities. Ensure you have version 0.1.4 or higher for semantic-router. ```python !pip install -qU \ semantic-router>=0.1.4 \ graphai-lib==0.0.2 ``` -------------------------------- ### Install Semantic Router with Pinecone Support Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/examples/pinecone-and-scaling.ipynb Installs the semantic-router library with local and Pinecone dependencies, along with the datasets library. Ensure you are using the correct version. ```python !pip install -qU \ "semantic-router[local, pinecone]==0.0.22" \ datasets==2.17.0 ``` -------------------------------- ### Install semantic-router and pydantic-ai Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/examples/integrations/pydantic-ai/chatbot-with-guardrails.ipynb Install the necessary libraries for semantic routing and pydantic-ai. Ensure you are using version 0.1.6 or higher for semantic-router and 0.0.42 or higher for pydantic-ai. ```python #!pip install -qU \ # semantic-router>=0.1.6 \ # pydantic-ai>=0.0.42 ``` -------------------------------- ### Initialize Semantic Router Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/integrations/agents-sdk/semantic-router-guardrail.ipynb Instantiate the SemanticRouter by providing the encoder, a list of routes, and the auto_sync parameter. The 'local' setting for auto_sync is used here. ```python from semantic_router.routers import SemanticRouter scam_router = SemanticRouter(encoder=encoder, routes=routes, auto_sync="local") ``` -------------------------------- ### Install Semantic Router with FastEmbed Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/encoders/fastembed.ipynb Install the semantic-router library with the necessary dependencies for FastEmbed. This command ensures all required packages for using FastEmbed encoders are included. ```python !pip install -qU "semantic-router[fastembed]==0.1.0" ``` -------------------------------- ### Install Semantic Router with Pinecone Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/integrations/pinecone.md Install the Semantic Router library with Pinecone support using pip. Ensure you have version 0.1.12 or later for Pinecone v7 compatibility. ```bash pip install "semantic-router[pinecone]" ``` -------------------------------- ### Initialize Semantic Router Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/07-multi-modal.ipynb Configures the router with the CLIP encoder and defined routes. ```python from semantic_router import SemanticRouter rl = SemanticRouter(encoder=encoder, routes=routes) ``` -------------------------------- ### Handle queries with no matching route Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/encoders/huggingface.ipynb This example shows the behavior when a query does not match any defined routes. In such cases, the router returns None, indicating no suitable route was found. ```text RouteChoice(name=None, function_call=None) ``` ```python rl("I'm interested in learning about llama 2") ``` -------------------------------- ### Initialize Semantic Router Source: https://github.com/aurelio-labs/semantic-router/blob/main/docs/indexes/postgres-sync.ipynb Instantiate the SemanticRouter with the configured encoder, defined routes, and the initialized PostgreSQL index. 'auto_sync="local"' enables automatic synchronization. ```python # Import the SemanticRouter class from semantic_router.routers import SemanticRouter # Initialize the SemanticRouter with the encoder, routes, and index rl = SemanticRouter(encoder=encoder, routes=routes, index=pg_index, auto_sync="local") ```