diff --git a/internal/catalog/live.go b/internal/catalog/live.go index e78308959..07fade20e 100644 --- a/internal/catalog/live.go +++ b/internal/catalog/live.go @@ -908,17 +908,23 @@ func fetchPage(ctx context.Context, url, key string, anthropic bool, headers map HasMore bool `json:"has_more"` LastID string `json:"last_id"` } - if err := json.Unmarshal(b, &v); err != nil { - return nil, false, "", fmt.Errorf("%s: not a model list", url) - } - rows := v.Data // each row as it was sent too, for one that is a Codex catalog entry // (Model.Codex): rows decoded, so these decode alike var raw struct { Data []json.RawMessage `json:"data"` Models []json.RawMessage `json:"models"` } - json.Unmarshal(b, &raw) + if err := json.Unmarshal(b, &v); err != nil { + // Together AI's list is the array alone, no "data" around it + // (together-python's models.list reads the answer as a list) + if json.Unmarshal(b, &v.Data) != nil { + return nil, false, "", fmt.Errorf("%s: not a model list", url) + } + json.Unmarshal(b, &raw.Data) + } else { + json.Unmarshal(b, &raw) + } + rows := v.Data raws := raw.Data if len(rows) == 0 { rows, raws = v.Models, raw.Models @@ -934,7 +940,7 @@ func fetchPage(ctx context.Context, url, key string, anthropic bool, headers map // Anthropic's and OpenAI's /v1/models both page with has_more and // last_id. var out []Model - vercel := VercelGateway(url) + vercel, together := VercelGateway(url), TogetherAPI(url) for i, r := range rows { id := r.ID if id == "" { @@ -994,6 +1000,32 @@ func fetchPage(ctx context.Context, url, key string, anthropic bool, headers map // elevenlabs/scribe-v2) serve their own APIs. A "-batch" id // (anthropic/claude-sonnet-4.6-batch) is for its batch API alone: // "Synchronous requests don't accept -batch IDs" + // Together AI's says it too (together-python's ModelType: chat, + // language, code, image, embedding, moderation, rerank, audio, + // transcribe, video), and its ids often don't: its embedding + // togethercomputer/m2-bert-80M-32k-retrieval, its reranker + // Salesforce/Llama-Rank-V1. Another list's row that says + // "embedding" or "rerank" means the same + switch strings.ToLower(r.Type) { + case "embedding", "embeddings": + drawer, films, serves = false, false, ServesEmbedding + case "rerank", "reranking": + drawer, films, serves = false, false, ServesRerank + } + if together { + switch r.Type { + case "moderation": + continue + case "image": + drawer, films, serves = true, false, "" + case "video": + drawer, films, serves = false, true, "" + case "audio": + serves = ServesSpeech + case "transcribe": + serves = ServesTranscription + } + } if vercel { if strings.HasSuffix(id, "-batch") { continue @@ -1397,6 +1429,13 @@ func (p Price) sane() bool { return true } +// TogetherAPI says u is Together AI's API, whose model list says what +// each model is for (its "type"). +func TogetherAPI(u string) bool { + x, err := url.Parse(strings.TrimSpace(u)) + return err == nil && strings.EqualFold(x.Hostname(), "api.together.xyz") +} + // VercelGateway reports whether u is at Vercel's AI Gateway // (ai-gateway.vercel.sh), whose model list marks each row's type. func VercelGateway(u string) bool { diff --git a/internal/provider/testdata/together-models.json b/internal/provider/testdata/together-models.json new file mode 100644 index 000000000..92a154026 --- /dev/null +++ b/internal/provider/testdata/together-models.json @@ -0,0 +1,10 @@ +[ + {"id":"meta-llama/Llama-3.3-70B-Instruct-Turbo","object":"model","created":1733443923,"type":"chat","running":false,"display_name":"Meta Llama 3.3 70B Instruct Turbo","organization":"Meta","link":"https://huggingface.co/meta-llama/Llama-3.3-70B-Instruct","license":"llama3.3","context_length":131072,"config":{"chat_template":null,"stop":["<|eot_id|>","<|eom_id|>"],"bos_token":null,"eos_token":null},"pricing":{"hourly":0,"input":0.88,"output":0.88,"base":0,"finetune":0}}, + {"id":"togethercomputer/m2-bert-80M-32k-retrieval","object":"model","created":0,"type":"embedding","running":false,"display_name":"M2-BERT-Retrieval-32k","organization":"Together","link":"https://huggingface.co/togethercomputer/m2-bert-80M-32k-retrieval","license":"apache-2.0","context_length":32768,"config":{"chat_template":null,"stop":[],"bos_token":null,"eos_token":null},"pricing":{"hourly":0,"input":0.008,"output":0.008,"base":0,"finetune":0}}, + {"id":"intfloat/multilingual-e5-large-instruct","object":"model","created":0,"type":"embedding","running":false,"display_name":"Multilingual E5 Large Instruct","organization":"Intfloat","link":"https://huggingface.co/intfloat/multilingual-e5-large-instruct","license":"mit","context_length":514,"config":{"chat_template":null,"stop":[],"bos_token":null,"eos_token":null},"pricing":{"hourly":0,"input":0.02,"output":0.02,"base":0,"finetune":0}}, + {"id":"Salesforce/Llama-Rank-V1","object":"model","created":0,"type":"rerank","running":false,"display_name":"Salesforce Llama Rank V1 (8B)","organization":"Salesforce","link":"https://huggingface.co/Salesforce/Llama-Rank-V1","license":"llama3","context_length":8192,"config":{"chat_template":null,"stop":[],"bos_token":null,"eos_token":null},"pricing":{"hourly":0,"input":0.1,"output":0.1,"base":0,"finetune":0}}, + {"id":"black-forest-labs/FLUX.1-schnell","object":"model","created":0,"type":"image","running":false,"display_name":"FLUX.1 Schnell","organization":"Black Forest Labs","link":"https://huggingface.co/black-forest-labs/FLUX.1-schnell","license":"apache-2.0","context_length":0,"config":{"chat_template":null,"stop":[],"bos_token":null,"eos_token":null},"pricing":{"hourly":0,"input":0,"output":0,"base":0,"finetune":0}}, + {"id":"meta-llama/Llama-Guard-4-12B","object":"model","created":0,"type":"moderation","running":false,"display_name":"Llama Guard 4 12B","organization":"Meta","link":"https://huggingface.co/meta-llama/Llama-Guard-4-12B","license":"llama4","context_length":1048576,"config":{"chat_template":null,"stop":[],"bos_token":null,"eos_token":null},"pricing":{"hourly":0,"input":0.2,"output":0.2,"base":0,"finetune":0}}, + {"id":"cartesia/sonic-2","object":"model","created":0,"type":"audio","running":false,"display_name":"Cartesia Sonic 2","organization":"Cartesia","link":"https://cartesia.ai","license":"","context_length":0,"config":{"chat_template":null,"stop":[],"bos_token":null,"eos_token":null},"pricing":{"hourly":0,"input":65,"output":0,"base":0,"finetune":0}}, + {"id":"openai/whisper-large-v3","object":"model","created":0,"type":"transcribe","running":false,"display_name":"Whisper large-v3","organization":"OpenAI","link":"https://huggingface.co/openai/whisper-large-v3","license":"apache-2.0","context_length":0,"config":{"chat_template":null,"stop":[],"bos_token":null,"eos_token":null},"pricing":{"hourly":0,"input":0.0015,"output":0,"base":0,"finetune":0}} +] diff --git a/internal/provider/together_list_test.go b/internal/provider/together_list_test.go new file mode 100644 index 000000000..25f59ed66 --- /dev/null +++ b/internal/provider/together_list_test.go @@ -0,0 +1,68 @@ +package provider + +import ( + "context" + "net/http" + "net/http/httptest" + "os" + "testing" + + "github.com/yetone/magpie/internal/catalog" +) + +// Together AI's /v1/models is a bare JSON array (together-python's +// models.list reads the answer as a list), each row with a "type" that +// says what the model is for. The array is read, and the type puts each +// model where it serves: its embedding and rerank models, whose ids +// (togethercomputer/m2-bert-80M-32k-retrieval, Salesforce/Llama-Rank-V1) +// don't say so, on /v1/embeddings and /v1/rerank (@rfiacne on X), its +// image model with the image models, its voice and transcriber on theirs, +// its moderation model nowhere. Only the chat model is offered to chat. +func TestTogetherLiveList(t *testing.T) { + body, err := os.ReadFile("testdata/together-models.json") + if err != nil { + t.Fatal(err) + } + srv := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/v1/models" || r.Header.Get("Authorization") != "Bearer k" { + rw.WriteHeader(http.StatusUnauthorized) + return + } + rw.Header().Set("Content-Type", "application/json") + rw.Write(body) + })) + defer srv.Close() + old := http.DefaultClient.Transport + t.Cleanup(func() { http.DefaultClient.Transport = old }) + http.DefaultClient.Transport = rewrite{srv} + + ms, _, err := catalog.FetchAt(context.Background(), "https://api.together.xyz/v1", "k", false, nil) + if err != nil { + t.Fatal(err) + } + by := map[string]catalog.Model{} + for _, m := range ms { + by[m.ID] = m + } + chat := catalog.Chat(ms) + if len(chat) != 1 || chat[0].ID != "meta-llama/Llama-3.3-70B-Instruct-Turbo" || chat[0].Context != 131072 { + t.Fatalf("chat: %+v", chat) + } + for id, serves := range map[string]string{ + "togethercomputer/m2-bert-80M-32k-retrieval": catalog.ServesEmbedding, + "intfloat/multilingual-e5-large-instruct": catalog.ServesEmbedding, + "Salesforce/Llama-Rank-V1": catalog.ServesRerank, + "cartesia/sonic-2": catalog.ServesSpeech, + "openai/whisper-large-v3": catalog.ServesTranscription, + } { + if m, ok := by[id]; !ok || m.Serves != serves { + t.Errorf("%s: %+v (want %s)", id, m, serves) + } + } + if m := by["black-forest-labs/FLUX.1-schnell"]; !m.Draws || m.Serves != "" { + t.Errorf("image: %+v", m) + } + if m, ok := by["meta-llama/Llama-Guard-4-12B"]; ok { + t.Errorf("moderation kept: %+v", m) + } +}