1
0
mirror of https://github.com/coalaura/whiskr.git synced 2025-12-02 20:22:52 +00:00
Files
whiskr/models.go

176 lines
3.1 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-09-30 22:25:01 +02:00
"strconv"
2025-08-08 14:49:14 +02:00
"strings"
"sync"
"time"
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
2025-09-30 22:25:01 +02:00
type ModelPricing struct {
Input float64 `json:"input"`
Output float64 `json:"output"`
2025-09-30 22:40:03 +02:00
Image float64 `json:"image,omitzero"`
2025-09-30 22:25:01 +02:00
}
2025-08-05 03:56:23 +02:00
type Model struct {
2025-09-30 22:25:01 +02:00
ID string `json:"id"`
Created int64 `json:"created"`
Name string `json:"name"`
Description string `json:"description"`
Pricing ModelPricing `json:"pricing"`
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-09-11 23:25:58 +02:00
Images bool `json:"-"`
2025-08-05 03:56:23 +02:00
}
var (
modelMx sync.RWMutex
2025-08-05 03:56:23 +02:00
ModelMap map[string]*Model
ModelList []*Model
)
func GetModel(name string) *Model {
modelMx.RLock()
defer modelMx.RUnlock()
return ModelMap[name]
}
func StartModelUpdateLoop() error {
err := LoadModels(true)
if err != nil {
return err
}
go func() {
ticker := time.NewTicker(2 * time.Hour)
for range ticker.C {
err := LoadModels(false)
if err != nil {
log.Warnln(err)
}
}
}()
return nil
}
func LoadModels(initial bool) error {
log.Println("Refreshing model list...")
2025-08-18 04:46:17 +02:00
2025-08-05 03:56:23 +02:00
client := OpenRouterClient()
list, err := client.ListUserModels(context.Background())
if err != nil {
return err
}
if !initial && len(list) == len(ModelList) {
log.Println("No new models, skipping update")
return nil
2025-08-05 03:56:23 +02:00
}
2025-08-09 22:20:18 +02:00
sort.Slice(list, func(i, j int) bool {
return list[i].Created > list[j].Created
})
var (
newList = make([]*Model, len(list))
newMap = make(map[string]*Model, len(list))
)
2025-08-05 03:56:23 +02:00
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-09-30 22:25:01 +02:00
input, _ := strconv.ParseFloat(model.Pricing.Prompt, 64)
output, _ := strconv.ParseFloat(model.Pricing.Completion, 64)
2025-09-30 22:40:03 +02:00
image, _ := strconv.ParseFloat(model.Pricing.Image, 64)
2025-09-30 22:25:01 +02:00
2025-08-05 03:56:23 +02:00
m := &Model{
2025-08-10 16:38:02 +02:00
ID: model.ID,
Created: model.Created,
2025-08-10 16:38:02 +02:00
Name: name,
Description: model.Description,
2025-09-30 22:25:01 +02:00
Pricing: ModelPricing{
Input: input * 1000000,
Output: output * 1000000,
2025-09-30 22:40:03 +02:00
Image: image,
2025-09-30 22:25:01 +02:00
},
2025-08-05 03:56:23 +02:00
}
2025-08-14 03:53:14 +02:00
GetModelTags(model, m)
newList[index] = m
newMap[model.ID] = m
2025-08-05 03:56:23 +02:00
}
log.Printf("Loaded %d models\n", len(newList))
modelMx.Lock()
ModelList = newList
ModelMap = newMap
modelMx.Unlock()
2025-08-18 04:46:17 +02:00
return nil
2025-08-05 03:56:23 +02:00
}
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-09-11 23:25:58 +02:00
for _, modality := range model.Architecture.OutputModalities {
if modality == "image" {
m.Images = true
m.Tags = append(m.Tags, "image")
}
}
2025-11-11 01:47:53 +01:00
if model.Pricing.Prompt == "0" && model.Pricing.Completion == "0" {
m.Tags = append(m.Tags, "free")
}
2025-08-14 03:53:14 +02:00
sort.Strings(m.Tags)
2025-08-10 16:38:02 +02:00
}