-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(example): add chromadb embedding function (#270)
- Loading branch information
Showing
4 changed files
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
""" | ||
Vector Databases | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
""" | ||
Chroma DB | ||
""" |
38 changes: 38 additions & 0 deletions
38
examples/extra/vector_database/chroma_db/chroma_db_embedding.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
""" | ||
Create ChromaDB Embedding Function | ||
""" | ||
from typing import Optional | ||
|
||
from chromadb.api.types import Documents, EmbeddingFunction, Embeddings | ||
from dotenv import load_dotenv | ||
|
||
from genai import Client, Credentials | ||
from genai.schema import TextEmbeddingParameters | ||
|
||
# make sure you have a .env file under genai root with | ||
# GENAI_KEY=<your-genai-key> | ||
# GENAI_API=<genai-api-endpoint> | ||
load_dotenv() | ||
|
||
|
||
class ChromaEmbeddingFunction(EmbeddingFunction): | ||
def __init__(self, *, model_id: str, client: Client, parameters: Optional[TextEmbeddingParameters] = None): | ||
self._model_id = model_id | ||
self._parameters = parameters | ||
self._client = client | ||
|
||
def __call__(self, inputs: Documents) -> Embeddings: | ||
embeddings: Embeddings = [] | ||
for response in self._client.text.embedding.create( | ||
model_id=self._model_id, inputs=inputs, parameters=self._parameters | ||
): | ||
embeddings.extend(response.results) | ||
|
||
return embeddings | ||
|
||
|
||
credentials = Credentials.from_env() | ||
client = Client(credentials=credentials) | ||
embedding_fn = ChromaEmbeddingFunction(model_id="sentence-transformers/all-minilm-l6-v2", client=client) | ||
|
||
print(embedding_fn(["Hello world!"])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters