This commit is contained in:
parent
36c57c6a41
commit
665ad78b20
|
|
@ -7,6 +7,7 @@ import { createClient } from "@supabase/supabase-js"
|
||||||
import { NextResponse } from "next/server"
|
import { NextResponse } from "next/server"
|
||||||
import OpenAI from "openai"
|
import OpenAI from "openai"
|
||||||
import { getRuntimeEnv } from "@/lib/ipconfig" // 新增引入
|
import { getRuntimeEnv } from "@/lib/ipconfig" // 新增引入
|
||||||
|
import { generateBgeM3Embedding } from "@/lib/generate-bge-m3-embedding"
|
||||||
|
|
||||||
export async function POST(req: Request) {
|
export async function POST(req: Request) {
|
||||||
const json = await req.json()
|
const json = await req.json()
|
||||||
|
|
@ -84,6 +85,38 @@ export async function POST(req: Request) {
|
||||||
embeddings = await Promise.all(embeddingPromises)
|
embeddings = await Promise.all(embeddingPromises)
|
||||||
} else if (embeddingsProvider === "bge-m3"){
|
} else if (embeddingsProvider === "bge-m3"){
|
||||||
// 示例:调用你自己的 BGE-M3 API 或本地函数
|
// 示例:调用你自己的 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) => ({
|
const file_items = chunks.map((chunk, index) => ({
|
||||||
|
|
@ -98,6 +131,11 @@ export async function POST(req: Request) {
|
||||||
local_embedding:
|
local_embedding:
|
||||||
embeddingsProvider === "local"
|
embeddingsProvider === "local"
|
||||||
? ((embeddings[index] || null) as any)
|
? ((embeddings[index] || null) as any)
|
||||||
|
: null,
|
||||||
|
bge_m3_embedding:
|
||||||
|
embeddingsProvider === "bge-m3" && embeddings[index] && embeddings[index].length === 1024
|
||||||
|
// ? (embeddings[index] || null) as any
|
||||||
|
? embeddings[index] as any
|
||||||
: null
|
: null
|
||||||
}))
|
}))
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue