Documentation
¶
Overview ¶
Package genai is a client for the generative VertexAI model.
Index ¶
- Constants
- type Blob
- type BlockedError
- type BlockedReason
- type Candidate
- type ChatSession
- type Citation
- type CitationMetadata
- type Client
- type Content
- type FileData
- type FinishReason
- type GenerateContentResponse
- type GenerateContentResponseIterator
- type GenerationConfig
- type GenerativeModel
- func (m *GenerativeModel) GenerateContent(ctx context.Context, parts ...Part) (*GenerateContentResponse, error)
- func (m *GenerativeModel) GenerateContentStream(ctx context.Context, parts ...Part) *GenerateContentResponseIterator
- func (m *GenerativeModel) Name() string
- func (m *GenerativeModel) StartChat() *ChatSession
- type HarmBlockThreshold
- type HarmCategory
- type HarmProbability
- type Part
- type PromptFeedback
- type SafetyRating
- type SafetySetting
- type Text
Examples ¶
Constants ¶
const ( BlockedReasonSafety = BlockedReason(pb.GenerateContentResponse_PromptFeedback_SAFETY) BlockedReasonOther = BlockedReason(pb.GenerateContentResponse_PromptFeedback_OTHER) )
Constants for BlockedReason.
const ( HarmCategoryHateSpeech = HarmCategory(pb.HarmCategory_HARM_CATEGORY_HATE_SPEECH) HarmCategoryDangerousContent = HarmCategory(pb.HarmCategory_HARM_CATEGORY_DANGEROUS_CONTENT) HarmCategoryHarassment = HarmCategory(pb.HarmCategory_HARM_CATEGORY_HARASSMENT) HarmCategorySexuallyExplicit = HarmCategory(pb.HarmCategory_HARM_CATEGORY_SEXUALLY_EXPLICIT) )
Constants for HarmCategory.
const ( HarmBlockLowAndAbove = HarmBlockThreshold(pb.SafetySetting_BLOCK_LOW_AND_ABOVE) HarmBlockMediumAndAbove = HarmBlockThreshold(pb.SafetySetting_BLOCK_MEDIUM_AND_ABOVE) HarmBlockOnlyHigh = HarmBlockThreshold(pb.SafetySetting_BLOCK_ONLY_HIGH) HarmBlockNone = HarmBlockThreshold(pb.SafetySetting_BLOCK_NONE) )
Constants for HarmBlock.
const ( HarmProbabilityNegligible = HarmProbability(pb.SafetyRating_NEGLIGIBLE) HarmProbabilityLow = HarmProbability(pb.SafetyRating_LOW) HarmProbabilityMedium = HarmProbability(pb.SafetyRating_MEDIUM) HarmProbabilityHigh = HarmProbability(pb.SafetyRating_HIGH) )
Constants for HarmProbability.
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.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
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 Candidate ¶
type Candidate struct { Index int32 Content *Content FinishReason FinishReason //FinishMessage string SafetyRatings []*SafetyRating CitationMetadata *CitationMetadata }
Candidate doc TBD.
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" ) 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("---") }
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 {
StartIndex, EndIndex int32
URI string
Title string
License string
PublicationDate civil.Date
}
Citation doc TBD.
type CitationMetadata ¶
type CitationMetadata struct {
Citations []*Citation
}
CitationMetadata doc TBD.
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.
You may configure the client by passing in options from the google.golang.org/api/option package.
func (*Client) GenerativeModel ¶
func (c *Client) GenerativeModel(name string) *GenerativeModel
GenerativeModel creates a new instance of the named model.
type 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
type GenerateContentResponse ¶
type GenerateContentResponse struct { Candidates []*Candidate PromptFeedback *PromptFeedback }
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 ¶
func (iter *GenerateContentResponseIterator) Next() (*GenerateContentResponse, error)
Next returns the next response.
type 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.
type 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("---") }
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" ) 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("---") }
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 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 { BlockReason BlockedReason BlockReasonMessage string SafetyRatings []*SafetyRating }
PromptFeedback is feedback about a prompt.
type SafetyRating ¶
type SafetyRating struct { Category HarmCategory Probability HarmProbability Blocked bool }
SafetyRating doc TBD.
type SafetySetting ¶
type SafetySetting struct { Category HarmCategory Threshold HarmBlockThreshold }
SafetySetting doc TBD.