Documentation
¶
Index ¶
- Constants
- func Retry(retryableFunc RetryableFuncWithResponse, options ...RetryOption) (*http.Response, error)
- type Client
- func (m *Client) CheckAndRefreshToken() error
- func (m *Client) EmbedDocuments(model string, texts []string, options ...EmbeddingOption) (EmbeddingResponse, error)
- func (m *Client) EmbedQuery(model string, text string, options ...EmbeddingOption) (EmbeddingResponse, error)
- func (m *Client) GenerateText(model, prompt string, options ...GenerateOption) (GenerateTextResult, error)
- func (m *Client) GenerateTextStream(model, prompt string, options ...GenerateOption) (<-chan GenerateTextResult, error)
- func (m *Client) RefreshToken() error
- type ClientOption
- func WithAPIVersion(apiVersion string) ClientOption
- func WithIAM(iam string) ClientOption
- func WithRegion(region IBMCloudRegion) ClientOption
- func WithURL(url string) ClientOption
- func WithWatsonxAPIKey(watsonxAPIKey WatsonxAPIKey) ClientOption
- func WithWatsonxProjectID(projectID WatsonxProjectID) ClientOption
- type ClientOptions
- type Doer
- type EmbeddingOption
- type EmbeddingOptions
- type EmbeddingPayload
- type EmbeddingResponse
- type EmbeddingResult
- type EmbeddingReturnOptions
- type GenerateOption
- func WithDecodingMethod(decodingMethod string) GenerateOption
- func WithLengthPenalty(decayFactor float64, startIndex uint) GenerateOption
- func WithMaxNewTokens(maxNewTokens uint) GenerateOption
- func WithMinNewTokens(minNewTokens uint) GenerateOption
- func WithRandomSeed(randomSeed uint) GenerateOption
- func WithRepetitionPenalty(repetitionPenalty float64) GenerateOption
- func WithReturnOptions(inputText, generatedTokens, inputTokens, tokenLogProbs, tokenRanks bool, ...) GenerateOption
- func WithStopSequences(stopSequences []string) GenerateOption
- func WithTemperature(temperature float64) GenerateOption
- func WithTimeLimit(timeLimit uint) GenerateOption
- func WithTopK(topK uint) GenerateOption
- func WithTopP(topP float64) GenerateOption
- func WithTruncateInputTokens(truncateInputTokens uint) GenerateOption
- type GenerateOptions
- type GenerateTextPayload
- type GenerateTextResult
- type HttpClient
- type IAMToken
- type IBMCloudRegion
- type LengthPenalty
- type ModelType
- type OnRetryFunc
- type RetryConfig
- type RetryIfFunc
- type RetryOption
- type RetryableFuncWithResponse
- type ReturnOptions
- type StopReason
- type Timer
- type TokenResponse
- type WatsonxAPIKey
- type WatsonxProjectID
Constants ¶
const ( GenerationEndpoint string = "/ml/v1/text" GenerateTextEndpoint string = GenerationEndpoint + "/generation" GenerateTextStreamEndpoint string = GenerationEndpoint + "/generation_stream" )
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" )
const (
EmbeddingEndpoint string = "/ml/v1/text/embeddings"
)
const (
IAMCloudHost = "iam.cloud.ibm.com"
)
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 ¶
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 ¶
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 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 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) DoWithRetry ¶ added in v1.0.2
type IAMToken ¶
type IAMToken struct {
// contains filtered or unexported fields
}
func GenerateToken ¶
func GenerateToken(client Doer, watsonxApiKey WatsonxAPIKey, iamCloudHost string) (IAMToken, error)
type IBMCloudRegion ¶
type IBMCloudRegion = string
type LengthPenalty ¶
type OnRetryFunc ¶ added in v1.0.2
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
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
RetryableFuncWithResponse represents a function that returns an HTTP response or an error.
type ReturnOptions ¶
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 TokenResponse ¶
type WatsonxAPIKey ¶
type WatsonxAPIKey = string
type WatsonxProjectID ¶
type WatsonxProjectID = string