セマンティックルーターは、LLMとエージェントにとって超高速意思決定レイヤーです。遅いLLM世代がツール使用の決定を下すのを待つのではなく、セマンティックベクタースペースの魔法を使用してそれらの決定を下します。
セマンティックルーターを始めるには、そのようにインストールしてください:
pip install -qU semantic-router
semanticルーターの完全にローカルバージョンの使用を希望する場合は、 HuggingFaceEncoderとLlamaCppLLM ( pip install -qU "semantic-router[local]"を使用できます。こちらを参照)。 HybridRouteLayerを使用するにはpip install -qU "semantic-router[hybrid]"必要があります。
まず、 Routeオブジェクトのセットを定義することから始めます。これらは、セマンティックルーターが使用することを決定できる決定パスです。今のところ2つの簡単なルートを試してみましょう。
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" ,
"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 ]ルートの準備が整いましたが、埋め込み /エンコーダーモデルを初期化します。現在、 CohereEncoderとOpenAIEncoderをサポートしています。より多くのエンコーダーがまもなく追加されます。それらを初期化するために私たちはします:
import os
from semantic_router . encoders import CohereEncoder , OpenAIEncoder
# for Cohere
os . environ [ "COHERE_API_KEY" ] = "<YOUR_API_KEY>"
encoder = CohereEncoder ()
# or for OpenAI
os . environ [ "OPENAI_API_KEY" ] = "<YOUR_API_KEY>"
encoder = OpenAIEncoder () routesとencoderが定義されていると、 RouteLayerが作成されます。ルートレイヤーは、セマンティックの意思決定を処理します。
from semantic_router . layer import RouteLayer
rl = RouteLayer ( encoder = encoder , routes = routes )ルートレイヤーを使用して、ユーザークエリに基づいて非常に高速な決定を下すことができます。ルートの決定をトリガーする2つのクエリを試してみましょう。
rl ( "don't you love politics?" ). name [Out]: 'politics'
正しい決定、別のことを試してみましょう:
rl ( "how's the weather today?" ). name [Out]: 'chitchat'
両方の決定が正しいです!ここで、無関係なクエリを送信してみましょう。
rl ( "I'm interested in learning about llama 2" ). name [Out]:
この場合、私たちが一致していなかったため、決定は行われませんでした。そのため、ルートレイヤーはNone返されませんでした!
セマンティックルーターのエンコーダーには、Cohere、Openai、Hugging Face、Fastembedなどとの使いやすい統合が含まれています。
私たちの発話ベクトル空間は、PineconeおよびQdrantとも統合されています!
| ノート | 説明 |
|---|---|
| 導入 | セマンティックルーターと静的ルートの紹介 |
| 動的ルート | パラメーター生成および関数呼び出しの動的ルート |
| レイヤーを保存/ロードします | ファイルからRouteLayerを保存およびロードする方法 |
| ラングチェーン統合 | セマンティックルーターをラングチェーンエージェントと統合する方法 |
| ローカル実行 | ダイナミックルートを備えた完全なローカルセマンティックルーター - Mistral 7Bなどのローカルモデルは、ほとんどのテストでGPT-3.5を上回ります |
| ルート最適化 | パフォーマンスを最適化するためにルートレイヤーしきい値をトレーニングする方法 |
| マルチモーダルルート | マルチモーダルルートを使用して、シュレックとノットシュレックの写真を識別します |

ollama / gemma2:9bホットライン