- added three iwm articles as test data
- added simple webview and FastAPI server
This commit is contained in:
+53
-13
@@ -5,14 +5,14 @@ Minimal dependencies, simple code
|
||||
import os
|
||||
from pathlib import Path
|
||||
from langchain_community.document_loaders import PyPDFLoader, TextLoader
|
||||
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
||||
from langchain_community.embeddings import HuggingFaceEmbeddings
|
||||
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
||||
from langchain_huggingface import HuggingFaceEmbeddings
|
||||
from langchain_community.vectorstores import FAISS
|
||||
from langchain_ollama import ChatOllama
|
||||
|
||||
|
||||
class LocalRAG:
|
||||
def __init__(self, vectorstore_path="./vectorstore", ollama_model="llama2"):
|
||||
def __init__(self, vectorstore_path="./vectorstore", ollama_model="mistral:7b"):
|
||||
"""Initialize local RAG system"""
|
||||
self.vectorstore_path = vectorstore_path
|
||||
self.ollama_model = ollama_model
|
||||
@@ -97,6 +97,42 @@ class LocalRAG:
|
||||
self.vectorstore.save_local(self.vectorstore_path)
|
||||
print(f"Vector store saved to {self.vectorstore_path}")
|
||||
|
||||
def list_documents(self):
|
||||
"""List all documents in the vector store"""
|
||||
if self.vectorstore is None:
|
||||
print("No documents in vector store.")
|
||||
return []
|
||||
|
||||
# Get all documents from the vector store
|
||||
# We'll retrieve a large number to get all documents
|
||||
all_docs = self.vectorstore.similarity_search("", k=10000) # Large k to get all
|
||||
|
||||
# Extract unique document sources from metadata
|
||||
documents = {}
|
||||
for doc in all_docs:
|
||||
source = doc.metadata.get('source', 'Unknown')
|
||||
if source not in documents:
|
||||
documents[source] = {
|
||||
'source': source,
|
||||
'chunks': 0,
|
||||
'page': doc.metadata.get('page', None)
|
||||
}
|
||||
documents[source]['chunks'] += 1
|
||||
|
||||
# Convert to list and sort
|
||||
doc_list = list(documents.values())
|
||||
doc_list.sort(key=lambda x: x['source'])
|
||||
|
||||
print(f"\nDocuments in vector store ({len(doc_list)} unique documents):")
|
||||
print("-" * 60)
|
||||
for doc_info in doc_list:
|
||||
print(f" - {doc_info['source']}")
|
||||
print(f" Chunks: {doc_info['chunks']}")
|
||||
if doc_info['page'] is not None:
|
||||
print(f" Page: {doc_info['page']}")
|
||||
|
||||
return doc_list
|
||||
|
||||
def query(self, question, k=4):
|
||||
"""Query the RAG system"""
|
||||
if self.vectorstore is None:
|
||||
@@ -134,21 +170,25 @@ def main():
|
||||
print("=" * 60)
|
||||
|
||||
# Initialize
|
||||
rag = LocalRAG(ollama_model="llama2")
|
||||
rag = LocalRAG(ollama_model="mistral:7b")
|
||||
|
||||
# Add documents (uncomment and add your file paths)
|
||||
rag.add_documents([
|
||||
"diverses/local_rag/test1.pdf",
|
||||
"diverses/local_rag/test2.txt"
|
||||
])
|
||||
# rag.add_documents([
|
||||
# "data/dok1.pdf",
|
||||
# "data/dok2.pdf",
|
||||
# "data/dok3.pdf"
|
||||
# ])
|
||||
|
||||
# List documents
|
||||
rag.list_documents()
|
||||
|
||||
# Query
|
||||
# question = "What is this document about?"
|
||||
# answer = rag.query(question)
|
||||
# print(f"\nQuestion: {question}")
|
||||
# print(f"Answer: {answer}")
|
||||
question = "What do the documents say about modality for perceived message perception?"
|
||||
answer = rag.query(question)
|
||||
print(f"\nQuestion: {question}")
|
||||
print(f"Answer: {answer}")
|
||||
|
||||
print("\nSetup complete! Uncomment the code above to add documents and query.")
|
||||
# print("\nSetup complete! Uncomment the code above to add documents and query.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user