genai

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: May 20, 2024 License: Apache-2.0, Apache-2.0 Imports: 13 Imported by: 23

Documentation

Overview

Package genai is a client for the generative VertexAI model.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Ptr added in v0.6.0

func Ptr[T any](t T) *T

Ptr returns a pointer to its argument. It can be used to initialize pointer fields:

model.Temperature = genai.Ptr[float32](0.1)

func WithREST added in v0.7.0

func WithREST() option.ClientOption

WithREST is an option that enables REST transport for the client. The default transport (if this option isn't provided) is gRPC.

Types

type Blob

type Blob struct {
	// Required. The IANA standard MIME type of the source data.
	MIMEType string
	// Required. Raw bytes.
	Data []byte
}

Blob contains binary data like images. Use Text for text.

func ImageData

func ImageData(format string, data []byte) Blob

ImageData is a convenience function for creating an image Blob for input to a model. The format should be the second part of the MIME type, after "image/". For example, for a PNG image, pass "png".

type BlockedError

type BlockedError struct {
	// If non-nil, the model's response was blocked.
	// Consult the Candidate and SafetyRatings fields for details.
	Candidate *Candidate

	// If non-nil, there was a problem with the prompt.
	PromptFeedback *PromptFeedback
}

A BlockedError indicates that the model's response was blocked. There can be two underlying causes: the prompt or a candidate response.

func (*BlockedError) Error

func (e *BlockedError) Error() string

type BlockedReason

type BlockedReason int32

BlockedReason is blocked reason enumeration.

const (
	// BlockedReasonUnspecified means unspecified blocked reason.
	BlockedReasonUnspecified BlockedReason = 0
	// BlockedReasonSafety means candidates blocked due to safety.
	BlockedReasonSafety BlockedReason = 1
	// BlockedReasonOther means candidates blocked due to other reason.
	BlockedReasonOther BlockedReason = 2
	// BlockedReasonBlocklist means candidates blocked due to the terms which are included from the
	// terminology blocklist.
	BlockedReasonBlocklist BlockedReason = 3
	// BlockedReasonProhibitedContent means candidates blocked due to prohibited content.
	BlockedReasonProhibitedContent BlockedReason = 4
)

func (BlockedReason) String added in v0.3.0

func (v BlockedReason) String() string

type Candidate

type Candidate struct {
	// Output only. Index of the candidate.
	Index int32
	// Output only. Content parts of the candidate.
	Content *Content
	// Output only. The reason why the model stopped generating tokens.
	// If empty, the model has not stopped generating the tokens.
	FinishReason FinishReason
	// Output only. List of ratings for the safety of a response candidate.
	//
	// There is at most one rating per category.
	SafetyRatings []*SafetyRating
	// Output only. Describes the reason the mode stopped generating tokens in
	// more detail. This is only filled when `finish_reason` is set.
	FinishMessage string
	// Output only. Source attribution of the generated content.
	CitationMetadata *CitationMetadata
}

Candidate is a response candidate generated from the model.

func (*Candidate) FunctionCalls added in v0.9.0

func (c *Candidate) FunctionCalls() []FunctionCall

FunctionCalls return all the FunctionCall parts in the candidate.

type ChatSession

type ChatSession struct {
	History []*Content
	// contains filtered or unexported fields
}

A ChatSession provides interactive chat.

Example
package main

import (
	"context"
	"fmt"
	"log"

	"cloud.google.com/go/vertexai/genai"

	"google.golang.org/api/iterator"
)

// Your GCP project
const projectID = "your-project"

// A GCP location like "us-central1"; if you're using standard Google-published
// models (like untuned Gemini models), you can keep location blank ("").
const location = "some-gcp-location"

// A model name like "gemini-1.0-pro"
// For custom models from different publishers, prepent the full publisher
// prefix for the model, e.g.:
//
//	model = publishers/some-publisher/models/some-model-name
const model = "some-model"

func main() {
	ctx := context.Background()
	client, err := genai.NewClient(ctx, projectID, location)
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()
	model := client.GenerativeModel(model)
	cs := model.StartChat()

	send := func(msg string) *genai.GenerateContentResponse {
		fmt.Printf("== Me: %s\n== Model:\n", msg)
		res, err := cs.SendMessage(ctx, genai.Text(msg))
		if err != nil {
			log.Fatal(err)
		}
		return res
	}

	res := send("Can you name some brands of air fryer?")
	printResponse(res)
	iter := cs.SendMessageStream(ctx, genai.Text("Which one of those do you recommend?"))
	for {
		res, err := iter.Next()
		if err == iterator.Done {
			break
		}
		if err != nil {
			log.Fatal(err)
		}
		printResponse(res)
	}

	for i, c := range cs.History {
		log.Printf("    %d: %+v", i, c)
	}
	res = send("Why do you like the Philips?")
	if err != nil {
		log.Fatal(err)
	}
	printResponse(res)
}

func printResponse(resp *genai.GenerateContentResponse) {
	for _, cand := range resp.Candidates {
		for _, part := range cand.Content.Parts {
			fmt.Println(part)
		}
	}
	fmt.Println("---")
}
Output:

func (*ChatSession) SendMessage

func (cs *ChatSession) SendMessage(ctx context.Context, parts ...Part) (*GenerateContentResponse, error)

SendMessage sends a request to the model as part of a chat session.

func (*ChatSession) SendMessageStream

func (cs *ChatSession) SendMessageStream(ctx context.Context, parts ...Part) *GenerateContentResponseIterator

SendMessageStream is like SendMessage, but with a streaming request.

type Citation

type Citation struct {
	// Output only. Start index into the content.
	StartIndex int32
	// Output only. End index into the content.
	EndIndex int32
	// Output only. Url reference of the attribution.
	URI string
	// Output only. Title of the attribution.
	Title string
	// Output only. License of the attribution.
	License string
	// Output only. Publication date of the attribution.
	PublicationDate civil.Date
}

Citation contains source attributions for content.

type CitationMetadata

type CitationMetadata struct {
	// Output only. List of citations.
	Citations []*Citation
}

CitationMetadata is a collection of source attributions for a piece of content.

type Client

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

A Client is a Google Vertex AI client.

func NewClient

func NewClient(ctx context.Context, projectID, location string, opts ...option.ClientOption) (*Client, error)

NewClient creates a new Google Vertex AI client.

Clients should be reused instead of created as needed. The methods of Client are safe for concurrent use by multiple goroutines. projectID is your GCP project; location is GCP region/location per https://cloud.google.com/vertex-ai/docs/general/locations If location is empty, this function attempts to infer it from environment variables and falls back to a default location if unsuccessful.

You may configure the client by passing in options from the google.golang.org/api/option package. You may also use options defined in this package, such as WithREST.

func (*Client) Close

func (c *Client) Close() error

Close closes the client.

func (*Client) GenerativeModel

func (c *Client) GenerativeModel(name string) *GenerativeModel

GenerativeModel creates a new instance of the named model. name is a string model name like "gemini-1.0-pro" or "models/gemini-1.0-pro" for Google-published models. See https://cloud.google.com/vertex-ai/generative-ai/docs/learn/model-versioning for details on model naming and versioning, and https://cloud.google.com/vertex-ai/generative-ai/docs/model-garden/explore-models for providing model garden names. The SDK isn't familiar with custom model garden models, and will pass your model name to the backend API server.

type Content

type Content struct {
	// Optional. The producer of the content. Must be either 'user' or 'model'.
	//
	// Useful to set for multi-turn conversations, otherwise can be left blank
	// or unset.
	Role string
	// Required. Ordered `Parts` that constitute a single message. Parts may have
	// different IANA MIME types.
	Parts []Part
}

Content is the base structured datatype containing multi-part content of a message.

A `Content` includes a `role` field designating the producer of the `Content` and a `parts` field containing multi-part data that contains the content of the message turn.

type CountTokensResponse added in v0.2.0

type CountTokensResponse struct {
	// The total number of tokens counted across all instances from the request.
	TotalTokens int32
	// The total number of billable characters counted across all instances from
	// the request.
	TotalBillableCharacters int32
}

CountTokensResponse is response message for [PredictionService.CountTokens][google.cloud.aiplatform.v1beta1.PredictionService.CountTokens].

type FileData

type FileData struct {
	// Required. The IANA standard MIME type of the source data.
	MIMEType string
	// Required. URI.
	FileURI string
}

FileData is URI based data.

type FinishReason

type FinishReason int32

FinishReason is the reason why the model stopped generating tokens. If empty, the model has not stopped generating the tokens.

const (
	// FinishReasonUnspecified means the finish reason is unspecified.
	FinishReasonUnspecified FinishReason = 0
	// FinishReasonStop means natural stop point of the model or provided stop sequence.
	FinishReasonStop FinishReason = 1
	// FinishReasonMaxTokens means the maximum number of tokens as specified in the request was reached.
	FinishReasonMaxTokens FinishReason = 2
	// FinishReasonSafety means the token generation was stopped as the response was flagged for safety
	// reasons. NOTE: When streaming the Candidate.content will be empty if
	// content filters blocked the output.
	FinishReasonSafety FinishReason = 3
	// FinishReasonRecitation means the token generation was stopped as the response was flagged for
	// unauthorized citations.
	FinishReasonRecitation FinishReason = 4
	// FinishReasonOther means all other reasons that stopped the token generation
	FinishReasonOther FinishReason = 5
	// FinishReasonBlocklist means the token generation was stopped as the response was flagged for the
	// terms which are included from the terminology blocklist.
	FinishReasonBlocklist FinishReason = 6
	// FinishReasonProhibitedContent means the token generation was stopped as the response was flagged for
	// the prohibited contents.
	FinishReasonProhibitedContent FinishReason = 7
	// FinishReasonSpii means the token generation was stopped as the response was flagged for
	// Sensitive Personally Identifiable Information (SPII) contents.
	FinishReasonSpii FinishReason = 8
)

func (FinishReason) String

func (v FinishReason) String() string

type FunctionCall added in v0.3.0

type FunctionCall struct {
	// Required. The name of the function to call.
	// Matches [FunctionDeclaration.name].
	Name string
	// Optional. Required. The function parameters and values in JSON object
	// format. See [FunctionDeclaration.parameters] for parameter details.
	Args map[string]any
}

FunctionCall is a predicted FunctionCall returned from the model that contains a string representing the [FunctionDeclaration.name] and a structured JSON object containing the parameters and their values.

type FunctionCallingConfig added in v0.8.0

type FunctionCallingConfig struct {
	// Optional. Function calling mode.
	Mode FunctionCallingMode
	// Optional. Function names to call. Only set when the Mode is ANY. Function
	// names should match [FunctionDeclaration.name]. With mode set to ANY, model
	// will predict a function call from the set of function names provided.
	AllowedFunctionNames []string
}

FunctionCallingConfig holds configuration for function calling.

type FunctionCallingMode added in v0.8.0

type FunctionCallingMode int32

FunctionCallingMode is function calling mode.

const (
	// FunctionCallingUnspecified means unspecified function calling mode. This value should not be used.
	FunctionCallingUnspecified FunctionCallingMode = 0
	// FunctionCallingAuto means default model behavior, model decides to predict either a function call
	// or a natural language repspose.
	FunctionCallingAuto FunctionCallingMode = 1
	// FunctionCallingAny means model is constrained to always predicting a function call only.
	// If "allowed_function_names" are set, the predicted function call will be
	// limited to any one of "allowed_function_names", else the predicted
	// function call will be any one of the provided "function_declarations".
	FunctionCallingAny FunctionCallingMode = 2
	// FunctionCallingNone means model will not predict any function call. Model behavior is same as when
	// not passing any function declarations.
	FunctionCallingNone FunctionCallingMode = 3
)

func (FunctionCallingMode) String added in v0.8.0

func (v FunctionCallingMode) String() string

type FunctionDeclaration added in v0.3.0

type FunctionDeclaration struct {
	// Required. The name of the function to call.
	// Must start with a letter or an underscore.
	// Must be a-z, A-Z, 0-9, or contain underscores, dots and dashes, with a
	// maximum length of 64.
	Name string
	// Optional. Description and purpose of the function.
	// Model uses it to decide how and whether to call the function.
	Description string
	// Optional. Describes the parameters to this function in JSON Schema Object
	// format. Reflects the Open API 3.03 Parameter Object. string Key: the name
	// of the parameter. Parameter names are case sensitive. Schema Value: the
	// Schema defining the type used for the parameter. For function with no
	// parameters, this can be left unset. Parameter names must start with a
	// letter or an underscore and must only contain chars a-z, A-Z, 0-9, or
	// underscores with a maximum length of 64. Example with 1 required and 1
	// optional parameter: type: OBJECT properties:
	//
	//	param1:
	//	  type: STRING
	//	param2:
	//	  type: INTEGER
	//
	// required:
	//   - param1
	Parameters *Schema
	// Optional. Describes the output from this function in JSON Schema format.
	// Reflects the Open API 3.03 Response Object. The Schema defines the type
	// used for the response value of the function.
	Response *Schema
}

FunctionDeclaration is structured representation of a function declaration as defined by the [OpenAPI 3.0 specification](https://spec.openapis.org/oas/v3.0.3). Included in this declaration are the function name and parameters. This FunctionDeclaration is a representation of a block of code that can be used as a `Tool` by the model and executed by the client.

type FunctionResponse added in v0.3.0

type FunctionResponse struct {
	// Required. The name of the function to call.
	// Matches [FunctionDeclaration.name] and [FunctionCall.name].
	Name string
	// Required. The function response in JSON object format.
	Response map[string]any
}

FunctionResponse is the result output from a FunctionCall that contains a string representing the [FunctionDeclaration.name] and a structured JSON object containing any output from the function is used as context to the model. This should contain the result of a FunctionCall made based on model prediction.

type GenerateContentResponse

type GenerateContentResponse struct {
	// Output only. Generated candidates.
	Candidates []*Candidate
	// Output only. Content filter results for a prompt sent in the request.
	// Note: Sent only in the first stream chunk.
	// Only happens when no candidates were generated due to content violations.
	PromptFeedback *PromptFeedback
	// Usage metadata about the response(s).
	UsageMetadata *UsageMetadata
}

GenerateContentResponse is the response from a GenerateContent or GenerateContentStream call.

type GenerateContentResponseIterator

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

GenerateContentResponseIterator is an iterator over GnerateContentResponse.

func (*GenerateContentResponseIterator) Next

Next returns the next response.

type GenerationConfig

type GenerationConfig struct {
	// Optional. Controls the randomness of predictions.
	Temperature *float32
	// Optional. If specified, nucleus sampling will be used.
	TopP *float32
	// Optional. If specified, top-k sampling will be used.
	TopK *int32
	// Optional. Number of candidates to generate.
	CandidateCount *int32
	// Optional. The maximum number of output tokens to generate per message.
	MaxOutputTokens *int32
	// Optional. Stop sequences.
	StopSequences []string
	// Optional. Positive penalties.
	PresencePenalty *float32
	// Optional. Frequency penalties.
	FrequencyPenalty *float32
	// Optional. Output response mimetype of the generated candidate text.
	// Supported mimetype:
	// - `text/plain`: (default) Text output.
	// - `application/json`: JSON response in the candidates.
	// The model needs to be prompted to output the appropriate response type,
	// otherwise the behavior is undefined.
	// This is a preview feature.
	ResponseMIMEType string
}

GenerationConfig is generation config.

func (*GenerationConfig) SetCandidateCount added in v0.6.0

func (c *GenerationConfig) SetCandidateCount(x int32)

SetCandidateCount sets the CandidateCount field.

func (*GenerationConfig) SetMaxOutputTokens added in v0.6.0

func (c *GenerationConfig) SetMaxOutputTokens(x int32)

SetMaxOutputTokens sets the MaxOutputTokens field.

func (*GenerationConfig) SetTemperature added in v0.6.0

func (c *GenerationConfig) SetTemperature(x float32)

SetTemperature sets the Temperature field.

func (*GenerationConfig) SetTopK added in v0.6.0

func (c *GenerationConfig) SetTopK(x int32)

SetTopK sets the TopK field.

func (*GenerationConfig) SetTopP added in v0.6.0

func (c *GenerationConfig) SetTopP(x float32)

SetTopP sets the TopP field.

type GenerativeModel

type GenerativeModel struct {
	GenerationConfig
	SafetySettings    []*SafetySetting
	Tools             []*Tool
	ToolConfig        *ToolConfig // configuration for tools
	SystemInstruction *Content
	// contains filtered or unexported fields
}

GenerativeModel is a model that can generate text. Create one with Client.GenerativeModel, then configure it by setting the exported fields.

The model holds all the config for a GenerateContentRequest, so the GenerateContent method can use a vararg for the content.

func (*GenerativeModel) CountTokens added in v0.2.0

func (m *GenerativeModel) CountTokens(ctx context.Context, parts ...Part) (*CountTokensResponse, error)

CountTokens counts the number of tokens in the content.

Example
package main

import (
	"context"
	"fmt"
	"log"

	"cloud.google.com/go/vertexai/genai"
)

// Your GCP project
const projectID = "your-project"

// A GCP location like "us-central1"; if you're using standard Google-published
// models (like untuned Gemini models), you can keep location blank ("").
const location = "some-gcp-location"

// A model name like "gemini-1.0-pro"
// For custom models from different publishers, prepent the full publisher
// prefix for the model, e.g.:
//
//	model = publishers/some-publisher/models/some-model-name
const model = "some-model"

func main() {
	ctx := context.Background()
	client, err := genai.NewClient(ctx, projectID, location)
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	model := client.GenerativeModel(model)

	resp, err := model.CountTokens(ctx, genai.Text("What kind of fish is this?"))
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println("Num tokens:", resp.TotalTokens)
}
Output:

func (*GenerativeModel) GenerateContent

func (m *GenerativeModel) GenerateContent(ctx context.Context, parts ...Part) (*GenerateContentResponse, error)

GenerateContent produces a single request and response.

Example
package main

import (
	"context"
	"fmt"
	"log"

	"cloud.google.com/go/vertexai/genai"
)

// Your GCP project
const projectID = "your-project"

// A GCP location like "us-central1"; if you're using standard Google-published
// models (like untuned Gemini models), you can keep location blank ("").
const location = "some-gcp-location"

// A model name like "gemini-1.0-pro"
// For custom models from different publishers, prepent the full publisher
// prefix for the model, e.g.:
//
//	model = publishers/some-publisher/models/some-model-name
const model = "some-model"

func main() {
	ctx := context.Background()
	client, err := genai.NewClient(ctx, projectID, location)
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	model := client.GenerativeModel(model)
	model.SetTemperature(0.9)
	resp, err := model.GenerateContent(ctx, genai.Text("What is the average size of a swallow?"))
	if err != nil {
		log.Fatal(err)
	}

	printResponse(resp)
}

func printResponse(resp *genai.GenerateContentResponse) {
	for _, cand := range resp.Candidates {
		for _, part := range cand.Content.Parts {
			fmt.Println(part)
		}
	}
	fmt.Println("---")
}
Output:

Example (Config)

This example shows how to a configure a model. See GenerationConfig for the complete set of configuration options.

package main

import (
	"context"
	"fmt"
	"log"

	"cloud.google.com/go/vertexai/genai"
)

func main() {
	ctx := context.Background()
	const projectID = "YOUR PROJECT ID"
	const location = "GCP LOCATION"
	client, err := genai.NewClient(ctx, projectID, location)
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	model := client.GenerativeModel("gemini-1.0-pro")
	model.SetTemperature(0.9)
	model.SetTopP(0.5)
	model.SetTopK(20)
	model.SetMaxOutputTokens(100)
	model.SystemInstruction = &genai.Content{
		Parts: []genai.Part{genai.Text("You are Yoda from Star Wars.")},
	}
	resp, err := model.GenerateContent(ctx, genai.Text("What is the average size of a swallow?"))
	if err != nil {
		log.Fatal(err)
	}
	printResponse(resp)
}

func printResponse(resp *genai.GenerateContentResponse) {
	for _, cand := range resp.Candidates {
		for _, part := range cand.Content.Parts {
			fmt.Println(part)
		}
	}
	fmt.Println("---")
}
Output:

func (*GenerativeModel) GenerateContentStream

func (m *GenerativeModel) GenerateContentStream(ctx context.Context, parts ...Part) *GenerateContentResponseIterator

GenerateContentStream returns an iterator that enumerates responses.

Example
package main

import (
	"context"
	"fmt"
	"log"

	"cloud.google.com/go/vertexai/genai"

	"google.golang.org/api/iterator"
)

// Your GCP project
const projectID = "your-project"

// A GCP location like "us-central1"; if you're using standard Google-published
// models (like untuned Gemini models), you can keep location blank ("").
const location = "some-gcp-location"

// A model name like "gemini-1.0-pro"
// For custom models from different publishers, prepent the full publisher
// prefix for the model, e.g.:
//
//	model = publishers/some-publisher/models/some-model-name
const model = "some-model"

func main() {
	ctx := context.Background()
	client, err := genai.NewClient(ctx, projectID, location)
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	model := client.GenerativeModel(model)

	iter := model.GenerateContentStream(ctx, genai.Text("Tell me a story about a lumberjack and his giant ox. Keep it very short."))
	for {
		resp, err := iter.Next()
		if err == iterator.Done {
			break
		}
		if err != nil {
			log.Fatal(err)
		}
		printResponse(resp)
	}
}

func printResponse(resp *genai.GenerateContentResponse) {
	for _, cand := range resp.Candidates {
		for _, part := range cand.Content.Parts {
			fmt.Println(part)
		}
	}
	fmt.Println("---")
}
Output:

func (*GenerativeModel) Name

func (m *GenerativeModel) Name() string

Name returns the name of the model.

func (*GenerativeModel) StartChat

func (m *GenerativeModel) StartChat() *ChatSession

StartChat starts a chat session.

type HarmBlockMethod added in v0.8.0

type HarmBlockMethod int32

HarmBlockMethod determines how harm blocking is done.

const (
	// HarmBlockMethodUnspecified means the harm block method is unspecified.
	HarmBlockMethodUnspecified HarmBlockMethod = 0
	// HarmBlockMethodSeverity means the harm block method uses both probability and severity scores.
	HarmBlockMethodSeverity HarmBlockMethod = 1
	// HarmBlockMethodProbability means the harm block method uses the probability score.
	HarmBlockMethodProbability HarmBlockMethod = 2
)

func (HarmBlockMethod) String added in v0.8.0

func (v HarmBlockMethod) String() string

type HarmBlockThreshold

type HarmBlockThreshold int32

HarmBlockThreshold specifies probability based thresholds levels for blocking.

const (
	// HarmBlockUnspecified means unspecified harm block threshold.
	HarmBlockUnspecified HarmBlockThreshold = 0
	// HarmBlockLowAndAbove means block low threshold and above (i.e. block more).
	HarmBlockLowAndAbove HarmBlockThreshold = 1
	// HarmBlockMediumAndAbove means block medium threshold and above.
	HarmBlockMediumAndAbove HarmBlockThreshold = 2
	// HarmBlockOnlyHigh means block only high threshold (i.e. block less).
	HarmBlockOnlyHigh HarmBlockThreshold = 3
	// HarmBlockNone means block none.
	HarmBlockNone HarmBlockThreshold = 4
)

func (HarmBlockThreshold) String added in v0.3.0

func (v HarmBlockThreshold) String() string

type HarmCategory

type HarmCategory int32

HarmCategory specifies harm categories that will block the content.

const (
	// HarmCategoryUnspecified means the harm category is unspecified.
	HarmCategoryUnspecified HarmCategory = 0
	// HarmCategoryHateSpeech means the harm category is hate speech.
	HarmCategoryHateSpeech HarmCategory = 1
	// HarmCategoryDangerousContent means the harm category is dangerous content.
	HarmCategoryDangerousContent HarmCategory = 2
	// HarmCategoryHarassment means the harm category is harassment.
	HarmCategoryHarassment HarmCategory = 3
	// HarmCategorySexuallyExplicit means the harm category is sexually explicit content.
	HarmCategorySexuallyExplicit HarmCategory = 4
)

func (HarmCategory) String added in v0.3.0

func (v HarmCategory) String() string

type HarmProbability

type HarmProbability int32

HarmProbability specifies harm probability levels in the content.

const (
	// HarmProbabilityUnspecified means harm probability unspecified.
	HarmProbabilityUnspecified HarmProbability = 0
	// HarmProbabilityNegligible means negligible level of harm.
	HarmProbabilityNegligible HarmProbability = 1
	// HarmProbabilityLow means low level of harm.
	HarmProbabilityLow HarmProbability = 2
	// HarmProbabilityMedium means medium level of harm.
	HarmProbabilityMedium HarmProbability = 3
	// HarmProbabilityHigh means high level of harm.
	HarmProbabilityHigh HarmProbability = 4
)

func (HarmProbability) String added in v0.3.0

func (v HarmProbability) String() string

type HarmSeverity added in v0.8.0

type HarmSeverity int32

HarmSeverity specifies harm severity levels.

const (
	// HarmSeverityUnspecified means harm severity unspecified.
	HarmSeverityUnspecified HarmSeverity = 0
	// HarmSeverityNegligible means negligible level of harm severity.
	HarmSeverityNegligible HarmSeverity = 1
	// HarmSeverityLow means low level of harm severity.
	HarmSeverityLow HarmSeverity = 2
	// HarmSeverityMedium means medium level of harm severity.
	HarmSeverityMedium HarmSeverity = 3
	// HarmSeverityHigh means high level of harm severity.
	HarmSeverityHigh HarmSeverity = 4
)

func (HarmSeverity) String added in v0.8.0

func (v HarmSeverity) String() string

type Part

type Part interface {
	// contains filtered or unexported methods
}

A Part is either a Text, a Blob, or a FileData.

type PromptFeedback

type PromptFeedback struct {
	// Output only. Blocked reason.
	BlockReason BlockedReason
	// Output only. Safety ratings.
	SafetyRatings []*SafetyRating
	// Output only. A readable block reason message.
	BlockReasonMessage string
}

PromptFeedback contains content filter results for a prompt sent in the request.

type SafetyRating

type SafetyRating struct {
	// Output only. Harm category.
	Category HarmCategory
	// Output only. Harm probability levels in the content.
	Probability HarmProbability
	// Output only. Harm probability score.
	ProbabilityScore float32
	// Output only. Harm severity levels in the content.
	Severity HarmSeverity
	// Output only. Harm severity score.
	SeverityScore float32
	// Output only. Indicates whether the content was filtered out because of this
	// rating.
	Blocked bool
}

SafetyRating is the safety rating corresponding to the generated content.

type SafetySetting

type SafetySetting struct {
	// Required. Harm category.
	Category HarmCategory
	// Required. The harm block threshold.
	Threshold HarmBlockThreshold
	// Optional. Specify if the threshold is used for probability or severity
	// score. If not specified, the threshold is used for probability score.
	Method HarmBlockMethod
}

SafetySetting is safety settings.

type Schema added in v0.3.0

type Schema struct {
	// Optional. The type of the data.
	Type Type
	// Optional. The format of the data.
	// Supported formats:
	//
	//	for NUMBER type: "float", "double"
	//	for INTEGER type: "int32", "int64"
	//	for STRING type: "email", "byte", etc
	Format string
	// Optional. The title of the Schema.
	Title string
	// Optional. The description of the data.
	Description string
	// Optional. Indicates if the value may be null.
	Nullable bool
	// Optional. SCHEMA FIELDS FOR TYPE ARRAY
	// Schema of the elements of Type.ARRAY.
	Items *Schema
	// Optional. Minimum number of the elements for Type.ARRAY.
	MinItems int64
	// Optional. Maximum number of the elements for Type.ARRAY.
	MaxItems int64
	// Optional. Possible values of the element of Type.STRING with enum format.
	// For example we can define an Enum Direction as :
	// {type:STRING, format:enum, enum:["EAST", NORTH", "SOUTH", "WEST"]}
	Enum []string
	// Optional. SCHEMA FIELDS FOR TYPE OBJECT
	// Properties of Type.OBJECT.
	Properties map[string]*Schema
	// Optional. Required properties of Type.OBJECT.
	Required []string
	// Optional. Minimum number of the properties for Type.OBJECT.
	MinProperties int64
	// Optional. Maximum number of the properties for Type.OBJECT.
	MaxProperties int64
	// Optional. SCHEMA FIELDS FOR TYPE INTEGER and NUMBER
	// Minimum value of the Type.INTEGER and Type.NUMBER
	Minimum float64
	// Optional. Maximum value of the Type.INTEGER and Type.NUMBER
	Maximum float64
	// Optional. SCHEMA FIELDS FOR TYPE STRING
	// Minimum length of the Type.STRING
	MinLength int64
	// Optional. Maximum length of the Type.STRING
	MaxLength int64
	// Optional. Pattern of the Type.STRING to restrict a string to a regular
	// expression.
	Pattern string
}

Schema is used to define the format of input/output data. Represents a select subset of an [OpenAPI 3.0 schema object](https://spec.openapis.org/oas/v3.0.3#schema). More fields may be added in the future as needed.

type Text

type Text string

A Text is a piece of text, like a question or phrase.

type Tool added in v0.3.0

type Tool struct {
	// Optional. Function tool type.
	// One or more function declarations to be passed to the model along with the
	// current user query. Model may decide to call a subset of these functions
	// by populating [FunctionCall][content.part.function_call] in the response.
	// User should provide a [FunctionResponse][content.part.function_response]
	// for each function call in the next turn. Based on the function responses,
	// Model will generate the final response back to the user.
	// Maximum 64 function declarations can be provided.
	FunctionDeclarations []*FunctionDeclaration
}

Tool details that the model may use to generate response.

A `Tool` is a piece of code that enables the system to interact with external systems to perform an action, or set of actions, outside of knowledge and scope of the model. A Tool object should contain exactly one type of Tool (e.g FunctionDeclaration, Retrieval or GoogleSearchRetrieval).

Example
package main

import (
	"context"
	"fmt"
	"log"

	"cloud.google.com/go/vertexai/genai"
)

// Your GCP project
const projectID = "your-project"

// A GCP location like "us-central1"; if you're using standard Google-published
// models (like untuned Gemini models), you can keep location blank ("").
const location = "some-gcp-location"

func main() {
	ctx := context.Background()
	client, err := genai.NewClient(ctx, projectID, location)
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	currentWeather := func(city string) string {
		switch city {
		case "New York, NY":
			return "cold"
		case "Miami, FL":
			return "warm"
		default:
			return "unknown"
		}
	}

	// To use functions / tools, we have to first define a schema that describes
	// the function to the model. The schema is similar to OpenAPI 3.0.
	//
	// In this example, we create a single function that provides the model with
	// a weather forecast in a given location.
	schema := &genai.Schema{
		Type: genai.TypeObject,
		Properties: map[string]*genai.Schema{
			"location": {
				Type:        genai.TypeString,
				Description: "The city and state, e.g. San Francisco, CA",
			},
			"unit": {
				Type: genai.TypeString,
				Enum: []string{"celsius", "fahrenheit"},
			},
		},
		Required: []string{"location"},
	}

	weatherTool := &genai.Tool{
		FunctionDeclarations: []*genai.FunctionDeclaration{{
			Name:        "CurrentWeather",
			Description: "Get the current weather in a given location",
			Parameters:  schema,
		}},
	}

	model := client.GenerativeModel("gemini-1.0-pro")

	// Before initiating a conversation, we tell the model which tools it has
	// at its disposal.
	model.Tools = []*genai.Tool{weatherTool}

	// For using tools, the chat mode is useful because it provides the required
	// chat context. A model needs to have tools supplied to it in the chat
	// history so it can use them in subsequent conversations.
	//
	// The flow of message expected here is:
	//
	// 1. We send a question to the model
	// 2. The model recognizes that it needs to use a tool to answer the question,
	//    an returns a FunctionCall response asking to use the CurrentWeather
	//    tool.
	// 3. We send a FunctionResponse message, simulating the return value of
	//    CurrentWeather for the model's query.
	// 4. The model provides its text answer in response to this message.
	session := model.StartChat()

	res, err := session.SendMessage(ctx, genai.Text("What is the weather like in New York?"))
	if err != nil {
		log.Fatal(err)
	}

	part := res.Candidates[0].Content.Parts[0]
	funcall, ok := part.(genai.FunctionCall)
	if !ok {
		log.Fatalf("expected FunctionCall: %v", part)
	}

	if funcall.Name != "CurrentWeather" {
		log.Fatalf("expected CurrentWeather: %v", funcall.Name)
	}

	// Expect the model to pass a proper string "location" argument to the tool.
	locArg, ok := funcall.Args["location"].(string)
	if !ok {
		log.Fatalf("expected string: %v", funcall.Args["location"])
	}

	weatherData := currentWeather(locArg)
	res, err = session.SendMessage(ctx, genai.FunctionResponse{
		Name: weatherTool.FunctionDeclarations[0].Name,
		Response: map[string]any{
			"weather": weatherData,
		},
	})
	if err != nil {
		log.Fatal(err)
	}

	printResponse(res)
}

func printResponse(resp *genai.GenerateContentResponse) {
	for _, cand := range resp.Candidates {
		for _, part := range cand.Content.Parts {
			fmt.Println(part)
		}
	}
	fmt.Println("---")
}
Output:

type ToolConfig added in v0.8.0

type ToolConfig struct {
	// Optional. Function calling config.
	FunctionCallingConfig *FunctionCallingConfig
}

ToolConfig configures tools.

type Type added in v0.3.0

type Type int32

Type contains the list of OpenAPI data types as defined by https://swagger.io/docs/specification/data-models/data-types/

const (
	// TypeUnspecified means not specified, should not be used.
	TypeUnspecified Type = 0
	// TypeString means openAPI string type
	TypeString Type = 1
	// TypeNumber means openAPI number type
	TypeNumber Type = 2
	// TypeInteger means openAPI integer type
	TypeInteger Type = 3
	// TypeBoolean means openAPI boolean type
	TypeBoolean Type = 4
	// TypeArray means openAPI array type
	TypeArray Type = 5
	// TypeObject means openAPI object type
	TypeObject Type = 6
)

func (Type) String added in v0.3.0

func (v Type) String() string

type UsageMetadata added in v0.4.0

type UsageMetadata struct {
	// Number of tokens in the request.
	PromptTokenCount int32
	// Number of tokens in the response(s).
	CandidatesTokenCount int32
	TotalTokenCount      int32
}

UsageMetadata is usage metadata about response(s).

Jump to

Keyboard shortcuts

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