vertexai

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 18, 2024 License: Apache-2.0 Imports: 11 Imported by: 4

Documentation

Index

Constants

View Source
const (
	// BaseURL is the Google Vertex AI HTTP API base URL
	BaseURL = "https://us-central1-aiplatform.googleapis.com/v1/projects"
	// ModelURI is the Google Vertex AI HTTP API model URI.
	ModelURI = "locations/us-central1/publishers/google/models"
	// EmbedAction is embedding API action.
	EmbedAction = ":predict"
)
View Source
const (
	// NOTE: https://developers.google.com/identity/protocols/oauth2/scopes
	Scopes = "https://www.googleapis.com/auth/cloud-platform"
)

Variables

View Source
var (
	// ErrMissingTokenSource is returned when the API client is missing a token source.
	ErrMissingTokenSource = errors.New("missing access token source")
)

Functions

func GetToken

func GetToken(tokenSrc oauth2.TokenSource) (string, error)

GetToken returns access token from the given token source. It returns error is tokenSrc is nil instead of panicking.

func NewEmbedder

func NewEmbedder(opts ...Option) embeddings.Embedder[*EmbeddingRequest]

NewEmbedder creates a client that implements embeddings.Embedder

Types

type APIError

type APIError struct {
	RespError struct {
		Code    int    `json:"code"`
		Message string `json:"message"`
		Status  string `json:"status"`
	} `json:"error"`
}

APIError is API error.

func (APIError) Error

func (e APIError) Error() string

Error implements error interface.

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is a Google Vertex AI HTTP API client.

func NewClient

func NewClient(opts ...Option) *Client

NewClient creates a new HTTP client and returns it. By default it reads the following env vars: * VERTEXAI_TOKEN for setting the API token * VERTEXAI_MODEL_ID for settings the API model ID * GOOGLE_PROJECT_ID for setting the Google Project ID It uses the default Go http.Client for making API requests to the BaseURL. You can override the default client options via the client methods. NOTE: you must provide either the token or the token source

func (*Client) Embed

func (c *Client) Embed(ctx context.Context, embReq *EmbeddingRequest) ([]*embeddings.Embedding, error)

Embed returns embeddings for every object in EmbeddingRequest.

func (*Client) MultiEmbeddings

func (c *Client) MultiEmbeddings(ctx context.Context, embReq *MultiEmbeddingRequest) (*MultiEmbedddingResponse, error)

MultiEmbeddings returns multimodal embeddings for every object in EmbeddingRequest.

type EmbedddingResponse

type EmbedddingResponse struct {
	Predictions []Predictions  `json:"predictions"`
	Metadata    map[string]any `json:"metadata"`
}

EmbedddingResponse received from API endpoint. https://cloud.google.com/vertex-ai/docs/generative-ai/model-reference/text-embeddings#response_body

func (*EmbedddingResponse) ToEmbeddings

func (e *EmbedddingResponse) ToEmbeddings() ([]*embeddings.Embedding, error)

ToEmbeddings converts the API response, into a slice of embeddings and returns it.

type EmbeddingRequest

type EmbeddingRequest struct {
	Instances []Instance `json:"instances"`
	Params    Params     `json:"parameters"`
}

EmbeddingRequest sent to API endpoint. https://cloud.google.com/vertex-ai/docs/generative-ai/embeddings/get-text-embeddings#generative-ai-get-text-embedding-drest

type ImageBase64

type ImageBase64 struct {
	Bytes string `json:"bytesBase64Encoded"`
}

ImageBase64 contains image encoded as base64 string.

type ImageGCS

type ImageGCS struct {
	URI string `json:"gcsUri"`
}

ImageGCS contains GCS URI to image file.

type Instance

type Instance struct {
	TaskType TaskType `json:"task_type"`
	Title    string   `json:"title,omitempty"`
	Content  string   `json:"content"`
}

NOTE: Title is only valid with TaskType set to RetrDocTask https://cloud.google.com/vertex-ai/docs/generative-ai/embeddings/get-text-embeddings#api_changes_to_models_released_on_or_after_august_2023

type Model

type Model string

Model is embedding model.

