1
import OpenAI from 'openai'
3
import { StatusCodes } from 'http-status-codes'
4
import { decryptCredentialData } from '../../utils'
5
import { getRunningExpressApp } from '../../utils/getRunningExpressApp'
6
import { Credential } from '../../database/entities/Credential'
7
import { InternalFlowiseError } from '../../errors/internalFlowiseError'
8
import { getErrorMessage } from '../../errors/utils'
10
// ----------------------------------------
12
// ----------------------------------------
14
// List available assistants
15
const getAllOpenaiAssistants = async (credentialId: string): Promise<any> => {
17
const appServer = getRunningExpressApp()
18
const credential = await appServer.AppDataSource.getRepository(Credential).findOneBy({
22
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Credential ${credentialId} not found in the database!`)
24
// Decrpyt credentialData
25
const decryptedCredentialData = await decryptCredentialData(credential.encryptedData)
26
const openAIApiKey = decryptedCredentialData['openAIApiKey']
28
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `OpenAI ApiKey not found`)
30
const openai = new OpenAI({ apiKey: openAIApiKey })
31
const retrievedAssistants = await openai.beta.assistants.list()
32
const dbResponse = retrievedAssistants.data
35
throw new InternalFlowiseError(
36
StatusCodes.INTERNAL_SERVER_ERROR,
37
`Error: openaiAssistantsService.getAllOpenaiAssistants - ${getErrorMessage(error)}`
42
// Get assistant object
43
const getSingleOpenaiAssistant = async (credentialId: string, assistantId: string): Promise<any> => {
45
const appServer = getRunningExpressApp()
46
const credential = await appServer.AppDataSource.getRepository(Credential).findOneBy({
50
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Credential ${credentialId} not found in the database!`)
52
// Decrpyt credentialData
53
const decryptedCredentialData = await decryptCredentialData(credential.encryptedData)
54
const openAIApiKey = decryptedCredentialData['openAIApiKey']
56
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `OpenAI ApiKey not found`)
59
const openai = new OpenAI({ apiKey: openAIApiKey })
60
const dbResponse = await openai.beta.assistants.retrieve(assistantId)
61
const resp = await openai.files.list()
62
const existingFiles = resp.data ?? []
63
if (dbResponse.tool_resources?.code_interpreter?.file_ids?.length) {
64
;(dbResponse.tool_resources.code_interpreter as any).files = [
65
...existingFiles.filter((file) => dbResponse.tool_resources?.code_interpreter?.file_ids?.includes(file.id))
68
if (dbResponse.tool_resources?.file_search?.vector_store_ids?.length) {
69
// Since there can only be 1 vector store per assistant
70
const vectorStoreId = dbResponse.tool_resources.file_search.vector_store_ids[0]
71
const vectorStoreFiles = await openai.beta.vectorStores.files.list(vectorStoreId)
72
const fileIds = vectorStoreFiles.data?.map((file) => file.id) ?? []
73
;(dbResponse.tool_resources.file_search as any).files = [...existingFiles.filter((file) => fileIds.includes(file.id))]
74
;(dbResponse.tool_resources.file_search as any).vector_store_object = await openai.beta.vectorStores.retrieve(vectorStoreId)
78
throw new InternalFlowiseError(
79
StatusCodes.INTERNAL_SERVER_ERROR,
80
`Error: openaiAssistantsService.getSingleOpenaiAssistant - ${getErrorMessage(error)}`
85
const uploadFilesToAssistant = async (credentialId: string, files: { filePath: string; fileName: string }[]) => {
86
const appServer = getRunningExpressApp()
87
const credential = await appServer.AppDataSource.getRepository(Credential).findOneBy({
91
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Credential ${credentialId} not found in the database!`)
93
// Decrpyt credentialData
94
const decryptedCredentialData = await decryptCredentialData(credential.encryptedData)
95
const openAIApiKey = decryptedCredentialData['openAIApiKey']
97
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `OpenAI ApiKey not found`)
100
const openai = new OpenAI({ apiKey: openAIApiKey })
101
const uploadedFiles = []
103
for (const file of files) {
104
const createdFile = await openai.files.create({
105
file: new File([new Blob([fs.readFileSync(file.filePath)])], file.fileName),
106
purpose: 'assistants'
108
uploadedFiles.push(createdFile)
109
fs.unlinkSync(file.filePath)
116
getAllOpenaiAssistants,
117
getSingleOpenaiAssistant,
118
uploadFilesToAssistant