1
0
mirror of https://github.com/coalaura/whiskr.git synced 2025-09-09 17:29:54 +00:00
Files
whiskr/models.go

94 lines
1.6 KiB
Go
Raw Normal View History

2025-08-05 03:56:23 +02:00
package main
2025-08-08 14:49:14 +02:00
import (
"context"
2025-08-09 22:20:18 +02:00
"sort"
2025-08-08 14:49:14 +02:00
"strings"
2025-08-10 16:38:02 +02:00
"github.com/revrost/go-openrouter"
2025-08-08 14:49:14 +02:00
)
2025-08-05 03:56:23 +02:00
type Model struct {
2025-08-10 16:38:02 +02:00
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Tags []string `json:"tags,omitempty"`
2025-08-10 22:32:40 +02:00
Reasoning bool `json:"-"`
2025-08-15 03:38:24 +02:00
Vision bool `json:"-"`
2025-08-11 00:15:58 +02:00
JSON bool `json:"-"`
2025-08-14 03:53:14 +02:00
Tools bool `json:"-"`
2025-08-05 03:56:23 +02:00
}
var ModelMap = make(map[string]*Model)
func LoadModels() ([]*Model, error) {
2025-08-18 04:46:17 +02:00
log.Info("Loading models...")
2025-08-05 03:56:23 +02:00
client := OpenRouterClient()
list, err := client.ListUserModels(context.Background())
if err != nil {
return nil, err
}
2025-08-09 22:20:18 +02:00
sort.Slice(list, func(i, j int) bool {
return list[i].Created > list[j].Created
})
2025-08-05 03:56:23 +02:00
models := make([]*Model, len(list))
for index, model := range list {
2025-08-08 14:49:14 +02:00
name := model.Name
if index := strings.Index(name, ": "); index != -1 {
name = name[index+2:]
}
2025-08-05 03:56:23 +02:00
m := &Model{
2025-08-10 16:38:02 +02:00
ID: model.ID,
Name: name,
Description: model.Description,
2025-08-05 03:56:23 +02:00
}
2025-08-14 03:53:14 +02:00
GetModelTags(model, m)
2025-08-05 03:56:23 +02:00
models[index] = m
ModelMap[model.ID] = m
}
2025-08-18 04:46:17 +02:00
log.Infof("Loaded %d models\n", len(models))
2025-08-05 03:56:23 +02:00
return models, nil
}
2025-08-10 16:38:02 +02:00
2025-08-14 03:53:14 +02:00
func GetModelTags(model openrouter.Model, m *Model) {
2025-08-10 16:38:02 +02:00
for _, parameter := range model.SupportedParameters {
2025-08-11 00:15:58 +02:00
switch parameter {
case "reasoning":
2025-08-14 03:53:14 +02:00
m.Reasoning = true
2025-08-10 22:32:40 +02:00
2025-08-14 03:53:14 +02:00
m.Tags = append(m.Tags, "reasoning")
2025-08-11 00:15:58 +02:00
case "response_format":
2025-08-14 03:53:14 +02:00
m.JSON = true
2025-08-11 00:15:58 +02:00
2025-08-14 03:53:14 +02:00
m.Tags = append(m.Tags, "json")
2025-08-11 00:15:58 +02:00
case "tools":
2025-08-14 03:53:14 +02:00
m.Tools = true
m.Tags = append(m.Tags, "tools")
2025-08-10 16:38:02 +02:00
}
}
2025-08-10 16:44:00 +02:00
for _, modality := range model.Architecture.InputModalities {
if modality == "image" {
2025-08-15 03:38:24 +02:00
m.Vision = true
2025-08-14 03:53:14 +02:00
m.Tags = append(m.Tags, "vision")
2025-08-10 16:44:00 +02:00
}
}
2025-08-14 03:53:14 +02:00
sort.Strings(m.Tags)
2025-08-10 16:38:02 +02:00
}