42 lines
1.3 KiB
TypeScript
42 lines
1.3 KiB
TypeScript
//import { getRuntimeEnv } from "@/lib/ipconfig"
|
|
|
|
export async function generateBgeM3Embedding(text: string): Promise<number[] | null> {
|
|
try {
|
|
// 取 Supabase URL 或本地默认
|
|
const supaUrl = getRuntimeEnv("SUPABASE_URL") ?? "http://localhost:8000"
|
|
// 构造 Embedding 服务地址:同 host + 8001 端口
|
|
const urlObj = new URL(supaUrl)
|
|
urlObj.port = "8001" // 强制改成 8001
|
|
const apiUrl = `${urlObj.origin}/v1/embeddings`
|
|
console.debug("......[generateBgeM3Embedding] apiUrl =", apiUrl)
|
|
|
|
const response = await fetch(apiUrl, {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({
|
|
// OpenAI 兼容请求字段
|
|
input: text,
|
|
model: "text-embedding-bge-m3"
|
|
})
|
|
});
|
|
|
|
if (!response.ok) {
|
|
throw new Error(`Failed to fetch BGE-M3 embedding: ${response.status}`);
|
|
}
|
|
|
|
// 返回结构为 { object, data: [{ embedding, … }], model, usage }
|
|
const result = await response.json();
|
|
|
|
// 取 data[0].embedding
|
|
if (Array.isArray(result.data) && result.data.length > 0) {
|
|
return result.data[0].embedding as number[];
|
|
} else {
|
|
console.error("Unexpected embedding response format:", result);
|
|
return null;
|
|
}
|
|
} catch (err) {
|
|
console.error("Error in generateBgeM3Embedding:", err);
|
|
return null;
|
|
}
|
|
}
|