-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_loading.py
More file actions
116 lines (98 loc) · 4.43 KB
/
db_loading.py
File metadata and controls
116 lines (98 loc) · 4.43 KB
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
from langchain import PromptTemplate, LLMChain
from langchain.llms import GPT4All
from langchain.callbacks.base import CallbackManager
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
from langchain.document_loaders import TextLoader # Function for loading only TXT files
from langchain.text_splitter import RecursiveCharacterTextSplitter # Text splitter for create chunks
from langchain.document_loaders import UnstructuredPDFLoader # to be able to load the pdf files
from langchain.document_loaders import PyMuPDFLoader
from langchain.document_loaders import DirectoryLoader
from langchain.indexes import VectorstoreIndexCreator # Vector Store Index to create our database about our knowledge
from langchain.embeddings import HuggingFaceEmbeddings # LLamaCpp embeddings from the Alpaca model
from langchain.vectorstores.faiss import FAISS # FAISS library for similarity search
import os # For interaction with the files
import datetime
import gpt4all
# model_path = 'C:/Users/Ivan/AppData/Local/nomic.ai/GPT4All'
model_path = 'C:/Users/Ivan/AppData/Local/nomic.ai/GPT4All/gpt4all-converted.bin'
language = "en"
# model = gpt4all.GPT4All("gpt4all-converted.bin", model_path, model_type='llama', allow_download=True)
# model.generate("Once upon a time, ", streaming = True)
# Callback manager for handling the calls with the model
callback_manager = CallbackManager([StreamingStdOutCallbackHandler()])
# Create the embedding object
embeddings = HuggingFaceEmbeddings(model_name = 'sentence-transformers/all-MiniLM-L6-v2')
# Create the GPT4ALL llm object
llm = GPT4All(model = model_path, callback_manager=callback_manager, verbose = True)
# Split text
def split_chunks(sources):
chunks = []
splitter = RecursiveCharacterTextSplitter(chunk_size = 256, chunk_overlap = 32)
for chunk in splitter.split_documents(sources):
chunks.append(chunk)
return chunks
def create_index(chunks):
texts = [doc.page_content for doc in chunks]
metadatas = [doc.metadata for doc in chunks]
search_index = FAISS.from_texts(texts, embeddings, metadatas = metadatas)
return search_index
def similarity_search(query, index):
# k is the number of similarity searched that matches the query
# default is 4
matched_docs = index.similarity_search(query, k = 3)
sources = []
for doc in matched_docs:
sources.append(
{
"page_content": doc.page_content,
"metadata": doc.metadata,
}
)
return matched_docs, sources
# Load our local index vector db
index = FAISS.load_local("my_faiss_index", embeddings)
# Create the prompt template
template = """
Please use the following context to answer questions.
Context: {context}
---
Question: {question}
Here my answer: """
############# INPUT ############################################################
question = "How to build an innovation process?" # Hardcoded question
# question = input("Enter your question: ") # User Input Question
################################################################################
################# HARD CODED QUESTION WITHOUT GPT ##############################
# docs = index.similarity_search(question)
# Get the matches best 3 results - defined in the function k=3
# print(f"The question is: {question}")
# print("Here the result of the semantic search on the index, without GPT4All...")
# print(docs[0])
################################################################################
# Creating the context
matched_docs, sources = similarity_search(question, index)
context = "\n".join([doc.page_content for doc in matched_docs])
# instantiating the prompt template and the GPT4ALL chain
prompt = PromptTemplate(template = template, input_variables = ["context", "question"]).partial(context = context)
llm_chain = LLMChain(prompt = prompt, llm = llm)
# Print the result
LLM_reply = llm_chain.run(question)
print(LLM_reply)
# Write full context and answer in a file
with open("LLM_reply.txt", "w", encoding = "utf-8") as txt:
txt.write(LLM_reply)
txt.close()
content = []
linenum = 0
substr = "Here my answer:".lower()
with open('LLM_reply.txt', 'rt') as myfile:
lines = myfile.readlines()
for line in lines:
if (line.lower().find(substr) != -1) or (linenum != 0):
linenum += 1
content.append(line)
LLM_reply_content = ' '.join(content)
print(LLM_reply_content)
file = open("LLM_reply_content.txt","w")
file.write(LLM_reply_content)
file.close()