(ep Endpoint, endpoint string, getenv func(string) string)
| 130 | } |
| 131 | |
| 132 | func fetchModelsURL(ep Endpoint, endpoint string, getenv func(string) string) ([]string, error) { |
| 133 | ctx, cancel := context.WithTimeout(context.Background(), modelDiscoveryTimeout) |
| 134 | defer cancel() |
| 135 | |
| 136 | request, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil) |
| 137 | if err != nil { |
| 138 | return nil, err |
| 139 | } |
| 140 | apiKey := ResolveAPIKey(ep, getenv) |
| 141 | if apiKey != "" { |
| 142 | request.Header.Set("Authorization", "Bearer "+apiKey) |
| 143 | request.Header.Set("x-litellm-api-key", apiKey) |
| 144 | } |
| 145 | request.Header.Set("Content-Type", "application/json") |
| 146 | request.Header.Set("accept", "application/json") |
| 147 | |
| 148 | client := &http.Client{ |
| 149 | Timeout: modelDiscoveryTimeout, |
| 150 | Transport: discoveryTransport(ep), |
| 151 | } |
| 152 | response, err := client.Do(request) |
| 153 | if err != nil { |
| 154 | return nil, err |
| 155 | } |
| 156 | defer response.Body.Close() |
| 157 | |
| 158 | if response.StatusCode < http.StatusOK || response.StatusCode >= http.StatusMultipleChoices { |
| 159 | return nil, fmt.Errorf("providers: fetch models from %s returned %s", request.URL, response.Status) |
| 160 | } |
| 161 | |
| 162 | var payload modelsResponse |
| 163 | if err := json.NewDecoder(response.Body).Decode(&payload); err != nil { |
| 164 | return nil, err |
| 165 | } |
| 166 | models := make([]string, 0, len(payload.Data)) |
| 167 | for _, model := range payload.Data { |
| 168 | id := strings.TrimSpace(model.ID) |
| 169 | if id != "" { |
| 170 | models = append(models, id) |
| 171 | } |
| 172 | } |
| 173 | return models, nil |
| 174 | } |
| 175 | |
| 176 | func modelDiscoveryURLs(endpoint string) []string { |
| 177 | primary := modelsURL(endpoint) |
no test coverage detected