use server side filtering for hugging face

This commit is contained in:
Cheikh Seck committed 2026-03-29 16:28:08 +00:00
1 parent 5c4ea9b91d
commit afeac51c15
3 files changed
+44 -36

No files matched your search

+5 -18
View File
@@ -30,16 +30,19 @@ type HuggingFaceModel struct {
type HuggingFaceResponse []HuggingFaceModel
// listHuggingFaceModels queries HuggingFace for available GGUF-compatible models.
func listHuggingFaceModels(ctx context.Context, p Provider) ([]Model, error) {
func listHuggingFaceModels(ctx context.Context, p Provider, query string) ([]Model, error) {
url := "https://huggingface.co/api/models"
queryParams := map[string]string{
"pipeline_tag": "text-generation",
"filter": "gguf",
"sort": "downloads",
"direction": "-1",
"limit": "500",
"limit": "10",
"full": "false",
}
if query != "" {
queryParams["search"] = query
}
url = addParams(url, queryParams)
data, err := doRequest(ctx, "GET", url, "", nil)
@@ -47,8 +50,6 @@ func listHuggingFaceModels(ctx context.Context, p Provider) ([]Model, error) {
return nil, fmt.Errorf("huggingface: %w", err)
}
//log.Printf("HuggingFace API response: %s", string(data))
var resp HuggingFaceResponse
if err := json.Unmarshal(data, &resp); err != nil {
return nil, fmt.Errorf("huggingface: failed to parse response: %w", err)
@@ -56,10 +57,6 @@ func listHuggingFaceModels(ctx context.Context, p Provider) ([]Model, error) {
models := make([]Model, 0, len(resp))
for _, m := range resp {
if !isGGUFCapable(m) {
continue
}
displayName := m.ID
if idx := strings.LastIndex(displayName, "/"); idx >= 0 {
displayName = displayName[idx+1:]
@@ -80,13 +77,3 @@ func listHuggingFaceModels(ctx context.Context, p Provider) ([]Model, error) {
return models, nil
}
// isGGUFCapable returns true if the model has GGUF files.
func isGGUFCapable(m HuggingFaceModel) bool {
for _, sibling := range m.Siblings {
if strings.HasSuffix(strings.ToLower(sibling.Rfilename), ".gguf") {
return true
}
}
return false
}
+38 -17
View File
@@ -49,47 +49,68 @@ type Provider struct {
// fmt.Println(m.ID, m.Name)
// }
func ListModels(ctx context.Context, p Provider) ([]Model, error) {
return ListModelsWithQuery(ctx, p, "")
}
func ListModelsWithQuery(ctx context.Context, p Provider, query string) ([]Model, error) {
switch p.Type {
case ProviderOllama:
return listOllamaModels(ctx, p)
case ProviderOpenRouter:
return listOpenRouterModels(ctx, p)
return listOpenRouterModels(ctx, p, query)
case ProviderGemini:
return listGeminiModels(ctx, p)
case ProviderHuggingFace:
return listHuggingFaceModels(ctx, p)
return listHuggingFaceModels(ctx, p, query)
default:
return nil, fmt.Errorf("unsupported provider type: %s", p.Type)
}
}
// SearchModels queries the provider and filters models matching the query.
// It searches in both model ID and name fields.
//
// Example usage:
//
// models, err := modelquery.SearchModels(ctx, provider, "claude")
// if err != nil {
// log.Fatal(err)
// }
func SearchModels(ctx context.Context, p Provider, query string) ([]Model, error) {
models, err := ListModels(ctx, p)
if err != nil {
return nil, err
}
return SearchModelsWithQuery(ctx, p, query)
}
func SearchModelsWithQuery(ctx context.Context, p Provider, query string) ([]Model, error) {
query = normalizeQuery(query)
if query == "" {
return models, nil
return ListModelsWithQuery(ctx, p, "")
}
switch p.Type {
case ProviderOllama:
models, err := listOllamaModels(ctx, p)
if err != nil {
return nil, err
}
return filterModels(models, query), nil
case ProviderOpenRouter:
models, err := listOpenRouterModels(ctx, p, "")
if err != nil {
return nil, err
}
return filterModels(models, query), nil
case ProviderGemini:
models, err := listGeminiModels(ctx, p)
if err != nil {
return nil, err
}
return filterModels(models, query), nil
case ProviderHuggingFace:
return listHuggingFaceModels(ctx, p, query)
default:
return nil, fmt.Errorf("unsupported provider type: %s", p.Type)
}
}
func filterModels(models []Model, query string) []Model {
var result []Model
for _, m := range models {
if contains(m.ID, query) || contains(m.Name, query) || contains(m.Description, query) {
result = append(result, m)
}
}
return result, nil
return result
}
// normalizeQuery lowercases and trims the query string.
+1 -1
View File
@@ -55,7 +55,7 @@ type OpenRouterTopProvider struct {
}
// listOpenRouterModels queries OpenRouter for available models.
func listOpenRouterModels(ctx context.Context, p Provider) ([]Model, error) {
func listOpenRouterModels(ctx context.Context, p Provider, query string) ([]Model, error) {
url := "https://openrouter.ai/api/v1/models"
data, err := doRequest(ctx, "GET", url, "", nil)