genai

package
v0.19.0 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2024 License: Apache-2.0, Apache-2.0 Imports: 25 Imported by: 322

Documentation

Overview

Package genai is a client for the Google AI generative models.

NOTE: This client uses the v1beta version of the API.

Getting started

Reading the examples is the best way to learn how to use this package.

Authorization

You will need an API key to use the service. See the setup tutorial for details.

Errors

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Ptr added in v0.5.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 WithClientInfo added in v0.17.0

func WithClientInfo(key, value string) option.ClientOption

WithClientInfo sets request information identifying the product that is calling this client.

Types

type BatchEmbedContentsResponse added in v0.6.0

type BatchEmbedContentsResponse struct {
	// Output only. The embeddings for each request, in the same order as provided
	// in the batch request.
	Embeddings []*ContentEmbedding
}

BatchEmbedContentsResponse is the response to a `BatchEmbedContentsRequest`.

type Blob

type Blob struct {
	// The IANA standard MIME type of the source data.
	// Examples:
	//   - image/png
	//   - image/jpeg
	//
	// If an unsupported MIME type is provided, an error will be returned. For a
	// complete list of supported types, see [Supported file
	// formats](https://ai.google.dev/gemini-api/docs/prompting_with_media#supported_file_formats).
	MIMEType string
	// Raw bytes for media formats.
	Data []byte
}

Blob contains raw media bytes.

Text should not be sent as raw bytes, use the 'text' field.

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 BlockReason

type BlockReason int32

BlockReason is specifies what was the reason why prompt was blocked.

const (
	// BlockReasonUnspecified means default value. This value is unused.
	BlockReasonUnspecified BlockReason = 0
	// BlockReasonSafety means prompt was blocked due to safety reasons. You can inspect
	// `safety_ratings` to understand which safety category blocked it.
	BlockReasonSafety BlockReason = 1
	// BlockReasonOther means prompt was blocked due to unknown reasons.
	BlockReasonOther BlockReason = 2
)

func (BlockReason) String

func (v BlockReason) String() string

type BlockedError

type BlockedError struct {
	// If non-nil, the model's response was blocked.
	// Consult the FinishReason field 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 CachedContent added in v0.15.0

type CachedContent struct {
	// Specifies when this resource will expire.
	//
	// Types that are assignable to Expiration:
	//
	//	*CachedContent_ExpireTime
	//	*CachedContent_Ttl
	Expiration ExpireTimeOrTTL
	// Optional. Identifier. The resource name referring to the cached content.
	// Format: `cachedContents/{id}`
	Name string
	// Optional. Immutable. The user-generated meaningful display name of the
	// cached content. Maximum 128 Unicode characters.
	DisplayName string
	// Required. Immutable. The name of the `Model` to use for cached content
	// Format: `models/{model}`
	Model string
	// Optional. Input only. Immutable. Developer set system instruction.
	// Currently text only.
	SystemInstruction *Content
	// Optional. Input only. Immutable. The content to cache.
	Contents []*Content
	// Optional. Input only. Immutable. A list of `Tools` the model may use to
	// generate the next response
	Tools []*Tool
	// Optional. Input only. Immutable. Tool config. This config is shared for all
	// tools.
	ToolConfig *ToolConfig
	// Output only. Creation time of the cache entry.
	CreateTime time.Time
	// Output only. When the cache entry was last updated in UTC time.
	UpdateTime time.Time
	// Output only. Metadata on the usage of the cached content.
	UsageMetadata *CachedContentUsageMetadata
}

CachedContent is content that has been preprocessed and can be used in subsequent request to GenerativeService.

Cached content can be only used with model it was created for.

Example (Create)
package main

import (
	"context"
	"fmt"
	"log"
	"os"
	"path/filepath"

	"github.com/google/generative-ai-go/genai"
	"github.com/google/generative-ai-go/genai/internal/testhelpers"
	"google.golang.org/api/option"
)

var testDataDir = filepath.Join(testhelpers.ModuleRootDir(), "genai", "testdata")

func main() {
	ctx := context.Background()
	client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	file, err := client.UploadFileFromPath(ctx,
		filepath.Join(testDataDir, "a11.txt"),
		&genai.UploadFileOptions{MIMEType: "text/plain"})
	if err != nil {
		log.Fatal(err)
	}
	defer client.DeleteFile(ctx, file.Name)

	fd := genai.FileData{URI: file.URI}

	argcc := &genai.CachedContent{
		Model:             "gemini-1.5-flash-001",
		SystemInstruction: genai.NewUserContent(genai.Text("You are an expert analyzing transcripts.")),
		Contents:          []*genai.Content{genai.NewUserContent(fd)},
	}
	cc, err := client.CreateCachedContent(ctx, argcc)
	if err != nil {
		log.Fatal(err)
	}
	defer client.DeleteCachedContent(ctx, cc.Name)

	modelWithCache := client.GenerativeModelFromCachedContent(cc)
	prompt := "Please summarize this transcript"
	resp, err := modelWithCache.GenerateContent(ctx, genai.Text(prompt))
	if err != nil {
		log.Fatal(err)
	}

	printResponse(resp)

}

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

Example (CreateFromChat)
package main

import (
	"context"
	"fmt"
	"log"
	"os"
	"path/filepath"

	"github.com/google/generative-ai-go/genai"
	"github.com/google/generative-ai-go/genai/internal/testhelpers"
	"google.golang.org/api/option"
)

var testDataDir = filepath.Join(testhelpers.ModuleRootDir(), "genai", "testdata")

func main() {
	ctx := context.Background()
	client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	file, err := client.UploadFileFromPath(ctx, filepath.Join(testDataDir, "a11.txt"), nil)
	if err != nil {
		log.Fatal(err)
	}
	defer client.DeleteFile(ctx, file.Name)
	fd := genai.FileData{URI: file.URI}

	modelName := "gemini-1.5-flash-001"
	model := client.GenerativeModel(modelName)
	model.SystemInstruction = genai.NewUserContent(genai.Text("You are an expert analyzing transcripts."))

	cs := model.StartChat()
	resp, err := cs.SendMessage(ctx, genai.Text("Hi, could you summarize this transcript?"), fd)
	if err != nil {
		log.Fatal(err)
	}

	resp, err = cs.SendMessage(ctx, genai.Text("Okay, could you tell me more about the trans-lunar injection"))
	if err != nil {
		log.Fatal(err)
	}

	// To cache the conversation so far, pass the chat history as the list of
	// contents.

	argcc := &genai.CachedContent{
		Model:             modelName,
		SystemInstruction: model.SystemInstruction,
		Contents:          cs.History,
	}
	cc, err := client.CreateCachedContent(ctx, argcc)
	if err != nil {
		log.Fatal(err)
	}
	defer client.DeleteCachedContent(ctx, cc.Name)

	modelWithCache := client.GenerativeModelFromCachedContent(cc)
	cs = modelWithCache.StartChat()
	resp, err = cs.SendMessage(ctx, genai.Text("I didn't understand that last part, could you please explain it in simpler language?"))
	if err != nil {
		log.Fatal(err)
	}
	printResponse(resp)

}

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

type CachedContentIterator added in v0.15.0

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

A CachedContentIterator iterates over CachedContents.

func (*CachedContentIterator) Next added in v0.15.0

func (it *CachedContentIterator) Next() (*CachedContent, error)

Next returns the next result. Its second return value is iterator.Done if there are no more results. Once Next returns Done, all subsequent calls will return Done.

func (*CachedContentIterator) PageInfo added in v0.15.0

func (it *CachedContentIterator) PageInfo() *iterator.PageInfo

PageInfo supports pagination. See the google.golang.org/api/iterator package for details.

type CachedContentToUpdate added in v0.15.0

type CachedContentToUpdate struct {
	// If non-nil, update the expire time or TTL.
	Expiration *ExpireTimeOrTTL
}

CachedContentToUpdate specifies which fields of a CachedContent to modify in a call to Client.UpdateCachedContent.

type CachedContentUsageMetadata added in v0.15.0

type CachedContentUsageMetadata struct {
	// Total number of tokens that the cached content consumes.
	TotalTokenCount int32
}

CachedContentUsageMetadata is metadata on the usage of the cached content.

type Candidate

type Candidate struct {
	// Output only. Index of the candidate in the list of candidates.
	Index int32
	// Output only. Generated content returned from the model.
	Content *Content
	// Optional. Output only. The reason why the model stopped generating tokens.
	//
	// If empty, the model has not stopped generating the tokens.
	FinishReason FinishReason
	// List of ratings for the safety of a response candidate.
	//
	// There is at most one rating per category.
	SafetyRatings []*SafetyRating
	// Output only. Citation information for model-generated candidate.
	//
	// This field may be populated with recitation information for any text
	// included in the `content`. These are passages that are "recited" from
	// copyrighted material in the foundational LLM's training data.
	CitationMetadata *CitationMetadata
	// Output only. Token count for this candidate.
	TokenCount int32
}

Candidate is a response candidate generated from the model.

func (*Candidate) FunctionCalls added in v0.11.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 (History)

This example shows how to set the History field on ChatSession explicitly.

package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"github.com/google/generative-ai-go/genai"
	"google.golang.org/api/option"
)

func main() {
	ctx := context.Background()
	client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	model := client.GenerativeModel("gemini-1.5-flash")
	cs := model.StartChat()

	cs.History = []*genai.Content{
		{
			Parts: []genai.Part{
				genai.Text("Hello, I have 2 dogs in my house."),
			},
			Role: "user",
		},
		{
			Parts: []genai.Part{
				genai.Text("Great to meet you. What would you like to know?"),
			},
			Role: "model",
		},
	}

	res, err := cs.SendMessage(ctx, genai.Text("How many paws are in my house?"))
	if err != nil {
		log.Fatal(err)
	}
	printResponse(res)

}

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

Example (Streaming)
package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"github.com/google/generative-ai-go/genai"
	"google.golang.org/api/iterator"
	"google.golang.org/api/option"
)

func main() {
	ctx := context.Background()
	client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	model := client.GenerativeModel("gemini-1.5-flash")
	cs := model.StartChat()

	cs.History = []*genai.Content{
		{
			Parts: []genai.Part{
				genai.Text("Hello, I have 2 dogs in my house."),
			},
			Role: "user",
		},
		{
			Parts: []genai.Part{
				genai.Text("Great to meet you. What would you like to know?"),
			},
			Role: "model",
		},
	}

	iter := cs.SendMessageStream(ctx, genai.Text("How many paws are in my house?"))
	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 {
		if cand.Content != nil {
			for _, part := range cand.Content.Parts {
				fmt.Println(part)
			}
		}
	}
	fmt.Println("---")
}
Output:

Example (StreamingWithImage)
package main

import (
	"context"
	"fmt"
	"log"
	"os"
	"path/filepath"

	"github.com/google/generative-ai-go/genai"
	"github.com/google/generative-ai-go/genai/internal/testhelpers"
	"google.golang.org/api/iterator"
	"google.golang.org/api/option"
)

var testDataDir = filepath.Join(testhelpers.ModuleRootDir(), "genai", "testdata")

