feat: zentrale selfhost-dienste bereitstellen
This commit is contained in:
@@ -5,6 +5,7 @@ import { zodResponseFormat } from "openai/helpers/zod";
|
||||
import { and, desc, eq, gte } from "drizzle-orm";
|
||||
import { FastifyInstance } from "fastify";
|
||||
import { createHash } from "node:crypto";
|
||||
import { centralServicesClient } from "../modules/push-server.client";
|
||||
|
||||
import {
|
||||
bankaccounts,
|
||||
@@ -382,9 +383,10 @@ const detectRecurringHeuristically = (statements: any[]): RecurringCandidate[] =
|
||||
};
|
||||
|
||||
const detectRecurringWithAi = async (server: FastifyInstance, statements: any[]): Promise<RecurringCandidate[]> => {
|
||||
if (!secrets.OPENAI_API_KEY || statements.length < 6) return [];
|
||||
const useCentralAi = Boolean(secrets.FEDEO_CENTRAL_SERVICES_ENABLED && centralServicesClient.configured());
|
||||
if ((!secrets.OPENAI_API_KEY && !useCentralAi) || statements.length < 6) return [];
|
||||
|
||||
const openai = new OpenAI({ apiKey: secrets.OPENAI_API_KEY });
|
||||
const openai = useCentralAi ? null : new OpenAI({ apiKey: secrets.OPENAI_API_KEY });
|
||||
const compactStatements = statements.slice(0, 220).map((statement) => ({
|
||||
date: statement.valueDate || statement.date,
|
||||
amount: roundMoney(Number(statement.amount || 0)),
|
||||
@@ -393,7 +395,7 @@ const detectRecurringWithAi = async (server: FastifyInstance, statements: any[])
|
||||
}));
|
||||
|
||||
try {
|
||||
const completion = await openai.chat.completions.parse({
|
||||
const completionRequest: any = {
|
||||
model: "gpt-4o",
|
||||
store: true,
|
||||
response_format: zodResponseFormat(AiRecurringFormat as any, "liquidity_recurring_transactions"),
|
||||
@@ -416,9 +418,15 @@ const detectRecurringWithAi = async (server: FastifyInstance, statements: any[])
|
||||
}),
|
||||
},
|
||||
],
|
||||
});
|
||||
};
|
||||
const completion = useCentralAi
|
||||
? await centralServicesClient.aiChatCompletions(completionRequest)
|
||||
: await openai!.chat.completions.parse(completionRequest);
|
||||
const parsed = useCentralAi
|
||||
? AiRecurringFormat.parse(JSON.parse(completion.choices[0]?.message?.content || "{}"))
|
||||
: completion.choices[0].message.parsed;
|
||||
|
||||
return (completion.choices[0].message.parsed?.candidates || [])
|
||||
return (parsed?.candidates || [])
|
||||
.filter((candidate) => dayjs(candidate.nextDate).isValid())
|
||||
.filter((candidate) => Number(candidate.amount || 0) < 0)
|
||||
.map((candidate) => ({
|
||||
@@ -832,7 +840,7 @@ export const generateLiquidityForecast = async (
|
||||
},
|
||||
points,
|
||||
ai: {
|
||||
enabled: Boolean(secrets.OPENAI_API_KEY),
|
||||
enabled: Boolean(secrets.OPENAI_API_KEY || (secrets.FEDEO_CENTRAL_SERVICES_ENABLED && centralServicesClient.configured())),
|
||||
candidates: aiRecurring.length,
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user