Skip to content

Commit

Permalink
feat(api): update image2transaction prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
duongdev committed Aug 8, 2024
1 parent 9af8d78 commit ff9a585
Showing 1 changed file with 38 additions and 2 deletions.
40 changes: 38 additions & 2 deletions apps/api/v1/services/ai.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,43 @@ export async function deleteFile({ fileId }: { fileId: string }) {
return deletedFile
}

function generateAdditionalInstruction({
noteLanguage,
categories,
}: {
noteLanguage: string
categories: {
id: string
name: string
icon?: string | null
type?: CategoryType
}[]
}) {
const template = `<categories>
{{user_categories}}
</categories>
<additional_info>
- Current datetime: {{current_datetime}}
- User's prefer language: {{user_prefer_lang}}
- User's prefer currency: {{user_prefer_currency}}
</additional_info>`

const userCategories = JSON.stringify(categories)

const instruction = template
.replace('{{user_categories}}', userCategories)
.replace('{{current_datetime}}', new Date().toISOString())
.replace('{{user_prefer_lang}}', noteLanguage)
.replace('{{user_prefer_currency}}', '<unknown>')

return instruction
}

export async function generateTransactionDataFromFile({
file: inputFile,
noteLanguage = 'English',
categories,
categories = [],
}: {
file: File
noteLanguage?: string
Expand All @@ -56,7 +89,10 @@ export async function generateTransactionDataFromFile({
}[]
}) {
const log = getLogger(`ai.service:${generateTransactionDataFromFile.name}`)
const additionalInstructions = `note must be in ${noteLanguage} language but do not translate names. Categories: ${JSON.stringify(categories?.map((cat) => ({ id: cat.id, name: cat.name, icon: cat.icon, type: cat.type })) || [])}`
const additionalInstructions = generateAdditionalInstruction({
noteLanguage,
categories,
})

// If cached, return cached response
const fileHash = await hashFile(inputFile)
Expand Down

0 comments on commit ff9a585

Please sign in to comment.