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

Merge pull request #281 from aurelio-labs/james/v0.0.41

chore: v0.0.41 release
parents d3ad005a 7b74a91d
No related branches found
Tags v0.0.41
Loading
%% Cell type:markdown id: tags:
[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/aurelio-labs/semantic-router/blob/main/docs/encoders/bedrock.ipynb) [![Open nbviewer](https://raw.githubusercontent.com/pinecone-io/examples/master/assets/nbviewer-shield.svg)](https://nbviewer.org/github/aurelio-labs/semantic-router/blob/main/docs/encoders/bedrock.ipynb)
%% Cell type:markdown id: tags:
# Using Bedrock embedding Models
%% Cell type:markdown id: tags:
The 3rd generation embedding models from AWS Bedrock (`amazon.titan-embed-text-v1`, `amazon.titan-embed-text-v2` and `cohere.embed-english-v3`) can both be used with our `BedrockEncoder`.
%% Cell type:markdown id: tags:
## Getting Started
%% Cell type:markdown id: tags:
We start by installing semantic-router. Support for the new `Bedrock` embedding models was added in `semantic-router==0.0.40`.
We start by installing semantic-router. Support for the new `Bedrock` embedding models was added in `0.0.40`.
%% Cell type:code id: tags:
``` python
!pip install -qU "semantic-router[bedrock]==0.0.40"
!pip install -qU "semantic-router[bedrock]"
```
%% Cell type:markdown id: tags:
We start by defining a dictionary mapping routes to example phrases that should trigger those routes.
%% Cell type:code id: tags:
``` 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!",
],
)
```
%% Cell type:markdown id: tags:
Let's define another for good measure:
%% Cell type:code id: tags:
``` python
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]
```
%% Cell type:markdown id: tags:
Now we initialize our embedding model, we will use the `-3-large` model alongside a `dimensions` value of `256`. This will produce _tiny_ 256-dimensional vectors that — according to OpenAI — outperform the 1536-dimensional vectors produced by `text-embedding-ada-002`.
%% Cell type:code id: tags:
``` python
import os
from getpass import getpass
from semantic_router.encoders import BedrockEncoder
aws_access_key_id = os.getenv("AWS_ACCESS_KEY_ID") or getpass(
"Enter AWS Access Key ID: "
)
aws_secret_access_key = os.getenv("AWS_SECRET_ACCESS_KEY") or getpass(
"Enter AWS Secret Access Key: "
)
aws_session_token = os.getenv("AWS_SESSION_TOKEN") or getpass(
"Enter AWS Session Token: "
)
aws_region = os.getenv("AWS_REGION") or getpass("Enter AWS Region: ")
encoder = BedrockEncoder(
name="amazon.titan-embed-image-v1",
score_threshold=0.5,
access_key_id=aws_access_key_id,
secret_access_key=aws_secret_access_key,
session_token=aws_session_token,
region=aws_region,
)
```
%% Cell type:code id: tags:
``` python
encoder(["hey"])
```
%% Output
[[0.012878418,
0.028442383,
-0.022094727,
-0.020751953,
-0.008300781,
0.033691406,
0.09326172,
0.0045166016,
0.033935547,
0.015319824,
0.012939453,
0.015380859,
0.012756348,
-0.064453125,
0.018432617,
0.03173828,
-0.018188477,
-0.007171631,
0.03955078,
0.0033874512,
0.007019043,
0.010131836,
-0.025878906,
0.056152344,
0.01373291,
-0.020263672,
0.055419922,
-0.06225586,
0.040039062,
-0.015075684,
0.012268066,
-0.056640625,
0.04736328,
-0.002609253,
-0.0064086914,
0.011291504,
-0.019165039,
-0.005493164,
0.003189087,
0.008666992,
0.03564453,
-0.0027923584,
-0.016601562,
0.014404297,
-0.01171875,
0.013183594,
-0.018920898,
-0.030639648,
0.010864258,
0.052734375,
-0.006164551,
0.0035705566,
0.0060424805,
-0.021606445,
-0.040527344,
0.020385742,
0.004638672,
-0.010314941,
-0.010681152,
-0.010803223,
-0.038330078,
-0.029174805,
0.036865234,
-0.03112793,
-0.034179688,
0.017944336,
-0.03515625,
0.068847656,
-0.032470703,
-0.03540039,
0.017944336,
-0.024047852,
-0.05834961,
-0.049804688,
-0.009277344,
0.021484375,
-0.036376953,
0.03540039,
-0.012939453,
-0.03491211,
-0.028808594,
0.017333984,
0.021484375,
0.0052490234,
-0.026611328,
-0.0026245117,
0.05078125,
-0.022949219,
-0.057128906,
-0.019042969,
0.01574707,
-0.0025482178,
0.02355957,
-0.0011367798,
0.0039367676,
-0.015197754,
-0.02758789,
-0.025268555,
-0.048339844,
0.04296875,
-0.01373291,
-0.0052490234,
-0.016357422,
-0.029663086,
0.024536133,
-0.03881836,
-0.035888672,
0.013793945,
-0.016357422,
-0.052734375,
0.0154418945,
-0.004058838,
-0.018432617,
-0.01574707,
0.06225586,
0.044433594,
-0.011474609,
0.019897461,
-0.018432617,
-0.03515625,
0.057861328,
-0.016967773,
0.008666992,
0.01574707,
0.024780273,
0.01953125,
-0.005554199,
0.042236328,
-0.026123047,
-0.111328125,
0.018798828,
0.018066406,
-0.032958984,
-0.0025024414,
-0.01159668,
-0.028930664,
-0.055908203,
0.037353516,
0.018432617,
0.015258789,
-0.021850586,
-0.0026245117,
0.016723633,
0.0095825195,
0.05029297,
0.011779785,
0.04711914,
-0.064941406,
0.0059509277,
-0.025390625,
0.03857422,
0.046875,
0.015258789,
0.03930664,
0.02355957,
0.03125,
-0.032958984,
0.056640625,
-0.056396484,
0.0146484375,
0.0025634766,
0.006591797,
0.0015563965,
-0.020385742,
0.016723633,
-0.008972168,
-0.024169922,
0.03125,
-0.028808594,
0.040283203,
0.0055236816,
-0.0025787354,
0.067871094,
-0.004119873,
-0.03515625,
0.030517578,
0.0077819824,
-0.026733398,
-0.01953125,
-0.014709473,
-0.045898438,
-0.012268066,
0.022216797,
0.008972168,
0.017211914,
-0.0234375,
-0.017211914,
0.030151367,
-0.0034942627,
0.029174805,
0.05029297,
-0.053222656,
-0.037841797,
0.008117676,
0.014038086,
0.015563965,
-0.060791016,
0.014221191,
-0.028808594,
-0.03955078,
-0.111328125,
0.041992188,
-0.043945312,
0.030273438,
-0.045898438,
-0.014770508,
-0.030395508,
-0.041748047,
0.0011291504,
-0.034423828,
-0.04272461,
0.008300781,
0.014831543,
-0.018798828,
-0.017700195,
-0.014099121,
-0.011169434,
-0.029418945,
0.027832031,
0.010986328,
0.030151367,
-0.021728516,
0.004547119,
-0.034423828,
-0.01977539,
0.047851562,
0.021362305,
0.044433594,
0.06933594,
-0.0046691895,
-0.049560547,
-0.091308594,
0.084472656,
0.015991211,
0.030883789,
0.03112793,
0.041503906,
0.018920898,
0.04663086,
0.0064697266,
0.0058288574,
-0.007873535,
0.016113281,
-0.0058898926,
0.040039062,
0.041748047,
0.04736328,
-0.06591797,
0.07861328,
-0.021850586,
-0.013427734,
0.033447266,
0.013183594,
0.025878906,
0.036376953,
-0.017211914,
0.0067443848,
-0.011291504,
-0.009155273,
0.005554199,
-0.00039863586,
0.08251953,
-0.03491211,
-0.025878906,
-0.037109375,
0.052734375,
-0.008911133,
-0.0390625,
0.021362305,
-0.022949219,
0.029907227,
0.041259766,
0.017211914,
-0.016845703,
0.043701172,
-0.025512695,
-0.020019531,
0.01953125,
-0.008422852,
0.016357422,
-0.044921875,
-0.030761719,
0.029541016,
-0.008422852,
0.01977539,
0.006652832,
0.0031433105,
0.044189453,
0.00793457,
0.02722168,
-0.043701172,
-0.01550293,
-0.0068359375,
-0.033935547,
0.025024414,
-0.038085938,
-0.037353516,
-0.032714844,
0.037841797,
-0.057373047,
-0.017211914,
-0.012878418,
-0.0069274902,
-0.020874023,
-0.037841797,
-0.036132812,
0.033691406,
-0.030273438,
0.033691406,
0.049316406,
-0.02746582,
-0.030761719,
0.03564453,
-0.02746582,
-0.03112793,
-0.00340271,
0.016845703,
0.03515625,
0.009033203,
0.026489258,
0.04663086,
0.0067443848,
0.017944336,
0.008850098,
-0.008544922,
0.0022277832,
-0.030029297,
0.010192871,
-0.021240234,
-0.020385742,
0.008666992,
0.005706787,
-0.02758789,
0.05419922,
0.036132812,
0.032714844,
-0.014526367,
-0.02758789,
0.03466797,
0.05883789,
0.026977539,
0.05102539,
0.052246094,
0.0056152344,
0.009094238,
-0.000579834,
-0.03100586,
-0.017822266,
0.040283203,
-0.011474609,
-0.063964844,
0.026977539,
0.006958008,
-0.009765625,
0.010253906,
-0.007385254,
0.0051574707,
-0.0030670166,
-0.011047363,
-0.017333984,
-0.015991211,
0.026245117,
-0.030639648,
-0.022460938,
0.0059814453,
-0.021240234,
-0.011962891,
0.010925293,
-0.021484375,
0.037353516,
-0.050048828,
-0.08544922,
-0.024658203,
-0.026611328,
0.020385742,
-0.033935547,
0.025390625,
0.0030670166,
-0.008117676,
-0.022338867,
0.024291992,
0.052246094,
-0.059570312,
-0.0138549805,
-0.01940918,
0.05517578,
-0.0006866455,
0.0049743652,
0.07519531,
0.057617188,
0.004425049,
-0.043945312,
-0.029663086,
0.017578125,
0.030029297,
-0.007446289,
-0.030761719,
-0.021484375,
-0.009765625,
0.013671875,
0.012207031,
-0.012878418,
-0.043945312,
-0.020141602,
0.013183594,
0.0074157715,
-0.028686523,
0.025268555,
0.026367188,
0.030395508,
0.041748047,
0.017944336,
0.036376953,
0.010437012,
-0.0625,
0.04296875,
0.0057373047,
0.059570312,
0.072753906,
0.03881836,
-0.0021972656,
-0.027832031,
0.0074157715,
0.00045394897,
-0.003753662,
-0.010070801,
0.008972168,
-0.0051574707,
0.007537842,
-0.0079956055,
-0.03173828,
-0.012451172,
-0.015563965,
0.027709961,
-0.039794922,
-0.016113281,
-0.056396484,
0.016601562,
-0.030395508,
-0.033447266,
0.052001953,
0.001159668,
-0.02368164,
-0.046142578,
0.01977539,
-0.02746582,
-0.038330078,
-0.052734375,
-0.030151367,
-0.030639648,
-0.0043945312,
0.025390625,
0.0048828125,
0.029663086,
0.01928711,
-0.025634766,
-0.022583008,
-0.019165039,
0.026733398,
-0.035888672,
-0.015014648,
-0.0069274902,
-0.005126953,
0.032958984,
0.033203125,
-0.019897461,
-0.038330078,
0.020874023,
0.027954102,
-0.06689453,
-0.0069274902,
-0.0036315918,
-0.025634766,
-0.020507812,
0.017333984,
-0.019165039,
0.04663086,
-0.052734375,
-0.017333984,
0.009338379,
-0.012756348,
-0.007507324,
0.045166016,
0.02722168,
-0.023071289,
-0.019042969,
0.0045166016,
0.017822266,
-0.024291992,
0.030883789,
-0.008361816,
-0.050048828,
-0.026000977,
0.021850586,
0.011413574,
0.0134887695,
0.013000488,
-0.0068359375,
-0.040039062,
0.007446289,
0.020751953,
0.037841797,
-0.03173828,
-0.044921875,
-0.012451172,
0.00032806396,
-0.026123047,
-0.059570312,
-0.028564453,
0.04272461,
0.0064086914,
0.030639648,
0.018188477,
0.016113281,
0.043945312,
0.015991211,
0.020019531,
0.055419922,
-0.016357422,
-0.002166748,
-0.025756836,
0.015625,
-0.020263672,
0.012573242,
0.029296875,
-0.06689453,
0.0062561035,
0.03857422,
-0.010803223,
-0.026245117,
-0.016235352,
-0.04248047,
0.033691406,
0.02746582,
0.024902344,
-0.025878906,
0.046142578,
-0.029541016,
-0.015075684,
0.015991211,
-0.030883789,
0.017700195,
0.03173828,
-0.005126953,
-0.0034484863,
-0.041992188,
-0.01159668,
-0.007293701,
0.04321289,
0.009399414,
-0.017578125,
-0.029418945,
0.06542969,
-0.03125,
0.020019531,
-0.05029297,
0.033447266,
-0.0154418945,
0.041748047,
0.04345703,
0.03515625,
-0.003479004,
0.021484375,
-0.025146484,
4.196167e-05,
0.007659912,
-0.03540039,
-0.012512207,
0.0087890625,
0.041259766,
0.015319824,
-0.018066406,
-0.0018920898,
0.033447266,
-0.01184082,
-0.04345703,
-0.024780273,
0.064453125,
-0.012207031,
-0.036132812,
0.10839844,
-0.016357422,
-0.0047302246,
-0.013793945,
0.018066406,
-0.017700195,
0.01953125,
-0.0027313232,
-0.04272461,
0.01940918,
-0.01586914,
0.024414062,
-0.044433594,
-0.026123047,
0.022094727,
-0.046142578,
0.030761719,
0.017578125,
-0.0028076172,
0.059326172,
0.025512695,
0.025146484,
-0.03125,
0.002319336,
-0.022827148,
0.053710938,
-0.010559082,
0.04345703,
0.005645752,
-0.021972656,
-0.018920898,
-0.040283203,
0.017456055,
0.056884766,
0.01928711,
-0.022827148,
0.012145996,
-0.047851562,
0.021118164,
-0.028930664,
-0.029907227,
-0.030883789,
-0.022827148,
-0.013977051,
0.043701172,
0.007080078,
0.04711914,
-0.010253906,
-0.041015625,
0.009216309,
-0.010986328,
0.04248047,
-0.02758789,
0.025268555,
0.03466797,
0.045166016,
-0.00023365021,
-0.021240234,
-0.016845703,
0.02355957,
0.024536133,
0.036376953,
-0.015014648,
9.393692e-05,
-0.0115356445,
0.033447266,
-0.012145996,
0.007080078,
0.017700195,
-0.014282227,
0.027709961,
0.037353516,
-0.041503906,
0.03149414,
0.041015625,
0.008483887,
0.00579834,
0.034179688,
0.025634766,
-0.038085938,
-0.06591797,
-0.036376953,
0.01171875,
-0.026000977,
0.057861328,
-0.008300781,
0.014282227,
-0.029052734,
0.044433594,
-0.026123047,
0.016601562,
-0.016357422,
0.0024261475,
-0.025268555,
0.05493164,
-0.025756836,
0.02746582,
0.037353516,
0.02734375,
-0.04736328,
-0.012756348,
0.016601562,
0.009765625,
0.013366699,
0.013305664,
0.036621094,
-0.034423828,
0.046875,
-0.0028533936,
0.018310547,
0.05517578,
-0.06591797,
0.042236328,
-0.013305664,
-0.007446289,
0.014343262,
-0.04296875,
-0.038330078,
-0.016235352,
-0.043701172,
0.004180908,
-0.045410156,
-0.009643555,
-0.012939453,
-0.0020141602,
-0.006713867,
-0.03881836,
-0.010559082,
0.036376953,
0.024169922,
-0.01977539,
0.025756836,
-0.010253906,
0.05493164,
0.01965332,
0.012451172,
0.053466797,
-0.0062561035,
0.028076172,
0.024902344,
0.068847656,
0.019897461,
0.01361084,
0.015991211,
0.017089844,
-0.053710938,
-0.056152344,
0.04296875,
-0.0021972656,
-0.05517578,
0.022460938,
-0.041259766,
-0.0234375,
-0.048583984,
-0.029296875,
0.034423828,
0.008056641,
0.011352539,
0.0390625,
0.013366699,
-0.023803711,
-0.03466797,
0.043701172,
0.02746582,
0.051757812,
-0.07128906,
0.0059509277,
0.022827148,
0.013977051,
-0.046142578,
-0.016235352,
0.017089844,
-0.001045227,
-0.014953613,
0.012084961,
-0.0035705566,
0.016845703,
0.0234375,
0.026611328,
-0.033203125,
0.076660156,
0.007873535,
0.03540039,
0.0061950684,
-0.028564453,
-0.03491211,
0.01586914,
-0.015991211,
-0.024780273,
-0.028686523,
0.028076172,
-0.005645752,
-0.043945312,
0.021118164,
-0.0027008057,
0.01550293,
0.031982422,
-0.024780273,
0.025878906,
0.05859375,
-0.0050354004,
0.033691406,
0.044677734,
0.018432617,
-0.007171631,
0.003829956,
-0.047851562,
0.026855469,
-0.005065918,
-0.02722168,
-0.03173828,
-0.0703125,
-0.016967773,
-0.008605957,
0.037353516,
0.03149414,
-0.06347656,
0.031982422,
-0.033691406,
-0.03540039,
0.021728516,
0.07080078,
-0.03491211,
-0.014221191,
0.046142578,
-0.010803223,
0.009094238,
-0.0048217773,
-5.7935715e-05,
0.055664062,
0.025512695,
-0.024291992,
0.04663086,
-0.008300781,
0.056640625,
-0.006713867,
-0.018188477,
0.012268066,
-0.045898438,
-0.051513672,
0.016357422,
-0.049316406,
-0.0020599365,
-0.04345703,
-0.08935547,
-0.056640625,
-0.048828125,
-0.020996094,
0.036376953,
-0.052001953,
0.020629883,
0.048339844,
0.029296875,
0.021728516,
0.028930664,
-0.024169922,
-0.030273438,
-0.036621094,
-0.028808594,
-0.0546875,
6.3478947e-06,
0.03857422,
0.01965332,
-0.016235352,
-0.017089844,
-0.012451172,
0.010498047,
0.025024414,
0.016601562,
0.032958984,
0.0047912598,
-0.011047363,
0.011352539,
-0.0044555664,
0.004211426,
-0.004119873,
0.0045776367,
0.03149414,
-0.025146484,
-0.010070801,
-0.02331543,
0.032714844,
0.018798828,
-0.020751953,
0.06201172,
0.0043029785,
-0.039794922,
0.010131836,
0.048828125,
-0.036621094,
-0.007873535,
-0.029296875,
-0.046142578,
-0.016845703,
0.056640625,
-0.048095703,
0.0051574707,
0.0008087158,
0.00018310547,
-0.019165039,
0.017700195,
-0.032226562,
-0.0047912598,
-0.053710938,
-0.06542969,
0.013427734,
0.004119873,
0.021362305,
-0.0038452148,
-0.008056641,
-0.021606445,
0.00793457,
-0.018798828,
-0.048828125,
0.006958008,
-0.0390625,
-0.044921875,
-0.029052734,
-0.0039367676,
-0.009460449,
0.03149414,
-0.024658203,
-0.007171631,
-0.020751953,
0.010620117,
0.027709961,
-0.012878418,
-0.006134033,
-0.036376953,
0.0234375,
-0.008056641,
-0.029296875,
0.0048217773,
0.053222656,
-0.03857422,
-0.03930664,
-0.041015625,
0.012084961,
-0.025146484,
0.03491211,
-0.041748047,
0.04248047,
-0.0003452301,
-0.018920898,
-0.046142578,
-0.014160156,
0.046875,
-0.022216797,
0.052246094,
-0.026611328,
0.029541016,
-0.016357422,
0.04272461,
-0.018920898,
0.0078125,
-0.018676758,
0.014770508,
-0.016357422,
-0.040527344,
-0.004486084,
-0.018066406,
-0.03100586,
0.033691406,
-0.016113281,
-0.051757812,
0.028320312,
-0.0234375,
0.005126953,
-0.01171875,
0.022216797,
-0.03466797,
0.044433594,
-0.012268066,
0.020263672,
0.016479492,
0.050048828,
-0.059570312,
0.016967773,
-0.010925293,
0.0013809204,
0.026977539,
-0.022460938,
0.034179688,
0.01953125,
0.005706787,
0.036376953,
-0.018188477,
0.041503906,
0.08251953,
0.009521484,
0.005493164,
0.0021820068,
-0.014465332,
0.01965332,
0.0008735657,
0.029418945,
-0.057617188,
0.021972656,
0.008483887,
0.064941406,
0.0013198853,
-0.032714844,
-0.0087890625,
-0.014160156,
0.080566406,
-0.012390137,
0.02746582,
0.0044555664,
-0.029541016,
0.011657715,
-0.010803223,
-0.020874023,
0.0030670166,
0.013549805,
0.0025787354,
-0.022827148,
-0.011291504,
0.018188477,
0.036132812,
0.008178711,
...]]
%% Cell type:markdown id: tags:
Now we define the `RouteLayer`. When called, the route layer will consume text (a query) and output the category (`Route`) it belongs to — to initialize a `RouteLayer` we need our `encoder` model and a list of `routes`.
%% Cell type:code id: tags:
``` python
from semantic_router.layer import RouteLayer
rl = RouteLayer(encoder=encoder, routes=routes)
```
%% Output
2024-05-13 22:26:54 INFO semantic_router.utils.logger local
%% Cell type:markdown id: tags:
We can check the dimensionality of our vectors by looking at the `index` attribute of the `RouteLayer`.
%% Cell type:code id: tags:
``` python
rl.index.index.shape
```
%% Output
(11, 1024)
%% Cell type:markdown id: tags:
We do have 256-dimensional vectors. Now let's test them:
%% Cell type:code id: tags:
``` python
rl("don't you love politics?")
```
%% Output
RouteChoice(name='politics', function_call=None, similarity_score=None)
%% Cell type:code id: tags:
``` python
rl("how's the weather today?")
```
%% Output
RouteChoice(name='chitchat', function_call=None, similarity_score=None)
%% Cell type:markdown id: tags:
Both are classified accurately, what if we send a query that is unrelated to our existing `Route` objects?
%% Cell type:code id: tags:
``` python
rl("How does llama model work?")
```
%% Output
RouteChoice(name=None, function_call=None, similarity_score=None)
%% Cell type:markdown id: tags:
In this case, we return `None` because no matches were identified. We always recommend optimizing your `RouteLayer` for optimal performance, you can see how in [this notebook](https://github.com/aurelio-labs/semantic-router/blob/main/docs/06-threshold-optimization.ipynb).
%% Cell type:markdown id: tags:
---
......
[tool.poetry]
name = "semantic-router"
version = "0.0.40"
version = "0.0.41"
description = "Super fast semantic router for AI decision making"
authors = [
"James Briggs <james@aurelio.ai>",
......
......@@ -4,4 +4,4 @@ from semantic_router.route import Route
__all__ = ["RouteLayer", "HybridRouteLayer", "Route", "LayerConfig"]
__version__ = "0.0.40"
__version__ = "0.0.41"
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