feat: GPT-4.1 optimierte Ergänzungssuche bei Maps-Lücke
Wenn Google Maps weniger Leads findet als angefragt, wird automatisch eine optimierte Suchanfrage via GPT-4.1 generiert und als SERP-Job gestartet, um die Lücke zu füllen. Die KI-Query wird im LoadingCard angezeigt. Fallback auf Original-Query wenn kein OpenAI-Key konfiguriert. - lib/services/openai.ts: GPT-4.1 Query-Generator - app/api/search/supplement: neuer Endpoint (GPT + SERP-Job) - LoadingCard: ruft /api/search/supplement statt direkt SERP - apiKey.ts + .env.local.example: openai Key-Support Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
59
app/api/search/supplement/route.ts
Normal file
59
app/api/search/supplement/route.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { getApiKey } from "@/lib/utils/apiKey";
|
||||
import { generateSupplementQuery } from "@/lib/services/openai";
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
try {
|
||||
const body = await req.json() as {
|
||||
query: string;
|
||||
region: string;
|
||||
targetCount: number;
|
||||
foundCount: number;
|
||||
};
|
||||
const { query, region, targetCount, foundCount } = body;
|
||||
|
||||
const base = req.nextUrl.origin;
|
||||
const deficit = targetCount - foundCount;
|
||||
|
||||
// 1. Try to generate an optimized query via GPT-4.1
|
||||
let optimizedQuery = region ? `${query} ${region}` : query;
|
||||
let usedAI = false;
|
||||
|
||||
const openaiKey = await getApiKey("openai");
|
||||
if (openaiKey) {
|
||||
const aiQuery = await generateSupplementQuery(query, region, foundCount, targetCount, openaiKey);
|
||||
if (aiQuery) {
|
||||
optimizedQuery = aiQuery;
|
||||
usedAI = true;
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Start SERP job with the (possibly optimized) query
|
||||
const maxPages = Math.min(Math.max(1, Math.ceil(deficit / 10)), 3);
|
||||
|
||||
const serpRes = await fetch(`${base}/api/jobs/serp-enrich`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
query: optimizedQuery,
|
||||
maxPages,
|
||||
countryCode: "de",
|
||||
languageCode: "de",
|
||||
filterSocial: true,
|
||||
categories: ["ceo"],
|
||||
enrichEmails: true,
|
||||
}),
|
||||
});
|
||||
|
||||
if (!serpRes.ok) {
|
||||
const err = await serpRes.json() as { error?: string };
|
||||
return NextResponse.json({ error: err.error || "SERP job konnte nicht gestartet werden" }, { status: 500 });
|
||||
}
|
||||
|
||||
const { jobId } = await serpRes.json() as { jobId: string };
|
||||
return NextResponse.json({ jobId, optimizedQuery, usedAI });
|
||||
} catch (err) {
|
||||
console.error("POST /api/search/supplement error:", err);
|
||||
return NextResponse.json({ error: "Supplement-Suche fehlgeschlagen" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user