func main() {
	ctx := context.Background()
	client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	model := client.GenerativeModel("gemini-1.5-flash")
	cs := model.StartChat()

	cs.SendMessage(ctx, genai.Text("Hello, I'm interested in learning about musical instruments. Can I show you one?"))

	imgData, err := os.ReadFile(filepath.Join(testDataDir, "organ.jpg"))
	if err != nil {
		log.Fatal(err)
	}

	iter := cs.SendMessageStream(ctx,
		genai.Text("What family of instruments does this instrument belong to?"),
		genai.ImageData("jpeg", imgData))
	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 {
		if cand.Content != nil {
			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 CitationMetadata

type CitationMetadata struct {
	// Citations to sources for a specific response.
	CitationSources []*CitationSource
}

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

type CitationSource

type CitationSource struct {
	// Optional. Start of segment of the response that is attributed to this
	// source.
	//
	// Index indicates the start of the segment, measured in bytes.
	StartIndex *int32
	// Optional. End of the attributed segment, exclusive.
	EndIndex *int32
	// Optional. URI that is attributed as a source for a portion of the text.
	URI *string
	// Optional. License for the GitHub project that is attributed as a source for
	// segment.
	//
	// License info is required for code citations.
	License string
}

CitationSource contains a citation to a source for a portion of a specific response.

type Client

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

A Client is a Google generative AI client.

Example (SetProxy)
package main

import (
	"context"
	"fmt"
	"log"
	"net/http"
	"net/url"
	"os"

	"github.com/google/generative-ai-go/genai"
	"google.golang.org/api/option"
)

// ProxyRoundTripper is an implementation of http.RoundTripper that supports
// setting a proxy server URL for genai clients. This type should be used with
// a custom http.Client that's passed to WithHTTPClient. For such clients,
// WithAPIKey doesn't apply so the key has to be explicitly set here.
type ProxyRoundTripper struct {
	APIKey string

	ProxyURL string
}

func (t *ProxyRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
	transport := http.DefaultTransport.(*http.Transport).Clone()

	if t.ProxyURL != "" {
		proxyURL, err := url.Parse(t.ProxyURL)
		if err != nil {
			return nil, err
		}
		transport.Proxy = http.ProxyURL(proxyURL)
	}

	newReq := req.Clone(req.Context())
	vals := newReq.URL.Query()
	vals.Set("key", t.APIKey)
	newReq.URL.RawQuery = vals.Encode()

	resp, err := transport.RoundTrip(newReq)
	if err != nil {
		return nil, err
	}

	return resp, nil
}

func main() {
	c := &http.Client{Transport: &ProxyRoundTripper{
		APIKey:   os.Getenv("GEMINI_API_KEY"),
		ProxyURL: "http://<proxy-url>",
	}}

	ctx := context.Background()
	client, err := genai.NewClient(ctx, option.WithHTTPClient(c))
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	model := client.GenerativeModel("gemini-1.5-pro")
	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 {
		if cand.Content != nil {
			for _, part := range cand.Content.Parts {
				fmt.Println(part)
			}
		}
	}
	fmt.Println("---")
}
Output:

func NewClient

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

NewClient creates a new Google generative 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) CreateCachedContent added in v0.15.0

func (c *Client) CreateCachedContent(ctx context.Context, cc *CachedContent) (*CachedContent, error)

CreateCachedContent creates a new CachedContent. The argument should contain a model name and some data to be cached, which can include contents, a system instruction, tools and/or tool configuration. It can also include an expiration time or TTL. But it should not include a name; the system will generate one.

The return value will contain the name, which should be used to refer to the CachedContent in other API calls. It will also hold various metadata like expiration and creation time. It will not contain any of the actual content provided as input.

You can use the return value to create a model with Client.GenerativeModelFromCachedContent. Or you can set [GenerativeModel.CachedContentName] to the name of the CachedContent, in which case you must ensure that the model provided in this call matches the name in the GenerativeModel.

func (*Client) DeleteCachedContent added in v0.15.0

func (c *Client) DeleteCachedContent(ctx context.Context, name string) error

DeleteCachedContent deletes the CachedContent with the given name.

func (*Client) DeleteFile added in v0.11.0

func (c *Client) DeleteFile(ctx context.Context, name string) error

DeleteFile deletes the file with the given name. It is an error to delete a file that does not exist.

func (*Client) EmbeddingModel added in v0.2.0

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

EmbeddingModel creates a new instance of the named embedding model. Example name: "embedding-001" or "models/embedding-001".

func (*Client) GenerativeModel

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

GenerativeModel creates a new instance of the named generative model. For instance, "gemini-1.0-pro" or "models/gemini-1.0-pro".

To access a tuned model named NAME, pass "tunedModels/NAME".

func (*Client) GenerativeModelFromCachedContent added in v0.15.0

func (c *Client) GenerativeModelFromCachedContent(cc *CachedContent) *GenerativeModel

GenerativeModelFromCachedContent returns a GenerativeModel that uses the given CachedContent. The argument should come from a call to Client.CreateCachedContent or Client.GetCachedContent.

func (*Client) GetCachedContent added in v0.15.0

func (c *Client) GetCachedContent(ctx context.Context, name string) (*CachedContent, error)

GetCachedContent retrieves the CachedContent with the given name.

Example
package main

import (
	"context"
	"fmt"
	"log"
	"os"
	"path/filepath"

	"github.com/google/generative-ai-go/genai"
	"github.com/google/generative-ai-go/genai/internal/testhelpers"
	"google.golang.org/api/option"
)

var testDataDir = filepath.Join(testhelpers.ModuleRootDir(), "genai", "testdata")

func main() {
	ctx := context.Background()
	client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	file, err := client.UploadFileFromPath(ctx, filepath.Join(testDataDir, "a11.txt"), nil)
	if err != nil {
		log.Fatal(err)
	}
	defer client.DeleteFile(ctx, file.Name)
	fd := genai.FileData{URI: file.URI}

	argcc := &genai.CachedContent{
		Model:             "gemini-1.5-flash-001",
		SystemInstruction: genai.NewUserContent(genai.Text("You are an expert analyzing transcripts.")),
		Contents:          []*genai.Content{genai.NewUserContent(fd)},
	}
	cc, err := client.CreateCachedContent(ctx, argcc)
	if err != nil {
		log.Fatal(err)
	}
	defer client.DeleteCachedContent(ctx, cc.Name)

	// Save the name for later
	cacheName := cc.Name

	// ... Later
	cc2, err := client.GetCachedContent(ctx, cacheName)
	if err != nil {
		log.Fatal(err)
	}
	modelWithCache := client.GenerativeModelFromCachedContent(cc2)
	prompt := "Find a lighthearted moment from this transcript"
	resp, err := modelWithCache.GenerateContent(ctx, genai.Text(prompt))
	if err != nil {
		log.Fatal(err)
	}

	printResponse(resp)

}

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

func (*Client) GetFile added in v0.11.0

func (c *Client) GetFile(ctx context.Context, name string) (*File, error)

GetFile returns the named file.

Example
package main

import (
	"context"
	"fmt"
	"log"
	"os"
	"path/filepath"

	"github.com/google/generative-ai-go/genai"
	"github.com/google/generative-ai-go/genai/internal/testhelpers"
	"google.golang.org/api/option"
)

var testDataDir = filepath.Join(testhelpers.ModuleRootDir(), "genai", "testdata")

func main() {
	ctx := context.Background()
	client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	file, err := client.UploadFileFromPath(ctx, filepath.Join(testDataDir, "personWorkingOnComputer.jpg"), nil)
	if err != nil {
		log.Fatal(err)
	}
	defer client.DeleteFile(ctx, file.Name)

	gotFile, err := client.GetFile(ctx, file.Name)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("Got file:", gotFile.Name)

	model := client.GenerativeModel("gemini-1.5-flash")
	resp, err := model.GenerateContent(ctx,
		genai.FileData{URI: file.URI},
		genai.Text("Describe this image"))
	if err != nil {
		log.Fatal(err)
	}

	printResponse(resp)

}

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

func (*Client) ListCachedContents added in v0.15.0

func (c *Client) ListCachedContents(ctx context.Context) *CachedContentIterator

ListCachedContents lists all the CachedContents associated with the project and location.

Example
package main

import (
	"context"
	"fmt"
	"log"
	"os"
	"path/filepath"

	"github.com/google/generative-ai-go/genai"
	"github.com/google/generative-ai-go/genai/internal/testhelpers"
	"google.golang.org/api/iterator"
	"google.golang.org/api/option"
)

var testDataDir = filepath.Join(testhelpers.ModuleRootDir(), "genai", "testdata")

func main() {
	ctx := context.Background()
	client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	file, err := client.UploadFileFromPath(ctx, filepath.Join(testDataDir, "a11.txt"), nil)
	if err != nil {
		log.Fatal(err)
	}
	defer client.DeleteFile(ctx, file.Name)
	fd := genai.FileData{URI: file.URI}

	argcc := &genai.CachedContent{
		Model:             "gemini-1.5-flash-001",
		SystemInstruction: genai.NewUserContent(genai.Text("You are an expert analyzing transcripts.")),
		Contents:          []*genai.Content{genai.NewUserContent(fd)},
	}
	cc, err := client.CreateCachedContent(ctx, argcc)
	if err != nil {
		log.Fatal(err)
	}
	defer client.DeleteCachedContent(ctx, cc.Name)

	fmt.Println("My caches:")
	iter := client.ListCachedContents(ctx)
	for {
		cc, err := iter.Next()
		if err == iterator.Done {
			break
		}
		if err != nil {
			log.Fatal(err)
		}
		fmt.Println("   ", cc.Name)
	}

}
Output:

func (*Client) ListFiles added in v0.11.0

func (c *Client) ListFiles(ctx context.Context) *FileIterator

ListFiles returns an iterator over the uploaded files.

Example
package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"github.com/google/generative-ai-go/genai"
	"google.golang.org/api/iterator"
	"google.golang.org/api/option"
)

func main() {
	ctx := context.Background()
	client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	iter := client.ListFiles(ctx)
	for {
		ifile, err := iter.Next()
		if err == iterator.Done {
			break
		}
		if err != nil {
			log.Fatal(err)
		}
		fmt.Println(ifile.Name)
	}

}
Output:

func (*Client) ListModels added in v0.2.0

func (c *Client) ListModels(ctx context.Context) *ModelInfoIterator
Example
package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"github.com/google/generative-ai-go/genai"
	"google.golang.org/api/iterator"
	"google.golang.org/api/option"
)

func main() {
	ctx := context.Background()
	client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	iter := client.ListModels(ctx)
	for {
		m, err := iter.Next()
		if err == iterator.Done {
			break
		}
		if err != nil {
			panic(err)
		}
		fmt.Println(m.Name, m.Description)
	}
}
Output:

func (*Client) UpdateCachedContent added in v0.15.0

func (c *Client) UpdateCachedContent(ctx context.Context, cc *CachedContent, ccu *CachedContentToUpdate) (*CachedContent, error)

UpdateCachedContent modifies the CachedContent according to the values of the CachedContentToUpdate struct. It returns the modified CachedContent.

The argument CachedContent must have its Name field populated. If its UpdateTime field is non-zero, it will be compared with the update time of the stored CachedContent and the call will fail if they differ. This avoids a race condition when two updates are attempted concurrently. All other fields of the argument CachedContent are ignored.

Example
package main

import (
	"context"
	"log"
	"os"
	"path/filepath"
	"time"

	"github.com/google/generative-ai-go/genai"
	"github.com/google/generative-ai-go/genai/internal/testhelpers"
	"google.golang.org/api/option"
)

var testDataDir = filepath.Join(testhelpers.ModuleRootDir(), "genai", "testdata")

