Update pinecone_rag.py
Browse files- pinecone_rag.py +20 -18
pinecone_rag.py
CHANGED
|
@@ -30,7 +30,7 @@ embedding_model = OpenAIEmbeddings(
|
|
| 30 |
|
| 31 |
nlp = spacy.load("xx_sent_ud_sm") # For multilingual support including Arabic
|
| 32 |
|
| 33 |
-
def sentence_overlap_chunks(text, chunk_size=
|
| 34 |
doc = nlp(text)
|
| 35 |
sentences = [sent.text.strip() for sent in doc.sents]
|
| 36 |
|
|
@@ -50,34 +50,36 @@ def sentence_overlap_chunks(text, chunk_size=900):
|
|
| 50 |
|
| 51 |
# Join and store chunk
|
| 52 |
if chunk:
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
# Overlap: start next chunk with last sentence of current chunk
|
| 57 |
-
i = i -
|
| 58 |
|
| 59 |
return chunks
|
| 60 |
|
| 61 |
|
| 62 |
-
#
|
| 63 |
for filename in os.listdir(DATA_DIR):
|
| 64 |
-
if filename.endswith(".
|
| 65 |
filepath = os.path.join(DATA_DIR, filename)
|
| 66 |
namespace = "ns4"
|
| 67 |
|
| 68 |
print(f"Processing {filename} → namespace: {namespace}")
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
with open(filepath, "r", encoding=encoding) as f:
|
| 77 |
-
content = f.read()
|
| 78 |
|
| 79 |
# Wrap in Langchain doc
|
| 80 |
-
#document = LangchainDocument(page_content=content, metadata={"source": filename})
|
| 81 |
docs_processed = sentence_overlap_chunks(content)
|
| 82 |
|
| 83 |
# Embed and prepare for upsert
|
|
@@ -85,7 +87,7 @@ for filename in os.listdir(DATA_DIR):
|
|
| 85 |
for i, chunk in tqdm(enumerate(docs_processed), total=len(docs_processed), desc="Embedding chunks"):
|
| 86 |
vector = embedding_model.embed_query(chunk)
|
| 87 |
upsert_data.append({
|
| 88 |
-
"id": f"{
|
| 89 |
"values": vector,
|
| 90 |
"metadata": {
|
| 91 |
"text": chunk,
|
|
@@ -98,7 +100,7 @@ for filename in os.listdir(DATA_DIR):
|
|
| 98 |
index.upsert(vectors=upsert_data, namespace=namespace)
|
| 99 |
print(f"✅ Done with {filename}\n")
|
| 100 |
|
| 101 |
-
#
|
| 102 |
for filename in os.listdir(JOURNAL_DIR):
|
| 103 |
if filename.endswith(".txt"):
|
| 104 |
filepath = os.path.join(JOURNAL_DIR, filename)
|
|
@@ -134,4 +136,4 @@ for filename in os.listdir(JOURNAL_DIR):
|
|
| 134 |
# Upsert to Pinecone under this file's namespace
|
| 135 |
print(f"⬆️ Upserting {len(upsert_data)} vectors to namespace '{namespace}'...")
|
| 136 |
index.upsert(vectors=upsert_data, namespace=namespace)
|
| 137 |
-
print(f"✅ Done with {filename}\n")
|
|
|
|
| 30 |
|
| 31 |
nlp = spacy.load("xx_sent_ud_sm") # For multilingual support including Arabic
|
| 32 |
|
| 33 |
+
def sentence_overlap_chunks(text, chunk_size=2000):
|
| 34 |
doc = nlp(text)
|
| 35 |
sentences = [sent.text.strip() for sent in doc.sents]
|
| 36 |
|
|
|
|
| 50 |
|
| 51 |
# Join and store chunk
|
| 52 |
if chunk:
|
| 53 |
+
try:
|
| 54 |
+
chunk = " ".join(chunk)
|
| 55 |
+
chunks.append(chunk)
|
| 56 |
+
i+=1
|
| 57 |
+
except:
|
| 58 |
+
print("can't process line.")
|
| 59 |
+
i+=4
|
| 60 |
|
| 61 |
# Overlap: start next chunk with last sentence of current chunk
|
| 62 |
+
i = i - 3
|
| 63 |
|
| 64 |
return chunks
|
| 65 |
|
| 66 |
|
| 67 |
+
# for data
|
| 68 |
for filename in os.listdir(DATA_DIR):
|
| 69 |
+
if filename.endswith(".pdf"):
|
| 70 |
filepath = os.path.join(DATA_DIR, filename)
|
| 71 |
namespace = "ns4"
|
| 72 |
|
| 73 |
print(f"Processing {filename} → namespace: {namespace}")
|
| 74 |
|
| 75 |
+
reader = fitz.open(filepath)
|
| 76 |
+
content = ""
|
| 77 |
+
for page in reader:
|
| 78 |
+
text = page.get_text()
|
| 79 |
+
if text:
|
| 80 |
+
content += text + "\n"
|
|
|
|
|
|
|
| 81 |
|
| 82 |
# Wrap in Langchain doc
|
|
|
|
| 83 |
docs_processed = sentence_overlap_chunks(content)
|
| 84 |
|
| 85 |
# Embed and prepare for upsert
|
|
|
|
| 87 |
for i, chunk in tqdm(enumerate(docs_processed), total=len(docs_processed), desc="Embedding chunks"):
|
| 88 |
vector = embedding_model.embed_query(chunk)
|
| 89 |
upsert_data.append({
|
| 90 |
+
"id": f"{filename[:-4]}_chunk_{i}",
|
| 91 |
"values": vector,
|
| 92 |
"metadata": {
|
| 93 |
"text": chunk,
|
|
|
|
| 100 |
index.upsert(vectors=upsert_data, namespace=namespace)
|
| 101 |
print(f"✅ Done with {filename}\n")
|
| 102 |
|
| 103 |
+
# for journals
|
| 104 |
for filename in os.listdir(JOURNAL_DIR):
|
| 105 |
if filename.endswith(".txt"):
|
| 106 |
filepath = os.path.join(JOURNAL_DIR, filename)
|
|
|
|
| 136 |
# Upsert to Pinecone under this file's namespace
|
| 137 |
print(f"⬆️ Upserting {len(upsert_data)} vectors to namespace '{namespace}'...")
|
| 138 |
index.upsert(vectors=upsert_data, namespace=namespace)
|
| 139 |
+
print(f"✅ Done with {filename}\n")
|