Skip to content

Commit

Permalink
feat(api): improve gpt prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
duongdev committed Jul 16, 2024
1 parent 837979e commit db149c1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 15 deletions.
14 changes: 12 additions & 2 deletions apps/api/v1/routes/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import { getLogger } from '../../lib/log'
import { getAuthUserStrict } from '../middlewares/auth'
import { generateTransactionDataFromFile } from '../services/ai.service'
import { canUserReadBudget, findBudget } from '../services/budget.service'
import { canUserReadCategory, findCategory } from '../services/category.service'
import {
canUserReadCategory,
findCategoriesOfUser,
findCategory,
} from '../services/category.service'
import {
canUserCreateTransaction,
canUserDeleteTransaction,
Expand Down Expand Up @@ -275,15 +279,21 @@ const router = new Hono()
})

.post('/ai', async (c) => {
const user = getAuthUserStrict(c)
const body = await c.req.parseBody()
const file = body.file as File | undefined

if (!file) {
return c.json({ message: 'file not found' }, 400)
}

const userCategories = await findCategoriesOfUser({ user })

try {
const transactionData = await generateTransactionDataFromFile({ file })
const transactionData = await generateTransactionDataFromFile({
file,
categories: userCategories,
})
return c.json(transactionData)
} catch {
return c.json(
Expand Down
32 changes: 24 additions & 8 deletions apps/api/v1/services/ai.service.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { CategoryType } from '@prisma/client'
import { OpenAI } from 'openai'
import { getLogger } from '../../lib/log'

Expand Down Expand Up @@ -40,7 +41,18 @@ export async function deleteFile({ fileId }: { fileId: string }) {

export async function generateTransactionDataFromFile({
file: inputFile,
}: { file: File }) {
noteLanguage = 'English',
categories,
}: {
file: File
noteLanguage?: string
categories?: {
id: string
name: string
icon?: string | null
type?: CategoryType
}[]
}) {
const log = getLogger(`ai.service:${generateTransactionDataFromFile.name}`)

const file = await uploadVisionFile({ file: inputFile })
Expand All @@ -58,12 +70,18 @@ export async function generateTransactionDataFromFile({

log.info('Created thread with uploaded file. Thread ID: %s', thread.id)
log.debug('Created thread with uploaded file. Thread details: %o', thread)
log.debug('Running assistant on thread. Assistant ID: %s', ASSISTANT_ID)

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 })) || [])}`

log.debug(
'Running assistant on thread.\nAssistant ID: %s\nAdditional instructions: %s',
ASSISTANT_ID,
additionalInstructions,
)

const run = await openai.beta.threads.runs.createAndPoll(thread.id, {
assistant_id: ASSISTANT_ID,
// TODO: PUT USER CATEGORIES HERE LATER
// additional_instructions: '<PUT USER CATEGORIES HERE LATER>',
additional_instructions: additionalInstructions,
})

log.info('Ran assistant on thread. Run ID: %s', run.id)
Expand All @@ -81,19 +99,17 @@ export async function generateTransactionDataFromFile({
const messages = await openai.beta.threads.messages.list(run.thread_id)
const firstMessage = messages.data[0].content[0]

log.debug('First message: %o', firstMessage)

const aiTransactionData =
firstMessage.type === 'text' ? JSON.parse(firstMessage.text.value) : null

log.info('AI transaction data: %o', aiTransactionData)

await cleanup()
cleanup()

return aiTransactionData
}

log.error('Assistant run failed. Run details: %o', run)
await cleanup()
cleanup()
throw new Error('Assistant run failed')
}
6 changes: 1 addition & 5 deletions apps/mobile/mutations/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,8 @@ export async function getAITransactionData(fileUri: string) {

const transaction = zUpdateTransaction.parse({
...body,
date: body?.datetime,
date: body?.date ? new Date(body.date) : undefined,
})

if (!transaction.amount) {
throw new Error('Cannot extract transaction data')
}

return transaction
}

0 comments on commit db149c1

Please sign in to comment.