func main() {
	ctx := context.Background()
	client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	file, err := client.UploadFileFromPath(ctx, filepath.Join(testDataDir, "a11.txt"), nil)
	if err != nil {
		log.Fatal(err)
	}
	defer client.DeleteFile(ctx, file.Name)
	fd := genai.FileData{URI: file.URI}

	argcc := &genai.CachedContent{
		Model:             "gemini-1.5-flash-001",
		SystemInstruction: genai.NewUserContent(genai.Text("You are an expert analyzing transcripts.")),
		Contents:          []*genai.Content{genai.NewUserContent(fd)},
	}
	cc, err := client.CreateCachedContent(ctx, argcc)
	if err != nil {
		log.Fatal(err)
	}
	defer client.DeleteCachedContent(ctx, cc.Name)

	// You can update the TTL
	newExpireTime := cc.Expiration.ExpireTime.Add(2 * time.Hour)
	_, err = client.UpdateCachedContent(ctx, cc, &genai.CachedContentToUpdate{
		Expiration: &genai.ExpireTimeOrTTL{ExpireTime: newExpireTime}})
	if err != nil {
		log.Fatal(err)
	}

}
Output:

func (*Client) UploadFile added in v0.11.0

func (c *Client) UploadFile(ctx context.Context, name string, r io.Reader, opts *UploadFileOptions) (*File, error)

UploadFile copies the contents of the given io.Reader to file storage associated with the service, and returns information about the resulting file.

The name is a relatively short, unique identifier for the file (rather than a typical filename). Typically it should be left empty, in which case a unique name will be generated. Otherwise, it can contain up to 40 characters that are lowercase alphanumeric or dashes (-), not starting or ending with a dash. To generate your own unique names, consider a cryptographic hash algorithm like SHA-1. The string "files/" is prepended to the name if it does not contain a '/'.

Use the returned file's URI field with a FileData Part to use it for generation.

It is an error to upload a file that already exists.

Example (Audio)
package main

import (
	"context"
	"fmt"
	"log"
	"os"
	"path/filepath"

	"github.com/google/generative-ai-go/genai"
	"github.com/google/generative-ai-go/genai/internal/testhelpers"
	"google.golang.org/api/option"
)

var testDataDir = filepath.Join(testhelpers.ModuleRootDir(), "genai", "testdata")

func main() {
	ctx := context.Background()
	client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	file, err := client.UploadFileFromPath(ctx, filepath.Join(testDataDir, "sample.mp3"), nil)
	if err != nil {
		log.Fatal(err)
	}
	defer client.DeleteFile(ctx, file.Name)

	model := client.GenerativeModel("gemini-1.5-flash")
	resp, err := model.GenerateContent(ctx,
		genai.FileData{URI: file.URI},
		genai.Text("Describe this audio clip"))
	if err != nil {
		log.Fatal(err)
	}

	printResponse(resp)

}

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

Example (Image)
package main

import (
	"context"
	"fmt"
	"log"
	"os"
	"path/filepath"

	"github.com/google/generative-ai-go/genai"
	"github.com/google/generative-ai-go/genai/internal/testhelpers"
	"google.golang.org/api/option"
)

var testDataDir = filepath.Join(testhelpers.ModuleRootDir(), "genai", "testdata")

func main() {
	ctx := context.Background()
	client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	file, err := client.UploadFileFromPath(ctx, filepath.Join(testDataDir, "Cajun_instruments.jpg"), nil)
	if err != nil {
		log.Fatal(err)
	}
	defer client.DeleteFile(ctx, file.Name)

	model := client.GenerativeModel("gemini-1.5-flash")
	resp, err := model.GenerateContent(ctx,
		genai.FileData{URI: file.URI},
		genai.Text("Can you tell me about the instruments in this photo?"))
	if err != nil {
		log.Fatal(err)
	}

	printResponse(resp)

}

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

Example (Pdf)
package main

import (
	"context"
	"fmt"
	"log"
	"os"
	"path/filepath"

	"github.com/google/generative-ai-go/genai"
	"github.com/google/generative-ai-go/genai/internal/testhelpers"
	"google.golang.org/api/option"
)

var testDataDir = filepath.Join(testhelpers.ModuleRootDir(), "genai", "testdata")

func main() {
	ctx := context.Background()
	client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	file, err := client.UploadFileFromPath(ctx, filepath.Join(testDataDir, "test.pdf"), nil)
	if err != nil {
		log.Fatal(err)
	}
	defer client.DeleteFile(ctx, file.Name)

	model := client.GenerativeModel("gemini-1.5-flash")
	resp, err := model.GenerateContent(ctx,
		genai.Text("Give me a summary of this pdf file."),
		genai.FileData{URI: file.URI})
	if err != nil {
		log.Fatal(err)
	}

	printResponse(resp)

}

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

Example (Text)
package main

import (
	"context"
	"fmt"
	"log"
	"os"
	"path/filepath"

	"github.com/google/generative-ai-go/genai"
	"github.com/google/generative-ai-go/genai/internal/testhelpers"
	"google.golang.org/api/option"
)

var testDataDir = filepath.Join(testhelpers.ModuleRootDir(), "genai", "testdata")

func main() {
	ctx := context.Background()
	client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	file, err := client.UploadFileFromPath(ctx, filepath.Join(testDataDir, "poem.txt"), nil)
	if err != nil {
		log.Fatal(err)
	}
	defer client.DeleteFile(ctx, file.Name)

	model := client.GenerativeModel("gemini-1.5-flash")
	resp, err := model.GenerateContent(ctx,
		genai.FileData{URI: file.URI},
		genai.Text("Can you add a few more lines to this poem?"))
	if err != nil {
		log.Fatal(err)
	}

	printResponse(resp)

}

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

Example (Video)
package main

import (
	"context"
	"fmt"
	"log"
	"os"
	"path/filepath"
	"time"

	"github.com/google/generative-ai-go/genai"
	"github.com/google/generative-ai-go/genai/internal/testhelpers"
	"google.golang.org/api/option"
)

var testDataDir = filepath.Join(testhelpers.ModuleRootDir(), "genai", "testdata")

func main() {
	ctx := context.Background()
	client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	file, err := client.UploadFileFromPath(ctx, filepath.Join(testDataDir, "earth.mp4"), nil)
	if err != nil {
		log.Fatal(err)
	}
	defer client.DeleteFile(ctx, file.Name)

	// Videos need to be processed before you can use them.
	for file.State == genai.FileStateProcessing {
		log.Printf("processing %s", file.Name)
		time.Sleep(5 * time.Second)
		var err error
		if file, err = client.GetFile(ctx, file.Name); err != nil {
			log.Fatal(err)
		}
	}
	if file.State != genai.FileStateActive {
		log.Fatalf("uploaded file has state %s, not active", file.State)
	}

	model := client.GenerativeModel("gemini-1.5-flash")
	resp, err := model.GenerateContent(ctx,
		genai.FileData{URI: file.URI},
		genai.Text("Describe this video clip"))
	if err != nil {
		log.Fatal(err)
	}

	printResponse(resp)

}

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

func (*Client) UploadFileFromPath added in v0.18.0

func (c *Client) UploadFileFromPath(ctx context.Context, path string, opts *UploadFileOptions) (*File, error)

UploadFileFromPath is a convenience method wrapping [UploadFile]. It takes a path to read the file from, and uses a default auto-generated ID for the uploaded file.

type CodeExecution added in v0.16.0

type CodeExecution struct {
}

CodeExecution is tool that executes code generated by the model, and automatically returns the result to the model.

See also `ExecutableCode` and `CodeExecutionResult` which are only generated when using this tool.

type CodeExecutionResult added in v0.16.0

type CodeExecutionResult struct {
	// Required. Outcome of the code execution.
	Outcome CodeExecutionResultOutcome
	// Optional. Contains stdout when code execution is successful, stderr or
	// other description otherwise.
	Output string
}

CodeExecutionResult is result of executing the `ExecutableCode`.

Only generated when using the `CodeExecution`, and always follows a `part` containing the `ExecutableCode`.

type CodeExecutionResultOutcome added in v0.16.0

type CodeExecutionResultOutcome int32

CodeExecutionResultOutcome is enumeration of possible outcomes of the code execution.

const (
	// CodeExecutionResultOutcomeUnspecified means unspecified status. This value should not be used.
	CodeExecutionResultOutcomeUnspecified CodeExecutionResultOutcome = 0
	// CodeExecutionResultOutcomeOK means code execution completed successfully.
	CodeExecutionResultOutcomeOK CodeExecutionResultOutcome = 1
	// CodeExecutionResultOutcomeFailed means code execution finished but with a failure. `stderr` should contain the
	// reason.
	CodeExecutionResultOutcomeFailed CodeExecutionResultOutcome = 2
	// CodeExecutionResultOutcomeDeadlineExceeded means code execution ran for too long, and was cancelled. There may or may not
	// be a partial output present.
	CodeExecutionResultOutcomeDeadlineExceeded CodeExecutionResultOutcome = 3
)

func (CodeExecutionResultOutcome) String added in v0.16.0

type Content

type Content struct {
	// Ordered `Parts` that constitute a single message. Parts may have different
	// MIME types.
	Parts []Part
	// 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
}

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.

func NewUserContent added in v0.17.0

func NewUserContent(parts ...Part) *Content

NewUserContent returns a *Content with a "user" role set and one or more parts.

type ContentEmbedding added in v0.2.0

type ContentEmbedding struct {
	// The embedding values.
	Values []float32
}

ContentEmbedding is a list of floats representing an embedding.

type CountTokensResponse

type CountTokensResponse struct {
	// The number of tokens that the `model` tokenizes the `prompt` into.
	//
	// Always non-negative. When cached_content is set, this is still the total
	// effective prompt size. I.e. this includes the number of tokens in the
	// cached content.
	TotalTokens int32
	// Number of tokens in the cached part of the prompt, i.e. in the cached
	// content.
	CachedContentTokenCount int32
}

CountTokensResponse is a response from `CountTokens`.

It returns the model's `token_count` for the `prompt`.

type EmbedContentResponse added in v0.2.0

type EmbedContentResponse struct {
	// Output only. The embedding generated from the input content.
	Embedding *ContentEmbedding
}

EmbedContentResponse is the response to an `EmbedContentRequest`.

type EmbeddingBatch added in v0.6.0

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

An EmbeddingBatch holds a collection of embedding requests.

Example
package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"github.com/google/generative-ai-go/genai"
	"google.golang.org/api/option"
)

func main() {
	ctx := context.Background()
	client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()
	em := client.EmbeddingModel("embedding-001")
	b := em.NewBatch().
		AddContent(genai.Text("cheddar cheese")).
		AddContentWithTitle("My Cheese Report", genai.Text("I love cheddar cheese."))
	res, err := em.BatchEmbedContents(ctx, b)
	if err != nil {
		panic(err)
	}
	for _, e := range res.Embeddings {
		fmt.Println(e.Values)
	}
}
Output:

func (*EmbeddingBatch) AddContent added in v0.6.0

func (b *EmbeddingBatch) AddContent(parts ...Part) *EmbeddingBatch

AddContent adds a content to the batch.

func (*EmbeddingBatch) AddContentWithTitle added in v0.6.0

func (b *EmbeddingBatch) AddContentWithTitle(title string, parts ...Part) *EmbeddingBatch

AddContent adds a content to the batch with a title.

type EmbeddingModel added in v0.2.0

type EmbeddingModel struct {

	// TaskType describes how the embedding will be used.
	TaskType TaskType
	// contains filtered or unexported fields
}

EmbeddingModel is a model that computes embeddings. Create one with Client.EmbeddingModel.

func (*EmbeddingModel) BatchEmbedContents added in v0.6.0

func (m *EmbeddingModel) BatchEmbedContents(ctx context.Context, b *EmbeddingBatch) (*BatchEmbedContentsResponse, error)

BatchEmbedContents returns the embeddings for all the contents in the batch.

func (*EmbeddingModel) EmbedContent added in v0.2.0

func (m *EmbeddingModel) EmbedContent(ctx context.Context, parts ...Part) (*EmbedContentResponse, error)

EmbedContent returns an embedding for the list of parts.

