gt

package module
v0.1.36 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2025 License: MIT Imports: 18 Imported by: 5

README

gemini-things-go

A helper library for generating things with Gemini API.

Usage

See the code examples in the generation_test.go file.

Test

$ API_KEY="AIabcdefghijklmnopqrstuvwxyz_ABCDEFG-00000000-00" go test

# for verbose output:
$ API_KEY="AIabcdefghijklmnopqrstuvwxyz_ABCDEFG-00000000-00" VERBOSE=true go test

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ErrToStr added in v0.1.2

func ErrToStr(err error) (str string)

ErrToStr converts error (possibly goolge api error) to string.

func FuncArg

func FuncArg[T any](from map[string]any, key string) (*T, error)

FuncArg searches for and returns a value with given `key` from the function call arguments `from`.

func IsModelOverloaded added in v0.1.33

func IsModelOverloaded(err error) bool

IsModelOverloaded returns if given error is from overloaded model.

func IsQuotaExceeded added in v0.1.32

func IsQuotaExceeded(err error) bool

IsQuotaExceeded returns if given error is from execeeded API quota.

func SupportedMimeType added in v0.0.14

func SupportedMimeType(data []byte) (matchedMimeType string, supported bool, err error)

SupportedMimeType detects and returns the matched mime type of given bytes data and whether it's supported or not.

func SupportedMimeTypePath added in v0.1.9

func SupportedMimeTypePath(filepath string) (matchedMimeType string, supported bool, err error)

SupportedMimeTypePath detects and returns the matched mime type of given path and whether it's supported or not.

Types

type ChunkedText added in v0.1.35

type ChunkedText struct {
	Original string
	Chunks   []string
}

ChunkedText contains the original text and the chunks.

func ChunkText added in v0.1.35

func ChunkText(text string, opts ...TextChunkOption) (ChunkedText, error)

ChunkText splits the given text into chunks of the specified size.

type Client

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

Client struct

func NewClient

func NewClient(apiKey, model string) (*Client, error)

NewClient returns a new client with given values.

func (*Client) CacheContext added in v0.1.0

func (c *Client) CacheContext(ctx context.Context, systemInstruction, promptText *string, promptFiles map[string]io.Reader, tools []*genai.Tool, toolConfig *genai.ToolConfig, cachedContextDisplayName *string) (cachedContextName string, err error)

CacheContext caches the context with given values and return the name of the cached context.

`promptText`, `promptFiles`, `tools`, `toolConfig`, and `cachedContextDisplayName` are optional.

func (*Client) Close added in v0.1.0

func (c *Client) Close() error

Close closes the client.

func (*Client) DeleteAllCachedContexts added in v0.1.0

func (c *Client) DeleteAllCachedContexts(ctx context.Context) (err error)

DeleteAllCachedContexts deletes all cached contexts.

func (*Client) DeleteAllFiles added in v0.1.0

func (c *Client) DeleteAllFiles(ctx context.Context) (err error)

DeleteAllFiles deletes all uploaded files.

func (*Client) DeleteCachedContext added in v0.1.4

func (c *Client) DeleteCachedContext(ctx context.Context, cachedContextName string) (err error)

DeleteCachedContext deletes a cached context.

func (*Client) Generate

func (c *Client) Generate(
	ctx context.Context,
	promptText string,
	promptFiles map[string]io.Reader,
	options ...*GenerationOptions,
) (res *genai.GenerateContentResponse, err error)

Generate generates with given values synchronously.

It times out in `timeoutSeconds` seconds.

It retries on 5xx errors for `maxRetryCount` times.

func (*Client) GenerateEmbeddings added in v0.1.24

func (c *Client) GenerateEmbeddings(ctx context.Context, title string, parts []genai.Part) (vectors []float32, err error)

GenerateEmbeddings generates embeddings with given values.

`title` can be empty.

https://ai.google.dev/gemini-api/docs/embeddings

func (*Client) GenerateStreamIterated added in v0.0.11

