mirror of
https://github.com/2930134478/AI-CS.git
synced 2026-06-15 00:44:30 +08:00
新增向量日志打印
This commit is contained in:
@@ -55,6 +55,17 @@ func (s *BGEEmbeddingService) EmbedTexts(ctx context.Context, texts []string) ([
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// 诊断日志:确认发请求前我们到底发了几条文本
|
||||
log.Printf("[嵌入] BGE EmbedTexts 请求: len(texts)=%d, model=%s, apiURL=%s", len(texts), s.model, strings.TrimSuffix(s.apiURL, "/"))
|
||||
for i, t := range texts {
|
||||
runeLen := len([]rune(t))
|
||||
preview := t
|
||||
if runeLen > 60 {
|
||||
preview = string([]rune(t)[:60]) + "..."
|
||||
}
|
||||
log.Printf("[嵌入] texts[%d] 长度=%d 字符, 预览: %q", i, runeLen, preview)
|
||||
}
|
||||
|
||||
// 支持填完整路径或仅填 base:若已以 /embeddings 结尾则不再追加,否则追加 /embeddings
|
||||
url := strings.TrimSuffix(s.apiURL, "/")
|
||||
if url != "" && !strings.HasSuffix(strings.ToLower(url), "/embeddings") {
|
||||
@@ -116,6 +127,14 @@ func (s *BGEEmbeddingService) EmbedTexts(ctx context.Context, texts []string) ([
|
||||
return nil, fmt.Errorf("解析响应失败: %w", err)
|
||||
}
|
||||
|
||||
// 诊断日志:API 实际返回了几个向量
|
||||
numIn := len(texts)
|
||||
numOut := len(response)
|
||||
log.Printf("[嵌入] BGE EmbedTexts 响应: len(texts)=%d -> len(response)=%d (API 返回向量数)", numIn, numOut)
|
||||
if numOut != numIn {
|
||||
log.Printf("[嵌入] 数量不一致: 我们发了 %d 条文本,API 返回了 %d 个向量", numIn, numOut)
|
||||
}
|
||||
|
||||
// 转换为 float32
|
||||
result := make([][]float32, len(response))
|
||||
for i, item := range response {
|
||||
|
||||
@@ -60,6 +60,17 @@ func (s *OpenAIEmbeddingService) EmbedTexts(ctx context.Context, texts []string)
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// 诊断日志:确认发请求前我们到底发了几条文本(用于排查 1 文档 vs 6 向量 问题)
|
||||
log.Printf("[嵌入] EmbedTexts 请求: len(texts)=%d, model=%s, apiURL=%s", len(texts), s.model, strings.TrimSuffix(s.apiURL, "/"))
|
||||
for i, t := range texts {
|
||||
runeLen := len([]rune(t))
|
||||
preview := t
|
||||
if runeLen > 60 {
|
||||
preview = string([]rune(t)[:60]) + "..."
|
||||
}
|
||||
log.Printf("[嵌入] texts[%d] 长度=%d 字符, 预览: %q", i, runeLen, preview)
|
||||
}
|
||||
|
||||
// 支持填完整路径或仅填 base:若已以 /embeddings 结尾则不再追加,否则追加 /embeddings
|
||||
url := strings.TrimSuffix(s.apiURL, "/")
|
||||
if url != "" && !strings.HasSuffix(strings.ToLower(url), "/embeddings") {
|
||||
@@ -125,6 +136,14 @@ func (s *OpenAIEmbeddingService) EmbedTexts(ctx context.Context, texts []string)
|
||||
return nil, fmt.Errorf("解析响应失败: %w", err)
|
||||
}
|
||||
|
||||
// 诊断日志:API 实际返回了几个向量(若与 len(texts) 不一致,说明接口按长文本分块或我们发了多条)
|
||||
numIn := len(texts)
|
||||
numOut := len(response.Data)
|
||||
log.Printf("[嵌入] EmbedTexts 响应: len(texts)=%d -> len(data)=%d (API 返回向量数)", numIn, numOut)
|
||||
if numOut != numIn {
|
||||
log.Printf("[嵌入] 数量不一致: 我们发了 %d 条文本,API 返回了 %d 个向量(可能接口对长文本分块,或请求被中间层改写)", numIn, numOut)
|
||||
}
|
||||
|
||||
// 转换为 float32
|
||||
result := make([][]float32, len(response.Data))
|
||||
for i, item := range response.Data {
|
||||
|
||||
@@ -3,6 +3,7 @@ package rag
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/2930134478/AI-CS/backend/service/embedding"
|
||||
)
|
||||
@@ -55,12 +56,16 @@ func (s *DocumentEmbeddingService) EmbedDocuments(ctx context.Context, documentI
|
||||
if err != nil {
|
||||
return fmt.Errorf("获取嵌入服务失败: %w", err)
|
||||
}
|
||||
// 诊断日志:批量向量化前,我们传给 EmbedTexts 的文档/内容条数
|
||||
log.Printf("[嵌入] EmbedDocuments 调用前: len(documentIDs)=%d, len(contents)=%d(若 contents 已是多条,说明上游在发请求前做了分块)", len(documentIDs), len(contents))
|
||||
// 批量向量化
|
||||
vectors, err := svc.EmbedTexts(ctx, contents)
|
||||
if err != nil {
|
||||
return fmt.Errorf("批量向量化失败: %w", err)
|
||||
}
|
||||
log.Printf("[嵌入] EmbedDocuments 调用后: len(vectors)=%d, len(contents)=%d", len(vectors), len(contents))
|
||||
if len(vectors) != len(contents) {
|
||||
log.Printf("[嵌入] 向量数与内容数不一致,将报错: 我们按 %d 行写入 Milvus 会与 embedding 列 %d 行冲突", len(contents), len(vectors))
|
||||
return fmt.Errorf("向量数量不匹配")
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user