models

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2024 License: Apache-2.0 Imports: 14 Imported by: 36

Documentation

Index

Constants

View Source
const (
	GenerationEndpoint         string = "/ml/v1/text"
	GenerateTextEndpoint       string = GenerationEndpoint + "/generation"
	GenerateTextStreamEndpoint string = GenerationEndpoint + "/generation_stream"
)
View Source
const (
	WatsonxURLEnvVarName = "WATSONX_URL_HOST" // Override the default URL host '*.ml.cloud.ibm.com'
	WatsonxIAMEnvVarName = "WATSONX_IAM_HOST" // Override the default IAM host 'iam.cloud.ibm.com'

	WatsonxAPIKeyEnvVarName    = "WATSONX_API_KEY"
	WatsonxProjectIDEnvVarName = "WATSONX_PROJECT_ID"

	US_South  IBMCloudRegion = "us-south"
	Dallas    IBMCloudRegion = US_South
	EU_DE     IBMCloudRegion = "eu-de"
	Frankfurt IBMCloudRegion = EU_DE
	JP_TOK    IBMCloudRegion = "jp-tok"
	Tokyo     IBMCloudRegion = JP_TOK

	DefaultRegion     = US_South
	BaseURLFormatStr  = "%s.ml.cloud.ibm.com" // Need to call SPrintf on it with region
	DefaultAPIVersion = "2024-05-20"
)
View Source
const (
	EmbeddingEndpoint string = "/ml/v1/text/embeddings"
)
View Source
const (
	IAMCloudHost = "iam.cloud.ibm.com"
)
View Source
const (
	TokenPath string = "/identity/token"
)

Variables

This section is empty.

Functions

func Retry added in v1.0.2

func Retry(retryableFunc RetryableFuncWithResponse, options ...RetryOption) (*http.Response, error)

Retry retries the provided retryableFunc according to the retry configuration options.

Types

type Client

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

func NewClient

func NewClient(options ...ClientOption) (*Client, error)

func (*Client) CheckAndRefreshToken

func (m *Client) CheckAndRefreshToken() error

CheckAndRefreshToken checks the IAM token if it expired; if it did, it refreshes it; nothing if not

func (*Client) EmbedDocuments added in v1.0.2

func (m *Client) EmbedDocuments(model string, texts []string, options ...EmbeddingOption) (EmbeddingResponse, error)

EmbedDocuments embeds the given texts using the specified model.

func (*Client) EmbedQuery added in v1.0.2

func (m *Client) EmbedQuery(model string, text string, options ...EmbeddingOption) (EmbeddingResponse, error)

EmbedQuery embeds the given text using the specified model.

func (*Client) GenerateText

func (m *Client) GenerateText(model, prompt string, options ...GenerateOption) (GenerateTextResult, error)

GenerateText generates completion text based on a given prompt and parameters

func (*Client) GenerateTextStream added in v1.0.2

func (m *Client) GenerateTextStream(model, prompt string, options ...GenerateOption) (<-chan GenerateTextResult, error)

GenerateTextStream generates completion text channel (stream) based on a given prompt and parameters

func (*Client) RefreshToken

func (m *Client) RefreshToken() error

RefreshToken generates and sets the model with a new token

type ClientOption

type ClientOption func(*ClientOptions)

func WithAPIVersion

func WithAPIVersion(apiVersion string) ClientOption

func WithIAM added in v1.0.1

func WithIAM(iam string) ClientOption

func WithRegion

func WithRegion(region IBMCloudRegion) ClientOption

func WithURL

func WithURL(url string) ClientOption

func WithWatsonxAPIKey

func WithWatsonxAPIKey(watsonxAPIKey WatsonxAPIKey) ClientOption

func WithWatsonxProjectID

func WithWatsonxProjectID(projectID WatsonxProjectID) ClientOption

type ClientOptions

type ClientOptions struct {
	URL        string
	IAM        string
	Region     IBMCloudRegion
	APIVersion string
	// contains filtered or unexported fields
}

type Doer

type Doer interface {
	Do(req *http.Request) (*http.Response, error)
	DoWithRetry(req *http.Request) (*http.Response, error)
}

type EmbeddingOption added in v1.0.2

type EmbeddingOption func(*EmbeddingOptions)

func WithEmbeddingReturnOptions added in v1.0.2

