Flowise

Форк
0
119 строк · 5.2 Кб
1
import OpenAI from 'openai'
2
import fs from 'fs'
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'
9

10
// ----------------------------------------
11
// Assistants
12
// ----------------------------------------
13

14
// List available assistants
15
const getAllOpenaiAssistants = async (credentialId: string): Promise<any> => {
16
    try {
17
        const appServer = getRunningExpressApp()
18
        const credential = await appServer.AppDataSource.getRepository(Credential).findOneBy({
19
            id: credentialId
20
        })
21
        if (!credential) {
22
            throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Credential ${credentialId} not found in the database!`)
23
        }
24
        // Decrpyt credentialData
25
        const decryptedCredentialData = await decryptCredentialData(credential.encryptedData)
26
        const openAIApiKey = decryptedCredentialData['openAIApiKey']
27
        if (!openAIApiKey) {
28
            throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `OpenAI ApiKey not found`)
29
        }
30
        const openai = new OpenAI({ apiKey: openAIApiKey })
31
        const retrievedAssistants = await openai.beta.assistants.list()
32
        const dbResponse = retrievedAssistants.data
33
        return dbResponse
34
    } catch (error) {
35
        throw new InternalFlowiseError(
36
            StatusCodes.INTERNAL_SERVER_ERROR,
37
            `Error: openaiAssistantsService.getAllOpenaiAssistants - ${getErrorMessage(error)}`
38
        )
39
    }
40
}
41

42
// Get assistant object
43
const getSingleOpenaiAssistant = async (credentialId: string, assistantId: string): Promise<any> => {
44
    try {
45
        const appServer = getRunningExpressApp()
46
        const credential = await appServer.AppDataSource.getRepository(Credential).findOneBy({
47
            id: credentialId
48
        })
49
        if (!credential) {
50
            throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Credential ${credentialId} not found in the database!`)
51
        }
52
        // Decrpyt credentialData
53
        const decryptedCredentialData = await decryptCredentialData(credential.encryptedData)
54
        const openAIApiKey = decryptedCredentialData['openAIApiKey']
55
        if (!openAIApiKey) {
56
            throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `OpenAI ApiKey not found`)
57
        }
58

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))
66
            ]
67
        }
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)
75
        }
76
        return dbResponse
77
    } catch (error) {
78
        throw new InternalFlowiseError(
79
            StatusCodes.INTERNAL_SERVER_ERROR,
80
            `Error: openaiAssistantsService.getSingleOpenaiAssistant - ${getErrorMessage(error)}`
81
        )
82
    }
83
}
84

85
const uploadFilesToAssistant = async (credentialId: string, files: { filePath: string; fileName: string }[]) => {
86
    const appServer = getRunningExpressApp()
87
    const credential = await appServer.AppDataSource.getRepository(Credential).findOneBy({
88
        id: credentialId
89
    })
90
    if (!credential) {
91
        throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Credential ${credentialId} not found in the database!`)
92
    }
93
    // Decrpyt credentialData
94
    const decryptedCredentialData = await decryptCredentialData(credential.encryptedData)
95
    const openAIApiKey = decryptedCredentialData['openAIApiKey']
96
    if (!openAIApiKey) {
97
        throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `OpenAI ApiKey not found`)
98
    }
99

100
    const openai = new OpenAI({ apiKey: openAIApiKey })
101
    const uploadedFiles = []
102

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'
107
        })
108
        uploadedFiles.push(createdFile)
109
        fs.unlinkSync(file.filePath)
110
    }
111

112
    return uploadedFiles
113
}
114

115
export default {
116
    getAllOpenaiAssistants,
117
    getSingleOpenaiAssistant,
118
    uploadFilesToAssistant
119
}
120

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

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

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

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