mirror of
https://github.com/hwchase17/langchain.git
synced 2025-07-05 20:58:25 +00:00
langchain[patch], templates[patch]: fix multi query retriever, web re… (#17434)
…search retriever Fixes #17352
This commit is contained in:
parent
c0ce93236a
commit
3925071dd6
@ -1,39 +1,28 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
import logging
|
import logging
|
||||||
from typing import List, Sequence
|
from typing import List, Optional, Sequence
|
||||||
|
|
||||||
from langchain_core.callbacks import (
|
from langchain_core.callbacks import (
|
||||||
AsyncCallbackManagerForRetrieverRun,
|
AsyncCallbackManagerForRetrieverRun,
|
||||||
CallbackManagerForRetrieverRun,
|
CallbackManagerForRetrieverRun,
|
||||||
)
|
)
|
||||||
from langchain_core.documents import Document
|
from langchain_core.documents import Document
|
||||||
from langchain_core.language_models import BaseLLM
|
from langchain_core.language_models import BaseLanguageModel
|
||||||
|
from langchain_core.output_parsers import BaseOutputParser
|
||||||
from langchain_core.prompts.prompt import PromptTemplate
|
from langchain_core.prompts.prompt import PromptTemplate
|
||||||
from langchain_core.pydantic_v1 import BaseModel, Field
|
|
||||||
from langchain_core.retrievers import BaseRetriever
|
from langchain_core.retrievers import BaseRetriever
|
||||||
|
|
||||||
from langchain.chains.llm import LLMChain
|
from langchain.chains.llm import LLMChain
|
||||||
from langchain.output_parsers.pydantic import PydanticOutputParser
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class LineList(BaseModel):
|
class LineListOutputParser(BaseOutputParser[List[str]]):
|
||||||
"""List of lines."""
|
|
||||||
|
|
||||||
lines: List[str] = Field(description="Lines of text")
|
|
||||||
"""List of lines."""
|
|
||||||
|
|
||||||
|
|
||||||
class LineListOutputParser(PydanticOutputParser):
|
|
||||||
"""Output parser for a list of lines."""
|
"""Output parser for a list of lines."""
|
||||||
|
|
||||||
def __init__(self) -> None:
|
def parse(self, text: str) -> List[str]:
|
||||||
super().__init__(pydantic_object=LineList)
|
|
||||||
|
|
||||||
def parse(self, text: str) -> LineList:
|
|
||||||
lines = text.strip().split("\n")
|
lines = text.strip().split("\n")
|
||||||
return LineList(lines=lines)
|
return lines
|
||||||
|
|
||||||
|
|
||||||
# Default prompt
|
# Default prompt
|
||||||
@ -63,6 +52,7 @@ class MultiQueryRetriever(BaseRetriever):
|
|||||||
llm_chain: LLMChain
|
llm_chain: LLMChain
|
||||||
verbose: bool = True
|
verbose: bool = True
|
||||||
parser_key: str = "lines"
|
parser_key: str = "lines"
|
||||||
|
"""DEPRECATED. parser_key is no longer used and should not be specified."""
|
||||||
include_original: bool = False
|
include_original: bool = False
|
||||||
"""Whether to include the original query in the list of generated queries."""
|
"""Whether to include the original query in the list of generated queries."""
|
||||||
|
|
||||||
@ -70,9 +60,9 @@ class MultiQueryRetriever(BaseRetriever):
|
|||||||
def from_llm(
|
def from_llm(
|
||||||
cls,
|
cls,
|
||||||
retriever: BaseRetriever,
|
retriever: BaseRetriever,
|
||||||
llm: BaseLLM,
|
llm: BaseLanguageModel,
|
||||||
prompt: PromptTemplate = DEFAULT_QUERY_PROMPT,
|
prompt: PromptTemplate = DEFAULT_QUERY_PROMPT,
|
||||||
parser_key: str = "lines",
|
parser_key: Optional[str] = None,
|
||||||
include_original: bool = False,
|
include_original: bool = False,
|
||||||
) -> "MultiQueryRetriever":
|
) -> "MultiQueryRetriever":
|
||||||
"""Initialize from llm using default template.
|
"""Initialize from llm using default template.
|
||||||
@ -91,7 +81,6 @@ class MultiQueryRetriever(BaseRetriever):
|
|||||||
return cls(
|
return cls(
|
||||||
retriever=retriever,
|
retriever=retriever,
|
||||||
llm_chain=llm_chain,
|
llm_chain=llm_chain,
|
||||||
parser_key=parser_key,
|
|
||||||
include_original=include_original,
|
include_original=include_original,
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -129,7 +118,7 @@ class MultiQueryRetriever(BaseRetriever):
|
|||||||
response = await self.llm_chain.acall(
|
response = await self.llm_chain.acall(
|
||||||
inputs={"question": question}, callbacks=run_manager.get_child()
|
inputs={"question": question}, callbacks=run_manager.get_child()
|
||||||
)
|
)
|
||||||
lines = getattr(response["text"], self.parser_key, [])
|
lines = response["text"]
|
||||||
if self.verbose:
|
if self.verbose:
|
||||||
logger.info(f"Generated queries: {lines}")
|
logger.info(f"Generated queries: {lines}")
|
||||||
return lines
|
return lines
|
||||||
@ -189,7 +178,7 @@ class MultiQueryRetriever(BaseRetriever):
|
|||||||
response = self.llm_chain(
|
response = self.llm_chain(
|
||||||
{"question": question}, callbacks=run_manager.get_child()
|
{"question": question}, callbacks=run_manager.get_child()
|
||||||
)
|
)
|
||||||
lines = getattr(response["text"], self.parser_key, [])
|
lines = response["text"]
|
||||||
if self.verbose:
|
if self.verbose:
|
||||||
logger.info(f"Generated queries: {lines}")
|
logger.info(f"Generated queries: {lines}")
|
||||||
return lines
|
return lines
|
||||||
|
@ -12,6 +12,7 @@ from langchain_core.callbacks import (
|
|||||||
)
|
)
|
||||||
from langchain_core.documents import Document
|
from langchain_core.documents import Document
|
||||||
from langchain_core.language_models import BaseLLM
|
from langchain_core.language_models import BaseLLM
|
||||||
|
from langchain_core.output_parsers import BaseOutputParser
|
||||||
from langchain_core.prompts import BasePromptTemplate, PromptTemplate
|
from langchain_core.prompts import BasePromptTemplate, PromptTemplate
|
||||||
from langchain_core.pydantic_v1 import BaseModel, Field
|
from langchain_core.pydantic_v1 import BaseModel, Field
|
||||||
from langchain_core.retrievers import BaseRetriever
|
from langchain_core.retrievers import BaseRetriever
|
||||||
@ -19,7 +20,6 @@ from langchain_core.vectorstores import VectorStore
|
|||||||
|
|
||||||
from langchain.chains import LLMChain
|
from langchain.chains import LLMChain
|
||||||
from langchain.chains.prompt_selector import ConditionalPromptSelector
|
from langchain.chains.prompt_selector import ConditionalPromptSelector
|
||||||
from langchain.output_parsers.pydantic import PydanticOutputParser
|
|
||||||
from langchain.text_splitter import RecursiveCharacterTextSplitter, TextSplitter
|
from langchain.text_splitter import RecursiveCharacterTextSplitter, TextSplitter
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
@ -50,21 +50,12 @@ should have a question mark at the end: {question}""",
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class LineList(BaseModel):
|
class QuestionListOutputParser(BaseOutputParser[List[str]]):
|
||||||
"""List of questions."""
|
|
||||||
|
|
||||||
lines: List[str] = Field(description="Questions")
|
|
||||||
|
|
||||||
|
|
||||||
class QuestionListOutputParser(PydanticOutputParser):
|
|
||||||
"""Output parser for a list of numbered questions."""
|
"""Output parser for a list of numbered questions."""
|
||||||
|
|
||||||
def __init__(self) -> None:
|
def parse(self, text: str) -> List[str]:
|
||||||
super().__init__(pydantic_object=LineList)
|
|
||||||
|
|
||||||
def parse(self, text: str) -> LineList:
|
|
||||||
lines = re.findall(r"\d+\..*?(?:\n|$)", text)
|
lines = re.findall(r"\d+\..*?(?:\n|$)", text)
|
||||||
return LineList(lines=lines)
|
return lines
|
||||||
|
|
||||||
|
|
||||||
class WebResearchRetriever(BaseRetriever):
|
class WebResearchRetriever(BaseRetriever):
|
||||||
@ -176,7 +167,7 @@ class WebResearchRetriever(BaseRetriever):
|
|||||||
logger.info("Generating questions for Google Search ...")
|
logger.info("Generating questions for Google Search ...")
|
||||||
result = self.llm_chain({"question": query})
|
result = self.llm_chain({"question": query})
|
||||||
logger.info(f"Questions for Google Search (raw): {result}")
|
logger.info(f"Questions for Google Search (raw): {result}")
|
||||||
questions = getattr(result["text"], "lines", [])
|
questions = result["text"]
|
||||||
logger.info(f"Questions for Google Search: {questions}")
|
logger.info(f"Questions for Google Search: {questions}")
|
||||||
|
|
||||||
# Get urls
|
# Get urls
|
||||||
|
@ -33,4 +33,4 @@ from langchain.retrievers.web_research import QuestionListOutputParser
|
|||||||
def test_list_output_parser(text: str, expected: List[str]) -> None:
|
def test_list_output_parser(text: str, expected: List[str]) -> None:
|
||||||
parser = QuestionListOutputParser()
|
parser = QuestionListOutputParser()
|
||||||
result = parser.parse(text)
|
result = parser.parse(text)
|
||||||
assert result.lines == expected
|
assert result == expected
|
||||||
|
@ -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.retrievers.multi_query import MultiQueryRetriever
|
||||||
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
||||||
from langchain_community.chat_models import ChatOllama, ChatOpenAI
|
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_community.vectorstores import Chroma
|
||||||
from langchain_core.output_parsers import StrOutputParser
|
from langchain_core.output_parsers import StrOutputParser
|
||||||
from langchain_core.prompts import ChatPromptTemplate, PromptTemplate
|
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
|
from langchain_core.runnables import RunnableParallel, RunnablePassthrough
|
||||||
|
|
||||||
# Load
|
# 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(
|
QUERY_PROMPT = PromptTemplate(
|
||||||
input_variables=["question"],
|
input_variables=["question"],
|
||||||
template="""You are an AI language model assistant. Your task is to generate five
|
template="""You are an AI language model assistant. Your task is to generate five
|
||||||
@ -60,12 +39,9 @@ QUERY_PROMPT = PromptTemplate(
|
|||||||
ollama_llm = "zephyr"
|
ollama_llm = "zephyr"
|
||||||
llm = ChatOllama(model=ollama_llm)
|
llm = ChatOllama(model=ollama_llm)
|
||||||
|
|
||||||
# Chain
|
|
||||||
llm_chain = LLMChain(llm=llm, prompt=QUERY_PROMPT, output_parser=output_parser)
|
|
||||||
|
|
||||||
# Run
|
# Run
|
||||||
retriever = MultiQueryRetriever(
|
retriever = MultiQueryRetriever.from_llm(
|
||||||
retriever=vectorstore.as_retriever(), llm_chain=llm_chain, parser_key="lines"
|
vectorstore.as_retriever(), llm, prompt=QUERY_PROMPT
|
||||||
) # "lines" is the key (attribute name) of the parsed output
|
) # "lines" is the key (attribute name) of the parsed output
|
||||||
|
|
||||||
# RAG prompt
|
# RAG prompt
|
||||||
|
Loading…
Reference in New Issue
Block a user