Commit Diff


commit - 7d5f9562c75185f9fe13bbc326577dc860146124
commit + 5a8cb9306ea5b15940ed7ef671f47d6b3f71c3b8
blob - 50086755e5e95a4d364613b0e3e8358e87751979
blob + 8f32fd79a22c5ce6b46d79e05671bc411288ac38
--- src/openai/openai.go
+++ src/openai/openai.go
@@ -6,6 +6,7 @@ import (
 	"fmt"
 	"net/http"
 	"strings"
+	"time"
 )
 
 type Role string
@@ -29,6 +30,15 @@ type Chat struct {
 	} `json:"response_format,omitempty"`
 }
 
+type Model struct {
+	ID               string
+	Name             string
+	Description      string
+	Aliases          []string
+	MaxContextLength int
+	Deprecation      time.Time
+}
+
 type Client struct {
 	*http.Client
 	Token   string
@@ -106,3 +116,30 @@ func (c *Client) Complete(chat *Chat) (*Message, error
 	}
 	return &cresp.Choices[0].Message, nil
 }
+
+func (c *Client) Models() ([]Model, error) {
+	u := c.BaseURL + "/v1/models"
+	req, err := http.NewRequest(http.MethodGet, u, nil)
+	if err != nil {
+		return nil, err
+	}
+	resp, err := c.do(req)
+	if err != nil {
+		return nil, err
+	}
+	defer resp.Body.Close()
+	if resp.StatusCode != http.StatusOK {
+		var aerr apiError
+		if err := json.NewDecoder(resp.Body).Decode(&aerr); err != nil {
+			return nil, fmt.Errorf(resp.Status)
+		}
+		return nil, aerr
+	}
+	v := struct {
+		Data []Model
+	}{}
+	if err := json.NewDecoder(resp.Body).Decode(&v); err != nil {
+		return nil, fmt.Errorf("decode response: %w", err)
+	}
+	return v.Data, nil
+}