国产亚洲精品福利在线无卡一,国产精久久一区二区三区,亚洲精品无码国模,精品久久久久久无码专区不卡

當前位置: 首頁 > news >正文

在國外網(wǎng)站做中國旅游推廣百度關(guān)鍵詞熱度排名

在國外網(wǎng)站做中國旅游推廣,百度關(guān)鍵詞熱度排名,房產(chǎn)網(wǎng)站流量排名,商標設(shè)計創(chuàng)意Router有什么用? 在RAG應用中,Router可以幫助我們基于用戶的查詢意圖來決定使用何種數(shù)據(jù)類型或數(shù)據(jù)源,比如是否需要進行語義檢索、是否需要進行text2sql查詢,是否需要用function call來進行API調(diào)用。 Router也可以根據(jù)用戶的查詢…

Router有什么用?

在RAG應用中,Router可以幫助我們基于用戶的查詢意圖來決定使用何種數(shù)據(jù)類型或數(shù)據(jù)源,比如是否需要進行語義檢索、是否需要進行text2sql查詢,是否需要用function call來進行API調(diào)用。

在這里插入圖片描述

Router也可以根據(jù)用戶的查詢意圖來決定使用何種組件類型,比如是否可以用agent來解決用戶的問題,是否需要用到向量數(shù)據(jù)庫來進行上下文信息補充,是否直接用LLM來回答用戶的問題。

在這里插入圖片描述

Route也可以讓我們根據(jù)問題意圖來使用不同的prompt 模板。

在這里插入圖片描述

總之,我們使用Router來幫助我們決定query如何被處理和響應。(注:上面的圖片都來自于參考資料1)

Router如何實現(xiàn)

基于LLM的Router

鑒于LLM強大的語義理解能力,讓LLM作為Router來決定一個query的意圖是什么。

  • LLM comletion router,在prompt中給LLM定義一些選項,讓LLM選擇與問題最相關(guān)的選項。

Llamaindex里的prompt如下:

# single select, LLMSingleSelector類默認使用這個prompt
DEFAULT_SINGLE_SELECT_PROMPT_TMPL = ("Some choices are given below. It is provided in a numbered list ""(1 to {num_choices}), ""where each item in the list corresponds to a summary.\n""---------------------\n""{context_list}""\n---------------------\n""Using only the choices above and not prior knowledge, return ""the choice that is most relevant to the question: '{query_str}'\n"
)# multiple select, LLMMultiSelector類默認使用這個prompt
DEFAULT_MULTI_SELECT_PROMPT_TMPL = ("Some choices are given below. It is provided in a numbered ""list (1 to {num_choices}), ""where each item in the list corresponds to a summary.\n""---------------------\n""{context_list}""\n---------------------\n""Using only the choices above and not prior knowledge, return the top choices ""(no more than {max_outputs}, but only select what is needed) that ""are most relevant to the question: '{query_str}'\n"
)
  • LLM Function router: 利用LLM的function call能力,也就是定義一些處理不同意圖的function,讓LLM將用戶query輸出function調(diào)用參數(shù)。LlamaIndex的pandatic Router 就是基于此來實現(xiàn)的。

Semantic Router

Semantic router: 基于語義相似度的Router。其主要思路是將每一類問題意圖給出一些示例問法后并用embedding編碼,在routing時編碼問題后用語義相似度來決定與問題最相關(guān)的意圖。

semantic-router是一個開源的Semantic router(安裝:pip install -qU semantic-router)。其使用示例如下:

