genai

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2024 License: Apache-2.0, Apache-2.0 Imports: 12 Imported by: 25

Documentation

Overview

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

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)

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.
	// Accepted types include: "image/png", "image/jpeg", "image/heic",
	// "image/heif", "image/webp".
	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 reaasons.
	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 Candidate and SafetyRatings fields for details.
	Candidate *Candidate

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

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

func (*BlockedError) Error

func (e *BlockedError) Error() string

type Candidate

type Candidate struct {
	// 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.

type ChatSession

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

A ChatSession provides interactive chat.

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("API_KEY")))
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()
	model := client.GenerativeModel("gemini-1.0-pro")
	cs := model.StartChat()

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

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

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

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

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

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.

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.
	TotalTokens 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("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("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 {
		panic(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 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 GenerateContentResponse

type GenerateContentResponse struct {
	// Candidate responses from the model.
	Candidates []*Candidate
	// Returns the prompt's feedback related to the content filters.
	PromptFeedback *PromptFeedback
}

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

Next returns the next response.

type GenerationConfig

type GenerationConfig struct {
	// Optional. Number of generated responses to return.
	//
	// This value must be between [1, 8], inclusive. 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.
	//
	// If unset, this will default to output_token_limit specified in the `Model`
	// specification.
	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 the `getModel` function.
	//
	// Values can range from [0.0,1.0],
	// inclusive. A value closer to 1.0 will produce responses that are more
	// varied and creative, while a value closer to 0.0 will typically result in
	// more straightforward responses from the model.
	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 the `getModel` function.
	TopP *float32
	// Optional. The maximum number of tokens to consider when sampling.
	//
	// The model uses combined Top-k and nucleus sampling.
	//
	// Top-k sampling considers the set of `top_k` most probable tokens.
	// Defaults to 40.
	//
	// Note: The default value varies by model, see the `Model.top_k`
	// attribute of the `Model` returned the `getModel` function.
	TopK *int32
}

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

func (*GenerativeModel) CountTokens

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

CountTokens counts the number of tokens in the content.

Example
package main

import (
	"context"
	"fmt"
	"log"
	"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("API_KEY")))
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

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

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

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

func (*GenerativeModel) GenerateContent

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

GenerateContent produces a single request and response.

Example
package main

import (
	"context"
	"fmt"
	"log"
	"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("API_KEY")))
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	model := client.GenerativeModel("gemini-1.0-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:

Example (Config)

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

package main

import (
	"context"
	"fmt"
	"log"
	"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("API_KEY")))
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	model := client.GenerativeModel("gemini-1.0-pro")
	model.SetTemperature(0.9)
	model.SetTopP(0.5)
	model.SetTopK(20)
	model.SetMaxOutputTokens(100)
	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 (SafetySetting)

This example shows how to use SafetySettings to change the threshold for unsafe responses.

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("API_KEY")))
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	model := client.GenerativeModel("gemini-1.0-pro")
	model.SafetySettings = []*genai.SafetySetting{
		{
			Category:  genai.HarmCategoryDangerousContent,
			Threshold: genai.HarmBlockLowAndAbove,
		},
		{
			Category:  genai.HarmCategoryHarassment,
			Threshold: genai.HarmBlockMediumAndAbove,
		},
	}
	resp, err := model.GenerateContent(ctx, genai.Text("I want to be bad. Please help."))
	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("API_KEY")))
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

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

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

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

	model := client.GenerativeModel("gemini-1.0-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:

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, disrepspectful, or profane.
	HarmCategoryToxicity HarmCategory = 2
	// HarmCategoryViolence means describes scenarios depictng 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,1.0]`, inclusive. A value closer to `1.0` 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
	// 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.
	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 either a Text or a Blob.

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 proability that content is blocked.

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
)

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.

Jump to

Keyboard shortcuts

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