Semantic-Router#

Semantic-Router - библиотека для создания слоев принятия решений для больших языковых моделей (LLM) и агентов. Она использует векторные вложения для принятия решений о применении инструментов вместо генераций LLM, направляя запросы по семантическому значению.

Vector DB доступен в качестве поддерживаемого индекса в Semantic-Router, чтобы загружать маршрутные данные и выполнять выборки.

Установка#

Чтобы использовать Semantic-Router с Vector DB, установите дополнительный пакет qdrant:

pip install semantic-router[qdrant]

Использование#

Настройте QdrantIndex с соответствующими конфигурациями:

from semantic_router.index import QdrantIndex

qdrant_index = QdrantIndex(
    url="https://xyz-example.eu-central.aws.cloud.qdrant.io", api_key="<your-api-key>"
)

После того, как индекс Vector DB настроен с подходящими конфигурациями, можно передать его в RouteLayer.

from semantic_router.layer import RouteLayer

RouteLayer(encoder=some_encoder, routes=some_routes, index=qdrant_index)

Пример#

import os

from semantic_router import Route
from semantic_router.encoders import OpenAIEncoder
from semantic_router.index import QdrantIndex
from semantic_router.layer import RouteLayer

# we could use this as a guide for our chatbot to avoid political conversations
politics = Route(
    name="politics value",
    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]

os.environ["OPENAI_API_KEY"] = "<YOUR_API_KEY>"
encoder = OpenAIEncoder()

rl = RouteLayer(
    encoder=encoder,
    routes=routes,
    index=QdrantIndex(location=":memory:"),
)

print(rl("What have you been upto?").name)

Это возвращает:

[Out]: 'chitchat'

Дополнительная документация#