完善文档页文本向量与重排序调用支持

This commit is contained in:
2026-05-31 21:18:41 +08:00
parent 8ee7a7969e
commit 644a6f9d17
24 changed files with 1945 additions and 71 deletions
+32 -12
View File
@@ -159,16 +159,14 @@ func hasRules(policy map[string]any) bool {
}
func estimateRequestTokens(body map[string]any) int {
text := ""
if prompt := stringFromMap(body, "prompt"); prompt != "" {
text += prompt
}
if input := stringFromMap(body, "input"); input != "" {
text += input
}
var text strings.Builder
appendTokenEstimateText(&text, body["prompt"])
appendTokenEstimateText(&text, body["input"])
appendTokenEstimateText(&text, body["query"])
appendTokenEstimateText(&text, body["documents"])
for _, item := range contentItems(body["content"]) {
if stringFromAny(item["type"]) == "text" {
text += stringFromAny(item["text"])
appendTokenEstimateText(&text, item["text"])
}
}
if messages, ok := body["messages"].([]any); ok {
@@ -176,19 +174,41 @@ func estimateRequestTokens(body map[string]any) int {
message, _ := raw.(map[string]any)
switch content := message["content"].(type) {
case string:
text += content
appendTokenEstimateText(&text, content)
case []any:
for _, rawPart := range content {
part, _ := rawPart.(map[string]any)
text += stringFromMap(part, "text")
appendTokenEstimateText(&text, part["text"])
}
}
}
}
if text == "" {
estimatedText := text.String()
if estimatedText == "" {
return 1
}
return len([]rune(text))/4 + 1
return len([]rune(estimatedText))/4 + 1
}
func appendTokenEstimateText(out *strings.Builder, value any) {
switch typed := value.(type) {
case string:
out.WriteString(typed)
case []string:
for _, item := range typed {
out.WriteString(item)
}
case []any:
for _, item := range typed {
appendTokenEstimateText(out, item)
}
case map[string]any:
for _, key := range []string{"text", "content", "query", "document"} {
if text := stringFromAny(typed[key]); text != "" {
out.WriteString(text)
}
}
}
}
func tokenUsageAmounts(usage clients.Usage) map[string]float64 {