Example
package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"github.com/google/generative-ai-go/genai"
	"google.golang.org/api/option"
)

func main() {
	ctx := context.Background()
	client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()
	em := client.EmbeddingModel("embedding-001")
	res, err := em.EmbedContent(ctx, genai.Text("cheddar cheese"))
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(res.Embedding.Values)
}
Output:

func (*EmbeddingModel) EmbedContentWithTitle added in v0.2.0

func (m *EmbeddingModel) EmbedContentWithTitle(ctx context.Context, title string, parts ...Part) (*EmbedContentResponse, error)

EmbedContentWithTitle returns an embedding for the list of parts. If the given title is non-empty, it is passed to the model and the task type is set to TaskTypeRetrievalDocument.

func (*EmbeddingModel) Info added in v0.7.0

func (m *EmbeddingModel) Info(ctx context.Context) (*ModelInfo, error)

Info returns information about the model.

func (*EmbeddingModel) Name added in v0.6.0

func (m *EmbeddingModel) Name() string

Name returns the name of the EmbeddingModel.

func (*EmbeddingModel) NewBatch added in v0.6.0

func (m *EmbeddingModel) NewBatch() *EmbeddingBatch

NewBatch returns a new, empty EmbeddingBatch with the same TaskType as the model. Make multiple calls to EmbeddingBatch.AddContent or EmbeddingBatch.AddContentWithTitle. Then pass the EmbeddingBatch to EmbeddingModel.BatchEmbedContents to get all the embeddings in a single call to the model.

type ExecutableCode added in v0.16.0

type ExecutableCode struct {
	// Required. Programming language of the `code`.
	Language ExecutableCodeLanguage
	// Required. The code to be executed.
	Code string
}

ExecutableCode is code generated by the model that is meant to be executed, and the result returned to the model.

Only generated when using the `CodeExecution` tool, in which the code will be automatically executed, and a corresponding `CodeExecutionResult` will also be generated.

type ExecutableCodeLanguage added in v0.16.0

type ExecutableCodeLanguage int32

ExecutableCodeLanguage is supported programming languages for the generated code.

const (
	// ExecutableCodeLanguageUnspecified means unspecified language. This value should not be used.
	ExecutableCodeLanguageUnspecified ExecutableCodeLanguage = 0
	// ExecutableCodePython means python >= 3.10, with numpy and simpy available.
	ExecutableCodePython ExecutableCodeLanguage = 1
)

func (ExecutableCodeLanguage) String added in v0.16.0

func (v ExecutableCodeLanguage) String() string

type ExpireTimeOrTTL added in v0.15.0

type ExpireTimeOrTTL struct {
	ExpireTime time.Time
	TTL        time.Duration
}

ExpireTimeOrTTL describes the time when a resource expires. If ExpireTime is non-zero, it is the expiration time. Otherwise, the expiration time is the value of TTL ("time to live") added to the current time.

type File added in v0.11.0

type File struct {
	// Metadata for the File.
	Metadata *FileMetadata
	// Immutable. Identifier. The `File` resource name. The ID (name excluding the
	// "files/" prefix) can contain up to 40 characters that are lowercase
	// alphanumeric or dashes (-). The ID cannot start or end with a dash. If the
	// name is empty on create, a unique name will be generated. Example:
	// `files/123-456`
	Name string
	// Optional. The human-readable display name for the `File`. The display name
	// must be no more than 512 characters in length, including spaces. Example:
	// "Welcome Image"
	DisplayName string
	// Output only. MIME type of the file.
	MIMEType string
	// Output only. Size of the file in bytes.
	SizeBytes int64
	// Output only. The timestamp of when the `File` was created.
	CreateTime time.Time
	// Output only. The timestamp of when the `File` was last updated.
	UpdateTime time.Time
	// Output only. The timestamp of when the `File` will be deleted. Only set if
	// the `File` is scheduled to expire.
	ExpirationTime time.Time
	// Output only. SHA-256 hash of the uploaded bytes.
	Sha256Hash []byte
	// Output only. The uri of the `File`.
	URI string
	// Output only. Processing state of the File.
	State FileState
	// Output only. Error status if File processing failed.
	Error *apierror.APIError
}

File is a file uploaded to the API.

type FileData added in v0.11.0

type FileData struct {
	// The IANA standard MIME type of the source data.
	// If present, this overrides the MIME type specified or inferred
	// when the file was uploaded.
	// The supported MIME types are documented on [this page].
	//
	// [this page]: https://ai.google.dev/gemini-api/docs/prompting_with_media?lang=go#supported_file_formats
	MIMEType string
	// The URI returned from UploadFile or GetFile.
	URI string
}

FileData is URI based data.

type FileIterator added in v0.11.0

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

A FileIterator iterates over Files.

func (*FileIterator) Next added in v0.11.0

func (it *FileIterator) Next() (*File, error)

Next returns the next result. Its second return value is iterator.Done if there are no more results. Once Next returns Done, all subsequent calls will return Done.

func (*FileIterator) PageInfo added in v0.11.0

func (it *FileIterator) PageInfo() *iterator.PageInfo

PageInfo supports pagination. See the google.golang.org/api/iterator package for details.

type FileMetadata added in v0.14.0

type FileMetadata struct {
	// Set if the file contains video.
	Video *VideoMetadata
}

FileMetadata holds metadata about a file.

type FileState added in v0.12.0

type FileState int32

FileState represents states for the lifecycle of a File.

const (
	// FileStateUnspecified means the default value. This value is used if the state is omitted.
	FileStateUnspecified FileState = 0
	// FileStateProcessing means file is being processed and cannot be used for inference yet.
	FileStateProcessing FileState = 1
	// FileStateActive means file is processed and available for inference.
	FileStateActive FileState = 2
	// FileStateFailed means file failed processing.
	FileStateFailed FileState = 10
)

func (FileState) String added in v0.12.0

func (v FileState) String() string

type FinishReason

type FinishReason int32

FinishReason is defines the reason why the model stopped generating tokens.

const (
	// FinishReasonUnspecified means default value. This value is unused.
	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 candidate content was flagged for safety reasons.
	FinishReasonSafety FinishReason = 3
	// FinishReasonRecitation means the candidate content was flagged for recitation reasons.
	FinishReasonRecitation FinishReason = 4
	// FinishReasonOther means unknown reason.
	FinishReasonOther FinishReason = 5
)

func (FinishReason) String

func (v FinishReason) String() string

type FunctionCall

type FunctionCall struct {
	// Required. The name of the function to call.
	// Must be a-z, A-Z, 0-9, or contain underscores and dashes, with a maximum
	// length of 63.
	Name string
	// Optional. The function parameters and values in JSON object format.
	Args map[string]any
}

FunctionCall is a predicted `FunctionCall` returned from the model that contains a string representing the `FunctionDeclaration.name` with the arguments and their values.

type FunctionCallingConfig added in v0.11.0

