VertexAIEmbeddings class
Wrapper around GCP Vertex AI text embedding models API
Example:
final embeddings = VertexAIEmbeddings(
httpClient: authClient,
project: 'your-project-id',
);
final result = await embeddings.embedQuery('Hello world');
Vertex AI documentation: https://cloud.google.com/vertex-ai/docs/generative-ai/language-model-overview
Set up your Google Cloud Platform project
- Select or create a Google Cloud project.
- Make sure that billing is enabled for your project.
- Enable the Vertex AI API.
- Configure the Vertex AI location.
Authentication
To create an instance of VertexAIEmbeddings
you need to provide an
HTTP client that handles authentication. The easiest way to do this is to
use AuthClient
from the googleapis_auth
package.
To create an instance of VertexAIEmbeddings
you need to provide an
AuthClient
instance.
There are several ways to obtain an AuthClient
depending on your use case.
Check out the googleapis_auth
package documentation for more details.
Example using a service account JSON:
final serviceAccountCredentials = ServiceAccountCredentials.fromJson(
json.decode(serviceAccountJson),
);
final authClient = await clientViaServiceAccount(
serviceAccountCredentials,
[VertexAIEmbeddings.cloudPlatformScope],
);
final vertexAi = VertexAIEmbeddings(
httpClient: authClient,
project: 'your-project-id',
);
The service account should have the following permission:
aiplatform.endpoints.predict
The requiredOAuth2 scope is:
https://www.googleapis.com/auth/cloud-platform
(you can use the constantVertexAIEmbeddings.cloudPlatformScope
)
See: https://cloud.google.com/vertex-ai/docs/generative-ai/access-control
Available models
textembedding-gecko
- Max input token: 3072
- Output: 768-dimensional vector embeddings
textembedding-gecko-multilingual
: support over 100 non-English languages- Max input token: 3072
- Output: 768-dimensional vector embeddings
The previous list of models may not be exhaustive or up-to-date. Check out the Vertex AI documentation for the latest list of available models.
Task type
Embedding models released after August 2023 support specifying a 'task type' when embedding documents. The task type is then used by the model to improve the quality of the embeddings.
This class uses the specifies the following task type:
retrievalDocument
: for embedding documentsretrievalQuery
: for embedding queries
Title
Embedding models released after August 2023 support specifying a document title when embedding documents. The title is then used by the model to improve the quality of the embeddings.
To specify a document title, add the title to the document's metadata. Then, specify the metadata key in the docTitleKey parameter.
Example:
final embeddings = VertexAIEmbeddings(
httpClient: authClient,
project: 'your-project-id',
docTitleKey: 'title',
);
final result = await embeddings.embedDocuments([
Document(
pageContent: 'Hello world',
metadata: {'title': 'Hello!'},
),
]);
Constructors
Properties
- batchSize → int
-
The maximum number of documents to embed in a single request.
final
- client → VertexAIGenAIClient
-
A client for interacting with Vertex AI API.
final
- docTitleKey → String
-
The metadata key used to store the document's (optional) title.
final
- hashCode → int
-
The hash code for this object.
no setterinherited
- model → String
-
The embeddings model to use.
final
- publisher → String
-
The publisher of the model.
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
embedDocuments(
List< Document> documents) → Future<List< List< >double> > - Embed search docs.
-
embedQuery(
String query) → Future< List< double> > - Embed query text.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Constants
- cloudPlatformScope → const String
- Scope required for Vertex AI API calls.