|
| 1 | +import os |
| 2 | + |
| 3 | +from langchain_community.document_loaders import PyMuPDFLoader |
| 4 | +from langchain_core.output_parsers import JsonOutputParser, StrOutputParser |
| 5 | +from langchain_core.prompts import PromptTemplate |
| 6 | +from langchain_core.runnables.utils import Output |
| 7 | +from langchain_ollama import OllamaLLM, ChatOllama |
| 8 | +from langchain_community.embeddings.fastembed import FastEmbedEmbeddings |
| 9 | +from langchain_qdrant import QdrantVectorStore, RetrievalMode |
| 10 | +from langchain_text_splitters import RecursiveCharacterTextSplitter |
| 11 | +from qdrant_client import QdrantClient |
| 12 | +from dotenv import load_dotenv, find_dotenv |
| 13 | +from qdrant_client.http.models import VectorParams, Distance |
| 14 | +from typing import List, Any |
| 15 | +from custom_templates import ( |
| 16 | + retrieval_grader_template, |
| 17 | + hallucination_grading_template, |
| 18 | + answer_generating_template, |
| 19 | + answer_grading_template |
| 20 | +) |
| 21 | + |
| 22 | + |
| 23 | +class LLMasJudge: |
| 24 | + def __init__(self, file_path: str, collection_name: str, qdrant_url: str, qdrant_api_key: str): |
| 25 | + load_dotenv(find_dotenv()) |
| 26 | + self.file_path = file_path |
| 27 | + self.collection_name = collection_name |
| 28 | + self.qdrant_url = qdrant_url |
| 29 | + self.qdrant_api_key = qdrant_api_key |
| 30 | + |
| 31 | + self.model = OllamaLLM(model=os.environ.get("OLLAMA_LLM_MODEL"), base_url=os.environ.get("OLLAMA_BASE_URL")) |
| 32 | + self.embedding = FastEmbedEmbeddings(model=os.environ.get("EMBEDDING_MODEL")) |
| 33 | + self.client = QdrantClient(url=self.qdrant_url, api_key=self.qdrant_api_key) |
| 34 | + # LLM |
| 35 | + self.llm = ChatOllama(model=os.environ.get('OLLAMA_LLM_MODEL'), format="json") |
| 36 | + self.vector_store: QdrantVectorStore = None |
| 37 | + self.documents = self.load_and_split_documents() |
| 38 | + self.setup_qdrant() |
| 39 | + |
| 40 | + def load_and_split_documents(self) -> List[Any]: |
| 41 | + loader = PyMuPDFLoader(file_path=self.file_path) |
| 42 | + text_splitter = RecursiveCharacterTextSplitter(chunk_size=200, chunk_overlap=20) |
| 43 | + return loader.load_and_split(text_splitter=text_splitter) |
| 44 | + |
| 45 | + def setup_qdrant(self): |
| 46 | + if not self.client.collection_exists(collection_name=self.collection_name): |
| 47 | + try: |
| 48 | + self.client.create_collection( |
| 49 | + collection_name=self.collection_name, |
| 50 | + vectors_config={ |
| 51 | + "content": VectorParams(size=384, distance=Distance.COSINE) |
| 52 | + } |
| 53 | + ) |
| 54 | + self.load_data_to_qdrant() |
| 55 | + except Exception as e: |
| 56 | + print(f"Exception: {str(e)}") |
| 57 | + else: |
| 58 | + self.vector_store = QdrantVectorStore.from_existing_collection( |
| 59 | + url=self.qdrant_url, |
| 60 | + api_key=self.qdrant_api_key, |
| 61 | + collection_name=self.collection_name, |
| 62 | + embedding=self.embedding, |
| 63 | + retrieval_mode=RetrievalMode.DENSE, |
| 64 | + vector_name="content" |
| 65 | + ) |
| 66 | + |
| 67 | + def load_data_to_qdrant(self): |
| 68 | + vector_store: QdrantVectorStore = QdrantVectorStore(client=self.client, collection_name=self.collection_name, |
| 69 | + embedding=self.embedding, vector_name="content", |
| 70 | + retrieval_mode=RetrievalMode.DENSE) |
| 71 | + vector_store.add_documents( |
| 72 | + documents=self.documents |
| 73 | + ) |
| 74 | + self.vector_store = vector_store |
| 75 | + |
| 76 | + def retrieval_grader(self, question: str): |
| 77 | + prompt = PromptTemplate( |
| 78 | + template=retrieval_grader_template, |
| 79 | + input_variables=["question", "document"], |
| 80 | + ) |
| 81 | + retrieval_grader = prompt | self.llm | JsonOutputParser() |
| 82 | + docs = self.vector_store.as_retriever().invoke(question) |
| 83 | + doc_txt = docs[1].page_content |
| 84 | + retrieval_grading_response = retrieval_grader.invoke({"question": question, "document": doc_txt}) |
| 85 | + return retrieval_grading_response |
| 86 | + |
| 87 | + def generate(self, question: str) -> Output: |
| 88 | + prompt = PromptTemplate( |
| 89 | + template=answer_generating_template, |
| 90 | + input_variables=["question", "context"] |
| 91 | + ) |
| 92 | + |
| 93 | + # Chain |
| 94 | + rag_chain = prompt | self.llm | StrOutputParser() |
| 95 | + |
| 96 | + # Run |
| 97 | + docs = self.vector_store.as_retriever().invoke(question) |
| 98 | + generation: Output = rag_chain.invoke({"context": docs, "question": question}) |
| 99 | + return generation |
| 100 | + |
| 101 | + def hallucination_grader(self, question: str, generation): |
| 102 | + prompt = PromptTemplate( |
| 103 | + template=hallucination_grading_template, |
| 104 | + input_variables=["generation", "documents"], |
| 105 | + ) |
| 106 | + docs = self.vector_store.as_retriever().invoke(question) |
| 107 | + hallucination_grader = prompt | self.llm | JsonOutputParser() |
| 108 | + hallucination_grading_response = hallucination_grader.invoke({"documents": docs, "generation": generation}) |
| 109 | + return hallucination_grading_response |
| 110 | + |
| 111 | + def answer_grader(self, question: str, generation: str): |
| 112 | + prompt = PromptTemplate( |
| 113 | + template=answer_grading_template, |
| 114 | + input_variables=["generation", "question"] |
| 115 | + ) |
| 116 | + answer_grader = prompt | self.llm | JsonOutputParser() |
| 117 | + answer_grading_response = answer_grader.invoke({"question": question, "generation": generation}) |
| 118 | + return answer_grading_response |
0 commit comments