const (
	// EmbedGeckoV* are English language embedding models
	// See for the information about the latest available version
	// https://cloud.google.com/vertex-ai/docs/generative-ai/learn/model-versioning#latest-version
	EmbedGeckoV1     Model = "textembedding-gecko@001"
	EmbedGeckoV2     Model = "textembedding-gecko@002"
	EmbedGeckoV3     Model = "textembedding-gecko@003"
	EmbedGeckoLatest Model = "textembedding-gecko@latest"
	EmbedPreviewV4   Model = "text-embedding-preview-0409"
	// EmbedMultiGecko is a multilanguage embeddings model
	EmbedMultiGecko     Model = "multimodalembedding@001"
	EmbedMultiPreviewV4 Model = "text-multilingual-embedding-preview-0409"
)

func (Model) String

func (m Model) String() string

String implements stringer.

type MultiEmbedddingResponse

type MultiEmbedddingResponse struct {
	Predictions []MultiPrediction `json:"predictions"`
	ModelID     string            `json:"deployedModelId"`
}

MultiEmbedddingResponse received from API.

type MultiEmbeddingRequest

type MultiEmbeddingRequest struct {
	Instances []MultiInstance `json:"instances"`
	Params    *MultiParams    `json:"parameters,omitempty"`
}

MultiEmbeddingRequest is multimodal embedding request.

type MultiInstance

type MultiInstance struct {
	Text  *string `json:"text,omitempty"`
	Image any     `json:"image,omitempty"`
}

MultiInstance contains the request payload.

type MultiParams

type MultiParams struct {
	Dimension uint `json:"dimension"`
}

MultiParams are additional API request parameters.

type MultiPrediction

type MultiPrediction struct {
	Image []float64 `json:"imageEmbedding"`
	Text  []float64 `json:"textEmbedding"`
}

MultiPrediction for a given request.

type Option

type Option func(*Options)

Option is functional option.

func WithBaseURL

func WithBaseURL(baseURL string) Option

WithBaseURL sets the API base URL.

func WithHTTPClient

func WithHTTPClient(httpClient *client.HTTP) Option

WithHTTPClient sets the HTTP client.

func WithModelID

func WithModelID(id string) Option

WithModelID sets the Vertex AI model ID. https://cloud.google.com/vertex-ai/docs/generative-ai/learn/model-versioning

func WithProjectID

func WithProjectID(id string) Option

WithProjectID sets the Google Project ID.

func WithToken

func WithToken(token string) Option

WithToken sets the API token.

func WithTokenSrc

func WithTokenSrc(ts oauth2.TokenSource) Option

WithTokenSrc sets the API token source. The source can be used for generating the API token if no token has been set.

type Options

type Options struct {
	Token      string
	TokenSrc   oauth2.TokenSource
	ProjectID  string
	ModelID    string
	BaseURL    string
	HTTPClient *client.HTTP
}

Options are client options

type Params

type Params struct {
	// If set to false, text that exceeds the token limit (3.072)
	// causes the request to fail. The default value is true
	AutoTruncate bool `json:"autoTruncate"`
}

Params are additional API request parameters passed via body.

type Predictions

type Predictions struct {
	Embeddings struct {
		Values     []float64  `json:"values"`
		Statistics Statistics `json:"statistics"`
	} `json:"embeddings"`
}

Predictions is the generated response

type Statistics

type Statistics struct {
	TokenCount int  `json:"token_count"`
	Truncated  bool `json:"truncated"`
}

Statistics define the statistics for a text embedding

type TaskType

type TaskType string

TaskType is embedding task type. It can be used to improve the embedding quality when targeting a specific task. See: https://cloud.google.com/vertex-ai/docs/generative-ai/embeddings/get-text-embeddings

const (
	RetrQueryTask      TaskType = "RETRIEVAL_QUERY"
	RetrDocTask        TaskType = "RETRIEVAL_DOCUMENT"
	SemanticSimTask    TaskType = "SEMANTIC_SIMILARITY"
	ClassificationTask TaskType = "CLASSIFICATION"
	ClusteringTask     TaskType = "CLUSTERING"
)

func (TaskType) String

func (t TaskType) String() string

String implements stringer.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL