Spaces:
Runtime error
Runtime error
Update processing.py
Browse files- processing.py +24 -2
processing.py
CHANGED
@@ -5,28 +5,50 @@ from langchain_community.vectorstores import FAISS
|
|
5 |
from llm_loader import load_model
|
6 |
from config import openai_api_key
|
7 |
from langchain.chains import RetrievalQA
|
|
|
8 |
import os
|
9 |
import json
|
10 |
|
|
|
11 |
embedding_model = OpenAIEmbeddings(openai_api_key=openai_api_key)
|
12 |
|
|
|
13 |
knowledge_files = {
|
14 |
"attachments": "knowledge/bartholomew_attachments_definitions.txt",
|
15 |
"bigfive": "knowledge/bigfive_definitions.txt",
|
16 |
"personalities": "knowledge/personalities_definitions.txt"
|
17 |
}
|
18 |
|
|
|
19 |
documents = []
|
20 |
for key, file_path in knowledge_files.items():
|
21 |
with open(file_path, 'r', encoding='utf-8') as file:
|
22 |
content = file.read().strip()
|
23 |
documents.append(content)
|
24 |
|
25 |
-
|
|
|
26 |
|
|
|
|
|
|
|
|
|
|
|
27 |
llm = load_model(openai_api_key)
|
28 |
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
def load_text(file_path: str) -> str:
|
32 |
with open(file_path, 'r', encoding='utf-8') as file:
|
|
|
5 |
from llm_loader import load_model
|
6 |
from config import openai_api_key
|
7 |
from langchain.chains import RetrievalQA
|
8 |
+
from langchain.retrievers import MultiQueryRetriever
|
9 |
import os
|
10 |
import json
|
11 |
|
12 |
+
# Initialize embedding model
|
13 |
embedding_model = OpenAIEmbeddings(openai_api_key=openai_api_key)
|
14 |
|
15 |
+
# Define knowledge files
|
16 |
knowledge_files = {
|
17 |
"attachments": "knowledge/bartholomew_attachments_definitions.txt",
|
18 |
"bigfive": "knowledge/bigfive_definitions.txt",
|
19 |
"personalities": "knowledge/personalities_definitions.txt"
|
20 |
}
|
21 |
|
22 |
+
# Load text-based knowledge
|
23 |
documents = []
|
24 |
for key, file_path in knowledge_files.items():
|
25 |
with open(file_path, 'r', encoding='utf-8') as file:
|
26 |
content = file.read().strip()
|
27 |
documents.append(content)
|
28 |
|
29 |
+
# Create FAISS index from text documents
|
30 |
+
text_faiss_index = FAISS.from_texts(documents, embedding_model)
|
31 |
|
32 |
+
# Load pre-existing FAISS indexes
|
33 |
+
attachments_faiss_index = FAISS.load_local("knowledge/faiss_index_Attachments_db", embedding_model)
|
34 |
+
personalities_faiss_index = FAISS.load_local("knowledge/faiss_index_Personalities_db", embedding_model)
|
35 |
+
|
36 |
+
# Initialize LLM
|
37 |
llm = load_model(openai_api_key)
|
38 |
|
39 |
+
# Create retrievers for each index
|
40 |
+
text_retriever = text_faiss_index.as_retriever()
|
41 |
+
attachments_retriever = attachments_faiss_index.as_retriever()
|
42 |
+
personalities_retriever = personalities_faiss_index.as_retriever()
|
43 |
+
|
44 |
+
# Combine retrievers
|
45 |
+
combined_retriever = MultiQueryRetriever(
|
46 |
+
retrievers=[text_retriever, attachments_retriever, personalities_retriever],
|
47 |
+
llm=llm
|
48 |
+
)
|
49 |
+
|
50 |
+
# Create QA chain with combined retriever
|
51 |
+
qa_chain = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=combined_retriever)
|
52 |
|
53 |
def load_text(file_path: str) -> str:
|
54 |
with open(file_path, 'r', encoding='utf-8') as file:
|