File size: 1,468 Bytes
f837c69 c0a802c 6005177 83e0fa0 6005177 bd2a75a 03993e2 bd2a75a f837c69 bd2a75a 57cfb76 bd2a75a 57cfb76 bd2a75a e16c6ed bd2a75a d34dafd | 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 | """Database module for user-uploaded files."""
from pathlib import Path
import chromadb
from chromadb import PersistentClient, Documents, EmbeddingFunction, Embeddings
from google import genai
from google.api_core import retry
from google.genai import types
from ..services import client
# Define a helper to retry when per-minute quota is reached.
is_retriable = lambda e: (isinstance(e, genai.errors.APIError) and e.code in {429, 503})
class GeminiEmbeddingFunction(EmbeddingFunction):
"""A class to handle retrieval embedding functions using Google Gemini API."""
def __init__(self):
self.document_mode = True
@retry.Retry(predicate=is_retriable)
def __call__(self, inputs: Documents) -> Embeddings:
if self.document_mode:
embedding_task = "retrieval_document"
else:
embedding_task = "retrieval_query"
response = client.models.embed_content(
model="models/text-embedding-004",
contents=inputs,
config=types.EmbedContentConfig(
task_type=embedding_task,
),
)
return [e.values for e in response.embeddings]
embed_fn = GeminiEmbeddingFunction()
embed_fn.document_mode = True
chroma_client = None
def init_chroma_client():
global chroma_client
chroma_path = Path.cwd() / "tmp"
chroma_path.mkdir(parents=True, exist_ok=True)
chroma_client = chromadb.PersistentClient(path=str(chroma_path))
|