Skip to content

chore: Add kwarg options to GCP Vectoriser transform method - #233

Open
frayle-ons wants to merge 2 commits into
mainfrom
232-gcp-transform-kwargs
Open

frayle-ons wants to merge 2 commits into
mainfrom
232-gcp-transform-kwargs

Conversation

@frayle-ons

@frayle-ons frayle-ons commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

✨ Summary

These changes rework the implementation of the GcpVectoriser class' transform() method to accept a kwargs argument that is passed to the embed_content() call. This allows the user to dynamically modify individual embedding requests, enabling changes to task type, embedding dimension and more.

Specific alterations to the code include adding a **kwargs argument to the GcpVectoriser transform() method:

    def transform(self, texts: str | list[str], **kwargs) -> np.ndarray:

and then introducing code that merges these kwarg items with relevant embed arguments that were passed to the constructor, before passing the content to the embed_content() call to GCP:

        # Dynamically create the EmbedContentConfig, preserving the constructor's task_type
        config = genai.types.EmbedContentConfig(
            task_type=kwargs.pop(
                "task_type", self.model_config.task_type
            ),  # Use the constructor's task_type if not overridden
            **kwargs,  # Pass any additional configuration options
        )

        # The Vertex AI call to embed content
        try:
            embeddings = self.vectoriser.models.embed_content(model=self.model_name, contents=texts, config=config)
        except Exception as e:
            ... # the rest of the code

📜 Changes Introduced

  • (feat:) modified the GcpVectoriser class transform method code.

✅ Checklist

Passes all precommit checks on commit and push

🔍 How to Test

To test these changes, try passing some different values to the GcpVectoriser and its transform() method. All code can be executed on this branch's version of the code. I cloned the rep, checkout out this branch and the performed uv lock, and uv sync --extra gcp to get an executable environment (uv run yourtestscript.py).

You also need a valid GCP account/project and need to be authenticated before running the below code, with the correct Vertex AI and Generative AI APIs enabled on you GCP project.

First instantiate a Vectoriser with a specific task type:

from classifai.vectorisers import GcpVectoriser

vectoriser = GcpVectoriser(project_id=<YOUR-PROJECT-ID>, location=<YOUR-LOCATION>, vertexai=True, task_type="SEMANTIC_SIMILARITY")

Then you can make some calls to the GcpVectoriser transform method to see the effects of the new code changes. First try getting an embedding of 10 elements:

output1 = vectoriser.transform("This is a test string to be embedded using the GCP vectoriser.", output_dimensionality=10)
print(output1)

Then try modifying the task type and observing the different values of the embeddings:

output2= vectoriser.transform("This is a test string to be embedded using the GCP vectoriser.", output_dimensionality=5, task_type="CLASSIFICATION")
print(output2)

Finally, try keeping the same shorter dimensionality but return to the original task type and see how the values of the third output are the same as the values in the first output - here it isn't actually be necessary to include the task_type argument explicitly, the instance config already contains task_type=SEMANTIC_Similarity - but I've included it anyway for illustration.

output3 = vectoriser.transform("This is a test string to be embedded using the GCP vectoriser.", output_dimensionality=5, task_type="SEMANTIC_SIMILARITY")
print(output3)

This gives a short demonstration of how the task type is being overwritten from the constructor to the transform method, and how we can now pass new kwarg arguments like the output_dimensionality

@frayle-ons frayle-ons linked an issue Sep 4, 2026 that may be closed by this pull request
@frayle-ons
frayle-ons marked this pull request as ready for review September 7, 2026 12:17
@frayle-ons
frayle-ons requested a review from a team as a code owner September 7, 2026 12:17
@frayle-ons frayle-ons changed the title Add kwarg options to GCP Vectoriser transform method chore: Add kwarg options to GCP Vectoriser transform method Sep 7, 2026
@github-actions github-actions Bot added the chore label Sep 7, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add kwargs argument to GCP vectoriser transform method.

1 participant