mirror of
https://github.com/R0m1k3/CollectFlow.git
synced 2026-10-23 06:58:10 +02:00
fix(ai): batch-analyze now reads OpenRouter key from DB config instead of env var
This commit is contained in:
1 parent
1cda9cef9d
commit
d3dfcf6be4
1 file changed
+11
-6
@@ -1,11 +1,10 @@
|
|||||||
import { NextRequest, NextResponse } from "next/server";
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
|
import { getSavedDatabaseConfig } from "@/features/settings/actions";
|
||||||
const OPENROUTER_API_KEY = process.env.OPENROUTER_API_KEY;
|
|
||||||
|
|
||||||
// Schema for the incoming request
|
// Schema for the incoming request
|
||||||
const BatchAnalyzeSchema = z.object({
|
const BatchAnalyzeSchema = z.object({
|
||||||
rayon: z.string(), // Context for the LLM
|
rayon: z.string(),
|
||||||
products: z.array(z.object({
|
products: z.array(z.object({
|
||||||
codein: z.string(),
|
codein: z.string(),
|
||||||
gtin: z.string().nullable().optional(),
|
gtin: z.string().nullable().optional(),
|
||||||
@@ -16,8 +15,14 @@ const BatchAnalyzeSchema = z.object({
|
|||||||
});
|
});
|
||||||
|
|
||||||
export async function POST(req: NextRequest) {
|
export async function POST(req: NextRequest) {
|
||||||
if (!OPENROUTER_API_KEY) {
|
// Read the API key from DB config (same pattern as /api/ai/analyze)
|
||||||
return NextResponse.json({ error: "Clé API OpenRouter manquante." }, { status: 500 });
|
const config = await getSavedDatabaseConfig();
|
||||||
|
const apiKey = process.env.OPENROUTER_API_KEY || config?.openRouterKey;
|
||||||
|
const model = config?.openRouterModel || "meta-llama/llama-3.3-70b-instruct:free";
|
||||||
|
|
||||||
|
if (!apiKey) {
|
||||||
|
console.error("[batch-analyze] API key manquante.");
|
||||||
|
return NextResponse.json({ error: "Clé API OpenRouter manquante. Configurez-la dans les Paramètres." }, { status: 500 });
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -60,7 +65,7 @@ Format attendu:
|
|||||||
const response = await fetch("https://openrouter.ai/api/v1/chat/completions", {
|
const response = await fetch("https://openrouter.ai/api/v1/chat/completions", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
"Authorization": `Bearer ${OPENROUTER_API_KEY}`,
|
"Authorization": `Bearer ${apiKey}`,
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
|
|||||||
Reference in new issue
Block a user