diff --git a/chatdesk-ui/app/api/retrieval/process/docx/route.ts b/chatdesk-ui/app/api/retrieval/process/docx/route.ts index c299ce3..cb328d2 100644 --- a/chatdesk-ui/app/api/retrieval/process/docx/route.ts +++ b/chatdesk-ui/app/api/retrieval/process/docx/route.ts @@ -7,6 +7,7 @@ import { createClient } from "@supabase/supabase-js" import { NextResponse } from "next/server" import OpenAI from "openai" import { getRuntimeEnv } from "@/lib/ipconfig" // 新增引入 +import { generateBgeM3Embedding } from "@/lib/generate-bge-m3-embedding" export async function POST(req: Request) { const json = await req.json() @@ -84,6 +85,38 @@ export async function POST(req: Request) { embeddings = await Promise.all(embeddingPromises) } else if (embeddingsProvider === "bge-m3"){ // 示例:调用你自己的 BGE-M3 API 或本地函数 + const embeddingPromises = chunks.map(async (chunk, index) => { + try { + // return await generateBgeM3Embedding(chunk.content) + + const result = await generateBgeM3Embedding(chunk.content) + + if (!Array.isArray(result)) { + console.error(`......❌ Chunk ${index}: result is not an array`, result) + return null + } + + if (result.length !== 1024) { + console.error(`......❌ Chunk ${index}: incorrect length: ${result.length}`) + return null + } + + if (!result.every(x => typeof x === "number")) { + console.error(`......❌ Chunk ${index}: contains non-numbers`, result) + return null + } + + return result + + } catch (error) { + + console.error(`Error generating BGE-M3 embedding for chunk: ${chunk}`, error) + return null + } + }) + embeddings = await Promise.all(embeddingPromises) + console.log(`......[embedding] 维度: ${embeddings.length}`); + } const file_items = chunks.map((chunk, index) => ({ @@ -98,7 +131,12 @@ export async function POST(req: Request) { local_embedding: embeddingsProvider === "local" ? ((embeddings[index] || null) as any) - : null + : null, + bge_m3_embedding: + embeddingsProvider === "bge-m3" && embeddings[index] && embeddings[index].length === 1024 + // ? (embeddings[index] || null) as any + ? embeddings[index] as any + : null })) await supabaseAdmin.from("file_items").upsert(file_items)