import os
from getpass import getpass
from semantic_router.encoders import CohereEncoder, OpenAIEncoder
from semantic_router import Route
from semantic_router.layer import RouteLayeros.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY") or getpass("Enter OpenAI API Key: "
)## 定義名為politics的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!",],
)
## 定義名為politics的Route,以及其對應的一些描述
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",],
)# 當前route匯總
routes = [politics, chitchat]
# embedding編碼器
encoder = OpenAIEncoder()## 定義Router層,在這一步會將routes中對應的描述(utterances)都向量編碼并存儲下來
rl = RouteLayer(encoder=encoder, routes=routes)## 對查詢做判斷屬于哪一個Route.
## 實現(xiàn)邏輯: 用query與定義好的問句進行相似度計算,返回與query最相似的top 5個預先定義的問法,并對其根據(jù)Route進行分組,取分組分數(shù)之和最大(組內(nèi)分數(shù)的聚合策略可選:sum/max/mean)的Route作為候選Route,如果這些匹配的問法里存在大于指定閾值的問法,則將該Route作為匹配Route返回,否則返回空。
rl("don't you love politics?")
# RouteChoice(name='politics', function_call=None, similarity_score=None)## 對查詢做判斷屬于哪些Route.
## 實現(xiàn)邏輯: 用query與定義好的問句進行相似度計算,返回與query最相似的top 5個預先定義的問法,并對其根據(jù)Route進行分組,返回全部分數(shù)大于指定閾值的組,否則返回空。
rl.retrieve_multiple_routes("Hi! How are you doing in politics??")
#[RouteChoice(name='politics', function_call=None, similarity_score=0.8595844842560181),
# RouteChoice(name='chitchat', function_call=None, similarity_score=0.8356704527362284)]#######
######## dynamic route 可以定義 function_schema, 利用LLM的function call 能力進行函數(shù)解析 #######
from datetime import datetime
from zoneinfo import ZoneInfodef get_time(timezone: str) -> str:"""Finds the current time in a specific timezone.:param timezone: The timezone to find the current time in, shouldbe a valid timezone from the IANA Time Zone Database like"America/New_York" or "Europe/London". Do NOT put the placename itself like "rome", or "new york", you must providethe IANA format.:type timezone: str:return: The current time in the specified timezone."""now = datetime.now(ZoneInfo(timezone))return now.strftime("%H:%M")
from semantic_router.llms.openai import get_schemas_openaischemas = get_schemas_openai([get_time])time_route = Route(name="get_time",utterances=["what is the time in new york city?","what is the time in london?","I live in Rome, what time is it?",],function_schemas=schemas,
)
# 添加新定義的route
rl.add(time_route)
# 因為定義的get_time包含function_schemas,所以會在通過相似度計算得到匹配的route之后,用LLM的function call功能來返回待調(diào)用的函數(shù)參數(shù)
response = rl("what is the time in new york city?")
response
# RouteChoice(name='get_time', function_call=[{'function_name': 'get_time', 'arguments': {'timezone': 'America/New_York'}}], similarity_score=None)### 除了向量比較相似性外,還可以與關(guān)鍵詞比較一起進行混合匹配,將兩者的分數(shù)用alpha分配后相加
import os
from semantic_router.encoders import CohereEncoder, BM25Encoder, TfidfEncoder
from getpass import getpassdense_encoder = CohereEncoder()
# sparse_encoder = BM25Encoder()
sparse_encoder = TfidfEncoder()
from semantic_router.hybrid_layer import HybridRouteLayerdl = HybridRouteLayer(encoder=dense_encoder, sparse_encoder=sparse_encoder, routes=routes
)
dl("don't you love politics?")## RouteLayer的evaluate函數(shù)計算將問題正確分類的準確度
## RouteLayer的fit函數(shù)輸入少量的標注數(shù)據(jù),在max_iter次數(shù)內(nèi),每次隨機為Route選取一個閾值,將準確率最高的閾值作為擬合出的閾值。

基于文本分類的Router

既然Router是在做類似意圖分類的工作,我們可以使用文本分類模型來當Router。如果沒有足夠的語料來訓練的話,可以先選Zero-shot text classfication模型來當做Router, haystack(source code)實現(xiàn)了用zero-shot分類模型來作為Router。

參考資料

  1. blog:Routing in RAG-Driven Applications
  2. LLamaindex router文檔
  3. Langchain router文檔
  4. Haystack router文檔
http://aloenet.com.cn/news/46320.html

相關(guān)文章:

  • 什么專業(yè)可以做網(wǎng)站百度店鋪免費入駐
  • 學校定制網(wǎng)站建設(shè)公司深圳優(yōu)化公司高粱seo較
  • 網(wǎng)站建設(shè)注冊密碼咋弄百度一下百度百科
  • 做網(wǎng)頁靠哪個網(wǎng)站賺錢湖南競價優(yōu)化哪家好
  • 服務器租用網(wǎng)站搜索關(guān)鍵詞排名優(yōu)化軟件
  • 網(wǎng)站 申請百度在線客服中心
  • 邗江區(qū)城鄉(xiāng)建設(shè)局網(wǎng)站數(shù)據(jù)分析軟件
  • 如何在本地安裝部署 wordpress深度優(yōu)化
  • 順德企業(yè)手機網(wǎng)站建設(shè)廣州seo推廣優(yōu)化
  • 好搜客網(wǎng)站優(yōu)勢的seo網(wǎng)站優(yōu)化排名
  • 眾云網(wǎng)聯(lián)做的網(wǎng)站效果好嗎網(wǎng)絡營銷外包公司
  • 茂易網(wǎng)站建設(shè)鏈接購買平臺
  • 淮安企業(yè)網(wǎng)站搜一搜
  • wordpress拿站清遠新聞最新消息
  • b2b平臺選亞馬遜企業(yè)購網(wǎng)站優(yōu)化排名的方法
  • 上海百度關(guān)鍵詞推廣名片seo什么意思
  • 做企業(yè)網(wǎng)站需要做什么seo托管
  • 廈門外貿(mào)網(wǎng)站建設(shè)公司惠州seo排名收費
  • 黃村網(wǎng)站建設(shè)價格臨沂做網(wǎng)絡優(yōu)化的公司
  • 網(wǎng)站源碼素材視頻專用客戶端app
  • 怎么開網(wǎng)店無貨源店鋪山東公司網(wǎng)站推廣優(yōu)化
  • 怎樣給網(wǎng)站做排名優(yōu)化網(wǎng)絡加速器
  • 事業(yè)單位做網(wǎng)站需要前置審批嗎如何進行網(wǎng)站性能優(yōu)化
  • 免費做翻頁頁面的網(wǎng)站微信crm管理系統(tǒng)
  • 知名網(wǎng)站建設(shè)制作平臺app開發(fā)制作
  • 騰訊網(wǎng)站建設(shè)外鏈生成網(wǎng)站
  • 青島專業(yè)網(wǎng)站設(shè)計的公司百度首頁優(yōu)化
  • 怎樣做企業(yè)手機網(wǎng)站工具站seo
  • 上傳wordpress網(wǎng)站如何制作一個網(wǎng)頁
  • 長沙商城網(wǎng)站網(wǎng)絡營銷策略都有哪些