Flowise

Форк
0
257 строк · 10.6 Кб
1
import OpenAI from 'openai'
2
import { StatusCodes } from 'http-status-codes'
3
import fs from 'fs'
4
import { Credential } from '../../database/entities/Credential'
5
import { InternalFlowiseError } from '../../errors/internalFlowiseError'
6
import { getErrorMessage } from '../../errors/utils'
7
import { getRunningExpressApp } from '../../utils/getRunningExpressApp'
8
import { decryptCredentialData } from '../../utils'
9

10
const getAssistantVectorStore = async (credentialId: string, vectorStoreId: string) => {
11
    try {
12
        const appServer = getRunningExpressApp()
13
        const credential = await appServer.AppDataSource.getRepository(Credential).findOneBy({
14
            id: credentialId
15
        })
16
        if (!credential) {
17
            throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Credential ${credentialId} not found in the database!`)
18
        }
19
        // Decrpyt credentialData
20
        const decryptedCredentialData = await decryptCredentialData(credential.encryptedData)
21
        const openAIApiKey = decryptedCredentialData['openAIApiKey']
22
        if (!openAIApiKey) {
23
            throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `OpenAI ApiKey not found`)
24
        }
25

26
        const openai = new OpenAI({ apiKey: openAIApiKey })
27
        const dbResponse = await openai.beta.vectorStores.retrieve(vectorStoreId)
28
        return dbResponse
29
    } catch (error) {
30
        throw new InternalFlowiseError(
31
            StatusCodes.INTERNAL_SERVER_ERROR,
32
            `Error: openaiAssistantsVectorStoreService.getAssistantVectorStore - ${getErrorMessage(error)}`
33
        )
34
    }
35
}
36

37
const listAssistantVectorStore = async (credentialId: string) => {
38
    try {
39
        const appServer = getRunningExpressApp()
40
        const credential = await appServer.AppDataSource.getRepository(Credential).findOneBy({
41
            id: credentialId
42
        })
43
        if (!credential) {
44
            throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Credential ${credentialId} not found in the database!`)
45
        }
46
        // Decrpyt credentialData
47
        const decryptedCredentialData = await decryptCredentialData(credential.encryptedData)
48
        const openAIApiKey = decryptedCredentialData['openAIApiKey']
49
        if (!openAIApiKey) {
50
            throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `OpenAI ApiKey not found`)
51
        }
52

53
        const openai = new OpenAI({ apiKey: openAIApiKey })
54
        const dbResponse = await openai.beta.vectorStores.list()
55
        return dbResponse.data
56
    } catch (error) {
57
        throw new InternalFlowiseError(
58
            StatusCodes.INTERNAL_SERVER_ERROR,
59
            `Error: openaiAssistantsVectorStoreService.listAssistantVectorStore - ${getErrorMessage(error)}`
60
        )
61
    }
62
}
63

64
const createAssistantVectorStore = async (credentialId: string, obj: OpenAI.Beta.VectorStores.VectorStoreCreateParams) => {
65
    try {
66
        const appServer = getRunningExpressApp()
67
        const credential = await appServer.AppDataSource.getRepository(Credential).findOneBy({
68
            id: credentialId
69
        })
70
        if (!credential) {
71
            throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Credential ${credentialId} not found in the database!`)
72
        }
73
        // Decrpyt credentialData
74
        const decryptedCredentialData = await decryptCredentialData(credential.encryptedData)
75
        const openAIApiKey = decryptedCredentialData['openAIApiKey']
76
        if (!openAIApiKey) {
77
            throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `OpenAI ApiKey not found`)
78
        }
79

80
        const openai = new OpenAI({ apiKey: openAIApiKey })
81
        const dbResponse = await openai.beta.vectorStores.create(obj)
82
        return dbResponse
83
    } catch (error) {
84
        throw new InternalFlowiseError(
85
            StatusCodes.INTERNAL_SERVER_ERROR,
86
            `Error: openaiAssistantsVectorStoreService.createAssistantVectorStore - ${getErrorMessage(error)}`
87
        )
88
    }
89
}
90

91
const updateAssistantVectorStore = async (
92
    credentialId: string,
93
    vectorStoreId: string,
94
    obj: OpenAI.Beta.VectorStores.VectorStoreUpdateParams
95
) => {
96
    try {
97
        const appServer = getRunningExpressApp()
98
        const credential = await appServer.AppDataSource.getRepository(Credential).findOneBy({
99
            id: credentialId
100
        })
101
        if (!credential) {
102
            throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Credential ${credentialId} not found in the database!`)
103
        }
104
        // Decrpyt credentialData
105
        const decryptedCredentialData = await decryptCredentialData(credential.encryptedData)
106
        const openAIApiKey = decryptedCredentialData['openAIApiKey']
107
        if (!openAIApiKey) {
108
            throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `OpenAI ApiKey not found`)
109
        }
110

111
        const openai = new OpenAI({ apiKey: openAIApiKey })
112
        const dbResponse = await openai.beta.vectorStores.update(vectorStoreId, obj)
113
        const vectorStoreFiles = await openai.beta.vectorStores.files.list(vectorStoreId)
114
        if (vectorStoreFiles.data?.length) {
115
            const files = []
116
            for (const file of vectorStoreFiles.data) {
117
                const fileData = await openai.files.retrieve(file.id)
118
                files.push(fileData)
119
            }
120
            ;(dbResponse as any).files = files
121
        }
122
        return dbResponse
