feat(provider): add NEAR AI Cloud provider
This commit is contained in:
1 parent
413d37494b
commit
7de3d1f7e3
10 files changed
+213
-2
No files matched your search
@@ -734,9 +734,10 @@ type upstreamModel struct {
|
||||
|
||||
func fetchUpstreamModels(ctx context.Context, provider, apiBase, apiKey string) ([]upstreamModel, error) {
|
||||
apiBase = strings.TrimRight(strings.TrimSpace(apiBase), "/")
|
||||
provider = providers.NormalizeProvider(provider)
|
||||
|
||||
var fetchURL string
|
||||
switch strings.ToLower(provider) {
|
||||
switch provider {
|
||||
case "ollama":
|
||||
// Strip /v1 suffix if present to get the Ollama root
|
||||
root := apiBase
|
||||
@@ -746,6 +747,9 @@ func fetchUpstreamModels(ctx context.Context, provider, apiBase, apiKey string)
|
||||
root = strings.TrimRight(root, "/")
|
||||
fetchURL = root + "/api/tags"
|
||||
return fetchOllamaModels(ctx, fetchURL)
|
||||
case "nearai":
|
||||
fetchURL = apiBase + "/model/list"
|
||||
return fetchNearAIModels(ctx, fetchURL, apiKey)
|
||||
default:
|
||||
// OpenAI-compatible: /v1/models
|
||||
fetchURL = apiBase + "/models"
|
||||
@@ -753,6 +757,53 @@ func fetchUpstreamModels(ctx context.Context, provider, apiBase, apiKey string)
|
||||
}
|
||||
}
|
||||
|
||||
func fetchNearAIModels(ctx context.Context, fetchURL, apiKey string) ([]upstreamModel, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, fetchURL, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if apiKey = strings.TrimSpace(apiKey); apiKey != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+apiKey)
|
||||
}
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("nearai returned status %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
var parsed struct {
|
||||
Models []struct {
|
||||
ModelID string `json:"modelId"`
|
||||
OwnedBy string `json:"ownedBy"`
|
||||
Metadata struct {
|
||||
OwnedBy string `json:"ownedBy"`
|
||||
} `json:"metadata"`
|
||||
} `json:"models"`
|
||||
}
|
||||
if err := json.NewDecoder(resp.Body).Decode(&parsed); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
models := make([]upstreamModel, 0, len(parsed.Models))
|
||||
for _, m := range parsed.Models {
|
||||
id := strings.TrimSpace(m.ModelID)
|
||||
if id == "" {
|
||||
continue
|
||||
}
|
||||
ownedBy := strings.TrimSpace(m.OwnedBy)
|
||||
if ownedBy == "" {
|
||||
ownedBy = strings.TrimSpace(m.Metadata.OwnedBy)
|
||||
}
|
||||
models = append(models, upstreamModel{ID: id, OwnedBy: ownedBy})
|
||||
}
|
||||
return models, nil
|
||||
}
|
||||
|
||||
func fetchOpenAICompatibleModels(ctx context.Context, fetchURL, apiKey string) ([]upstreamModel, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, fetchURL, nil)
|
||||
if err != nil {
|
||||
|
||||
Reference in new issue
Block a user