gcharanteja commited on
Commit ·
ad469c9
1
Parent(s): caeef05
ch6
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ from sentence_transformers import SentenceTransformer
|
|
| 3 |
import uvicorn
|
| 4 |
import os
|
| 5 |
from pathlib import Path
|
|
|
|
| 6 |
from chromadb.config import Settings
|
| 7 |
from chromadb.server.fastapi import FastAPI as ChromaFastAPI
|
| 8 |
import torch
|
|
@@ -72,10 +73,33 @@ def write_bucket_probe(path: str) -> None:
|
|
| 72 |
)
|
| 73 |
|
| 74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
@app.on_event("startup")
|
| 76 |
def load_model():
|
| 77 |
global model
|
| 78 |
write_bucket_probe(chroma_persist_directory)
|
|
|
|
| 79 |
logger.info(f"[*] Loading Harrier OSS 0.6B model: {MODEL_NAME}...")
|
| 80 |
try:
|
| 81 |
model = SentenceTransformer(MODEL_NAME, trust_remote_code=True, device=device)
|
|
|
|
| 3 |
import uvicorn
|
| 4 |
import os
|
| 5 |
from pathlib import Path
|
| 6 |
+
import chromadb
|
| 7 |
from chromadb.config import Settings
|
| 8 |
from chromadb.server.fastapi import FastAPI as ChromaFastAPI
|
| 9 |
import torch
|
|
|
|
| 73 |
)
|
| 74 |
|
| 75 |
|
| 76 |
+
def seed_chroma_data(path: str) -> None:
|
| 77 |
+
client = chromadb.PersistentClient(path=path)
|
| 78 |
+
collection = client.get_or_create_collection(name="knowledge_base")
|
| 79 |
+
if collection.count() > 0:
|
| 80 |
+
logger.info("[*] Chroma already has data; skipping seed.")
|
| 81 |
+
return
|
| 82 |
+
|
| 83 |
+
documents = [
|
| 84 |
+
"Chroma is a lightweight, open-source vector database built for AI.",
|
| 85 |
+
"Python is a high-level programming language used extensively in data science.",
|
| 86 |
+
"The celestial body closest to Earth is the Moon.",
|
| 87 |
+
]
|
| 88 |
+
metadatas = [
|
| 89 |
+
{"category": "tech", "source": "docs"},
|
| 90 |
+
{"category": "tech", "source": "wiki"},
|
| 91 |
+
{"category": "science", "source": "space-facts"},
|
| 92 |
+
]
|
| 93 |
+
ids = ["doc1", "doc2", "doc3"]
|
| 94 |
+
collection.add(documents=documents, metadatas=metadatas, ids=ids)
|
| 95 |
+
logger.info("[+] Seeded Chroma with sample documents.")
|
| 96 |
+
|
| 97 |
+
|
| 98 |
@app.on_event("startup")
|
| 99 |
def load_model():
|
| 100 |
global model
|
| 101 |
write_bucket_probe(chroma_persist_directory)
|
| 102 |
+
seed_chroma_data(chroma_persist_directory)
|
| 103 |
logger.info(f"[*] Loading Harrier OSS 0.6B model: {MODEL_NAME}...")
|
| 104 |
try:
|
| 105 |
model = SentenceTransformer(MODEL_NAME, trust_remote_code=True, device=device)
|
vector.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import argparse
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
from typing import List
|
| 4 |
+
|
| 5 |
+
import chromadb
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def _parse_args() -> argparse.Namespace:
|
| 9 |
+
parser = argparse.ArgumentParser(description="Local ChromaDB persistence demo")
|
| 10 |
+
parser.add_argument(
|
| 11 |
+
"--path",
|
| 12 |
+
default="chroma_data",
|
| 13 |
+
help="Local persistence directory",
|
| 14 |
+
)
|
| 15 |
+
parser.add_argument(
|
| 16 |
+
"--collection",
|
| 17 |
+
default="knowledge_base",
|
| 18 |
+
help="Collection name",
|
| 19 |
+
)
|
| 20 |
+
parser.add_argument(
|
| 21 |
+
"--query",
|
| 22 |
+
default="Tell me about vector stores",
|
| 23 |
+
help="Query text",
|
| 24 |
+
)
|
| 25 |
+
return parser.parse_args()
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def _seed_collection(collection: chromadb.Collection) -> None:
|
| 29 |
+
documents = [
|
| 30 |
+
"Chroma is a lightweight, open-source vector database built for AI.",
|
| 31 |
+
"Python is a high-level programming language used extensively in data science.",
|
| 32 |
+
"The celestial body closest to Earth is the Moon.",
|
| 33 |
+
]
|
| 34 |
+
metadatas = [
|
| 35 |
+
{"category": "tech", "source": "docs"},
|
| 36 |
+
{"category": "tech", "source": "wiki"},
|
| 37 |
+
{"category": "science", "source": "space-facts"},
|
| 38 |
+
]
|
| 39 |
+
ids = ["doc1", "doc2", "doc3"]
|
| 40 |
+
collection.add(documents=documents, metadatas=metadatas, ids=ids)
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def main() -> None:
|
| 44 |
+
args = _parse_args()
|
| 45 |
+
persist_path = Path(args.path).resolve()
|
| 46 |
+
persist_path.mkdir(parents=True, exist_ok=True)
|
| 47 |
+
|
| 48 |
+
print(f"Using local Chroma persistence at: {persist_path}")
|
| 49 |
+
client = chromadb.PersistentClient(path=str(persist_path))
|
| 50 |
+
|
| 51 |
+
collection = client.get_or_create_collection(name=args.collection)
|
| 52 |
+
if collection.count() == 0:
|
| 53 |
+
print("Seeding collection with sample documents...")
|
| 54 |
+
_seed_collection(collection)
|
| 55 |
+
print(f"Collection '{args.collection}' has {collection.count()} documents.")
|
| 56 |
+
|
| 57 |
+
results = collection.query(query_texts=[args.query], n_results=2)
|
| 58 |
+
print("\n--- Search Results ---")
|
| 59 |
+
for doc, meta, distance in zip(
|
| 60 |
+
results["documents"][0],
|
| 61 |
+
results["metadatas"][0],
|
| 62 |
+
results["distances"][0],
|
| 63 |
+
):
|
| 64 |
+
print(f"Matched Document: {doc}")
|
| 65 |
+
print(f"Metadata: {meta}")
|
| 66 |
+
print(f"Distance Score (Lower is better): {distance:.4f}")
|
| 67 |
+
print()
|
| 68 |
+
print("----------------------")
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
if __name__ == "__main__":
|
| 72 |
+
main()
|