[PYTHON/LANGCHAIN] MultiQueryRetriever 클래스 : invoke 메소드 사용하기 2
■ MultiQueryRetriever 클래스의 invoke 메소드를 사용하는 방법을 보여준다. 2 ※ OPENAI_API_KEY 환경 변수 값은 .env 파일에 정의한다. ▶ main.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
from dotenv import load_dotenv from typing import List from langchain_community.document_loaders import WebBaseLoader from langchain_text_splitters import RecursiveCharacterTextSplitter from langchain_openai import OpenAIEmbeddings from langchain_chroma import Chroma from langchain_core.prompts import PromptTemplate from langchain_openai import ChatOpenAI from langchain_core.output_parsers import BaseOutputParser from langchain.retrievers.multi_query import MultiQueryRetriever load_dotenv() webBaseLoader = WebBaseLoader("https://lilianweng.github.io/posts/2023-06-23-agent/") documentList = webBaseLoader.load() recursiveCharacterTextSplitter = RecursiveCharacterTextSplitter(chunk_size = 500, chunk_overlap = 0) splitdocumentList = recursiveCharacterTextSplitter.split_documents(documentList) openAIEmbeddings = OpenAIEmbeddings() chroma = Chroma.from_documents(documents = splitdocumentList, embedding = openAIEmbeddings) promptTemplate = PromptTemplate( input_variables = ["question"], template = """You are an AI language model assistant. Your task is to generate five different versions of the given user question to retrieve relevant documents from a vector database. By generating multiple perspectives on the user question, your goal is to help the user overcome some of the limitations of the distance-based similarity search. Provide these alternative questions separated by newlines. Original question : {question}""" ) chatOpenAI = ChatOpenAI(temperature = 0) class LineListOutputParser(BaseOutputParser[List[str]]): """Output parser for a list of lines.""" def parse(self, text : str) -> List[str]: lineList = text.strip().split("\n") return list(filter(None, lineList)) lineListOutputParser = LineListOutputParser() runnableSequence = promptTemplate | chatOpenAI | lineListOutputParser rultiQueryRetriever = MultiQueryRetriever(retriever = chroma.as_retriever(), llm_chain = runnableSequence, parser_key = "lines") responseDocumentList = rultiQueryRetriever.invoke("What does the course say about regression?") print(len(responseDocumentList)) |
▶ requirements.txt