123
    } catch (error) {
124
        throw new InternalFlowiseError(
125
            StatusCodes.INTERNAL_SERVER_ERROR,
126
            `Error: openaiAssistantsVectorStoreService.updateAssistantVectorStore - ${getErrorMessage(error)}`
127
        )
128
    }
129
}
130

131
const deleteAssistantVectorStore = async (credentialId: string, vectorStoreId: string) => {
132
    try {
133
        const appServer = getRunningExpressApp()
134
        const credential = await appServer.AppDataSource.getRepository(Credential).findOneBy({
135
            id: credentialId
136
        })
137
        if (!credential) {
138
            throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Credential ${credentialId} not found in the database!`)
139
        }
140
        // Decrpyt credentialData
141
        const decryptedCredentialData = await decryptCredentialData(credential.encryptedData)
142
        const openAIApiKey = decryptedCredentialData['openAIApiKey']
143
        if (!openAIApiKey) {
144
            throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `OpenAI ApiKey not found`)
145
        }
146

147
        const openai = new OpenAI({ apiKey: openAIApiKey })
148
        const dbResponse = await openai.beta.vectorStores.del(vectorStoreId)
149
        return dbResponse
150
    } catch (error) {
151
        throw new InternalFlowiseError(
152
            StatusCodes.INTERNAL_SERVER_ERROR,
153
            `Error: openaiAssistantsVectorStoreService.deleteAssistantVectorStore - ${getErrorMessage(error)}`
154
        )
155
    }
156
}
157

158
const uploadFilesToAssistantVectorStore = async (
159
    credentialId: string,
160
    vectorStoreId: string,
161
    files: { filePath: string; fileName: string }[]
162
): Promise<any> => {
163
    try {
164
        const appServer = getRunningExpressApp()
165
        const credential = await appServer.AppDataSource.getRepository(Credential).findOneBy({
166
            id: credentialId
167
        })
168
        if (!credential) {
169
            throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Credential ${credentialId} not found in the database!`)
170
        }
171
        // Decrpyt credentialData
172
        const decryptedCredentialData = await decryptCredentialData(credential.encryptedData)
173
        const openAIApiKey = decryptedCredentialData['openAIApiKey']
174
        if (!openAIApiKey) {
175
            throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `OpenAI ApiKey not found`)
176
        }
177

178
        const openai = new OpenAI({ apiKey: openAIApiKey })
179
        const uploadedFiles = []
180
        for (const file of files) {
181
            const createdFile = await openai.files.create({
182
                file: new File([new Blob([fs.readFileSync(file.filePath)])], file.fileName),
183
                purpose: 'assistants'
184
            })
185
            uploadedFiles.push(createdFile)
186
            fs.unlinkSync(file.filePath)
187
        }
188

189
        const file_ids = [...uploadedFiles.map((file) => file.id)]
190

191
        const res = await openai.beta.vectorStores.fileBatches.createAndPoll(vectorStoreId, {
192
            file_ids
193
        })
194
        if (res.status === 'completed' && res.file_counts.completed === uploadedFiles.length) return uploadedFiles
195
        else if (res.status === 'failed')
196
            throw new InternalFlowiseError(
197
                StatusCodes.INTERNAL_SERVER_ERROR,
198
                'Error: openaiAssistantsVectorStoreService.uploadFilesToAssistantVectorStore - Upload failed!'
199
            )
200
        else
201
            throw new InternalFlowiseError(
202
                StatusCodes.INTERNAL_SERVER_ERROR,
203
                'Error: openaiAssistantsVectorStoreService.uploadFilesToAssistantVectorStore - Upload cancelled!'
204
            )
205
    } catch (error) {
206
        throw new InternalFlowiseError(
207
            StatusCodes.INTERNAL_SERVER_ERROR,
208
            `Error: openaiAssistantsVectorStoreService.uploadFilesToAssistantVectorStore - ${getErrorMessage(error)}`
209
        )
210
    }
211
}
212

213
const deleteFilesFromAssistantVectorStore = async (credentialId: string, vectorStoreId: string, file_ids: string[]) => {
214
    try {
215
        const appServer = getRunningExpressApp()
216
        const credential = await appServer.AppDataSource.getRepository(Credential).findOneBy({
217
            id: credentialId
218
        })
219
        if (!credential) {
220
            throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Credential ${credentialId} not found in the database!`)
221
        }
222
        // Decrpyt credentialData
223
        const decryptedCredentialData = await decryptCredentialData(credential.encryptedData)
224
        const openAIApiKey = decryptedCredentialData['openAIApiKey']
225
        if (!openAIApiKey) {
226
            throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `OpenAI ApiKey not found`)
227
        }
228

229
        const openai = new OpenAI({ apiKey: openAIApiKey })
230
        const deletedFileIds = []
231
        let count = 0
232
        for (const file of file_ids) {
233
            const res = await openai.beta.vectorStores.files.del(vectorStoreId, file)
234
            if (res.deleted) {
235
                deletedFileIds.push(file)
236
                count += 1
237
            }
238
        }
239

240
        return { deletedFileIds, count }
241
    } catch (error) {
242
        throw new InternalFlowiseError(
243
            StatusCodes.INTERNAL_SERVER_ERROR,
244
            `Error: openaiAssistantsVectorStoreService.uploadFilesToAssistantVectorStore - ${getErrorMessage(error)}`
245
        )
246
    }
247
}
248

249
export default {
250
    getAssistantVectorStore,
251
    listAssistantVectorStore,
252
    createAssistantVectorStore,
253
    updateAssistantVectorStore,
254
    deleteAssistantVectorStore,
255
    uploadFilesToAssistantVectorStore,
256
    deleteFilesFromAssistantVectorStore
257
}
258

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.