func (c *Client) GenerateStreamIterated(
	ctx context.Context,
	promptText string,
	promptFiles map[string]io.Reader,
	options ...*GenerationOptions,
) (iterator *genai.GenerateContentResponseIterator, err error)

GenerateStreamIterated generates stream iterator with given values.

It does not timeout itself, so set timeout with `ctx` when needed.

func (*Client) GenerateStreamed

func (c *Client) GenerateStreamed(
	ctx context.Context,
	promptText string,
	promptFiles map[string]io.Reader,
	fnStreamCallback FnStreamCallback,
	options ...*GenerationOptions,
) error

GenerateStreamed generates with given values synchronously and calls back `fnStreamCallback`.

It times out in `timeoutSeconds` seconds.

func (*Client) ListAllCachedContexts added in v0.1.3

func (c *Client) ListAllCachedContexts(ctx context.Context) (listed map[string]genai.CachedContent, err error)

ListAllCachedContexts lists all cached contexts.

`listed` is a map of cached context name and display name.

func (*Client) SetCachedContextExpireTime added in v0.1.0

func (c *Client) SetCachedContextExpireTime(ctx context.Context, cachedContextName string, expireTime time.Time) (err error)

SetCachedContextExpireTime sets the expiration time of a cached context.

(default: 1 hour later)

func (*Client) SetCachedContextTTL added in v0.1.0

func (c *Client) SetCachedContextTTL(ctx context.Context, cachedContextName string, ttl time.Duration) (err error)

SetCachedContextTTL sets the TTL of a cached context.

(default: 1 hour)

func (*Client) SetSystemInstructionFunc

func (c *Client) SetSystemInstructionFunc(fn FnSystemInstruction)

SetSystemInstructionFunc sets the system instruction function.

func (*Client) SetTimeout

func (c *Client) SetTimeout(seconds int)

SetTimeout sets the timeout in seconds.

func (*Client) UploadFilesAndWait added in v0.0.10

func (c *Client) UploadFilesAndWait(ctx context.Context, files map[string]io.Reader) (uploaded []genai.FileData, err error)

UploadFilesAndWait uploads files and wait for them to be ready.

`files` is a map of keys: display name, and values: io.Reader.

type FnStreamCallback

type FnStreamCallback func(callbackData StreamCallbackData)

function definitions

type FnSystemInstruction

type FnSystemInstruction func() string

function definitions

type GenerationOptions

type GenerationOptions struct {
	// generation config
	Config *genai.GenerationConfig

	// tool config
	Tools      []*genai.Tool
	ToolConfig *genai.ToolConfig

	// safety settings: harm block threshold
	HarmBlockThreshold *genai.HarmBlockThreshold

	// cached context
	CachedContextName *string

	// history (for session)
	History []*genai.Content
}

GenerationOptions struct for function Generate.

func NewGenerationOptions added in v0.1.12

func NewGenerationOptions() *GenerationOptions

NewGenerationOptions returns a new GenerationOptions with default values.

type NumTokens

type NumTokens struct {
	Input  int32
	Output int32
	Cached int32
}

NumTokens struct for input/output token numbers

type StreamCallbackData

type StreamCallbackData struct {
	// when there is a text delta,
	TextDelta *string

	// when there is a function call,
	FunctionCall *genai.FunctionCall

	// when there is a code execution result,
	ExecutableCode      *genai.ExecutableCode
	CodeExecutionResult *genai.CodeExecutionResult

	// when the number of tokens are calculated, (after the stream is finished)
	NumTokens *NumTokens

	// when there is a finish reason,
	FinishReason *genai.FinishReason

	// when there is an error,
	Error error
}

StreamCallbackData struct contains the data for stream callback function.

type TextChunkOption added in v0.1.35

type TextChunkOption struct {
	ChunkSize                uint
	OverlappedSize           uint
	KeepBrokenUTF8Characters bool
	EllipsesText             string
}

TextChunkOption contains options for chunking text.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