Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Adaptive RAG 🤹‍♂️

Adaptive RAG introduces a RAG technique that combines query analysis with self-corrective RAG.

For Query Analysis, it uses a small classifier(LLM), to decide the query’s complexity. Query Analysis guides adjustment between different retrieval strategies: No retrieval, Single-shot RAG or Iterative RAG.

Official Paper

agent-based-rag
Adaptive-RAG:Source

Official Implementation

Here’s a code snippet for query analysis:

fromlangchain_core.promptsimportChatPromptTemplatefromlangchain_core.pydantic_v1importBaseModel,Fieldfromlangchain_openaiimportChatOpenAIclassRouteQuery(BaseModel):"""Route a user query to the most relevant datasource."""datasource:Literal["vectorstore","web_search"]=Field(...,description="Given a user question choose to route it to web search or a vectorstore.",)# LLM with function callllm=ChatOpenAI(model="gpt-3.5-turbo-0125",temperature=0)structured_llm_router=llm.with_structured_output(RouteQuery)

The following example defines and queries a retriever:

# add documents in LanceDBvectorstore=LanceDB.from_documents(documents=doc_splits,embedding=OpenAIEmbeddings(),)retriever=vectorstore.as_retriever()# query using defined retrieverquestion="How adaptive RAG works"docs=retriever.get_relevant_documents(question)

[8]ページ先頭

©2009-2025 Movatter.jp