Semantic-Router

Semantic-Router 是一个用于为您的 LLM 和代理构建决策层的库。它使用向量嵌入来做出工具使用决策,而不是依赖 LLM 生成,通过语义意义来路由请求。

Qdrant 作为 Semantic-Router 支持的索引之一,可用于摄取路由数据并执行检索。

安装

要将 Semantic-Router 与 Qdrant 一起使用,请安装 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>"
)

设置好 Qdrant 索引并配置适当后,我们可以将其传递给 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'

📚 延伸阅读

本页面是否有用?

感谢您的反馈! 🙏

听到这个消息我们很抱歉。😔 您可以在 GitHub 上编辑此页面,或创建 GitHub 问题。