Documentation
¶
Overview ¶
Package openai provides an llms.Model implementation for OpenAI-compatible APIs, including OpenAI and Azure OpenAI endpoints. Use options to select the provider style, model, base URL, token, and API version where applicable.
Example
mdl, err := openai.New(
openai.WithProvider(openai.ProviderOpenAI),
openai.WithToken(os.Getenv("OPENAI_API_KEY")),
openai.WithModel("gpt-4o-mini"),
)
_ = mdl; _ = err
Index ¶
- Constants
- Variables
- func ExtractToolParts(msg *ChatMessage) ([]llms.ContentPart, []llms.ToolCall)
- type ChatMessage
- type LLM
- func (o *LLM) CancelBatch(ctx context.Context, batchID string) (*llms.BatchHandle, error)
- func (o *LLM) CreateEmbedding(ctx context.Context, inputTexts []string) ([][]float32, error)
- func (o *LLM) FetchBatchResults(ctx context.Context, batchID string) ([]llms.BatchResult, error)
- func (o *LLM) GenerateContent(ctx context.Context, messages []llms.Message, options ...llms.CallOption) (*llms.ContentResponse, error)
- func (o *LLM) GetBatch(ctx context.Context, batchID string) (*llms.BatchHandle, error)
- func (o *LLM) GetName() string
- func (o *LLM) GetProviderType() llms.ProviderType
- func (o *LLM) SubmitBatch(ctx context.Context, requests []llms.BatchRequest, ...) (*llms.BatchHandle, error)
- type Option
- func WithAPIVersion(apiVersion string) Option
- func WithAWSConfig(cfg *aws.Config) Option
- func WithBaseURL(baseURL string) Option
- func WithEmbeddingModel(embeddingModel string) Option
- func WithHTTPClient(client openaiclient.Doer) Option
- func WithModel(model string) Option
- func WithOrganization(organization string) Option
- func WithProvider(apiType llms.ProviderType) Option
- func WithResponseFormat(responseFormat *schema.ResponseFormat) Option
- func WithToken(token string) Option
Constants ¶
const ( RoleSystem = "system" RoleAssistant = "assistant" RoleUser = "user" RoleFunction = "function" RoleTool = "tool" )
const ( DefaultTokenEnvVarName = "OPENAI_API_KEY" //nolint:gosec DefaultModelEnvVarName = "OPENAI_MODEL" //nolint:gosec DefaultBaseURLEnvVarName = "OPENAI_BASE_URL" //nolint:gosec DefaultOrganizationEnvVarName = "OPENAI_ORGANIZATION" //nolint:gosec )
const (
DefaultAPIVersion = "2023-05-15"
)
Variables ¶
var ( ErrEmptyResponse = errors.New("no response") ErrMissingToken = errors.New("missing the OpenAI API key") ErrMissingAzureModel = errors.New("model needs to be provided when using Azure API") ErrMissingAzureEmbeddingModel = errors.New("embeddings model needs to be provided when using Azure API") ErrUnexpectedResponseLength = errors.New("unexpected length of response") )
Functions ¶
func ExtractToolParts ¶
func ExtractToolParts(msg *ChatMessage) ([]llms.ContentPart, []llms.ToolCall)
ExtractToolParts extracts the tool parts from a message.
Types ¶
type ChatMessage ¶
type ChatMessage = openaiclient.ChatMessage
type LLM ¶
type LLM struct {
// contains filtered or unexported fields
}
func (*LLM) CancelBatch ¶ added in v0.17.130
CancelBatch implements llms.Batcher.
func (*LLM) CreateEmbedding ¶
CreateEmbedding creates embeddings for the given input texts.
func (*LLM) FetchBatchResults ¶ added in v0.17.130
FetchBatchResults implements llms.Batcher. It refuses to read partial output: callers must wait until BatchHandle.Status.IsTerminal() returns true (typically by polling GetBatch).
func (*LLM) GenerateContent ¶
func (o *LLM) GenerateContent(ctx context.Context, messages []llms.Message, options ...llms.CallOption) (*llms.ContentResponse, error)
GenerateContent implements the Model interface.
func (*LLM) GetBatch ¶ added in v0.17.130
GetBatch implements llms.Batcher.
func (*LLM) GetProviderType ¶ added in v0.10.54
func (o *LLM) GetProviderType() llms.ProviderType
GetProviderType implements the Model interface.
func (*LLM) SubmitBatch ¶ added in v0.17.130
func (o *LLM) SubmitBatch(ctx context.Context, requests []llms.BatchRequest, options ...llms.BatchSubmitOption) (*llms.BatchHandle, error)
SubmitBatch implements llms.Batcher. It marshals each BatchRequest into a JSONL line, uploads the document as a file with purpose=batch, and creates a batch targeting either /v1/chat/completions or /v1/responses depending on whether the underlying provider configuration uses the Responses API.
type Option ¶
type Option func(*options)
Option is a functional option for the OpenAI client.
func WithAPIVersion ¶
WithAPIVersion passes the api version to the client. If not set, the default value is DefaultAPIVersion.
func WithAWSConfig ¶ added in v0.19.148
WithAWSConfig passes the AWS config to the client.
func WithBaseURL ¶
WithBaseURL passes the OpenAI base url to the client. If not set, the base url is read from the OPENAI_BASE_URL environment variable. If still not set in ENV VAR OPENAI_BASE_URL, then the default value is https://api.openai.com/v1 is used.
func WithEmbeddingModel ¶
WithEmbeddingModel passes the OpenAI model to the client. Required when ApiType is Azure.
func WithHTTPClient ¶
func WithHTTPClient(client openaiclient.Doer) Option
WithHTTPClient allows setting a custom HTTP client. If not set, the default value is http.DefaultClient.
func WithModel ¶
WithModel passes the OpenAI model to the client. If not set, the model is read from the OPENAI_MODEL environment variable. Required when ApiType is Azure.
func WithOrganization ¶
WithOrganization passes the OpenAI organization to the client. If not set, the organization is read from the OPENAI_ORGANIZATION.
func WithProvider ¶ added in v0.14.88
func WithProvider(apiType llms.ProviderType) Option
WithProvider passes the api type to the client. If not set, the default value is ProviderOpenAI.
func WithResponseFormat ¶
func WithResponseFormat(responseFormat *schema.ResponseFormat) Option
WithResponseFormat allows setting a custom response format.