File size: 9,306 Bytes
a1ece96
 
 
 
 
 
 
 
 
 
 
981d69e
 
a1ece96
b507438
a1ece96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b507438
 
 
 
 
 
 
 
 
 
 
981d69e
a1ece96
 
 
981d69e
a1ece96
 
 
 
 
 
 
 
981d69e
a1ece96
981d69e
 
a1ece96
 
 
 
 
 
 
 
 
981d69e
a1ece96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
981d69e
 
a1ece96
 
 
 
 
981d69e
a1ece96
 
981d69e
a1ece96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
981d69e
a1ece96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
981d69e
 
 
a1ece96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
981d69e
a1ece96
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
from langchain_community.document_loaders import DirectoryLoader, PyPDFLoader, Docx2txtLoader
from pathlib import Path
from langchain_community.embeddings import HuggingFaceInferenceAPIEmbeddings
from langchain_community.vectorstores import Chroma
from itertools import combinations
import numpy as np
from langchain.memory import ConversationBufferMemory
from langchain.prompts import PromptTemplate
from langchain.chains import RetrievalQA

from langchain_community.llms import  HuggingFaceEndpoint
import gradio as gr

import os
import zipfile
from dotenv import load_dotenv
# from llama.api import HuggingFaceEndpoint
load_dotenv()


LOCAL_VECTOR_STORE_DIR = Path('./data')


def langchain_document_loader(TMP_DIR):
    """
    Load documents from the temporary directory (TMP_DIR). 
    Files can be in txt, pdf, CSV or docx format.
    """

    documents = []

    # txt_loader = DirectoryLoader(
    #     TMP_DIR.as_posix(), glob="**/*.txt", loader_cls=TextLoader, show_progress=True
    # )
    # documents.extend(txt_loader.load())

    pdf_loader = DirectoryLoader(
        TMP_DIR.as_posix(), glob="**/*.pdf", loader_cls=PyPDFLoader, show_progress=True
    )
    documents.extend(pdf_loader.load())

    # csv_loader = DirectoryLoader(
    #     TMP_DIR.as_posix(), glob="**/*.csv", loader_cls=CSVLoader, show_progress=True,
    #     loader_kwargs={"encoding":"utf8"}
    # )
    # documents.extend(csv_loader.load())

    doc_loader = DirectoryLoader(
        TMP_DIR.as_posix(),
        glob="**/*.docx",
        loader_cls=Docx2txtLoader,
        show_progress=True,
    )
    documents.extend(doc_loader.load())
    return documents

zip_file_path = 'course reviews.zip'

# Get the directory of the zip file
current_dir = os.getcwd()

print(current_dir)

# Extract the zip file in the same directory
with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:
    zip_ref.extractall(current_dir)


directory_path = 'course reviews'
TMP_DIR = Path(directory_path)
documents = langchain_document_loader(TMP_DIR)

HUGGING_FACE_API_KEY = os.getenv("HUGGING_FACE_API_KEY") # Using our secret API key from the .env file
def select_embedding_model():
    # embedding = OllamaEmbeddings(model='nomic-embed-text')
    embedding = HuggingFaceInferenceAPIEmbeddings(
            api_key=HUGGING_FACE_API_KEY,
            model_name="sentence-transformers/all-MiniLM-L6-v2" #This is the embedding model
        )
    return embedding

embeddings = select_embedding_model() # Calling the function to select the model


def create_vectorstore(embeddings,documents,vectorstore_name):
    """Create a Chroma vector database."""
    persist_directory = (LOCAL_VECTOR_STORE_DIR.as_posix() + "/" + vectorstore_name)
    vector_store = Chroma.from_documents(
        documents=documents,
        embedding=embeddings,
        persist_directory=persist_directory
    )
    return vector_store


create_vectorstores = True # change to True to create vectorstores

if create_vectorstores:
    vector_store = create_vectorstore(embeddings,documents,"vector_store")
    print("Vector store created")
    print("")
    

    
vector_store = Chroma(persist_directory = LOCAL_VECTOR_STORE_DIR.as_posix() + "/vector_store", 
                            embedding_function=embeddings)
print("vector_store:",vector_store._collection.count(),"chunks.")

        
def Vectorstore_backed_retriever(vectorstore,search_type="mmr",k=6,score_threshold=None):
    """create a vectorsore-backed retriever
    Parameters: 
        search_type: Defines the type of search that the Retriever should perform.
            Can be "similarity" (default), "mmr", or "similarity_score_threshold"
        k: number of documents to return (Default: 4) 
        score_threshold: Minimum relevance threshold for similarity_score_threshold (default=None)
    """
    search_kwargs={}
    if k is not None:
        search_kwargs['k'] = k
    if score_threshold is not None:
        search_kwargs['score_threshold'] = score_threshold

    retriever = vectorstore.as_retriever(
        search_type=search_type,
        search_kwargs=search_kwargs
    )
    return retriever


# Similarity search
retriever = Vectorstore_backed_retriever(vector_store,search_type="similarity",k=4)



