-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrag_search.py
More file actions
62 lines (47 loc) · 2.04 KB
/
Copy pathrag_search.py
File metadata and controls
62 lines (47 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import os
import chromadb
from chromadb.utils import embedding_functions
import argparse
# Configuration
DB_DIR = "chroma_db"
COLLECTION_NAME = "blog_posts"
def search(query, k=5, category=None):
client = chromadb.PersistentClient(path=DB_DIR)
ef = embedding_functions.SentenceTransformerEmbeddingFunction(model_name="all-MiniLM-L6-v2")
collection = client.get_collection(name=COLLECTION_NAME, embedding_function=ef)
where_filter = {}
if category:
where_filter["category"] = category
results = collection.query(
query_texts=[query],
n_results=k,
where=where_filter if where_filter else None
)
return results
def main():
parser = argparse.ArgumentParser(description="Search the Naver Blog Archive")
parser.add_argument("query", help="The search query")
parser.add_argument("--k", type=int, default=3, help="Number of results to return")
parser.add_argument("--category", help="Filter by category")
args = parser.parse_args()
print(f"Searching for: '{args.query}' (Category: {args.category or 'All'})")
try:
results = search(args.query, args.k, args.category)
if not results['documents'][0]:
print("No results found.")
return
print(f"\nFound {len(results['documents'][0])} relevant results:\n")
for i, doc in enumerate(results['documents'][0]):
meta = results['metadatas'][0][i]
dist = results['distances'][0][i]
print(f"--- Result {i+1} (Score: {dist:.4f}) ---")
print(f"File: {meta.get('source')} | Header: {meta.get('header')}")
print(f"Category: {meta.get('category')} | Date: {meta.get('date')}")
print(f"URL: {meta.get('url')}")
print(f"Content: {doc[:300]}...") # Preview content
print("\n")
except Exception as e:
print(f"Error: {e}")
print("Make sure you have run 'python3 rag_indexer.py' first.")
if __name__ == "__main__":
main()