用向量嵌入为LLM请求做语义路由
Semantic-Router借助向量嵌入而非LLM生成来做工具调用决策,按语义把请求分配到预设路由。Qdrant可作为其索引后端,用于写入路由数据并执行检索,文中给出安装、配置与完整示例。
## 语义-路透社 [语义-路透社](<https://www.aurelio.ai/semantic-router/>) 是一个为您的 LLMS 和代理程序构建决策层的库。 它使用矢量嵌入来做出工具使用决定, 而不是 LLM 世代, 用语义来引导我们的请求 。 Qdrant 作为支持的索引在语义-路透社中可供您获取路由数据并进行检索。 ## 安装 要使用语义学-路透与 Qdrant,请安装 `qdrant` 额外 : ```console pip install semantic-router[qdrant] ``` ## 使用量 设置 `QdrantIndex` 带有适当配置的: ```python from semantic_router.index import QdrantIndex qdrant_index = QdrantIndex( url="https://xyz-example.eu-central.aws.cloud.qdrant.io", api_key="<your-api-key>" ) ``` Qudrant指数一旦设置了适当的配置,我们就可以把它传递给 `RouteLayer`. ```python from semantic_router.layer import RouteLayer RouteLayer(encoder=some_encoder, routes=some_routes, index=qdrant_index) ``` ## 完整示例 **单击以扩展** ```python 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) ``` 此返回 : ```console [Out]: 'chitchat' ``` ## ++ 进一步阅读 - 语义-路透社 [文档](<https://github.com/aurelio-labs/semantic-router/tree/main/docs>) - 语义-路透社 [视频课程](<https://www.aurelio.ai/course/semantic-router>) - [源代码](<https://github.com/aurelio-labs/semantic-router/blob/main/semantic_router/index/qdrant.py>)
查看原始来源