func WithEmbeddingReturnOptions(inputText bool) EmbeddingOption

func WithEmbeddingTruncateInputTokens added in v1.0.2

func WithEmbeddingTruncateInputTokens(truncateInputTokens uint) EmbeddingOption

type EmbeddingOptions added in v1.0.2

type EmbeddingOptions struct {
	TruncateInputTokens *uint                   `json:"truncate_input_tokens,omitempty"`
	ReturnOptions       *EmbeddingReturnOptions `json:"return_options,omitempty"`
}

func (*EmbeddingOptions) String added in v1.0.2

func (ep *EmbeddingOptions) String() string

type EmbeddingPayload added in v1.0.2

type EmbeddingPayload struct {
	ProjectID  string            `json:"project_id"`
	Model      string            `json:"model_id"`
	Inputs     []string          `json:"inputs"`
	Parameters *EmbeddingOptions `json:"parameters,omitempty"`
}

type EmbeddingResponse added in v1.0.2

type EmbeddingResponse struct {
	Model           string            `json:"model_id"`
	Results         []EmbeddingResult `json:"results"`
	CreatedAt       time.Time         `json:"created_at"`
	InputTokenCount int               `json:"input_token_count"`
}

type EmbeddingResult added in v1.0.2

type EmbeddingResult struct {
	Embedding []float64 `json:"embedding"`
	Input     string    `json:"input,omitempty"`
}

type EmbeddingReturnOptions added in v1.0.2

type EmbeddingReturnOptions struct {
	InputText bool `json:"input_text"`
}

type GenerateOption

type GenerateOption func(*GenerateOptions)

func WithDecodingMethod

func WithDecodingMethod(decodingMethod string) GenerateOption

func WithLengthPenalty

func WithLengthPenalty(decayFactor float64, startIndex uint) GenerateOption

func WithMaxNewTokens

func WithMaxNewTokens(maxNewTokens uint) GenerateOption

func WithMinNewTokens

func WithMinNewTokens(minNewTokens uint) GenerateOption

func WithRandomSeed

func WithRandomSeed(randomSeed uint) GenerateOption

func WithRepetitionPenalty

func WithRepetitionPenalty(repetitionPenalty float64) GenerateOption

func WithReturnOptions

func WithReturnOptions(inputText, generatedTokens, inputTokens, tokenLogProbs, tokenRanks bool, topNTokens int) GenerateOption

func WithStopSequences

func WithStopSequences(stopSequences []string) GenerateOption

func WithTemperature

func WithTemperature(temperature float64) GenerateOption

func WithTimeLimit

func WithTimeLimit(timeLimit uint) GenerateOption

func WithTopK

func WithTopK(topK uint) GenerateOption

func WithTopP

func WithTopP(topP float64) GenerateOption

func WithTruncateInputTokens

func WithTruncateInputTokens(truncateInputTokens uint) GenerateOption

type GenerateOptions

type GenerateOptions struct {
	// https://ibm.github.io/watson-machine-learning-sdk/_modules/metanames.html#GenTextParamsMetaNames
	DecodingMethod      *string        `json:"decoding_method,omitempty"`
	LengthPenalty       *LengthPenalty `json:"length_penalty,omitempty"`
	Temperature         *float64       `json:"temperature,omitempty"`
	TopP                *float64       `json:"top_p,omitempty"`
	TopK                *uint          `json:"top_k,omitempty"`
	RandomSeed          *uint          `json:"random_seed,omitempty"`
	RepetitionPenalty   *float64       `json:"repetition_penalty,omitempty"`
	MinNewTokens        *uint          `json:"min_new_tokens,omitempty"`
	MaxNewTokens        *uint          `json:"max_new_tokens,omitempty"`
	StopSequences       *[]string      `json:"stop_sequences,omitempty"`
	TimeLimit           *uint          `json:"time_limit,omitempty"`
	TruncateInputTokens *uint          `json:"truncate_input_tokens,omitempty"`
	ReturnOptions       *ReturnOptions `json:"return_options,omitempty"`
}

func (*GenerateOptions) String

func (gp *GenerateOptions) String() string

type GenerateTextPayload

type GenerateTextPayload struct {
	ProjectID  string           `json:"project_id"`
	Model      string           `json:"model_id"`
	Prompt     string           `json:"input"`
	Parameters *GenerateOptions `json:"parameters,omitempty"`
}

