diff --git a/openai/client.go b/openai/client.go index ce6f892..92a95a6 100644 --- a/openai/client.go +++ b/openai/client.go @@ -12,7 +12,9 @@ import ( const ( // BaseURL is OpenAI HTTP API base URL. - BaseURL = "https://api.openai.com/v1" + BaseURL = "https://api.openai.com" + // EmbedAPIVersion is the latest stable embedding API version. + EmbedAPIVersion = "v1" // Org header OrgHeader = "OpenAI-Organization" ) @@ -21,6 +23,7 @@ const ( type Client struct { apiKey string baseURL string + version string orgID string hc *http.Client } @@ -34,6 +37,7 @@ func NewClient() (*Client, error) { return &Client{ apiKey: os.Getenv("OPENAI_API_KEY"), baseURL: BaseURL, + version: EmbedAPIVersion, orgID: "", hc: &http.Client{}, }, nil @@ -51,6 +55,12 @@ func (c *Client) WithBaseURL(baseURL string) *Client { return c } +// WithVersion sets the API version. +func (c *Client) WithVersion(version string) *Client { + c.version = version + return c +} + // WithOrgID sets the organization ID. func (c *Client) WithOrgID(orgID string) *Client { c.orgID = orgID diff --git a/openai/embedding.go b/openai/embedding.go index 683e374..6d0214c 100644 --- a/openai/embedding.go +++ b/openai/embedding.go @@ -19,11 +19,11 @@ type Usage struct { TotalTokens int `json:"total_tokens"` } -// Embedding is openai API vector embedding. +// Embedding is openai API embedding. type Embedding struct { - Object string `json:"object"` - Index int `json:"index"` - Embedding []float64 `json:"embedding"` + Object string `json:"object"` + Index int `json:"index"` + Vector []float64 `json:"vector"` } // EmbeddingString is base64 encoded embedding. @@ -87,9 +87,9 @@ func ToEmbeddings[T any](resp io.Reader) ([]*Embedding, error) { return nil, err } emb := &Embedding{ - Object: d.Object, - Index: d.Index, - Embedding: floats, + Object: d.Object, + Index: d.Index, + Vector: floats, } embs = append(embs, emb) } @@ -98,9 +98,9 @@ func ToEmbeddings[T any](resp io.Reader) ([]*Embedding, error) { embs := make([]*Embedding, 0, len(e.Data)) for _, d := range e.Data { emb := &Embedding{ - Object: d.Object, - Index: d.Index, - Embedding: d.Embedding, + Object: d.Object, + Index: d.Index, + Vector: d.Embedding, } embs = append(embs, emb) } @@ -112,7 +112,7 @@ func ToEmbeddings[T any](resp io.Reader) ([]*Embedding, error) { // Embeddings returns embeddings for every object in EmbeddingRequest. func (c *Client) Embeddings(ctx context.Context, embReq *EmbeddingRequest) ([]*Embedding, error) { - u, err := url.Parse(c.baseURL + "/embeddings") + u, err := url.Parse(c.baseURL + "/" + c.version + "/embeddings") if err != nil { return nil, err }