convertModelsDevToRatioData parses models.dev /api.json and converts provider pricing metadata into local ratio format. models.dev costs are USD per 1M tokens: model_ratio = input_cost_per_1M / 2 completion_ratio = output_cost / input_cost cache_ratio = cache_read_cost / input_cost Duplicate mo
(reader io.Reader)
| 904 | // cheapest non-zero input cost. If only zero-priced candidates exist, |
| 905 | // a zero ratio is kept. |
| 906 | func convertModelsDevToRatioData(reader io.Reader) (map[string]any, error) { |
| 907 | var upstreamData map[string]modelsDevProvider |
| 908 | if err := common.DecodeJson(reader, &upstreamData); err != nil { |
| 909 | return nil, fmt.Errorf("failed to decode models.dev response: %w", err) |
| 910 | } |
| 911 | if len(upstreamData) == 0 { |
| 912 | return nil, fmt.Errorf("empty models.dev response") |
| 913 | } |
| 914 | |
| 915 | providers := make([]string, 0, len(upstreamData)) |
| 916 | for provider := range upstreamData { |
| 917 | providers = append(providers, provider) |
| 918 | } |
| 919 | sort.Strings(providers) |
| 920 | |
| 921 | selectedCandidates := make(map[string]modelsDevCandidate) |
| 922 | for _, provider := range providers { |
| 923 | providerData := upstreamData[provider] |
| 924 | if len(providerData.Models) == 0 { |
| 925 | continue |
| 926 | } |
| 927 | |
| 928 | modelNames := make([]string, 0, len(providerData.Models)) |
| 929 | for modelName := range providerData.Models { |
| 930 | modelNames = append(modelNames, modelName) |
| 931 | } |
| 932 | sort.Strings(modelNames) |
| 933 | |
| 934 | for _, modelName := range modelNames { |
| 935 | candidate, ok := buildModelsDevCandidate(provider, providerData.Models[modelName].Cost) |
| 936 | if !ok { |
| 937 | continue |
| 938 | } |
| 939 | current, exists := selectedCandidates[modelName] |
| 940 | if !exists || shouldReplaceModelsDevCandidate(current, candidate) { |
| 941 | selectedCandidates[modelName] = candidate |
| 942 | } |
| 943 | } |
| 944 | } |
| 945 | |
| 946 | if len(selectedCandidates) == 0 { |
| 947 | return nil, fmt.Errorf("no valid models.dev pricing entries found") |
| 948 | } |
| 949 | |
| 950 | modelRatioMap := make(map[string]any) |
| 951 | completionRatioMap := make(map[string]any) |
| 952 | cacheRatioMap := make(map[string]any) |
| 953 | |
| 954 | for modelName, candidate := range selectedCandidates { |
| 955 | if candidate.Input == 0 { |
| 956 | modelRatioMap[modelName] = 0.0 |
| 957 | continue |
| 958 | } |
| 959 | |
| 960 | modelRatio := candidate.Input * float64(ratio_setting.USD) / modelsDevInputCostRatioBase |
| 961 | modelRatioMap[modelName] = roundRatioValue(modelRatio) |
| 962 | |
| 963 | if candidate.Output != nil { |
no test coverage detected