feat: zentrale selfhost-dienste bereitstellen
This commit is contained in:
@@ -7,6 +7,7 @@ import { FastifyInstance } from "fastify";
|
||||
import { storeExtractedTextForFile } from "./documentText";
|
||||
import { loadFileBuffer } from "./fileBuffer";
|
||||
import { secrets } from "./secrets";
|
||||
import { centralServicesClient } from "../modules/push-server.client";
|
||||
|
||||
// Drizzle schema
|
||||
import { vendors, accounts, tenants } from "../../db/schema";
|
||||
@@ -21,6 +22,7 @@ const nullableNumber = z.number().nullable();
|
||||
// INITIALIZE OPENAI
|
||||
// ---------------------------------------------------------
|
||||
export const initOpenAi = async () => {
|
||||
if (secrets.FEDEO_CENTRAL_SERVICES_ENABLED && centralServicesClient.configured()) return;
|
||||
openai = new OpenAI({
|
||||
apiKey: secrets.OPENAI_API_KEY,
|
||||
});
|
||||
@@ -84,8 +86,9 @@ export const getInvoiceDataFromGPT = async function (
|
||||
suppliedFileData?: Buffer,
|
||||
) {
|
||||
await initOpenAi();
|
||||
const useCentralAi = Boolean(secrets.FEDEO_CENTRAL_SERVICES_ENABLED && centralServicesClient.configured());
|
||||
|
||||
if (!openai) {
|
||||
if (!useCentralAi && !openai) {
|
||||
throw new Error("OpenAI not initialized. Call initOpenAi() first.");
|
||||
}
|
||||
|
||||
@@ -164,7 +167,7 @@ export const getInvoiceDataFromGPT = async function (
|
||||
|
||||
|
||||
|
||||
const completion = await openai.chat.completions.parse({
|
||||
const completionRequest: any = {
|
||||
model: "gpt-4o",
|
||||
store: true,
|
||||
response_format: zodResponseFormat(InstructionFormat as any, "instruction"),
|
||||
@@ -190,9 +193,13 @@ export const getInvoiceDataFromGPT = async function (
|
||||
"Keep invoice items in original order.\n",
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const parsed = completion.choices[0].message.parsed;
|
||||
};
|
||||
const completion = useCentralAi
|
||||
? await centralServicesClient.aiChatCompletions(completionRequest)
|
||||
: await openai!.chat.completions.parse(completionRequest);
|
||||
const parsed = useCentralAi
|
||||
? InstructionFormat.parse(JSON.parse(completion.choices[0]?.message?.content || "{}"))
|
||||
: completion.choices[0].message.parsed;
|
||||
|
||||
console.log(`🧾 Extracted invoice data for file ${file.id}`);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user