Automatic category from expense title (#80)

* environment variable

* random category draft

* get category from ai

* input limit and documentation

* use watch

* use field.name

* prettier

* presigned upload, readme warning, category to string util

* prettier

* check whether feature is enabled

* use process.env

* improved prompt to return id only

* remove console.debug

* show loader

* share class name

* prettier

* use template literals

* rename format util

* prettier
This commit is contained in:
Mert Demir
2024-02-05 02:23:11 +09:00
committed by GitHub
parent 10fd69404a
commit fb49fb596a
8 changed files with 136 additions and 15 deletions

View File

@@ -19,6 +19,7 @@ const envSchema = z
S3_UPLOAD_REGION: z.string().optional(),
S3_UPLOAD_ENDPOINT: z.string().optional(),
NEXT_PUBLIC_ENABLE_RECEIPT_EXTRACT: z.coerce.boolean().default(false),
NEXT_PUBLIC_ENABLE_CATEGORY_EXTRACT: z.coerce.boolean().default(false),
OPENAI_API_KEY: z.string().optional(),
})
.superRefine((env, ctx) => {
@@ -36,11 +37,15 @@ const envSchema = z
'If NEXT_PUBLIC_ENABLE_EXPENSE_DOCUMENTS is specified, then S3_* must be specified too',
})
}
if (env.NEXT_PUBLIC_ENABLE_RECEIPT_EXTRACT && !env.OPENAI_API_KEY) {
if (
(env.NEXT_PUBLIC_ENABLE_RECEIPT_EXTRACT ||
env.NEXT_PUBLIC_ENABLE_CATEGORY_EXTRACT) &&
!env.OPENAI_API_KEY
) {
ctx.addIssue({
code: ZodIssueCode.custom,
message:
'If NEXT_PUBLIC_ENABLE_RECEIPT_EXTRACT is specified, then OPENAI_API_KEY must be specified too',
'If NEXT_PUBLIC_ENABLE_RECEIPT_EXTRACT or NEXT_PUBLIC_ENABLE_CATEGORY_EXTRACT is specified, then OPENAI_API_KEY must be specified too',
})
}
})

View File

@@ -1,3 +1,4 @@
import { Category } from '@prisma/client'
import { clsx, type ClassValue } from 'clsx'
import { twMerge } from 'tailwind-merge'
@@ -15,3 +16,7 @@ export function formatExpenseDate(date: Date) {
timeZone: 'UTC',
})
}
export function formatCategoryForAIPrompt(category: Category) {
return `"${category.grouping}/${category.name}" (ID: ${category.id})`
}