import { Ingestion } from "@polar-sh/ingestion";
import { LLMStrategy } from "@polar-sh/ingestion/strategies/LLM";
import { generateText } from "ai";
import { openai } from "@ai-sdk/openai";
// Setup the LLM Ingestion Strategy
const llmIngestion = Ingestion({ accessToken: process.env.POLAR_ACCESS_TOKEN })
.strategy(new LLMStrategy(openai("gpt-4o")))
.ingest("openai-usage");
export async function POST(req: Request) {
const { prompt }: { prompt: string } = await req.json();
// Get the wrapped LLM model with ingestion capabilities
// Pass Customer Id to properly annotate the ingestion events with a specific customer
const model = llmIngestion.client({
customerId: request.headers.get("X-Polar-Customer-Id") ?? "",
});
const { text } = await generateText({
model,
system: "You are a helpful assistant.",
prompt,
});
return Response.json({ text });
}