mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-13 21:47:12 +00:00
langchain[patch], templates[patch]: fix multi query retriever, web re… (#17434)
…search retriever Fixes #17352
This commit is contained in:
@@ -1,7 +1,3 @@
|
||||
from typing import List
|
||||
|
||||
from langchain.chains import LLMChain
|
||||
from langchain.output_parsers import PydanticOutputParser
|
||||
from langchain.retrievers.multi_query import MultiQueryRetriever
|
||||
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
||||
from langchain_community.chat_models import ChatOllama, ChatOpenAI
|
||||
@@ -10,7 +6,7 @@ from langchain_community.embeddings import OpenAIEmbeddings
|
||||
from langchain_community.vectorstores import Chroma
|
||||
from langchain_core.output_parsers import StrOutputParser
|
||||
from langchain_core.prompts import ChatPromptTemplate, PromptTemplate
|
||||
from langchain_core.pydantic_v1 import BaseModel, Field
|
||||
from langchain_core.pydantic_v1 import BaseModel
|
||||
from langchain_core.runnables import RunnableParallel, RunnablePassthrough
|
||||
|
||||
# Load
|
||||
@@ -29,23 +25,6 @@ vectorstore = Chroma.from_documents(
|
||||
)
|
||||
|
||||
|
||||
# Output parser will split the LLM result into a list of queries
|
||||
class LineList(BaseModel):
|
||||
# "lines" is the key (attribute name) of the parsed output
|
||||
lines: List[str] = Field(description="Lines of text")
|
||||
|
||||
|
||||
class LineListOutputParser(PydanticOutputParser):
|
||||
def __init__(self) -> None:
|
||||
super().__init__(pydantic_object=LineList)
|
||||
|
||||
def parse(self, text: str) -> LineList:
|
||||
lines = text.strip().split("\n")
|
||||
return LineList(lines=lines)
|
||||
|
||||
|
||||
output_parser = LineListOutputParser()
|
||||
|
||||
QUERY_PROMPT = PromptTemplate(
|
||||
input_variables=["question"],
|
||||
template="""You are an AI language model assistant. Your task is to generate five
|
||||
@@ -60,12 +39,9 @@ QUERY_PROMPT = PromptTemplate(
|
||||
ollama_llm = "zephyr"
|
||||
llm = ChatOllama(model=ollama_llm)
|
||||
|
||||
# Chain
|
||||
llm_chain = LLMChain(llm=llm, prompt=QUERY_PROMPT, output_parser=output_parser)
|
||||
|
||||
# Run
|
||||
retriever = MultiQueryRetriever(
|
||||
retriever=vectorstore.as_retriever(), llm_chain=llm_chain, parser_key="lines"
|
||||
retriever = MultiQueryRetriever.from_llm(
|
||||
vectorstore.as_retriever(), llm, prompt=QUERY_PROMPT
|
||||
) # "lines" is the key (attribute name) of the parsed output
|
||||
|
||||
# RAG prompt
|
||||
|
Reference in New Issue
Block a user