aboutsummaryrefslogtreecommitdiff
path: root/web/openai/models.go
blob: 423c11b0d85ecbff7e1d85998eaff5c7d182f07a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package openai

import (
	"context"
	"encoding/json"
	"net/http"
)

type Model struct {
	ID      string `json:"id"`
	Object  string `json:"object"`
	Created int64  `json:"created"`
	OwnedBy string `json:"owned_by"`
}

// ListModels returns a list of all models available to the user.
func ListModels(ctx context.Context, cli *http.Client, openAIKey string) ([]Model, error) {
	req, err := http.NewRequestWithContext(ctx, "GET", "https://api.openai.com/v1/models", nil)
	if err != nil {
		return nil, err
	}

	type response struct {
		Object string  `json:"object"`
		Data   []Model `json:"data"`
	}

	resp, err := cli.Do(req)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	var result response
	if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
		return nil, err
	}

	return result.Data, nil
}