Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api): upload ai transaction image to blob #317

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions apps/api/v1/services/ai.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { CategoryType } from '@prisma/client'
import { OpenAI } from 'openai'
import { getLogger } from '../../lib/log'
import { createAiCache, findAiCacheByQuery } from './ai-cache.service'
import { putBlobObject } from './blob.service'
import { hashFile } from './file.service'

const ASSISTANT_ID = process.env.OPENAI_ASSISTANT_ID!
Expand Down Expand Up @@ -100,6 +101,7 @@ export async function generateTransactionDataFromFile({
fileHash,
additionalInstructions,
})}`
const blobObjectPathname = `transaction_files/${fileHash}`

log.debug('Checking cache for query: %s', cacheQuery)

Expand All @@ -110,15 +112,23 @@ export async function generateTransactionDataFromFile({
return JSON.parse(cachedResponse.response)
}

const file = await uploadVisionFile({ file: inputFile })
const [visionFile, blobObject] = await Promise.all([
uploadVisionFile({ file: inputFile }),
putBlobObject({
file: inputFile,
pathname: blobObjectPathname,
}),
])

log.debug('Creating thread with uploaded file. File ID: %s', file.id)
log.debug('Creating thread with uploaded file. File ID: %s', visionFile.id)

const thread = await openai.beta.threads.create({
messages: [
{
role: 'user',
content: [{ type: 'image_file', image_file: { file_id: file.id } }],
content: [
{ type: 'image_file', image_file: { file_id: visionFile.id } },
],
},
],
})
Expand All @@ -141,7 +151,7 @@ export async function generateTransactionDataFromFile({

async function cleanup() {
// Delete the file after processing
await deleteFile({ fileId: file.id })
await deleteFile({ fileId: visionFile.id })
// Delete the thread after processing
log.debug('Deleting thread. Thread ID: %s', thread.id)
await openai.beta.threads.del(thread.id)
Expand All @@ -165,7 +175,7 @@ export async function generateTransactionDataFromFile({
response: JSON.stringify(aiTransactionData),
})

return aiTransactionData
return { ...aiTransactionData, blobObject }
}

log.error('Assistant run failed. Run details: %o', run)
Expand Down