type FunctionCallingConfig struct {
	// Optional. Specifies the mode in which function calling should execute. If
	// unspecified, the default value will be set to AUTO.
	Mode FunctionCallingMode
	// Optional. A set of function names that, when provided, limits the functions
	// the model will call.
	//
	// This should only be 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.11.0

type FunctionCallingMode int32

FunctionCallingMode is defines the execution behavior for function calling by defining the execution 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 response.
	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.11.0

func (v FunctionCallingMode) String() string

type FunctionDeclaration

type FunctionDeclaration struct {
	// Required. The name of the function.
	// Must be a-z, A-Z, 0-9, or contain underscores and dashes, with a maximum
	// length of 63.
	Name string
	// Required. A brief description of the function.
	Description string
	// Optional. Describes the parameters to this function. 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.
	Parameters *Schema
}

FunctionDeclaration is structured representation of a function declaration as defined by the [OpenAPI 3.03 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

type FunctionResponse struct {
	// Required. The name of the function to call.
	// Must be a-z, A-Z, 0-9, or contain underscores and dashes, with a maximum
	// length of 63.
	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 {
	// Candidate responses from the model.
	Candidates []*Candidate
	// Returns the prompt's feedback related to the content filters.
	PromptFeedback *PromptFeedback
	// Output only. Metadata on the generation requests' token usage.
	UsageMetadata *UsageMetadata
}

GenerateContentResponse is the response from a GenerateContent or GenerateContentStream call.

Note on safety ratings and content filtering. They are reported for both prompt in `GenerateContentResponse.prompt_feedback` and for each candidate in `finish_reason` and in `safety_ratings`. The API contract is that:

  • either all requested candidates are returned or no candidates at all
  • no candidates are returned only if there was something wrong with the prompt (see `prompt_feedback`)
  • feedback on each candidate is reported on `finish_reason` and `safety_ratings`.

type GenerateContentResponseIterator

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

GenerateContentResponseIterator is an iterator over GnerateContentResponse.

func (*GenerateContentResponseIterator) MergedResponse added in v0.14.0

MergedResponse returns the result of combining all the streamed responses seen so far. After iteration completes, the merged response should match the response obtained without streaming (that is, if GenerativeModel.GenerateContent were called).

func (*GenerateContentResponseIterator) Next

Next returns the next response.

type GenerationConfig

type GenerationConfig struct {
	// Optional. Number of generated responses to return.
	//
	// Currently, this value can only be set to 1. If unset, this will default
	// to 1.
	CandidateCount *int32
	// Optional. The set of character sequences (up to 5) that will stop output
	// generation. If specified, the API will stop at the first appearance of a
	// stop sequence. The stop sequence will not be included as part of the
	// response.
	StopSequences []string
	// Optional. The maximum number of tokens to include in a candidate.
	//
	// Note: The default value varies by model, see the `Model.output_token_limit`
	// attribute of the `Model` returned from the `getModel` function.
	MaxOutputTokens *int32
	// Optional. Controls the randomness of the output.
	//
	// Note: The default value varies by model, see the `Model.temperature`
	// attribute of the `Model` returned from the `getModel` function.
	//
	// Values can range from [0.0, 2.0].
	Temperature *float32
	// Optional. The maximum cumulative probability of tokens to consider when
	// sampling.
	//
	// The model uses combined Top-k and nucleus sampling.
	//
	// Tokens are sorted based on their assigned probabilities so that only the
	// most likely tokens are considered. Top-k sampling directly limits the
	// maximum number of tokens to consider, while Nucleus sampling limits number
	// of tokens based on the cumulative probability.
	//
	// Note: The default value varies by model, see the `Model.top_p`
	// attribute of the `Model` returned from the `getModel` function.
	TopP *float32
	// Optional. The maximum number of tokens to consider when sampling.
	//
	// Models use nucleus sampling or combined Top-k and nucleus sampling.
	// Top-k sampling considers the set of `top_k` most probable tokens.
	// Models running with nucleus sampling don't allow top_k setting.
	//
	// Note: The default value varies by model, see the `Model.top_k`
	// attribute of the `Model` returned from the `getModel` function. Empty
	// `top_k` field in `Model` indicates the model doesn't apply top-k sampling
	// and doesn't allow setting `top_k` on requests.
	TopK *int32
	// Optional. Output response mimetype of the generated candidate text.
	// Supported mimetype:
	// `text/plain`: (default) Text output.
	// `application/json`: JSON response in the candidates.
	ResponseMIMEType string
	// Optional. Output response schema of the generated candidate text when
	// response mime type can have schema. Schema can be objects, primitives or
	// arrays and is a subset of [OpenAPI
	// schema](https://spec.openapis.org/oas/v3.0.3#schema).
	//
	// If set, a compatible response_mime_type must also be set.
	// Compatible mimetypes:
	// `application/json`: Schema for JSON response.
	ResponseSchema *Schema
}

GenerationConfig is configuration options for model generation and outputs. Not all parameters may be configurable for every model.

func (*GenerationConfig) SetCandidateCount added in v0.5.0

func (c *GenerationConfig) SetCandidateCount(x int32)

SetCandidateCount sets the CandidateCount field.

func (*GenerationConfig) SetMaxOutputTokens added in v0.5.0

func (c *GenerationConfig) SetMaxOutputTokens(x int32)

SetMaxOutputTokens sets the MaxOutputTokens field.

func (*GenerationConfig) SetTemperature added in v0.5.0

func (c *GenerationConfig) SetTemperature(x float32)

SetTemperature sets the Temperature field.

func (*GenerationConfig) SetTopK added in v0.5.0

func (c *GenerationConfig) SetTopK(x int32)

SetTopK sets the TopK field.

func (*GenerationConfig) SetTopP added in v0.5.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 (also known as "system prompt") is a more forceful prompt to the model.
	// The model will adhere the instructions more strongly than if they appeared in a normal prompt.
	SystemInstruction *Content
	// The name of the CachedContent to use.
	// Must have already been created with [Client.CreateCachedContent].
	CachedContentName string
	// 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.

Example (JSONNoSchema)
package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"github.com/google/generative-ai-go/genai"
	"google.golang.org/api/option"
)

func main() {
	// This example shows how to get a JSON response without requestin a specific
	// schema.
	ctx := context.Background()
	client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	model := client.GenerativeModel("gemini-1.5-pro-latest")
	// Ask the model to respond with JSON.
	model.ResponseMIMEType = "application/json"
	prompt := `List a few popular cookie recipes using this JSON schema:
                   Recipe = {'recipeName': string}
	           Return: Array<Recipe>`
	resp, err := model.GenerateContent(ctx, genai.Text(prompt))
	if err != nil {
		log.Fatal(err)
	}

	printResponse(resp)

}

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

Example (JSONSchema)
package main

import (
	"context"
	"encoding/json"
	"fmt"
	"log"
	"os"

	"github.com/google/generative-ai-go/genai"
	"google.golang.org/api/option"
)

func main() {
	// This example shows how to get a JSON response that conforms to a schema.
	ctx := context.Background()
	client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	model := client.GenerativeModel("gemini-1.5-pro-latest")
	// Ask the model to respond with JSON.
	model.ResponseMIMEType = "application/json"
	// Specify the schema.
	model.ResponseSchema = &genai.Schema{
		Type:  genai.TypeArray,
		Items: &genai.Schema{Type: genai.TypeString},
	}
	resp, err := model.GenerateContent(ctx, genai.Text("List a few popular cookie recipes using this JSON schema."))
	if err != nil {
		log.Fatal(err)
	}
	for _, part := range resp.Candidates[0].Content.Parts {
		if txt, ok := part.(genai.Text); ok {
			var recipes []string
			if err := json.Unmarshal([]byte(txt), &recipes); err != nil {
				log.Fatal(err)
			}
			fmt.Println(recipes)
		}
	}

}
Output:

func (*GenerativeModel) CountTokens

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

CountTokens counts the number of tokens in the content.

Example (CachedContent)
package main

import (
	"context"
	"fmt"
	"log"
	"os"
	"strings"

	"github.com/google/generative-ai-go/genai"
	"google.golang.org/api/option"
)

func main() {
	ctx := context.Background()
	client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	txt := strings.Repeat("George Washington was the first president of the United States. ", 3000)
	argcc := &genai.CachedContent{
		Model:    "gemini-1.5-flash-001",
		Contents: []*genai.Content{genai.NewUserContent(genai.Text(txt))},
	}
	cc, err := client.CreateCachedContent(ctx, argcc)
	if err != nil {
		log.Fatal(err)
	}
	defer client.DeleteCachedContent(ctx, cc.Name)

	modelWithCache := client.GenerativeModelFromCachedContent(cc)
	prompt := "Summarize this statement"
	tokResp, err := modelWithCache.CountTokens(ctx, genai.Text(prompt))
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("total_tokens:", tokResp.TotalTokens)
	// ( total_tokens: 5 )

	resp, err := modelWithCache.GenerateContent(ctx, genai.Text(prompt))
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println("prompt_token_count:", resp.UsageMetadata.PromptTokenCount)
	fmt.Println("candidates_token_count:", resp.UsageMetadata.CandidatesTokenCount)
	fmt.Println("cached_content_token_count:", resp.UsageMetadata.CachedContentTokenCount)
	fmt.Println("total_token_count:", resp.UsageMetadata.TotalTokenCount)
	// ( prompt_token_count: 33007,  candidates_token_count: 39, cached_content_token_count: 33002, total_token_count: 33046 )

}
Output:

Example (Chat)
package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"github.com/google/generative-ai-go/genai"
	"google.golang.org/api/option"
)

func main() {
	ctx := context.Background()
	client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	model := client.GenerativeModel("gemini-1.5-flash")
	cs := model.StartChat()

	cs.History = []*genai.Content{
		{
			Parts: []genai.Part{
				genai.Text("Hi my name is Bob"),
			},
			Role: "user",
		},
		{
			Parts: []genai.Part{
				genai.Text("Hi Bob!"),
			},
			Role: "model",
		},
	}

	prompt := "Explain how a computer works to a young child."
	resp, err := cs.SendMessage(ctx, genai.Text(prompt))
	if err != nil {
		log.Fatal(err)
	}

	// On the response for SendMessage, use `UsageMetadata` to get
	// separate input and output token counts
	// (`prompt_token_count` and `candidates_token_count`, respectively),
	// as well as the combined token count (`total_token_count`).
	fmt.Println("prompt_token_count:", resp.UsageMetadata.PromptTokenCount)
	fmt.Println("candidates_token_count:", resp.UsageMetadata.CandidatesTokenCount)
	fmt.Println("total_token_count:", resp.UsageMetadata.TotalTokenCount)
	// ( prompt_token_count: 25, candidates_token_count: 21, total_token_count: 46 )

}
Output:

Example (ContextWindow)
package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"github.com/google/generative-ai-go/genai"
	"google.golang.org/api/option"
)

func main() {
	ctx := context.Background()
	client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	model := client.GenerativeModel("gemini-1.0-pro-001")
	info, err := model.Info(ctx)
	if err != nil {
		log.Fatal(err)
	}

	// Returns the "context window" for the model,
	// which is the combined input and output token limits.
	fmt.Printf("input_token_limit=%v\n", info.InputTokenLimit)
	fmt.Printf("output_token_limit=%v\n", info.OutputTokenLimit)
	// ( input_token_limit=30720, output_token_limit=2048 )

}
Output:

Example (ImageInline)
package main

import (
	"context"
	"fmt"
	"log"
	"os"
	"path/filepath"

	"github.com/google/generative-ai-go/genai"
	"github.com/google/generative-ai-go/genai/internal/testhelpers"
	"google.golang.org/api/option"
)

var testDataDir = filepath.Join(testhelpers.ModuleRootDir(), "genai", "testdata")

func main() {
	ctx := context.Background()
	client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	model := client.GenerativeModel("gemini-1.5-flash")
	prompt := "Tell me about this image"
	imageFile, err := os.ReadFile(filepath.Join(testDataDir, "personWorkingOnComputer.jpg"))
	if err != nil {
		log.Fatal(err)
	}
	// Call `CountTokens` to get the input token count
	// of the combined text and file (`total_tokens`).
	// An image's display or file size does not affect its token count.
	// Optionally, you can call `count_tokens` for the text and file separately.
	tokResp, err := model.CountTokens(ctx, genai.Text(prompt), genai.ImageData("jpeg", imageFile))
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("total_tokens:", tokResp.TotalTokens)
	// ( total_tokens: 264 )

	resp, err := model.GenerateContent(ctx, genai.Text(prompt), genai.ImageData("jpeg", imageFile))
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println("prompt_token_count:", resp.UsageMetadata.PromptTokenCount)
	fmt.Println("candidates_token_count:", resp.UsageMetadata.CandidatesTokenCount)
	fmt.Println("total_token_count:", resp.UsageMetadata.TotalTokenCount)
	// ( prompt_token_count: 264, candidates_token_count: 100, total_token_count: 364 )

}
Output:

Example (ImageUploadFile)
package main

import (
	"context"
	"fmt"
	"log"
	"os"
	"path/filepath"

	"github.com/google/generative-ai-go/genai"
	"github.com/google/generative-ai-go/genai/internal/testhelpers"
	"google.golang.org/api/option"
)

var testDataDir = filepath.Join(testhelpers.ModuleRootDir(), "genai", "testdata")

func main() {
	ctx := context.Background()
	client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	model := client.GenerativeModel("gemini-1.5-flash")
	prompt := "Tell me about this image"
	file, err := client.UploadFileFromPath(ctx, filepath.Join(testDataDir, "personWorkingOnComputer.jpg"), nil)
	if err != nil {
		log.Fatal(err)
	}
	defer client.DeleteFile(ctx, file.Name)

	fd := genai.FileData{URI: file.URI}
	// Call `CountTokens` to get the input token count
	// of the combined text and file (`total_tokens`).
	// An image's display or file size does not affect its token count.
	// Optionally, you can call `count_tokens` for the text and file separately.
	tokResp, err := model.CountTokens(ctx, genai.Text(prompt), fd)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("total_tokens:", tokResp.TotalTokens)
	// ( total_tokens: 264 )

	resp, err := model.GenerateContent(ctx, genai.Text(prompt), fd)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println("prompt_token_count:", resp.UsageMetadata.PromptTokenCount)
	fmt.Println("candidates_token_count:", resp.UsageMetadata.CandidatesTokenCount)
	fmt.Println("total_token_count:", resp.UsageMetadata.TotalTokenCount)
	// ( prompt_token_count: 264, candidates_token_count: 100, total_token_count: 364 )

}
Output:

Example (PdfUploadFile)
package main

import (
	"context"
	"fmt"
	"log"
	"os"
	"path/filepath"

	"github.com/google/generative-ai-go/genai"
	"github.com/google/generative-ai-go/genai/internal/testhelpers"
	"google.golang.org/api/option"
)

var testDataDir = filepath.Join(testhelpers.ModuleRootDir(), "genai", "testdata")

func main() {
	ctx := context.Background()
	client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	model := client.GenerativeModel("gemini-1.5-flash")
	prompt := "Give me a summary of this document."
	file, err := client.UploadFileFromPath(ctx, filepath.Join(testDataDir, "test.pdf"), nil)
	if err != nil {
		log.Fatal(err)
	}
	defer client.DeleteFile(ctx, file.Name)

	fd := genai.FileData{URI: file.URI}
	resp, err := model.GenerateContent(ctx, genai.Text(prompt), fd)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(resp.UsageMetadata)

}
Output:

Example (SystemInstruction)
package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"github.com/google/generative-ai-go/genai"
	"google.golang.org/api/option"
)

func main() {
	ctx := context.Background()
	client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	model := client.GenerativeModel("gemini-1.5-flash")
	prompt := "The quick brown fox jumps over the lazy dog"

	respNoInstruction, err := model.CountTokens(ctx, genai.Text(prompt))
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("total_tokens:", respNoInstruction.TotalTokens)
	// ( total_tokens: 10 )

	// The total token count includes everything sent to the GenerateContent
	// request. When you use system instructions, the total token
	// count increases.
	model.SystemInstruction = genai.NewUserContent(genai.Text("You are a cat. Your name is Neko."))
	respWithInstruction, err := model.CountTokens(ctx, genai.Text(prompt))
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("total_tokens:", respWithInstruction.TotalTokens)
	// ( total_tokens: 21 )

}
Output:

