| 1 | package deepseek |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "fmt" |
| 6 | "net/http" |
| 7 | "sort" |
| 8 | "strings" |
| 9 | ) |
| 10 | |
| 11 | const modelsURL = "https://api.deepseek.com/models" |
| 12 | |
| 13 | type Model struct { |
| 14 | ID string `json:"id"` |
| 15 | } |
| 16 | |
| 17 | func (m Model) DisplayName() string { |
| 18 | return strings.TrimPrefix(m.ID, "deepseek-") |
| 19 | } |
| 20 | |
| 21 | func ListModels(apiKey string) ([]Model, error) { |
| 22 | req, err := http.NewRequest("GET", modelsURL, nil) |
| 23 | if err != nil { |
| 24 | return nil, err |
| 25 | } |
| 26 | if apiKey != "" { |
| 27 | req.Header.Set("Authorization", "Bearer "+apiKey) |
| 28 | } |
| 29 | req.Header.Set("Accept", "application/json") |
| 30 | |
| 31 | resp, err := http.DefaultClient.Do(req) |
| 32 | if err != nil { |
| 33 | return nil, fmt.Errorf("fetching models: %w", err) |
| 34 | } |
| 35 | defer resp.Body.Close() |
| 36 | |
| 37 | if resp.StatusCode != http.StatusOK { |
| 38 | return nil, fmt.Errorf("models endpoint returned %s", resp.Status) |
| 39 | } |
| 40 | |
| 41 | var result struct { |
| 42 | Data []Model `json:"data"` |
| 43 | } |
| 44 | if err := json.NewDecoder(resp.Body).Decode(&result); err != nil { |
| 45 | return nil, fmt.Errorf("parsing models: %w", err) |
| 46 | } |
| 47 | |
| 48 | return result.Data, nil |
| 49 | } |
| 50 | |
| 51 | func CategorizeModels(models []Model) ([]string, map[string][]Model) { |
| 52 | grouped := make(map[string][]Model) |
| 53 | grouped["Models"] = append(grouped["Models"], models...) |
| 54 | |
| 55 | sort.Slice(grouped["Models"], func(i, j int) bool { |
| 56 | return grouped["Models"][i].ID < grouped["Models"][j].ID |
| 57 | }) |
| 58 | |
| 59 | return []string{"Models"}, grouped |
| 60 | } |
| 61 | |
| 62 | func AllModelIDs(models []Model) map[string]bool { |
| 63 | ids := make(map[string]bool, len(models)) |
| 64 | for _, m := range models { |
| 65 | ids[m.ID] = true |
| 66 | } |
| 67 | return ids |
| 68 | } |
| 69 | |