genai

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 7, 2023 License: Apache-2.0 Imports: 11 Imported by: 144

Documentation

Overview

Package genai is a client for the generative VertexAI model.

Index

Examples

Constants

Constants for BlockedReason.

Constants for HarmCategory.

Constants for HarmBlock.

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

Constants for HarmProbability.

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

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 doc TBD.

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

type Content

type Content struct {
	Role  string
	Parts []Part
}

Content doc TBD.

type FileData

type FileData struct {
	MIMEType string
	FileURI  string
}

FileData doc TBD.

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

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 HarmBlockThreshold

type HarmBlockThreshold int32

HarmBlockThreshold doc TBD.

type HarmCategory

type HarmCategory int32

HarmCategory doc TBD.

type HarmProbability

type HarmProbability int32

HarmProbability doc TBD.

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.

type Text

type Text string

Text doc TBD.

Jump to

Keyboard shortcuts

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