Example (TextOnly)
package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"github.com/google/generative-ai-go/genai"
	"google.golang.org/api/option"
)

func main() {
	ctx := context.Background()
	client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	model := client.GenerativeModel("gemini-1.5-flash")
	prompt := "The quick brown fox jumps over the lazy dog"

	// Call CountTokens to get the input token count (`total tokens`).
	tokResp, err := model.CountTokens(ctx, genai.Text(prompt))
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println("total_tokens:", tokResp.TotalTokens)
	// ( total_tokens: 10 )

	resp, err := model.GenerateContent(ctx, genai.Text(prompt))
	if err != nil {
		log.Fatal(err)
	}

	// On the response for GenerateContent, use UsageMetadata to get
	// separate input and output token counts (PromptTokenCount and
	// CandidatesTokenCount, respectively), as well as the combined
	// token count (TotalTokenCount).
	fmt.Println("prompt_token_count:", resp.UsageMetadata.PromptTokenCount)
	fmt.Println("candidates_token_count:", resp.UsageMetadata.CandidatesTokenCount)
	fmt.Println("total_token_count:", resp.UsageMetadata.TotalTokenCount)
	// ( prompt_token_count: 10, candidates_token_count: 38, total_token_count: 48 )

}
Output:

Example (Tools)
package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"github.com/google/generative-ai-go/genai"
	"google.golang.org/api/option"
)

func main() {
	ctx := context.Background()
	client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	model := client.GenerativeModel("gemini-1.5-flash-001")
	prompt := "I have 57 cats, each owns 44 mittens, how many mittens is that in total?"

	tokResp, err := model.CountTokens(ctx, genai.Text(prompt))
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println("total_tokens:", tokResp.TotalTokens)
	// ( total_tokens: 23 )

	tools := []*genai.Tool{
		{FunctionDeclarations: []*genai.FunctionDeclaration{
			{Name: "add"},
			{Name: "subtract"},
			{Name: "multiply"},
			{Name: "divide"},
		}}}

	model.Tools = tools

	// The total token count includes everything sent to the GenerateContent
	// request. When you use tools (like function calling), the total
	// token count increases.
	tokResp, err = model.CountTokens(ctx, genai.Text(prompt))
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println("total_tokens:", tokResp.TotalTokens)
	// ( total_tokens: 99 )

}
Output:

Example (VideoUploadFile)
package main

import (
	"context"
	"fmt"
	"log"
	"os"
	"path/filepath"

	"github.com/google/generative-ai-go/genai"
	"github.com/google/generative-ai-go/genai/internal/testhelpers"
	"google.golang.org/api/option"
)

var testDataDir = filepath.Join(testhelpers.ModuleRootDir(), "genai", "testdata")

func main() {
	ctx := context.Background()
	client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	model := client.GenerativeModel("gemini-1.5-flash")
	prompt := "Tell me about this video"
	file, err := client.UploadFileFromPath(ctx, filepath.Join(testDataDir, "earth.mp4"), nil)
	if err != nil {
		log.Fatal(err)
	}
	defer client.DeleteFile(ctx, file.Name)

	fd := genai.FileData{URI: file.URI}
	// Call `CountTokens` to get the input token count
	// of the combined text and file (`total_tokens`).
	// A video or audio file is converted to tokens at a fixed rate of tokens per
	// second.
	// Optionally, you can call `count_tokens` for the text and file separately.
	tokResp, err := model.CountTokens(ctx, genai.Text(prompt), fd)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("total_tokens:", tokResp.TotalTokens)
	// ( total_tokens: 1481 )

	resp, err := model.GenerateContent(ctx, genai.Text(prompt), fd)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println("prompt_token_count:", resp.UsageMetadata.PromptTokenCount)
	fmt.Println("candidates_token_count:", resp.UsageMetadata.CandidatesTokenCount)
	fmt.Println("total_token_count:", resp.UsageMetadata.TotalTokenCount)
	// ( prompt_token_count: 1481, candidates_token_count: 43, total_token_count: 1524 )

}
Output:

func (*GenerativeModel) GenerateContent

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

GenerateContent produces a single request and response.

Example (AudioPrompt)
package main

import (
	"context"
	"fmt"
	"log"
	"os"
	"path/filepath"

	"github.com/google/generative-ai-go/genai"
	"github.com/google/generative-ai-go/genai/internal/testhelpers"
	"google.golang.org/api/option"
)

var testDataDir = filepath.Join(testhelpers.ModuleRootDir(), "genai", "testdata")

func main() {
	ctx := context.Background()
	client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	model := client.GenerativeModel("gemini-1.5-flash")

	file, err := client.UploadFileFromPath(ctx, filepath.Join(testDataDir, "sample.mp3"), nil)
	if err != nil {
		log.Fatal(err)
	}
	defer client.DeleteFile(ctx, file.Name)

	resp, err := model.GenerateContent(ctx,
		genai.Text("Give me a summary of this audio file."),
		genai.FileData{URI: file.URI})
	if err != nil {
		log.Fatal(err)
	}
	printResponse(resp)

}

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

Example (CodeExecution)
package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"github.com/google/generative-ai-go/genai"
	"google.golang.org/api/option"
)

func main() {
	ctx := context.Background()
	client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	model := client.GenerativeModel("gemini-1.5-pro")
	// To enable code execution, set the CodeExecution tool.
	model.Tools = []*genai.Tool{{CodeExecution: &genai.CodeExecution{}}}
	resp, err := model.GenerateContent(ctx, genai.Text(`
		788477675 * 778 = x.  Find x and also compute largest odd number smaller than this number.
		`))
	if err != nil {
		log.Fatal(err)
	}
	// The model will generate code to solve the problem, which is returned in an ExecutableCode part.
	// It will also run that code and use the result, which is returned in a CodeExecutionResult part.
	printResponse(resp)
}

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

Example (Config)
package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"github.com/google/generative-ai-go/genai"
	"google.golang.org/api/option"
)

func main() {
	// This example shows how to a configure a model. See [GenerationConfig]
	// for the complete set of configuration options.
	ctx := context.Background()
	client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	model := client.GenerativeModel("gemini-1.5-pro-latest")
	model.SetTemperature(0.9)
	model.SetTopP(0.5)
	model.SetTopK(20)
	model.SetMaxOutputTokens(100)
	model.SystemInstruction = genai.NewUserContent(genai.Text("You are Yoda from Star Wars."))
	model.ResponseMIMEType = "application/json"
	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 {
		if cand.Content != nil {
			for _, part := range cand.Content.Parts {
				fmt.Println(part)
			}
		}
	}
	fmt.Println("---")
}
Output:

Example (ImagePrompt)
package main

import (
	"context"
	"fmt"
	"log"
	"os"
	"path/filepath"

	"github.com/google/generative-ai-go/genai"
	"github.com/google/generative-ai-go/genai/internal/testhelpers"
	"google.golang.org/api/option"
)

var testDataDir = filepath.Join(testhelpers.ModuleRootDir(), "genai", "testdata")

func main() {
	ctx := context.Background()
	client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	model := client.GenerativeModel("gemini-1.5-flash")

	imgData, err := os.ReadFile(filepath.Join(testDataDir, "organ.jpg"))
	if err != nil {
		log.Fatal(err)
	}

	resp, err := model.GenerateContent(ctx,
		genai.Text("Tell me about this instrument"),
		genai.ImageData("jpeg", imgData))
	if err != nil {
		log.Fatal(err)
	}

	printResponse(resp)

}

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

Example (MultiImagePrompt)
package main

import (
	"context"
	"fmt"
	"log"
	"os"
	"path/filepath"

	"github.com/google/generative-ai-go/genai"
	"github.com/google/generative-ai-go/genai/internal/testhelpers"
	"google.golang.org/api/option"
)

var testDataDir = filepath.Join(testhelpers.ModuleRootDir(), "genai", "testdata")

func main() {
	ctx := context.Background()
	client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	model := client.GenerativeModel("gemini-1.5-flash")

	imgData1, err := os.ReadFile(filepath.Join(testDataDir, "Cajun_instruments.jpg"))
	if err != nil {
		log.Fatal(err)
	}
	imgData2, err := os.ReadFile(filepath.Join(testDataDir, "organ.jpg"))
	if err != nil {
		log.Fatal(err)
	}

	resp, err := model.GenerateContent(ctx,
		genai.Text("What is the difference between these instruments?"),
		genai.ImageData("jpeg", imgData1),
		genai.ImageData("jpeg", imgData2),
	)
	if err != nil {
		log.Fatal(err)
	}

	printResponse(resp)

}

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

Example (PdfPrompt)
package main

import (
	"context"
	"fmt"
	"log"
	"os"
	"path/filepath"

	"github.com/google/generative-ai-go/genai"
	"github.com/google/generative-ai-go/genai/internal/testhelpers"
	"google.golang.org/api/option"
)

var testDataDir = filepath.Join(testhelpers.ModuleRootDir(), "genai", "testdata")

func main() {
	ctx := context.Background()
	client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	model := client.GenerativeModel("gemini-1.5-flash")

	file, err := client.UploadFileFromPath(ctx, filepath.Join(testDataDir, "test.pdf"), nil)
	if err != nil {
		log.Fatal(err)
	}
	defer client.DeleteFile(ctx, file.Name)

	resp, err := model.GenerateContent(ctx,
		genai.Text("Give me a summary of this document:"),
		genai.FileData{URI: file.URI})
	if err != nil {
		log.Fatal(err)
	}

	printResponse(resp)

}

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

Example (SafetySetting)
package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"github.com/google/generative-ai-go/genai"
	"google.golang.org/api/option"
)

func main() {
	ctx := context.Background()
	client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	model := client.GenerativeModel("gemini-1.5-flash")
	model.SafetySettings = []*genai.SafetySetting{
		{
			Category:  genai.HarmCategoryHarassment,
			Threshold: genai.HarmBlockOnlyHigh,
		},
	}
	resp, err := model.GenerateContent(ctx, genai.Text("I support Martians Soccer Club and I think Jupiterians Football Club sucks! Write a ironic phrase about them."))
	if err != nil {
		log.Fatal(err)
	}
	printResponse(resp)

}

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

Example (SafetySettingMulti)
package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"github.com/google/generative-ai-go/genai"
	"google.golang.org/api/option"
)

func main() {
	ctx := context.Background()
	client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	model := client.GenerativeModel("gemini-1.5-flash")
	model.SafetySettings = []*genai.SafetySetting{
		{
			Category:  genai.HarmCategoryDangerousContent,
			Threshold: genai.HarmBlockLowAndAbove,
		},
		{
			Category:  genai.HarmCategoryHarassment,
			Threshold: genai.HarmBlockMediumAndAbove,
		},
	}
	resp, err := model.GenerateContent(ctx, genai.Text("I support Martians Soccer Club and I think Jupiterians Football Club sucks! Write a ironic phrase about them."))
	if err != nil {
		log.Fatal(err)
	}
	printResponse(resp)

}

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

Example (SystemInstruction)
package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"github.com/google/generative-ai-go/genai"
	"google.golang.org/api/option"
)

func main() {
	ctx := context.Background()
	client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	model := client.GenerativeModel("gemini-1.5-flash")
	model.SystemInstruction = genai.NewUserContent(genai.Text("You are a cat. Your name is Neko."))
	resp, err := model.GenerateContent(ctx, genai.Text("Good morning! How are you?"))
	if err != nil {
		log.Fatal(err)
	}
	printResponse(resp)

}

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

Example (TextOnly)
package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"github.com/google/generative-ai-go/genai"
	"google.golang.org/api/option"
)

