Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] fix bug when receive embedding from vertex api #2000

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions chromadb/test/ef/test_vertex_ef.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from os import getenv

import pytest
from chromadb.utils.embedding_functions import GoogleVertexEmbeddingFunction


def test_api_key() -> None:
pig7788 marked this conversation as resolved.
Show resolved Hide resolved
with pytest.raises(KeyError, match="Request had invalid authentication credentials."):
vertex_ef = GoogleVertexEmbeddingFunction(None,
'textembedding-gecko-multilingual',
getenv('PROJECT_ID'))
embeddingsclear = vertex_ef(['Open source is awsome.'])


@pytest.mark.skipif(
not getenv('GVAI_API_KEY') or not getenv('PROJECT_ID'),
reason='API_TOKEN or PROJECT_ID is not set, skipping test.'
)
def test_vertex_ef() -> None:
vertex_ef = GoogleVertexEmbeddingFunction(getenv('GVAI_API_KEY'),
'textembedding-gecko-multilingual',
getenv('PROJECT_ID'))
embeddings = vertex_ef(['Open source is awsome.'])
assert embeddings is not None
assert len(embeddings) > 0
10 changes: 7 additions & 3 deletions chromadb/utils/embedding_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -661,8 +661,8 @@ class GoogleVertexEmbeddingFunction(EmbeddingFunction[Documents]):
def __init__(
self,
api_key: str,
project_id: str,
model_name: str = "textembedding-gecko",
project_id: str = "cloud-large-language-models",
region: str = "us-central1",
):
self._api_url = f"https://{region}-aiplatform.googleapis.com/v1/projects/{project_id}/locations/{region}/publishers/goole/models/{model_name}:predict"
Expand All @@ -676,8 +676,12 @@ def __call__(self, input: Documents) -> Embeddings:
self._api_url, json={"instances": [{"content": text}]}
).json()

if "predictions" in response:
embeddings.append(response["predictions"]["embeddings"]["values"])
predictions = response.get('predictions')
if isinstance(predictions, List) and predictions:
for prediction in predictions:
embeddings.append(prediction["embeddings"]["values"])
else:
raise KeyError(f'Something went wrong -- Response: {response}')

return embeddings

Expand Down