Spaces:
Sleeping
Sleeping
Update agent.py
Browse files
agent.py
CHANGED
@@ -365,11 +365,13 @@ class MyVectorStore:
|
|
365 |
# Prepare Documents
|
366 |
# -----------------------------
|
367 |
# Define the URL where the JSON file is hosted
|
368 |
-
|
369 |
from typing import TypedDict, Annotated, List
|
370 |
import gradio as gr
|
371 |
from langchain.schema import Document
|
372 |
import json
|
|
|
|
|
|
|
373 |
|
374 |
# 1. Type-Checked State for Gradio
|
375 |
class ChatState(TypedDict):
|
@@ -416,29 +418,45 @@ def create_documents(data_source: str, data: List[dict]) -> List[Document]:
|
|
416 |
|
417 |
return docs
|
418 |
|
419 |
-
|
420 |
-
|
421 |
-
|
422 |
-
|
423 |
-
|
424 |
-
docs = [Document(page_content=texts, metadata={"task_id": item["task_id"]}) for item in data]
|
425 |
|
426 |
-
|
427 |
-
|
428 |
-
vector_store = FAISS.from_texts(texts = text_chunks, embedding = embeddings)
|
429 |
|
430 |
-
|
431 |
-
|
|
|
|
|
432 |
|
433 |
-
#
|
434 |
-
|
435 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
436 |
|
437 |
-
#
|
438 |
-
|
|
|
|
|
439 |
|
440 |
-
#
|
441 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
442 |
|
443 |
|
444 |
|
|
|
365 |
# Prepare Documents
|
366 |
# -----------------------------
|
367 |
# Define the URL where the JSON file is hosted
|
|
|
368 |
from typing import TypedDict, Annotated, List
|
369 |
import gradio as gr
|
370 |
from langchain.schema import Document
|
371 |
import json
|
372 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
373 |
+
from langchain.vectorstores import FAISS
|
374 |
+
import faiss
|
375 |
|
376 |
# 1. Type-Checked State for Gradio
|
377 |
class ChatState(TypedDict):
|
|
|
418 |
|
419 |
return docs
|
420 |
|
421 |
+
# 4. Vector Store Integration
|
422 |
+
class MyVectorStore:
|
423 |
+
def __init__(self, index: faiss.Index):
|
424 |
+
self.index = index
|
|
|
|
|
425 |
|
426 |
+
def save_local(self, path: str):
|
427 |
+
faiss.write_index(self.index, path)
|
|
|
428 |
|
429 |
+
@classmethod
|
430 |
+
def load_local(cls, path: str):
|
431 |
+
index = faiss.read_index(path)
|
432 |
+
return cls(index)
|
433 |
|
434 |
+
# Usage Example
|
435 |
+
if __name__ == "__main__":
|
436 |
+
# Process JSON data
|
437 |
+
with open("questions.json", "r", encoding="utf-8") as f:
|
438 |
+
json_data = json.load(f)
|
439 |
+
|
440 |
+
# Create documents from JSON
|
441 |
+
docs = create_documents("json", json_data)
|
442 |
+
texts = [doc.page_content for doc in docs]
|
443 |
|
444 |
+
# Initialize embedding model
|
445 |
+
embedding_model = HuggingFaceEmbeddings(
|
446 |
+
model_name="sentence-transformers/all-MiniLM-L6-v2"
|
447 |
+
)
|
448 |
|
449 |
+
# Create FAISS index
|
450 |
+
vector_store = FAISS.from_documents(
|
451 |
+
documents=docs,
|
452 |
+
embedding=embedding_model
|
453 |
+
)
|
454 |
+
|
455 |
+
# Save
|
456 |
+
vector_store.save_local("/home/wendy/Downloads/faiss_index.index")
|
457 |
+
|
458 |
+
# Load
|
459 |
+
loaded_store = MyVectorStore.load_local("/home/wendy/Downloads/faiss_index.index")
|
460 |
|
461 |
|
462 |
|