MCPcopy Create free account
hub / github.com/BlueSkyXN/AI2API / intelligentContentExtraction

Function intelligentContentExtraction

wo2api.go:1190–1276  ·  view source on GitHub ↗

根据API响应的特征智能提取内容

(body []byte, reqID string)

Source from the content-addressed store, hash-verified

1188
1189// 根据API响应的特征智能提取内容
1190func intelligentContentExtraction(body []byte, reqID string) string {
1191 bodyStr := string(body)
1192 lines := strings.Split(bodyStr, "\n")
1193
1194 // 方法1: 使用找到最常见路径的方法
1195 content := findMostLikelyContent(lines, reqID)
1196 if content != "" && len(content) > 10 {
1197 logInfo("[reqID:%s] 使用最可能内容路径方法提取到内容,长度=%d", reqID, len(content))
1198 return content
1199 }
1200
1201 // 方法2: 使用最长响应行策略
1202 var longestResponse string
1203 var maxLength int = 0
1204
1205 for _, line := range lines {
1206 line = strings.TrimSpace(line)
1207 if !strings.HasPrefix(line, "data:") {
1208 continue
1209 }
1210
1211 jsonStr := strings.TrimPrefix(line, "data:")
1212 jsonStr = strings.TrimSpace(jsonStr)
1213
1214 if jsonStr == "[DONE]" || jsonStr == "" {
1215 continue
1216 }
1217
1218 var obj map[string]interface{}
1219 if err := json.Unmarshal([]byte(jsonStr), &obj); err != nil {
1220 continue
1221 }
1222
1223 // 提取response字段
1224 if resp, ok := obj["response"]; ok {
1225 if respStr, ok := resp.(string); ok {
1226 if len(respStr) > maxLength {
1227 longestResponse = respStr
1228 maxLength = len(respStr)
1229 }
1230 }
1231 }
1232 }
1233
1234 if longestResponse != "" {
1235 logInfo("[reqID:%s] 使用最长响应策略提取到内容,长度=%d", reqID, len(longestResponse))
1236 return longestResponse
1237 }
1238
1239 // 方法3: 使用正则表达式
1240 patterns := []string{
1241 `"response"\s*:\s*"((?:.|\n)*?)"`,
1242 `"response":"([^"]*)"`,
1243 `response":"([^"]+)"`,
1244 }
1245
1246 for _, pattern := range patterns {
1247 re := regexp.MustCompile(pattern)

Callers 3

Calls 2

findMostLikelyContentFunction · 0.85
logInfoFunction · 0.70

Tested by

no test coverage detected