File size: 736 Bytes
d1a818a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import chromadb
from chromadb.config import Settings
from sentence_transformers import SentenceTransformer
import torch
import tempfile

CHROMA_DIR = "chromadb_local"
COLLECTION_NAME = "prompts"
EMBEDDING_MODEL = "all-MiniLM-L6-v2"

def get_chroma_client():
    return chromadb.Client(Settings(persist_directory=CHROMA_DIR))

def get_chroma_collection(client):
    try:
        return client.get_collection(COLLECTION_NAME)
    except Exception:
        return client.create_collection(COLLECTION_NAME)

def get_embedding_model():
    device = "cuda" if torch.cuda.is_available() else "cpu"
    cache_dir = tempfile.mkdtemp()
    return SentenceTransformer(EMBEDDING_MODEL, device=device, cache_folder=cache_dir)