func main() {
	ctx := context.Background()
	client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	model := client.GenerativeModel("gemini-1.5-flash")
	resp, err := model.GenerateContent(ctx, genai.Text("Write a story about a magic backpack."))
	if err != nil {
		log.Fatal(err)
	}

	printResponse(resp)

}

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

Example (VideoPrompt)
package main

import (
	"context"
	"fmt"
	"log"
	"os"
	"path/filepath"
	"time"

	"github.com/google/generative-ai-go/genai"
	"github.com/google/generative-ai-go/genai/internal/testhelpers"
	"google.golang.org/api/option"
)

var testDataDir = filepath.Join(testhelpers.ModuleRootDir(), "genai", "testdata")

func main() {
	ctx := context.Background()
	client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	model := client.GenerativeModel("gemini-1.5-flash")

	file, err := client.UploadFileFromPath(ctx, filepath.Join(testDataDir, "earth.mp4"), nil)
	if err != nil {
		log.Fatal(err)
	}
	defer client.DeleteFile(ctx, file.Name)

	// Videos need to be processed before you can use them.
	for file.State == genai.FileStateProcessing {
		log.Printf("processing %s", file.Name)
		time.Sleep(5 * time.Second)
		var err error
		if file, err = client.GetFile(ctx, file.Name); err != nil {
			log.Fatal(err)
		}
	}
	if file.State != genai.FileStateActive {
		log.Fatalf("uploaded file has state %s, not active", file.State)
	}

	resp, err := model.GenerateContent(ctx,
		genai.Text("Describe this video clip"),
		genai.FileData{URI: file.URI})
	if err != nil {
		log.Fatal(err)
	}

	printResponse(resp)

}

func printResponse(resp *genai.GenerateContentResponse) {
	for _, cand := range resp.Candidates {
		if cand.Content != nil {
			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"
	"os"

	"github.com/google/generative-ai-go/genai"
	"google.golang.org/api/iterator"
	"google.golang.org/api/option"
)

func main() {
	ctx := context.Background()
	client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	model := client.GenerativeModel("gemini-1.5-flash")
	iter := model.GenerateContentStream(ctx, genai.Text("Write a story about a magic backpack."))
	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 {
		if cand.Content != nil {
			for _, part := range cand.Content.Parts {
				fmt.Println(part)
			}
		}
	}
	fmt.Println("---")
}
Output:

Example (AudioPrompt)
package main

import (
	"context"
	"fmt"
	"log"
	"os"
	"path/filepath"

	"github.com/google/generative-ai-go/genai"
	"github.com/google/generative-ai-go/genai/internal/testhelpers"
	"google.golang.org/api/iterator"
	"google.golang.org/api/option"
)

var testDataDir = filepath.Join(testhelpers.ModuleRootDir(), "genai", "testdata")

func main() {
	ctx := context.Background()
	client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	model := client.GenerativeModel("gemini-1.5-flash")

	file, err := client.UploadFileFromPath(ctx, filepath.Join(testDataDir, "sample.mp3"), nil)
	if err != nil {
		log.Fatal(err)
	}
	defer client.DeleteFile(ctx, file.Name)

	iter := model.GenerateContentStream(ctx,
		genai.Text("Give me a summary of this audio file."),
		genai.FileData{URI: file.URI})
	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 {
		if cand.Content != nil {
			for _, part := range cand.Content.Parts {
				fmt.Println(part)
			}
		}
	}
	fmt.Println("---")
}
Output:

Example (Errors)

This example shows how to get more information from an error.

package main

import (
	"context"
	"errors"
	"log"
	"os"

	"github.com/google/generative-ai-go/genai"
	"google.golang.org/api/googleapi"
	"google.golang.org/api/option"
)

func main() {
	ctx := context.Background()
	client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
	if err != nil {
		log.Fatal(err)
	}

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

	iter := model.GenerateContentStream(ctx, genai.ImageData("foo", []byte("bar")))
	res, err := iter.Next()
	if err != nil {
		var gerr *googleapi.Error
		if !errors.As(err, &gerr) {
			log.Fatalf("error: %s\n", err)
		} else {
			log.Fatalf("error details: %s\n", gerr)
		}
	}
	_ = res
}
Output:

Example (ImagePrompt)
package main

import (
	"context"
	"fmt"
	"log"
	"os"
	"path/filepath"

	"github.com/google/generative-ai-go/genai"
	"github.com/google/generative-ai-go/genai/internal/testhelpers"
	"google.golang.org/api/iterator"
	"google.golang.org/api/option"
)

var testDataDir = filepath.Join(testhelpers.ModuleRootDir(), "genai", "testdata")

func main() {
	ctx := context.Background()
	client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	model := client.GenerativeModel("gemini-1.5-flash")

	imgData, err := os.ReadFile(filepath.Join(testDataDir, "organ.jpg"))
	if err != nil {
		log.Fatal(err)
	}
	iter := model.GenerateContentStream(ctx,
		genai.Text("Tell me about this instrument"),
		genai.ImageData("jpeg", imgData))
	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 {
		if cand.Content != nil {
			for _, part := range cand.Content.Parts {
				fmt.Println(part)
			}
		}
	}
	fmt.Println("---")
}
Output:

Example (MultiImagePrompt)
package main

import (
	"context"
	"fmt"
	"log"
	"os"
	"path/filepath"

	"github.com/google/generative-ai-go/genai"
	"github.com/google/generative-ai-go/genai/internal/testhelpers"
	"google.golang.org/api/iterator"
	"google.golang.org/api/option"
)

var testDataDir = filepath.Join(testhelpers.ModuleRootDir(), "genai", "testdata")

func main() {
	ctx := context.Background()
	client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	model := client.GenerativeModel("gemini-1.5-flash")

	imgData1, err := os.ReadFile(filepath.Join(testDataDir, "Cajun_instruments.jpg"))
	if err != nil {
		log.Fatal(err)
	}
	imgData2, err := os.ReadFile(filepath.Join(testDataDir, "organ.jpg"))
	if err != nil {
		log.Fatal(err)
	}

	iter := model.GenerateContentStream(ctx,
		genai.Text("What is the difference between these instruments?"),
		genai.ImageData("jpeg", imgData1),
		genai.ImageData("jpeg", imgData2),
	)
	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 {
		if cand.Content != nil {
			for _, part := range cand.Content.Parts {
				fmt.Println(part)
			}
		}
	}
	fmt.Println("---")
}
Output:

Example (PdfPrompt)
package main

import (
	"context"
	"fmt"
	"log"
	"os"
	"path/filepath"

	"github.com/google/generative-ai-go/genai"
	"github.com/google/generative-ai-go/genai/internal/testhelpers"
	"google.golang.org/api/iterator"
	"google.golang.org/api/option"
)

var testDataDir = filepath.Join(testhelpers.ModuleRootDir(), "genai", "testdata")

func main() {
	ctx := context.Background()
	client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	model := client.GenerativeModel("gemini-1.5-flash")

	file, err := client.UploadFileFromPath(ctx, filepath.Join(testDataDir, "test.pdf"), nil)
	if err != nil {
		log.Fatal(err)
	}
	defer client.DeleteFile(ctx, file.Name)

	iter := model.GenerateContentStream(ctx,
		genai.Text("Give me a summary of this document:"),
		genai.FileData{URI: file.URI})
	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 {
		if cand.Content != nil {
			for _, part := range cand.Content.Parts {
				fmt.Println(part)
			}
		}
	}
	fmt.Println("---")
}
Output:

Example (VideoPrompt)
package main

import (
	"context"
	"fmt"
	"log"
	"os"
	"path/filepath"

	"github.com/google/generative-ai-go/genai"
	"github.com/google/generative-ai-go/genai/internal/testhelpers"
	"google.golang.org/api/iterator"
	"google.golang.org/api/option"
)

var testDataDir = filepath.Join(testhelpers.ModuleRootDir(), "genai", "testdata")

func main() {
	ctx := context.Background()
	client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	model := client.GenerativeModel("gemini-1.5-flash")

	file, err := client.UploadFileFromPath(ctx, filepath.Join(testDataDir, "earth.mp4"), nil)
	if err != nil {
		log.Fatal(err)
	}
	defer client.DeleteFile(ctx, file.Name)

	iter := model.GenerateContentStream(ctx,
		genai.Text("Describe this video clip"),
		genai.FileData{URI: file.URI})
	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 {
		if cand.Content != nil {
			for _, part := range cand.Content.Parts {
				fmt.Println(part)
			}
		}
	}
	fmt.Println("---")
}
Output:

func (*GenerativeModel) Info added in v0.7.0

func (m *GenerativeModel) Info(ctx context.Context) (*ModelInfo, error)

Info returns information about the model.

func (*GenerativeModel) StartChat

func (m *GenerativeModel) StartChat() *ChatSession

StartChat starts a chat session.

type HarmBlockThreshold

type HarmBlockThreshold int32

HarmBlockThreshold specifies block at and beyond a specified harm probability.

const (
	// HarmBlockUnspecified means threshold is unspecified.
	HarmBlockUnspecified HarmBlockThreshold = 0
	// HarmBlockLowAndAbove means content with NEGLIGIBLE will be allowed.
	HarmBlockLowAndAbove HarmBlockThreshold = 1
	// HarmBlockMediumAndAbove means content with NEGLIGIBLE and LOW will be allowed.
	HarmBlockMediumAndAbove HarmBlockThreshold = 2
	// HarmBlockOnlyHigh means content with NEGLIGIBLE, LOW, and MEDIUM will be allowed.
	HarmBlockOnlyHigh HarmBlockThreshold = 3
	// HarmBlockNone means all content will be allowed.
	HarmBlockNone HarmBlockThreshold = 4
)

func (HarmBlockThreshold) String

func (v HarmBlockThreshold) String() string

type HarmCategory

type HarmCategory int32

HarmCategory specifies the category of a rating.

These categories cover various kinds of harms that developers may wish to adjust.

const (
	// HarmCategoryUnspecified means category is unspecified.
	HarmCategoryUnspecified HarmCategory = 0
	// HarmCategoryDerogatory means negative or harmful comments targeting identity and/or protected attribute.
	HarmCategoryDerogatory HarmCategory = 1
	// HarmCategoryToxicity means content that is rude, disrespectful, or profane.
	HarmCategoryToxicity HarmCategory = 2
	// HarmCategoryViolence means describes scenarios depicting violence against an individual or group, or
	// general descriptions of gore.
	HarmCategoryViolence HarmCategory = 3
	// HarmCategorySexual means contains references to sexual acts or other lewd content.
	HarmCategorySexual HarmCategory = 4
	// HarmCategoryMedical means promotes unchecked medical advice.
	HarmCategoryMedical HarmCategory = 5
	// HarmCategoryDangerous means dangerous content that promotes, facilitates, or encourages harmful acts.
	HarmCategoryDangerous HarmCategory = 6
	// HarmCategoryHarassment means harasment content.
	HarmCategoryHarassment HarmCategory = 7
	// HarmCategoryHateSpeech means hate speech and content.
	HarmCategoryHateSpeech HarmCategory = 8
	// HarmCategorySexuallyExplicit means sexually explicit content.
	HarmCategorySexuallyExplicit HarmCategory = 9
	// HarmCategoryDangerousContent means dangerous content.
	HarmCategoryDangerousContent HarmCategory = 10
)

func (HarmCategory) String

func (v HarmCategory) String() string

type HarmProbability

type HarmProbability int32

HarmProbability specifies the probability that a piece of content is harmful.

The classification system gives the probability of the content being unsafe. This does not indicate the severity of harm for a piece of content.

const (
	// HarmProbabilityUnspecified means probability is unspecified.
	HarmProbabilityUnspecified HarmProbability = 0
	// HarmProbabilityNegligible means content has a negligible chance of being unsafe.
	HarmProbabilityNegligible HarmProbability = 1
	// HarmProbabilityLow means content has a low chance of being unsafe.
	HarmProbabilityLow HarmProbability = 2
	// HarmProbabilityMedium means content has a medium chance of being unsafe.
	HarmProbabilityMedium HarmProbability = 3
	// HarmProbabilityHigh means content has a high chance of being unsafe.
	HarmProbabilityHigh HarmProbability = 4
)

func (HarmProbability) String

func (v HarmProbability) String() string

type ModelInfo added in v0.7.0

type ModelInfo struct {
	// Required. The resource name of the `Model`.
	//
	// Format: `models/{model}` with a `{model}` naming convention of:
	//
	// * "{base_model_id}-{version}"
	//
	// Examples:
	//
	// * `models/chat-bison-001`
	Name string
	// Required. The name of the base model, pass this to the generation request.
	//
	// Examples:
	//
	// * `chat-bison`
	BaseModelID string
	// Required. The version number of the model.
	//
	// This represents the major version
	Version string
	// The human-readable name of the model. E.g. "Chat Bison".
	//
	// The name can be up to 128 characters long and can consist of any UTF-8
	// characters.
	DisplayName string
	// A short description of the model.
	Description string
	// Maximum number of input tokens allowed for this model.
	InputTokenLimit int32
	// Maximum number of output tokens available for this model.
	OutputTokenLimit int32
	// The model's supported generation methods.
	//
	// The method names are defined as Pascal case
	// strings, such as `generateMessage` which correspond to API methods.
	SupportedGenerationMethods []string
	// Controls the randomness of the output.
	//
	// Values can range over `[0.0,max_temperature]`, inclusive. A higher value
	// will produce responses that are more varied, while a value closer to `0.0`
	// will typically result in less surprising responses from the model.
	// This value specifies default to be used by the backend while making the
	// call to the model.
	Temperature float32
	// The maximum temperature this model can use.
	MaxTemperature *float32
	// For Nucleus sampling.
	//
	// Nucleus sampling considers the smallest set of tokens whose probability
	// sum is at least `top_p`.
	// This value specifies default to be used by the backend while making the
	// call to the model.
	TopP float32
	// For Top-k sampling.
	//
	// Top-k sampling considers the set of `top_k` most probable tokens.
	// This value specifies default to be used by the backend while making the
	// call to the model.
	// If empty, indicates the model doesn't use top-k sampling, and `top_k` isn't
	// allowed as a generation parameter.
	TopK int32
}

ModelInfo is information about a language model.

type ModelInfoIterator added in v0.7.0

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

A ModelInfoIterator iterates over Models.

func (*ModelInfoIterator) Next added in v0.7.0

func (it *ModelInfoIterator) Next() (*ModelInfo, error)

Next returns the next result. Its second return value is iterator.Done if there are no more results. Once Next returns Done, all subsequent calls will return Done.

func (*ModelInfoIterator) PageInfo added in v0.7.0

func (it *ModelInfoIterator) PageInfo() *iterator.PageInfo

PageInfo supports pagination. See the google.golang.org/api/iterator package for details.

type Part

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

A Part is a piece of model content. A Part can be one of the following types:

  • Text
  • Blob
  • FunctionCall
  • FunctionResponse
  • ExecutableCode
  • CodeExecutionResult

type PromptFeedback

type PromptFeedback struct {
	// Optional. If set, the prompt was blocked and no candidates are returned.
	// Rephrase your prompt.
	BlockReason BlockReason
	// Ratings for safety of the prompt.
	// There is at most one rating per category.
	SafetyRatings []*SafetyRating
}

PromptFeedback contains a set of the feedback metadata the prompt specified in `GenerateContentRequest.content`.

type SafetyRating

type SafetyRating struct {
	// Required. The category for this rating.
	Category HarmCategory
	// Required. The probability of harm for this content.
	Probability HarmProbability
	// Was this content blocked because of this rating?
	Blocked bool
}

SafetyRating is the safety rating for a piece of content.

The safety rating contains the category of harm and the harm probability level in that category for a piece of content. Content is classified for safety across a number of harm categories and the probability of the harm classification is included here.

type SafetySetting

type SafetySetting struct {
	// Required. The category for this setting.
	Category HarmCategory
	// Required. Controls the probability threshold at which harm is blocked.
	Threshold HarmBlockThreshold
}

SafetySetting is safety setting, affecting the safety-blocking behavior.

Passing a safety setting for a category changes the allowed probability that content is blocked.

type Schema

type Schema struct {
	// Required. Data type.
	Type Type
	// Optional. The format of the data. This is used only for primitive
	// datatypes. Supported formats:
	//
	//	for NUMBER type: float, double
	//	for INTEGER type: int32, int64
	Format string
	// Optional. A brief description of the parameter. This could contain examples
	// of use. Parameter description may be formatted as Markdown.
	Description string
	// Optional. Indicates if the value may be null.
	Nullable bool
	// 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 of the elements of Type.ARRAY.
	Items *Schema
	// Optional. Properties of Type.OBJECT.
	Properties map[string]*Schema
	// Optional. Required properties of Type.OBJECT.
	Required []string
}

Schema is the `Schema` object allows the definition of input and output data types. These types can be objects, but also primitives and arrays. Represents a select subset of an [OpenAPI 3.0 schema object](https://spec.openapis.org/oas/v3.0.3#schema).

type TaskType added in v0.2.0

type TaskType int32

TaskType is type of task for which the embedding will be used.

const (
	// TaskTypeUnspecified means unset value, which will default to one of the other enum values.
	TaskTypeUnspecified TaskType = 0
	// TaskTypeRetrievalQuery means specifies the given text is a query in a search/retrieval setting.
	TaskTypeRetrievalQuery TaskType = 1
	// TaskTypeRetrievalDocument means specifies the given text is a document from the corpus being searched.
	TaskTypeRetrievalDocument TaskType = 2
	// TaskTypeSemanticSimilarity means specifies the given text will be used for STS.
	TaskTypeSemanticSimilarity TaskType = 3
	// TaskTypeClassification means specifies that the given text will be classified.
	TaskTypeClassification TaskType = 4
	// TaskTypeClustering means specifies that the embeddings will be used for clustering.
	TaskTypeClustering TaskType = 5
	// TaskTypeQuestionAnswering means specifies that the given text will be used for question answering.
	TaskTypeQuestionAnswering TaskType = 6
	// TaskTypeFactVerification means specifies that the given text will be used for fact verification.
	TaskTypeFactVerification TaskType = 7
)

func (TaskType) String added in v0.2.0

func (v TaskType) String() string

type Text

type Text string

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

type Tool

type Tool struct {
	// Optional. A list of FunctionDeclarations available to the model that
	// can be used for function calling. The model or system does not execute
	// the function. Instead the defined function may be returned as a [FunctionCall]
	// part with arguments to the client side for execution. The next conversation
	// turn may contain a [FunctionResponse] with the role "function" generation
	// context for the next model turn.
	FunctionDeclarations []*FunctionDeclaration
	// Optional. Enables the model to execute code as part of generation.
	CodeExecution *CodeExecution
}

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.

Example
package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"github.com/google/generative-ai-go/genai"
	"google.golang.org/api/option"
)

func main() {
	ctx := context.Background()
	client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	// 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.
	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 or a zip code e.g. 95616",
			},
			"title": {
				Type:        genai.TypeString,
				Description: "Any movie title",
			},
		},
		Required: []string{"location"},
	}

	movieTool := &genai.Tool{
		FunctionDeclarations: []*genai.FunctionDeclaration{{
			Name:        "find_theaters",
			Description: "find theaters based on location and optionally movie title which is currently playing in theaters",
			Parameters:  schema,
		}},
	}

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

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

	// 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 tool.
	// 3. We send a FunctionResponse message, simulating the return value of
	//    the tool 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("Which theaters in Mountain View show Barbie movie?"))
	if err != nil {
		log.Fatalf("session.SendMessage: %v", err)
	}

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

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

	// Provide the model with a hard-coded reply.
	res, err = session.SendMessage(ctx, genai.FunctionResponse{
		Name: movieTool.FunctionDeclarations[0].Name,
		Response: map[string]any{
			"theater": "AMC16",
		},
	})
	if err != nil {
		log.Fatal(err)
	}
	printResponse(res)
}

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

