{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Semantic Router Walkthrough" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The Semantic Router library can be used as a super fast decision making layer on top of LLMs. That means rather than waiting on a slow agent to decide what to do, we can use the magic of semantic vector space to make decisions. Cutting decision making time down from seconds to milliseconds." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Getting Started" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We start by installing the library:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!pip install -qU semantic-router==0.0.1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We start by defining a dictionary mapping decisions to example phrases that should trigger those decisions." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from semantic_router.schema import Decision\n", "\n", "politics = Decision(\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\"\n", " \"don't you just hate the president\",\n", " \"they're going to destroy this country!\",\n", " \"they will save the country!\"\n", " ]\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's define another for good measure:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "chitchat = Decision(\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", "decisions = [politics, chitchat]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we initialize our embedding model:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from semantic_router.encoders import CohereEncoder\n", "from getpass import getpass\n", "import os\n", "\n", "os.environ[\"COHERE_API_KEY\"] = os.environ[\"COHERE_API_KEY\"] or \\\n", " getpass(\"Enter Cohere API Key: \")\n", "\n", "encoder = CohereEncoder()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we define the `DecisionLayer`. When called, the decision layer will consume text (a query) and output the category (`Decision`) it belongs to — to initialize a `DecisionLayer` we need our `encoder` model and a list of `decisions`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from semantic_router import DecisionLayer\n", "\n", "dl = DecisionLayer(encoder=encoder, decisions=decisions)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we can test it:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dl(\"don't you love politics?\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dl(\"how's the weather today?\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Both are classified accurately, what if we send a query that is unrelated to our existing `Decision` objects?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dl(\"I'm interested in learning about llama 2\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this case, we return `None` because no matches were identified." ] } ], "metadata": { "kernelspec": { "display_name": "decision-layer", "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", "version": "3.11.3" } }, "nbformat": 4, "nbformat_minor": 2 }