Package cloud.google.com/go/vertexai/genai (v0.1.1)

Package genai is a client for the generative VertexAI model.

Constants

BlockedReasonSafety, BlockedReasonOther

Constants for BlockedReason.

HarmCategoryHateSpeech, HarmCategoryDangerousContent, HarmCategoryHarassment, HarmCategorySexuallyExplicit

Constants for HarmCategory.

HarmBlockLowAndAbove, HarmBlockMediumAndAbove, HarmBlockOnlyHigh, HarmBlockNone

Constants for HarmBlock.

HarmProbabilityNegligible, HarmProbabilityLow, HarmProbabilityMedium, HarmProbabilityHigh

const (
	HarmProbabilityNegligible = HarmProbability(pb.SafetyRating_NEGLIGIBLE)
	HarmProbabilityLow        = HarmProbability(pb.SafetyRating_LOW)
	HarmProbabilityMedium     = HarmProbability(pb.SafetyRating_MEDIUM)
	HarmProbabilityHigh       = HarmProbability(pb.SafetyRating_HIGH)
)

Constants for HarmProbability.

FinishReasonUnspecified, FinishReasonStop, FinishReasonMaxTokens, FinishReasonSafety, FinishReasonRecitation, FinishReasonOther

const (
	FinishReasonUnspecified = FinishReason(pb.Candidate_FINISH_REASON_UNSPECIFIED)
	FinishReasonStop        = FinishReason(pb.Candidate_STOP)
	FinishReasonMaxTokens   = FinishReason(pb.Candidate_MAX_TOKENS)
	FinishReasonSafety      = FinishReason(pb.Candidate_SAFETY)
	FinishReasonRecitation  = FinishReason(pb.Candidate_RECITATION)
	FinishReasonOther       = FinishReason(pb.Candidate_OTHER)
)

Constants for FinishReason.

Blob

type Blob struct {
	MIMEType string
	Data     []byte
}

Blob doc TBD.

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".

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

BlockedReason

type BlockedReason int32

BlockedReason doc TBD.

Candidate

type Candidate struct {
	Index        int32
	Content      *Content
	FinishReason FinishReason
	//FinishMessage    string
	SafetyRatings    []*SafetyRating
	CitationMetadata *CitationMetadata
}

Candidate doc TBD.

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"
)

const projectID = "your-project"
const model = "some-model"
const location = "some-location"

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("---")
}

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.

Citation

type Citation struct {
	StartIndex, EndIndex int32
	URI                  string
	Title                string
	License              string
	PublicationDate      civil.Date
}

Citation doc TBD.

CitationMetadata

type CitationMetadata struct {
	Citations []*Citation
}

CitationMetadata doc TBD.

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.

You may configure the client by passing in options from the [google.golang.org/api/option] package.

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.

Content

type Content struct {
	Role  string
	Parts []Part
}

Content doc TBD.

FileData

type FileData struct {
	MIMEType string
	FileURI  string
}

FileData doc TBD.

FinishReason

type FinishReason int32

FinishReason doc TBD.

func (FinishReason) MarshalJSON

func (f FinishReason) MarshalJSON() ([]byte, error)

MarshalJSON implements [encoding/json.Marshaler].

func (FinishReason) String

func (f FinishReason) String() string

GenerateContentResponse

type GenerateContentResponse struct {
	Candidates     []*Candidate
	PromptFeedback *PromptFeedback
}

GenerateContentResponse is the response from a GenerateContent or GenerateContentStream call.

GenerateContentResponseIterator

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

GenerateContentResponseIterator is an iterator over GnerateContentResponse.

func (*GenerateContentResponseIterator) Next

Next returns the next response.

GenerationConfig

type GenerationConfig struct {
	Temperature      float32
	TopP             float32 // if non-zero, use nucleus sampling
	TopK             float32 // if non-zero, use top-K sampling
	CandidateCount   int32
	MaxOutputTokens  int32
	StopSequences    []string
	Logprobs         int32
	PresencePenalty  float32
	FrequencyPenalty float32
	LogitBias        map[string]float32
	Echo             bool
}

GenerationConfig doc TBD.

GenerativeModel

type GenerativeModel struct {
	GenerationConfig
	SafetySettings []*SafetySetting
	// 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) 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"
)

const projectID = "your-project"
const model = "some-model"
const location = "some-location"

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.Temperature = 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("---")
}

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"
)

const projectID = "your-project"
const model = "some-model"
const location = "some-location"

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("---")
}

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.

HarmBlockThreshold

type HarmBlockThreshold int32

HarmBlockThreshold doc TBD.

HarmCategory

type HarmCategory int32

HarmCategory doc TBD.

HarmProbability

type HarmProbability int32

HarmProbability doc TBD.

Part

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

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

PromptFeedback

type PromptFeedback struct {
	BlockReason        BlockedReason
	BlockReasonMessage string
	SafetyRatings      []*SafetyRating
}

PromptFeedback is feedback about a prompt.

SafetyRating

type SafetyRating struct {
	Category    HarmCategory
	Probability HarmProbability
	Blocked     bool
}

SafetyRating doc TBD.

SafetySetting

type SafetySetting struct {
	Category  HarmCategory
	Threshold HarmBlockThreshold
}

SafetySetting doc TBD.

Text

type Text string

Text doc TBD.