Skip to content

Commit f297133

Browse files
Update VertexAI Vectorizer (#132)
Update the VertexAITextVectorizer to be able to utilizer newer models and updates to the SDK. Also update the semantic caching imports.
1 parent 540171a commit f297133

File tree

2 files changed

+17
-24
lines changed

2 files changed

+17
-24
lines changed

redisvl/extensions/llmcache/semantic.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ def __init__(
2525
prefix: Optional[str] = None,
2626
distance_threshold: float = 0.1,
2727
ttl: Optional[int] = None,
28-
vectorizer: BaseVectorizer = HFTextVectorizer(
29-
model="sentence-transformers/all-mpnet-base-v2"
30-
),
28+
vectorizer: Optional[BaseVectorizer] = None,
3129
redis_client: Optional[Redis] = None,
3230
redis_url: str = "redis://localhost:6379",
3331
connection_args: Dict[str, Any] = {},
@@ -66,6 +64,12 @@ def __init__(
6664
if prefix is None:
6765
prefix = name
6866

67+
# Set vectorizer default
68+
if vectorizer is None:
69+
vectorizer = HFTextVectorizer(
70+
model="sentence-transformers/all-mpnet-base-v2"
71+
)
72+
6973
# build cache index schema
7074
schema = IndexSchema.from_dict({"index": {"name": name, "prefix": prefix}})
7175
# add fields

redisvl/utils/vectorize/text/vertexai.py

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ class VertexAITextVectorizer(BaseVectorizer):
1717
1818
Utilizing this vectorizer requires an active GCP project and location
1919
(region), along with appropriate application credentials. These can be
20-
provided through the `api_config` dictionary or by setting the corresponding
21-
environment variables. Additionally, the vertexai python client must be
20+
provided through the `api_config` dictionary or set the GOOGLE_APPLICATION_CREDENTIALS
21+
env var. Additionally, the vertexai python client must be
2222
installed with `pip install google-cloud-aiplatform>=1.26`.
2323
2424
.. code-block:: python
@@ -29,8 +29,6 @@ class VertexAITextVectorizer(BaseVectorizer):
2929
api_config={
3030
"project_id": "your_gcp_project_id", # OR set GCP_PROJECT_ID
3131
"location": "your_gcp_location", # OR set GCP_LOCATION
32-
"google_application_credentials": "path_to_your_creds"
33-
# OR set GOOGLE_APPLICATION_CREDENTIALS
3432
})
3533
embedding = vectorizer.embed("Hello, world!")
3634
@@ -51,7 +49,7 @@ def __init__(
5149
model (str): Model to use for embedding. Defaults to
5250
'textembedding-gecko'.
5351
api_config (Optional[Dict], optional): Dictionary containing the
54-
API key. Defaults to None.
52+
API config details. Defaults to None.
5553
5654
Raises:
5755
ImportError: If the google-cloud-aiplatform library is not installed.
@@ -79,25 +77,16 @@ def __init__(
7977
"or set the GCP_LOCATION environment variable."
8078
)
8179

82-
# Check for Google Application Credentials
83-
if "GOOGLE_APPLICATION_CREDENTIALS" not in os.environ:
84-
creds_path = (
85-
api_config.get("google_application_credentials") if api_config else None
86-
)
87-
if creds_path:
88-
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = creds_path
89-
else:
90-
raise ValueError(
91-
"Missing Google Application Credentials. "
92-
"Provide the path to the credentials JSON file in the api_config with\
93-
key 'google_application_credentials' or set the \
94-
GOOGLE_APPLICATION_CREDENTIALS environment variable."
95-
)
80+
# Check for credentials
81+
credentials = api_config.get("credentials") if api_config else None
82+
9683
try:
9784
import vertexai
98-
from vertexai.preview.language_models import TextEmbeddingModel
85+
from vertexai.language_models import TextEmbeddingModel
9986

100-
vertexai.init(project=project_id, location=location)
87+
vertexai.init(
88+
project=project_id, location=location, credentials=credentials
89+
)
10190
except ImportError:
10291
raise ImportError(
10392
"VertexAI vectorizer requires the google-cloud-aiplatform library. "

0 commit comments

Comments
 (0)