-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchroma_utils.py
More file actions
61 lines (46 loc) · 1.87 KB
/
Copy pathchroma_utils.py
File metadata and controls
61 lines (46 loc) · 1.87 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
from __future__ import annotations
from ast import In
import os
from functools import lru_cache
def _chroma_openai_api_key_env_var() -> str | None:
if os.getenv("CHROMA_OPENAI_API_KEY"):
return "CHROMA_OPENAI_API_KEY"
if os.getenv("OPENAI_API_KEY"):
return "OPENAI_API_KEY"
return None
def chroma_query_enabled() -> bool:
required = ("CHROMA_API_KEY", "CHROMA_DATABASE", "CHROMA_TENANT")
return all(os.getenv(name) for name in required) and _chroma_openai_api_key_env_var() is not None
@lru_cache(maxsize=1) # it caches the result of the function, so that subsequent calls with the same arguments return the cached result instead of recomputing it.
#In this case, since there are no arguments, it will cache the first computed value and return it for all future calls.
def get_chroma_embedding_function():
api_key_env_var = _chroma_openai_api_key_env_var()
if api_key_env_var is None:
return None
try:
from chromadb.utils.embedding_functions import OpenAIEmbeddingFunction
except ImportError:
return None
return OpenAIEmbeddingFunction(
api_key_env_var=api_key_env_var,
model_name=os.getenv("CHROMA_OPENAI_EMBEDDING_MODEL", "text-embedding-3-small"),
)
def embed_texts(texts: list[str]) -> list:
embedding_function = get_chroma_embedding_function()
if embedding_function is None:
return []
return embedding_function(texts)
def get_chroma_cloud_client():
try:
import chromadb
except ImportError:
return None
cloud_host = (os.getenv("CHROMA_HOST") or "").strip()
client_kwargs = {
"api_key": os.getenv("CHROMA_API_KEY"),
"database": os.getenv("CHROMA_DATABASE"),
"tenant": os.getenv("CHROMA_TENANT"),
}
if cloud_host:
client_kwargs["cloud_host"] = cloud_host
return chromadb.CloudClient(**client_kwargs)