def instantiate_LLM(api_key,temperature=0.5,top_p=0.95,model_name=None):
    """Instantiate LLM in Langchain.
    Parameters:
        LLM_provider (str): the LLM provider; in ["OpenAI","Google","HuggingFace"]
        model_name (str): in ["gpt-3.5-turbo", "gpt-3.5-turbo-0125", "gpt-4-turbo-preview", 
            "gemini-pro", "mistralai/Mistral-7B-Instruct-v0.2"].            
        api_key (str): google_api_key or openai_api_key or huggingfacehub_api_token 
        temperature (float): Range: 0.0 - 1.0; default = 0.5
        top_p (float): : Range: 0.0 - 1.0; default = 1.
    """
    
  
    llm = HuggingFaceEndpoint(
        # repo_id = "openai-community/gpt2-large",
        # repo_id = "google/gemma-2b-it", 
        repo_id="mistralai/Mistral-7B-Instruct-v0.2",          # working
        # repo_id = "NexaAIDev/Octopus-v4",
        # repo_id="Snowflake/snowflake-arctic-instruct",
        # repo_id="apple/OpenELM-3B-Instruct",                 # erros: remote trust something
        # repo_id="meta-llama/Meta-Llama-3-8B-Instruct",       # Takes too long
        # repo_id="mistralai/Mixtral-8x22B-Instruct-v0.1",     # RAM insufficient
        # repo_id=model_name,
        huggingfacehub_api_token=api_key,
        # model_kwargs={
        #     "temperature":temperature,
        #     "top_p": top_p,
        #     "do_sample": True,
        #     "max_new_tokens":1024
        # },
        # model_kwargs={stop: "Human:", "stop_sequence": "Human:"},
        
        stop_sequences = ["Human:"],
        temperature=temperature,
        top_p=top_p,
        do_sample=True,
        max_new_tokens=1024,
        trust_remote_code=True
    )
    return llm

# get the API key from .env file
llm = instantiate_LLM(api_key=HUGGING_FACE_API_KEY)



def create_memory():
    """Creates a ConversationSummaryBufferMemory for our model
    Creates a ConversationBufferWindowMemory for our models."""
    
    memory = ConversationBufferMemory(
        memory_key="history",
        input_key="question",
        return_messages=True,
        k=3
    )

    return memory

memory = create_memory()


memory.save_context(
    {"question": "What can you do?"},
    {"output": "I can answer queries based on the past reviews and course outlines of various courses offered at LUMS."}
)

context_qa = """
You are a professional chatbot assistant for helping students at LUMS regarding course selection.

Please follow the following rules:

1. Answer the question in your own words from the context given to you.
2. If you don't know the answer, don't try to make up an answer.
3. If you don't have a course's review or outline, just say that you do not know about this course.
4. If a user enters a course code (e.g. ECON100 or CS370), match it with reviews with that course code. If the user enters a course name (e.g. Introduction to Economics or Database Systems), match it with reviews with that course name.
5. If you do not have information of a course, do not make up a course or suggest courses from universities other than LUMS.

Context: {context}

You are having a converation with a student at LUMS.

Chat History: {history}

Human: {question}

Assistant:
"""

prompt = PromptTemplate(
    input_variables=["history", "context", "question"],
    template=context_qa
)


qa = RetrievalQA.from_chain_type(
    llm=llm,
    retriever=retriever,
    verbose=False,
    return_source_documents=False,
    chain_type_kwargs={
        "prompt": prompt,
        "memory": memory
    },
)


# Global list to store chat history
chat_history = []

def print_documents(docs,search_with_score=False):
    """helper function to print documents."""
    if search_with_score:
        # used for similarity_search_with_score
        print(
            f"\n{'-' * 100}\n".join(
                [f"Document {i+1}:\n\n" + doc[0].page_content +"\n\nscore:"+str(round(doc[-1],3))+"\n" 
                 for i, doc in enumerate(docs)]
            )
        )
    else:
        # used for similarity_search or max_marginal_relevance_search
        print(
            f"\n{'-' * 100}\n".join(
                [f"Document {i+1}:\n\n" + doc.page_content 
                 for i, doc in enumerate(docs)]
            )
        )  

def rag_model(query):
    # Your RAG model code here
    result = qa({'query': query})

    relevant_docs = retriever.get_relevant_documents(query)
    print_documents(relevant_docs)
    # Extract the answer from the result
    answer = result['result']
    # print(result)


    # Append the query and answer to the chat history
    chat_history.append(f'User: {query}\nAssistant: {answer}\n')

    # Join the chat history into a string
    chat_string = '\n'.join(chat_history)

    return chat_string

# This is for Gradio interface
gradio_app = gr.Interface(fn=rag_model, inputs="text", outputs="text", title="RAGs to Riches", theme=gr.themes.Soft(), description="This is a RAG model that can answer queries based on the past reviews and course outlines of various courses offered at LUMS.")

if __name__ == "__main__":
    gradio_app.launch()