type ToolConfig added in v0.11.0

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

ToolConfig is the Tool configuration containing parameters for specifying `Tool` use in the request.

Example
package main

import (
	"context"
	"log"

	"github.com/google/generative-ai-go/genai"
)

func main() {
	// This example shows how to affect how the model uses the tools provided to it.
	// By setting the ToolConfig, you can disable function calling.

	// Assume we have created a Model and have set its Tools field with some functions.
	// See the Example for Tool for details.
	var model *genai.GenerativeModel

	// By default, the model will use the functions in its responses if it thinks they are
	// relevant, by returning FunctionCall parts.
	// Here we set the model's ToolConfig to disable function calling completely.
	model.ToolConfig = &genai.ToolConfig{
		FunctionCallingConfig: &genai.FunctionCallingConfig{
			Mode: genai.FunctionCallingNone,
		},
	}

	// Subsequent calls to ChatSession.SendMessage will not result in FunctionCall responses.
	session := model.StartChat()
	res, err := session.SendMessage(context.Background(), genai.Text("What is the weather like in New York?"))
	if err != nil {
		log.Fatal(err)
	}
	for _, part := range res.Candidates[0].Content.Parts {
		if _, ok := part.(genai.FunctionCall); ok {
			log.Fatal("did not expect FunctionCall")
		}
	}

	// It is also possible to force a function call by using FunctionCallingAny
	// instead of FunctionCallingNone. See the documentation for FunctionCallingMode
	// for details.
}
Output:

type Type

type Type int32

Type contains the list of OpenAPI data types as defined by https://spec.openapis.org/oas/v3.0.3#data-types

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

func (Type) String

func (v Type) String() string

type UploadFileOptions added in v0.11.0

type UploadFileOptions struct {
	// A more readable name for the file.
	DisplayName string

	// The IANA standard MIME type of the file. It will be stored with the file as metadata.
	// If omitted, the service will try to infer it. You may instead wish to use
	// [http.DetectContentType].
	// The supported MIME types are documented on [this page].
	//
	// [this page]: https://ai.google.dev/gemini-api/docs/document-processing?lang=go#technical-details
	MIMEType string
}

UploadFileOptions are options for Client.UploadFile.

type UsageMetadata added in v0.12.0

type UsageMetadata struct {
	// Number of tokens in the prompt. When cached_content is set, this is still
	// the total effective prompt size. I.e. this includes the number of tokens
	// in the cached content.
	PromptTokenCount int32
	// Number of tokens in the cached part of the prompt, i.e. in the cached
	// content.
	CachedContentTokenCount int32
	// Total number of tokens across the generated candidates.
	CandidatesTokenCount int32
	// Total token count for the generation request (prompt + candidates).
	TotalTokenCount int32
}

UsageMetadata is metadata on the generation request's token usage.

type VideoMetadata added in v0.14.0

type VideoMetadata struct {
	// Duration of the video.
	Duration time.Duration
}

VideoMetadata is metadata for a video `File`.

Directories

Path Synopsis
generativelanguage/v1beta
Package generativelanguage provides access to the Generative Language API.
Package generativelanguage provides access to the Generative Language API.
gensupport
Package gensupport is an internal implementation detail used by code generated by the google-api-go-generator tool.
Package gensupport is an internal implementation detail used by code generated by the google-api-go-generator tool.

Jump to

Keyboard shortcuts

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