ArunSamespace commited on
Commit
9921884
1 Parent(s): f34fc63

Upload 8 files

Browse files
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ indexs/text-embedding-ada-002/index.faiss filter=lfs diff=lfs merge=lfs -text
app.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import time
3
+
4
+ import gradio as gr
5
+ import pandas as pd
6
+ from model import Model
7
+ from tqdm import tqdm
8
+
9
+ tqdm.pandas()
10
+
11
+ OUTPUT_FILE = "./results_qa.csv"
12
+
13
+ def new_vote(data: gr.LikeData, question, model_name, **kwargs):
14
+ feedback = "Good" if data.liked else "Bad"
15
+ df = pd.read_csv(OUTPUT_FILE)
16
+ df['Feedback'] = df.apply(lambda x: feedback if (x.Model == model_name and x.Question == question) else None, axis = 1)
17
+ df.to_csv(OUTPUT_FILE, index=False)
18
+
19
+ # def answer_question(question: str, model_name: str, system_prompt: str):
20
+ # start_time = time.time()
21
+ # qa_model = Model(model_name=model_name)
22
+ # response, sources = qa_model.run(system_prompt=system_prompt, query=question)
23
+ # time_taken = time.time() - start_time
24
+ # words = len(question) + len(response)
25
+ # efficiency = words / time_taken
26
+ # final_response = f"{response} \n\nTime Taken: {time_taken}"
27
+ # new_row = {'Model': model_name, 'Question': question, 'Answer': response, "Sources": sources, "Time": time_taken, "Words": words, "Efficiency": efficiency, "Feedback": None, "final_response": final_response}
28
+ # if os.path.isfile(OUTPUT_FILE):
29
+ # df = pd.read_csv(OUTPUT_FILE)
30
+ # rows = df.values.tolist()
31
+ # # print("df.values.tolist(): ", df.values.tolist())
32
+ # # df = df.append(new_row, ignore_index=True)
33
+ # rows.append(new_row)
34
+ # else:
35
+ # rows = [new_row]
36
+ # df = pd.DataFrame(rows)
37
+ # df.to_csv(OUTPUT_FILE, index=False)
38
+ # yield [(question, final_response)]
39
+
40
+ def answer_question(question: str, model_name: str, system_prompt: str):
41
+ start_time = time.time()
42
+ qa_model = Model(model_name=model_name)
43
+ gen_response = qa_model.run(system_prompt=system_prompt, query=question)
44
+ response = ""
45
+ for resp in gen_response:
46
+ if isinstance(resp, list):
47
+ sources = resp
48
+ break
49
+ resp = resp.replace("$", "₹")
50
+ response += resp
51
+ yield [(question, response)], OUTPUT_FILE
52
+
53
+ time_taken = time.time() - start_time
54
+ words = len(question) + len(response)
55
+ efficiency = words / time_taken
56
+ temp_sources = "\n".join([f"{i + 1}. {d}" for i, d in enumerate(sources)])
57
+ final_response = f"{response} \n\nSources: \n{temp_sources} \n\nTime Taken: {time_taken}"
58
+ new_row = {'Model': model_name, 'Question': question, 'Answer': response, "Sources": sources, "Time": time_taken, "Words": words, "Efficiency": efficiency, "Feedback": None, "final_response": final_response}
59
+ if os.path.isfile(OUTPUT_FILE):
60
+ try:
61
+ df = pd.read_csv(OUTPUT_FILE)
62
+ rows = df.to_dict(orient="records")
63
+ rows.append(new_row)
64
+ except Exception:
65
+ rows = [new_row]
66
+ else:
67
+ rows = [new_row]
68
+
69
+ df = pd.DataFrame(rows)
70
+ df.to_csv(OUTPUT_FILE, index=False)
71
+ final_response = final_response.strip("Question").strip("\n")
72
+ final_response = final_response.strip("\n").strip(" ").strip("Answer:").strip("Question").strip("\n").replace("Answer:", "")
73
+ yield [(question, final_response)], OUTPUT_FILE
74
+
75
+
76
+
77
+ if __name__ == "__main__":
78
+ with gr.Blocks() as demo:
79
+ chatbot = gr.Chatbot()
80
+
81
+ # with gr.Row():
82
+
83
+ textbox = gr.Textbox(label="Query")
84
+ # system_prompt = """Answer the question using the context. Provide examples only from the context and use only Rupees (₹) in examples. If you don't know the answer, just say 'Please rephrase the question I am unable to answer'"""
85
+ system_prompt = """"Answer the question using the context. Provide examples only from the context and use only Rupees (₹) in examples. If you don't know the answer, just say 'Please rephrase the question I am unable to answer'"""
86
+ system_prompt = "Use the following pieces of book to answer the question at the end. \nIf you don't know the answer, please think rationally and answer from the book"
87
+ system_prompt = """Answer the question using the context. Provide examples only from the context and use only Rupees (₹) in examples. If you don't know the answer, just say 'Please rephrase the question I am unable to answer'"""
88
+ system_prompt = """Answer the question from the book. Provide examples only from the book. If you don't know the answer, just say 'Please rephrase the question'"""
89
+
90
+ choices=["gpt4", "gpt-3.5-turbo"]
91
+
92
+ system_prompt = gr.Textbox(value=system_prompt, label="System Prompt")
93
+ model_name = gr.Dropdown(choices=choices, value="gpt-3.5-turbo", label="Model")
94
+ file = gr.File(value = OUTPUT_FILE, file_types=["csv"], label="Output")
95
+ textbox.submit(answer_question, [textbox, model_name, system_prompt], [chatbot, file])
96
+ chatbot.like(new_vote, [textbox, model_name], None)
97
+
98
+ demo.queue()
99
+ demo.launch(share=True)
embedder.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import List
2
+
3
+ import requests
4
+ from langchain.pydantic_v1 import BaseModel
5
+ from langchain.schema.embeddings import Embeddings
6
+ from retry import retry
7
+ from tqdm import tqdm
8
+
9
+
10
+ # @dataclass
11
+ class CustomEmbeddings(BaseModel, Embeddings):
12
+ """Wrapper around OpenAI embedding models.
13
+
14
+ To use, you should have the ``openai`` python package installed, and the
15
+ environment variable ``OPENAI_API_KEY`` set with your API key or pass it
16
+ as a named parameter to the constructor.
17
+
18
+ Example:
19
+ .. code-block:: python
20
+
21
+ from langchain.embeddings import OpenAIEmbeddings
22
+ openai = OpenAIEmbeddings(model_name="davinci", openai_api_key="my-api-key")
23
+ """
24
+ model: str = ""
25
+ model_url: str = ""
26
+ api_key: str = "EMPTY"
27
+ # engine: str = None
28
+ # api_type: str = None
29
+
30
+ def _embedding_func(self, text: str) -> List[float]:
31
+ """Call out to OpenAI's embedding endpoint."""
32
+ # replace newlines, which can negatively affect performance.
33
+ text = text.replace("\n", " ")
34
+ result = self.api_call(input_text=text)
35
+ return result['data'][0]['embedding']
36
+
37
+ @retry(tries=3, delay=2, backoff=2, exceptions=(requests.RequestException,))
38
+ def api_call(self, input_text: str):
39
+ data = {
40
+ "input": input_text,
41
+ "model": self.model
42
+ }
43
+
44
+ response = requests.post(
45
+ self.model_url,
46
+ headers={
47
+ "Content-Type": "application/json",
48
+ # "Authorization": f"Bearer {self.api_key}",
49
+ "api-key": self.api_key
50
+ },
51
+ json=data
52
+ )
53
+
54
+ if response.status_code == 200:
55
+ return response.json()
56
+ else:
57
+ response.raise_for_status()
58
+
59
+ def embed_documents(self, texts: List[str]) -> List[List[float]]:
60
+ """Call out to OpenAI's embedding endpoint for embedding search docs.
61
+
62
+ Args:
63
+ texts: The list of texts to embed.
64
+
65
+ Returns:
66
+ List of embeddings, one for each text.
67
+ """
68
+ return [self._embedding_func(text) for text in tqdm(texts)]
69
+
70
+ def embed_query(self, text: str) -> List[float]:
71
+ """Call out to OpenAI's embedding endpoint for embedding query text.
72
+
73
+ Args:
74
+ text: The text to embed.
75
+
76
+ Returns:
77
+ Embeddings for the text.
78
+ """
79
+ return self._embedding_func(text)
80
+
indexs/text-embedding-ada-002/index.faiss ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e0d252dde59cab3da1aa892c4c430aadd9ac0bc16b3e27595d6806997690580f
3
+ size 4497453
indexs/text-embedding-ada-002/index.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:586a91e52cce6dd1750160eec565a24222617a8187ae8145899e7abba5b44daf
3
+ size 2602597
model.py ADDED
@@ -0,0 +1,205 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ os.environ["GOOGLE_API_KEY"] = "AIzaSyAGoYnNPu__70AId7EJS7F_61i69Qmn-wM"
4
+ os.environ["OPENAI_API_TYPE"] = "azure"
5
+ # os.environ["OPENAI_API_VERSION"] = "2023-07-01-preview"
6
+ # # os.environ["OPENAI_API_KEY"] = "5b624f6b71884a488560a86b1fffbf42"
7
+ # os.environ["OPENAI_API_KEY"] = "9e337d6696ce4a22a9a1b901e2ebb5fb"
8
+
9
+
10
+ from embedder import CustomEmbeddings
11
+ from langchain.chat_models import AzureChatOpenAI, ChatOpenAI
12
+ from langchain.prompts.chat import (ChatPromptTemplate,
13
+ HumanMessagePromptTemplate,
14
+ SystemMessagePromptTemplate)
15
+ from langchain_google_genai import ChatGoogleGenerativeAI
16
+ from search import SimilaritySearch
17
+
18
+ embeddings = CustomEmbeddings(
19
+ model="text-embedding-ada-002",
20
+ model_url="https://year-embedding-ada-002-aiservices-2136192926.openai.azure.com//openai/deployments/fresh-embedding-ada-002/embeddings?api-version=2023-10-01-preview",
21
+ api_key="6eed3006cdd3445cb3f422a7358ce461"
22
+ )
23
+ vector_store = SimilaritySearch.load_from_disk(
24
+ embedding_function=embeddings,
25
+ data_dir="../indexs/text-embedding-ada-002/"
26
+ # data_dir="../indexs/basic-fno-text-embedding-ada-002/"
27
+ )
28
+
29
+ class Model:
30
+ def __init__(self, model_name: str, **kwargs) -> None:
31
+ self.model_name = model_name
32
+ self.llm = self.load_llm(model_name=model_name, **kwargs)
33
+
34
+ def load_llm(self, model_name: str, **kwargs):
35
+ if self.model_name == "gemini-pro":
36
+ self.retriever = vector_store.as_retriever(search_kwargs={"k": 2}, search_type="similarity")
37
+ return ChatGoogleGenerativeAI(model=model_name, temperature=0, max_tokens=4096)
38
+ elif self.model_name == "gpt-3.5-turbo":
39
+ self.retriever = vector_store.as_retriever(search_kwargs={"k": 2}, search_type="similarity")
40
+ return AzureChatOpenAI(
41
+ deployment_name="latest-gpt-35-turbo-16k",
42
+ temperature=0,
43
+ max_tokens=4096,
44
+ # azure_endpoint="https://high-gpt4-32k-0613-aiservices336365459.openai.azure.com/",
45
+ openai_api_key="9e337d6696ce4a22a9a1b901e2ebb5fb",
46
+ # openai_api_base="https://jan-2024-gpt35-turbo16k-aiservices800630185.openai.azure.com/",
47
+ openai_api_base = "https://fresh-gpt35-turbo-aiservices-2112150452.openai.azure.com/",
48
+ openai_api_version="2023-07-01-preview"
49
+ )
50
+ elif self.model_name == "gpt4":
51
+ self.retriever = vector_store.as_retriever(search_kwargs={"k": kwargs.get("k", 2)}, search_type="similarity")
52
+ return AzureChatOpenAI(
53
+ deployment_name="gpt-4-32k",
54
+ temperature=0,
55
+ max_tokens=4096,
56
+ # azure_endpoint="https://high-gpt4-32k-0613-aiservices336365459.openai.azure.com/",
57
+ openai_api_key="e91a341abb2f4646ab7b0acd3b9d461e",
58
+ openai_api_base="https://jan-2024-gpt4-ai-aiservices-1959882301.openai.azure.com/",
59
+ openai_api_version="2023-07-01-preview"
60
+ )
61
+
62
+ self.retriever = vector_store.as_retriever(search_kwargs={"k": kwargs.get("k", 1)}, search_type="similarity")
63
+ return ChatOpenAI(
64
+ model=model_name,
65
+ openai_api_key="EMPTY",
66
+ openai_api_base="http://localhost:8000/v1",
67
+ max_tokens=1024,
68
+ temperature=0,
69
+ model_kwargs={"stop": ["<|im_end|>", "Query:", "Question:"], "top_p": 0.95}
70
+ )
71
+
72
+
73
+ def run_qa_result(self, query: str):
74
+ support_docs = self.retriever.get_relevant_documents(query)
75
+ sources = list({d.metadata['source'] for d in support_docs})
76
+ context = "\n\n".join([f"{i + 1}. {d.page_content}" for i, d in enumerate(support_docs)])
77
+ return context, sources
78
+
79
+ def return_prompt(self, system_prompt: str, query: str, context: str):
80
+
81
+ # human_template = "Context:\n\n{context}\n\nQuery: {query}"
82
+ # human_template = "E-Book:\n\n{context}\n\nQuestion: {query}"
83
+
84
+ human_template = "\n\nContext:\n\n{context}\n\nQuestion: {query}"
85
+ # human_template = "\n\nBook:\n\n{context}\n\nQuestion: {query}"
86
+
87
+ messages = []
88
+ if self.model_name in [
89
+ "gemini-pro",
90
+ "TheBloke/Mistral-7B-Instruct-v0.2-AWQ",
91
+ ]:
92
+ human_template = system_prompt + "\n\n" + human_template
93
+ human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)
94
+ messages.append(human_message_prompt)
95
+ else:
96
+ system_message_prompt = SystemMessagePromptTemplate.from_template(system_prompt)
97
+ human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)
98
+ messages.extend([system_message_prompt, human_message_prompt])
99
+
100
+ chat_prompt = ChatPromptTemplate.from_messages(messages)
101
+ return chat_prompt.format_prompt(context=context, query=query).to_messages()
102
+
103
+ def run(self, system_prompt: str, query: str):
104
+ context, sources = self.run_qa_result(query=query)
105
+ chat_prompt = self.return_prompt(system_prompt=system_prompt, query=query, context=context)
106
+ # text = "".join(resp.content for resp in self.llm.stream(chat_prompt))
107
+ # text += "\nSources: \n" + "\n".join([f"{i + 1}. {d}" for i, d in enumerate(sources)])
108
+ # return text, sources
109
+ for resp in self.llm.stream(chat_prompt):
110
+ yield resp.content.replace("$", "₹")
111
+
112
+ yield sources
113
+ # text = "".join(resp.content for resp in self.llm.stream(chat_prompt))
114
+ # text += "\nSources: \n" + "\n".join([f"{i + 1}. {d}" for i, d in enumerate(sources)])
115
+ # return text, sources
116
+
117
+ def get_sources(query):
118
+ results = vector_store.similarity_search_with_relevance_scores(query, k=2)
119
+ return [
120
+ {
121
+ "score": r[-1],
122
+ "source": r[0].metadata['source']
123
+ }
124
+ for r in results
125
+ ]
126
+
127
+ if __name__ == "__main__":
128
+ # model = Model(model_name="phi2")
129
+ # model = Model(model_name="gpt-3.5-turbo")
130
+ # model = Model(model_name="gemini-pro")
131
+ # model = Model(model_name="TheBloke/zephyr-7B-beta-AWQ")
132
+ # model = Model(model_name="TheBloke/neural-chat-7B-v3-3-AWQ")
133
+ model = Model(model_name="TheBloke/Mistral-7B-Instruct-v0.2-AWQ")
134
+ model = Model(model_name="gpt4")
135
+ model = Model(model_name="gpt-3.5-turbo")
136
+
137
+ # query = "what is reliance?"
138
+ # print("results: ", get_sources(query))
139
+
140
+ # query = "explain FNO trading?"
141
+ # print("results: ", get_sources(query))
142
+
143
+ # query="What is FNO trading?"
144
+ # query = "Describe ITM, ATM and OTM"
145
+ # query = "give formula to calculate intrinsic value in Put and provide an example"
146
+ # query = "what is the order of delta, theta, gamma and vega amongst options in a given options chain"
147
+ # query = "Explain apple stock and nasdaq"
148
+
149
+ # query = "generate a table with long and short in F&O instruments"
150
+ # query = "how can we calculate intrinsic value and time value"
151
+ # query = "give formula to calculate intrinsic value in Put"
152
+
153
+ query = "explain exit from a put trade"
154
+ #
155
+ # query = "what will be buying cost if I long tesla CE"
156
+
157
+
158
+ # system_prompt="""Use the following pieces of context to answer the question in detail. Provide example only if it is in provided context and make sure to use them in rupees.""",
159
+
160
+ # system_prompt = """Use the following pieces of context to answer the question in detail. Provide example only if it is in context and make sure to use them in ₹.
161
+ # If you don't know the answer, just say 'Please rephrase the question I am unable to answer'"""
162
+
163
+ # system_prompt = """Answer the question using the context. Provide examples only from the context and use only Rupees (₹) in examples. If you don't know the answer, just say 'Please rephrase the question I am unable to answer'"""
164
+
165
+ # system_prompt = """Your task is to answer the question using the given context.
166
+
167
+ # Follow the below rules while answering the question:
168
+ # - Only create example using the context
169
+ # - Use only Rupees '₹' to represent currency.
170
+ # - If you don't know the answer, just say 'Please rephrase the question I am unable to answer'"""
171
+
172
+ # system_prompt = """You are an Indian Stock Market Assistant. Your task is to answer the question using the given context. Only create example from the given context and don't use '$'."""
173
+
174
+ # query = "what is reliance?"
175
+ # query = "what is python?"
176
+ query = "what is an apple stock and nasdq"
177
+ query = "Generate a tabular format on playing long and short through options"
178
+ query = "What is FNO Trading?"
179
+
180
+ system_prompt = """Answer the question only from context.
181
+ Provide examples only from the context.
182
+ If you don't know the answer, just say 'Please rephrase the question I am unable to answer'"""
183
+
184
+ system_prompt = "Answer the question only from the e-book. If it is not sufficient then respond as \"Unknown\""
185
+ system_prompt = """Use the following pieces of book to answer the question at the end. \nIf you don't know the answer, please think rationally and answer from the book"""
186
+ # system_prompt = """Answer the question using the context. Provide examples only from the context and use only Rupees (₹) in examples. If you don't know the answer, just say 'Please rephrase the question I am unable to answer'"""
187
+
188
+ # system_prompt = """Answer the question from the context. Provide examples only from the context. If you don't know the answer, just say 'Please rephrase the question'"""
189
+ # system_prompt = """Answer the question from the book. Provide examples only from the book. If you don't know the answer, just say 'Please rephrase the question'"""
190
+
191
+ response = model.run(
192
+ system_prompt=system_prompt,
193
+ query=query
194
+ )
195
+ text = ""
196
+ for resp in response:
197
+ if isinstance(resp, list):
198
+ sources = resp
199
+ break
200
+ text += resp
201
+
202
+ text = text.split("Question")[0].strip("\n")
203
+
204
+ print("text: ", text)
205
+ open("./text.txt", "w").write(text)
requirements.txt ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ langchain==0.0.353
2
+ langchain-community==0.0.7
3
+ langchain-core==0.1.4
4
+ langchain-google-genai==0.0.5
5
+ gradio==4.12.0
6
+ gradio_client==0.8.0
7
+ tqdm==4.66.1
8
+ faiss-cpu==1.7.4
9
+ pandas==2.2.0
10
+ numpy==1.26.4
results_qa.csv ADDED
File without changes
search.py ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pickle
2
+ import uuid
3
+ from typing import Any, Callable, List, Optional
4
+
5
+ import faiss
6
+ import numpy as np
7
+ from langchain.docstore.document import Document
8
+ from langchain.docstore.in_memory import InMemoryDocstore
9
+ from langchain.embeddings.base import Embeddings
10
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
11
+ from langchain.vectorstores import FAISS
12
+ from tqdm import tqdm
13
+
14
+
15
+ def return_on_failure(value):
16
+ def decorate(f):
17
+ def applicator(*args, **kwargs):
18
+ try:
19
+ return f(*args,**kwargs)
20
+ except Exception as e:
21
+ print(f'Error "{e}" in {f.__name__}')
22
+ return value
23
+
24
+ return applicator
25
+
26
+ return decorate
27
+
28
+
29
+ class SimilaritySearch(FAISS):
30
+
31
+ @classmethod
32
+ @return_on_failure(None)
33
+ def load_from_disk(cls, embedding_function: Callable, data_dir: str = None):
34
+ docstore, index_to_docstore_id = pickle.load(open(f"{data_dir}/index.pkl", "rb"))
35
+ index_cpu = faiss.read_index(f"{data_dir}/index.faiss")
36
+
37
+ # index_gpu = faiss.index_cpu_to_gpu(GPU_RESOURCE, 0, index_cpu)
38
+ # vector_store = FAISS(embedding_function, index_gpu, docstore, index_to_docstore_id)
39
+
40
+ return FAISS(embedding_function, index_cpu, docstore, index_to_docstore_id)
41
+
42
+ @classmethod
43
+ def __from(
44
+ cls,
45
+ texts: List[str],
46
+ embeddings: List[List[float]],
47
+ embedding: Embeddings,
48
+ metadatas: Optional[List[dict]] = None,
49
+ **kwargs: Any,
50
+ ) -> FAISS:
51
+ print("embeddings: ", len(embeddings), len(texts), len(metadatas))
52
+ index = faiss.IndexFlatIP(len(embeddings[0]))
53
+ index.add(np.array(embeddings, dtype=np.float32))
54
+ documents = []
55
+ for i, text in tqdm(enumerate(texts), total=len(texts)):
56
+ metadata = metadatas[i] if metadatas else {}
57
+ documents.append(Document(page_content=text, metadata=metadata))
58
+ index_to_id = {i: str(uuid.uuid4()) for i in range(len(documents))}
59
+ docstore = InMemoryDocstore(
60
+ {index_to_id[i]: doc for i, doc in enumerate(documents)}
61
+ )
62
+ return cls(embedding.embed_query, index, docstore, index_to_id, **kwargs)
63
+
64
+ @classmethod
65
+ def from_texts(
66
+ cls,
67
+ texts: List[str],
68
+ embedding: Embeddings,
69
+ metadatas: Optional[List[dict]] = None,
70
+ ids: Optional[List[str]] = None,
71
+ **kwargs: Any,
72
+ ) -> FAISS:
73
+ """Construct FAISS wrapper from raw documents.
74
+
75
+ This is a user friendly interface that:
76
+ 1. Embeds documents.
77
+ 2. Creates an in memory docstore
78
+ 3. Initializes the FAISS database
79
+
80
+ This is intended to be a quick way to get started.
81
+
82
+ Example:
83
+ .. code-block:: python
84
+
85
+ from langchain import FAISS
86
+ from langchain.embeddings import OpenAIEmbeddings
87
+ embeddings = OpenAIEmbeddings()
88
+ faiss = FAISS.from_texts(texts, embeddings)
89
+ """
90
+ # embeddings = embedding.embed_documents(texts)
91
+ final_texts, final_metadatas = [], []
92
+ embeddings = []
93
+ for i, text in tqdm(enumerate(texts), total=len(texts)):
94
+ try:
95
+ embeddings.append(embedding._embedding_func(text))
96
+ final_texts.append(text)
97
+ if len(metadatas) > 0:
98
+ final_metadatas.append(metadatas[i])
99
+ except Exception as e:
100
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=4096, chunk_overlap=128)
101
+ splitted_texts = text_splitter.split_text(text)
102
+ embeddings.extend(embedding.embed_documents(splitted_texts))
103
+ final_texts.extend(splitted_texts)
104
+ final_metadatas.extend([metadatas[i]] * len(splitted_texts))
105
+
106
+ return cls.__from(
107
+ final_texts,
108
+ embeddings,
109
+ embedding,
110
+ metadatas=final_metadatas,
111
+ # ids=ids,
112
+ **kwargs,
113
+ )