type GenerateTextResult

type GenerateTextResult struct {
	Text                string     `json:"generated_text"`
	GeneratedTokenCount int        `json:"generated_token_count"`
	InputTokenCount     int        `json:"input_token_count"`
	StopReason          StopReason `json:"stop_reason"`
}

type HttpClient added in v1.0.2

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

Custom wrapper for http.Client that implements the Doer interface. - Do - DoWithRetry

func NewHttpClient added in v1.0.2

func NewHttpClient() *HttpClient

func (*HttpClient) Do added in v1.0.2

func (c *HttpClient) Do(req *http.Request) (*http.Response, error)

func (*HttpClient) DoWithRetry added in v1.0.2

func (c *HttpClient) DoWithRetry(req *http.Request) (*http.Response, error)

type IAMToken

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

func GenerateToken

func GenerateToken(client Doer, watsonxApiKey WatsonxAPIKey, iamCloudHost string) (IAMToken, error)

func (*IAMToken) Expired

func (t *IAMToken) Expired() bool

type IBMCloudRegion

type IBMCloudRegion = string

type LengthPenalty

type LengthPenalty struct {
	DecayFactor float64 `json:"decay_factor"`
	StartIndex  uint    `json:"start_index"`
}

type ModelType

type ModelType = string

type OnRetryFunc added in v1.0.2

type OnRetryFunc func(attempt uint, err error)

OnRetryFunc is a function type that is called on each retry attempt.

type RetryConfig added in v1.0.2

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

RetryConfig contains configuration options for the retry mechanism.

type RetryIfFunc added in v1.0.2

type RetryIfFunc func(error) bool

RetryIfFunc determines whether a retry should be attempted based on the error.

type RetryOption added in v1.0.2

type RetryOption func(*RetryConfig)

RetryOption is a function type for modifying RetryConfig options.

func WithBackoff added in v1.0.2

func WithBackoff(backoff time.Duration) RetryOption

WithBackoff sets the backoff duration between retries.

func WithMaxJitter added in v1.0.2

func WithMaxJitter(maxJitter time.Duration) RetryOption

WithMaxJitter sets the maximum jitter duration to add to the backoff.

func WithOnRetry added in v1.0.2

func WithOnRetry(onRetry OnRetryFunc) RetryOption

WithOnRetry sets the callback function to execute on each retry.

func WithRetries added in v1.0.2

func WithRetries(retries uint) RetryOption

WithRetries sets the number of retries for the retry configuration.

func WithRetryIf added in v1.0.2

func WithRetryIf(retryIf RetryIfFunc) RetryOption

WithRetryIf sets the condition to determine whether to retry based on the error.

type RetryableFuncWithResponse added in v1.0.2

type RetryableFuncWithResponse func() (*http.Response, error)

RetryableFuncWithResponse represents a function that returns an HTTP response or an error.

type ReturnOptions

type ReturnOptions struct {
	InputText       bool `json:"input_text"`
	GeneratedTokens bool `json:"generated_tokens"`
	InputTokens     bool `json:"input_tokens"`
	TokenLogProbs   bool `json:"token_logprobs"`
	TokenRanks      bool `json:"token_ranks"`
	TopNTokens      int  `json:"top_n_tokens"`
}

type StopReason

type StopReason = string
const (
	NotFinished        StopReason = "not_finished"  // Possibly more tokens to be streamed
	MaxTokens          StopReason = "max_tokens"    // Maximum requested tokens reached
	EndOfSequenceToken StopReason = "eos_token"     // End of sequence token encountered
	Cancelled          StopReason = "cancelled"     // Request canceled by the client
	TimeLimit          StopReason = "time_limit"    // Time limit reached
	StopSequence       StopReason = "stop_sequence" // Stop sequence encountered
	TokenLimit         StopReason = "token_limit"   // Token limit reached
	Error              StopReason = "error"         // Error encountered
)

type Timer added in v1.0.2

type Timer interface {
	After(time.Duration) <-chan time.Time
}

Timer interface to abstract time-based operations for retries.

type TokenResponse

type TokenResponse struct {
	AccessToken string `json:"access_token"`
	Expiration  int64  `json:"expiration"`
}

type WatsonxAPIKey

type WatsonxAPIKey = string

type WatsonxProjectID

type WatsonxProjectID = string

Jump to

Keyboard shortcuts

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