File size: 2,260 Bytes
ea08e05
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Load libraries and dependencies
from pypdf import PdfReader
from langchain.text_splitter import CharacterTextSplitter
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.embeddings.huggingface import HuggingFaceInstructEmbeddings
from langchain.vectorstores import FAISS
from langchain.chat_models import ChatOpenAI
from langchain.memory import ConversationBufferMemory
from langchain.chains import ConversationalRetrievalChain

# Transform a list of PDFs into a single string
def get_pdf_text(pdf_documents):
    # Initialize line of text
    text = ""
    # Append text extracted from the documents into the text string
    for pdf in pdf_documents:
        pdf_reader = PdfReader(pdf, strict=True)    
        for page in pdf_reader.pages:
            text += page.extract_text()
    return text

# Transform a single line of text into an array of text chunks
def get_text_chunks(raw_text, separator="\n", chunk_size=1000, chunk_overlap=200, lenght_function=len):
    # Initialize TextSplitter with default variables
    text_splitter = CharacterTextSplitter(
        separator=separator,
        chunk_size=chunk_size,
        chunk_overlap=chunk_overlap,
        length_function=lenght_function
    )
    # Create list of text chunks
    return text_splitter.split_text(raw_text)

# Initialize embeddings
def init_embeddings(type=1):
    # Choose embeding depending on the project's necessities
    if type == 1:
        # OpenAI Embeddings
        return OpenAIEmbeddings()
    else:
        # Instructor Embeddings
        return HuggingFaceInstructEmbeddings(model_name="hkunlp/instructor-xl")

# Initialize Conversation Chain
def get_conversation_chain(chunks, embeddings):
    # Create Vector Database from text chunks and embeddings
    knowledge_base = FAISS.from_texts(chunks, embeddings).as_retriever()
    # Create buffer to store the conversation memory
    memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
    # Initialize language model
    language_model = ChatOpenAI()
    # Create conversation chain
    conversation_chain = ConversationalRetrievalChain.from_llm(
        llm=language_model,
        retriever=knowledge_base,
        memory=memory
    )
    return conversation_chain