File size: 3,565 Bytes
8596832
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff71e02
ebddcd4
8596832
 
 
 
 
 
 
 
 
ff71e02
 
 
 
 
 
 
8596832
 
 
ebddcd4
 
 
8596832
 
 
ebddcd4
8596832
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import os
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM 
from transformers import pipeline
import torch 
import textwrap 
from PyPDF2 import PdfReader 
from typing_extensions import Concatenate
from langchain.text_splitter import CharacterTextSplitter 
from langchain_community.embeddings import HuggingFaceInstructEmbeddings 
from langchain.vectorstores import Chroma 
from langchain.llms import HuggingFacePipeline
from langchain.chains import RetrievalQA
from langchain.chains.conversation.memory import ConversationBufferMemory
from langchain import PromptTemplate
from dotenv import load_dotenv

load_dotenv()  # take environment variables from .env
os.environ["LANGCHAIN_API_KEY"] = str(os.getenv("LANGCHAIN_API_KEY"))
os.environ["LANGCHAIN_TRACING_V2"] = "true"
os.environ["LANGCHAIN_ENDPOINT"] = "https://api.smith.langchain.com"
os.environ["LANGCHAIN_PROJECT"] = "2.pdf_chat_router_issue_assistant"

from dotenv import load_dotenv
load_dotenv()  # take environment variables from .env (especially openai api key)

# Create LLM model
# model = "C:/Users/arasu/Workspace/Projects/GenAI/models/MBZUAILaMini-Flan-T5-248M/"
model = "MBZUAI/LaMini-Flan-T5-248M"
tokenizer = AutoTokenizer.from_pretrained(model,truncation=True)
base_model = AutoModelForSeq2SeqLM.from_pretrained(model)
pipe = pipeline(
        'text2text-generation',
        model = base_model,
        tokenizer = tokenizer,
        max_length = 256,
        do_sample = True,
        temperature = 0.3,
        top_p= 0.95
    )
llm = HuggingFacePipeline(pipeline=pipe)

# # Initialize instructor embeddings using the Hugging Face model
# instructor_embeddings = HuggingFaceInstructEmbeddings(model_name="C:/Users/arasu/Workspace/Projects/GenAI/embeddings/hkunlp_instructor-large")
instructor_embeddings = HuggingFaceInstructEmbeddings(model_name="hkunlp/instructor-large")
# db_path = "vector_db"
vector_db = ""
def create_vector_db():
    # Load data from pdf
    raw_text = ""
    text_splitter = CharacterTextSplitter(
        separator = "\n",
        chunk_size = 500,
        chunk_overlap  = 100,
        length_function = len,
    )
    from PyPDF2 import PdfReader
    pdf = PdfReader("employment-agreement2018.pdf")
    raw_text = ""
    for i, page in enumerate(pdf.pages):
        content = page.extract_text()
        if content:
            raw_text += content
    texts = text_splitter.split_text(raw_text)

    # Create a  vector database from 'text'
    vector_db = Chroma.from_texts(texts,instructor_embeddings)
    # vector_db.persist()
    # vector_db = None 

def get_qa_chain():
    # Load the vector database from the local folder
    # vector_db = Chroma(persist_directory=db_path, embedding_function = instructor_embeddings)

    # Create a retriever for querying the vector database
    retriever = vector_db.as_retriever(search_kwargs={"k":3})

    template = """
    You are friendly customer care assistant trying to help user on the context provided.\
    if the question contains greetings then greet the user back. be friendly.\
    if the answer is not found in the context then reply "No Evidence Found".\
    context: {context}
    question: {question}
    """
    prompt = PromptTemplate(template=template, input_variables=["context", "question"])

    chain_type_kwargs = {"prompt": prompt}
    qa = RetrievalQA.from_chain_type(
        llm = llm,
        chain_type = "stuff",
        retriever = retriever,
        input_key="query",
        return_source_documents=True,
        chain_type_kwargs=chain_type_kwargs
    )
    return qa