Files
TaxHacker_s23/ai/analyze.ts
Dmitrii Anfimov dee915ffd6 feat: more llm provider options (google, mistral) (#28)
* feat: add google provider

* fix: default for google model

* feat: multiple providers

* fix: defaults from env for login form

* fix: add mistral to env files

* chore: delete unused code

* chore: revert database url to original

* fix: render default value for api key from env on server

* fix: type errors during compilation

---------

Co-authored-by: Vasily Zubarev <me@vas3k.ru>
2025-07-22 21:49:54 +02:00

59 lines
1.4 KiB
TypeScript

"use server"
import { ActionState } from "@/lib/actions"
import { AnalyzeAttachment } from "./attachments"
import { updateFile } from "@/models/files"
import { getSettings, getLLMSettings } from "@/models/settings"
import { requestLLM } from "./providers/llmProvider"
export type AnalysisResult = {
output: Record<string, string>
tokensUsed: number
}
export async function analyzeTransaction(
prompt: string,
schema: Record<string, unknown>,
attachments: AnalyzeAttachment[],
fileId: string,
userId: string
): Promise<ActionState<AnalysisResult>> {
const settings = await getSettings(userId)
const llmSettings = getLLMSettings(settings)
try {
const response = await requestLLM(llmSettings, {
prompt,
schema,
attachments,
})
if (response.error) {
throw new Error(response.error)
}
const result = response.output
const tokensUsed = response.tokensUsed || 0
console.log("LLM response:", result)
console.log("LLM tokens used:", tokensUsed)
await updateFile(fileId, userId, { cachedParseResult: result })
return {
success: true,
data: {
output: result,
tokensUsed: tokensUsed
},
}
} catch (error) {
console.error("AI Analysis error:", error)
return {
success: false,
error: error instanceof Error ? error.message : "Failed to analyze invoice",
}
}
}