File size: 1,715 Bytes
f4bf832
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 json
import chromadb
from chromadb.utils.embedding_functions import GoogleGenerativeAiEmbeddingFunction

_collection: chromadb.Collection | None = None

_DATA_PATH = os.path.join(os.path.dirname(__file__), "../data/labor_law_chunks.json")


def _get_collection() -> chromadb.Collection:
    global _collection
    if _collection is not None:
        return _collection

    db_path = os.getenv("VECTOR_DB_PATH", "./data/chromadb")
    client = chromadb.PersistentClient(path=db_path)

    embedding_fn = GoogleGenerativeAiEmbeddingFunction(
        api_key=os.getenv("GEMINI_API_KEY", ""),
        model_name="models/text-embedding-004",
    )

    _collection = client.get_or_create_collection(
        name="labor_law",
        embedding_function=embedding_fn,
    )

    if _collection.count() == 0:
        _seed_collection(_collection)

    return _collection


def _seed_collection(col: chromadb.Collection) -> None:
    with open(_DATA_PATH, encoding="utf-8") as f:
        chunks: list[dict] = json.load(f)

    col.add(
        ids=[c["id"] for c in chunks],
        documents=[c["content"] for c in chunks],
        metadatas=[
            {
                "topic": c["topic"],
                "law": c["law"],
                "keywords": ", ".join(c.get("keywords", [])),
            }
            for c in chunks
        ],
    )


def search(query: str, n_results: int = 3) -> list[dict]:
    col = _get_collection()
    results = col.query(query_texts=[query], n_results=n_results)
    docs = results["documents"][0]
    metas = results["metadatas"][0]
    return [
        {"content": doc, "topic": meta["topic"], "law": meta["law"]}
        for doc, meta in zip(docs, metas)
    ]