Files
llm-intelligence/scripts/siliconflow_pricing_lib.go

62 lines
1.9 KiB
Go
Raw Normal View History

//go:build llm_script
package main
import (
"fmt"
"regexp"
"strings"
)
const defaultSiliconFlowPricingURL = "https://siliconflow.cn/pricing"
var siliconFlowCardPattern = regexp.MustCompile(`(?s)([A-Za-z0-9._/-]+)\n输入 \(元 / M tokens\)\n输出 \(元 / M tokens\)\n(免费|[\d.]+)\n(免费|[\d.]+)`)
func parseSiliconFlowPricingCatalog(raw string) ([]officialPricingRecord, error) {
matches := siliconFlowCardPattern.FindAllStringSubmatch(raw, -1)
if len(matches) == 0 {
return nil, fmt.Errorf("unexpected siliconflow pricing content")
}
records := make([]officialPricingRecord, 0, len(matches))
for _, match := range matches {
modelName := strings.TrimSpace(match[1])
providerName := providerFromModelPath(modelName)
providerNameCn, providerCountry, providerWebsite := providerMetadata(providerName)
inputPrice := parseSiliconFlowPrice(match[2])
outputPrice := parseSiliconFlowPrice(match[3])
record := officialPricingRecord{
ModelID: normalizeExternalID("siliconflow", modelName),
ModelName: modelName,
ProviderName: providerName,
ProviderNameCn: providerNameCn,
ProviderCountry: providerCountry,
ProviderWebsite: providerWebsite,
OperatorName: "SiliconCloud",
OperatorNameCn: "SiliconCloud",
OperatorCountry: "CN",
OperatorWebsite: "https://siliconflow.cn",
OperatorType: "relay",
Region: "CN",
Currency: "CNY",
InputPrice: inputPrice,
OutputPrice: outputPrice,
SourceURL: defaultSiliconFlowPricingURL,
ModelSourceURL: defaultSiliconFlowPricingURL,
DateConfidence: "unknown",
DateSourceKind: "official_product_page",
Modality: detectModality(modelName),
}
record.IsFree = record.InputPrice == 0 && record.OutputPrice == 0
records = append(records, record)
}
return records, nil
}
func parseSiliconFlowPrice(raw string) float64 {
if strings.TrimSpace(raw) == "免费" {
return 0
}
return mustParseSubscriptionPrice(raw)
}