| 1 | package lmstudio |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "fmt" |
| 6 | "net/http" |
| 7 | "time" |
| 8 | ) |
| 9 | |
| 10 | // Model represents an LM Studio model from the /v1/models endpoint. |
| 11 | type Model struct { |
| 12 | ID string `json:"id"` |
| 13 | Object string `json:"object"` |
| 14 | Created int64 `json:"created"` |
| 15 | OwnedBy string `json:"owned_by"` |
| 16 | } |
| 17 | |
| 18 | // ListResponse is the response from the /v1/models endpoint. |
| 19 | type ListResponse struct { |
| 20 | Object string `json:"object"` |
| 21 | Data []Model `json:"data"` |
| 22 | } |
| 23 | |
| 24 | // ListModels fetches available models from the LM Studio server. |
| 25 | func ListModels(baseURL string) ([]Model, error) { |
| 26 | if baseURL == "" { |
| 27 | baseURL = "http://localhost:1234/v1" |
| 28 | } |
| 29 | |
| 30 | client := &http.Client{Timeout: 10 * time.Second} |
| 31 | resp, err := client.Get(baseURL + "/models") |
| 32 | if err != nil { |
| 33 | return nil, fmt.Errorf("connecting to LM Studio: %w", err) |
| 34 | } |
| 35 | defer resp.Body.Close() |
| 36 | |
| 37 | if resp.StatusCode != http.StatusOK { |
| 38 | return nil, fmt.Errorf("LM Studio returned status %d", resp.StatusCode) |
| 39 | } |
| 40 | |
| 41 | var listResp ListResponse |
| 42 | if err := json.NewDecoder(resp.Body).Decode(&listResp); err != nil { |
| 43 | return nil, fmt.Errorf("decoding response: %w", err) |
| 44 | } |
| 45 | |
| 46 | return listResp.Data, nil |
| 47 | } |
| 48 | |
| 49 | // DisplayName returns a human-friendly name for the model. |
| 50 | func (m Model) DisplayName() string { |
| 51 | return m.ID |
| 52 | } |
| 53 | |