openai

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: May 18, 2024 License: Apache-2.0 Imports: 11 Imported by: 6

Documentation

Index

Constants

View Source
const (
	// BaseURL is OpenAI HTTP API base URL.
	BaseURL = "https://api.openai.com"
	// EmbedAPIVersion is the latest stable embeddings API version.
	EmbedAPIVersion = "v1"
	// OrgHeader is an Organization header
	OrgHeader = "OpenAI-Organization"
)

Variables

View Source
var (
	// ErrInValidData is returned when the API client fails to decode the returned data.
	ErrInValidData = errors.New("invalid data")
	// ErrUnsupportedEncoding is returned when API client attempts to use unsupported encoding format.
	ErrUnsupportedEncoding = errors.New("unsupported encoding format")
)

Functions

func NewEmbedder

func NewEmbedder(opts ...Option) embeddings.Embedder[*EmbeddingRequest]

NewEmbedder creates a client that implements embeddings.Embedder

Types

type APIError

type APIError struct {
	Err struct {
		Message string  `json:"message"`
		Type    string  `json:"type"`
		Param   *string `json:"param,omitempty"`
		Code    any     `json:"code,omitempty"`
	} `json:"error"`
}

APIError is open AI API error.

func (APIError) Error

func (e APIError) Error() string

Error implements error interface.

type Client

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

Client is an OpenAI HTTP API client.

func NewClient

func NewClient(opts ...Option) *Client

NewClient creates a new OpenAI HTTP API client and returns it. By default it reads the OpenAI API key from OPENAI_API_KEY env var and uses the default Go http.Client for making API requests. You can override the default options via the client methods.

func (*Client) Embed

func (c *Client) Embed(ctx context.Context, embReq *EmbeddingRequest) ([]*embeddings.Embedding, error)

Embed returns embeddings for every object in EmbeddingRequest.

type Data

type Data struct {
	Object    string    `json:"object"`
	Index     int       `json:"index"`
	Embedding []float64 `json:"embedding"`
}

Data stores vector embeddings.

type DataGen

type DataGen[T any] struct {
	Object    string `json:"object"`
	Index     int    `json:"index"`
	Embedding T      `json:"embedding"`
}

DataGen is a generic struct used for deserializing vector embeddings.

type EmbeddingRequest

type EmbeddingRequest struct {
	Input          any            `json:"input"`
	Model          Model          `json:"model"`
	User           string         `json:"user"`
	EncodingFormat EncodingFormat `json:"encoding_format,omitempty"`
	// NOTE: only supported in V3 and later
	Dims int `json:"dimensions,omitempty"`
}

EmbeddingRequest is serialized and sent to the API server.

type EmbeddingResponse

type EmbeddingResponse struct {
	Object string `json:"object"`
	Data   []Data `json:"data"`
	Model  Model  `json:"model"`
	Usage  Usage  `json:"usage"`
}

EmbeddingResponseGen is the API response.

func (*EmbeddingResponse) ToEmbeddings

func (e *EmbeddingResponse) ToEmbeddings() ([]*embeddings.Embedding, error)

ToEmbeddings converts the API response, into a slice of embeddings and returns it.

type EmbeddingResponseGen

type EmbeddingResponseGen[T any] struct {
	Object string       `json:"object"`
	Data   []DataGen[T] `json:"data"`
	Model  Model        `json:"model"`
	Usage  Usage        `json:"usage"`
}

EmbeddingResponseGen is a generic struct used for deserializing API response.

type EncodingFormat

type EncodingFormat string

EncodingFormat for embedding API requests.

const (
	EncodingFloat EncodingFormat = "float"
	// EncodingBase64 makes OpenAI API return embeddings
	// encoded as base64 string
	EncodingBase64 EncodingFormat = "base64"
)

func (EncodingFormat) String

func (f EncodingFormat) String() string

String implements stringer.

type Model

type Model string

Model is embedding model.

const (
	TextAdaV2   Model = "text-embedding-ada-002"
	TextLargeV3 Model = "text-embedding-3-large"
	TextSmallV3 Model = "text-embedding-3-small"
)

func (Model) String

func (m Model) String() string

String implements stringer.

type Option

type Option func(*Options)

Option is functional option.

func WithAPIKey

func WithAPIKey(apiKey string) Option

WithAPIKey sets the API key.

func WithBaseURL

func WithBaseURL(baseURL string) Option

WithBaseURL sets the API base URL.

func WithHTTPClient

func WithHTTPClient(httpClient *client.HTTP) Option

WithHTTPClient sets the HTTP client.

func WithOrgID

func WithOrgID(orgID string) Option

WithOrgID sets the organization ID.

func WithVersion

func WithVersion(version string) Option

WithVersion sets the API version.

type Options

type Options struct {
	APIKey     string
	BaseURL    string
	Version    string
	OrgID      string
	HTTPClient *client.HTTP
}

type Usage

type Usage struct {
	PromptTokens int `json:"prompt_tokens"`
	TotalTokens  int `json:"total_tokens"`
}

Usage tracks API token usage.

Jump to

Keyboard shortcuts

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