Feature/OpenAI Assistant V2 (#2258)

* add gpt4 turbo to assistant

* OpenAI Assistant V2

* update langfuse handler
This commit is contained in:
Henry Heng
2024-04-25 20:14:04 +01:00
committed by GitHub
parent 4782c0f6fc
commit 7360d1d9a6
25 changed files with 23422 additions and 17637 deletions
@@ -0,0 +1,201 @@
import { Request, Response, NextFunction } from 'express'
import { StatusCodes } from 'http-status-codes'
import { InternalFlowiseError } from '../../errors/internalFlowiseError'
import openAIAssistantVectorStoreService from '../../services/openai-assistants-vector-store'
const getAssistantVectorStore = async (req: Request, res: Response, next: NextFunction) => {
try {
if (typeof req.params === 'undefined' || !req.params.id) {
throw new InternalFlowiseError(
StatusCodes.PRECONDITION_FAILED,
`Error: openaiAssistantsVectorStoreController.getAssistantVectorStore - id not provided!`
)
}
if (typeof req.query === 'undefined' || !req.query.credential) {
throw new InternalFlowiseError(
StatusCodes.PRECONDITION_FAILED,
`Error: openaiAssistantsVectorStoreController.getAssistantVectorStore - credential not provided!`
)
}
const apiResponse = await openAIAssistantVectorStoreService.getAssistantVectorStore(req.query.credential as string, req.params.id)
return res.json(apiResponse)
} catch (error) {
next(error)
}
}
const listAssistantVectorStore = async (req: Request, res: Response, next: NextFunction) => {
try {
if (typeof req.query === 'undefined' || !req.query.credential) {
throw new InternalFlowiseError(
StatusCodes.PRECONDITION_FAILED,
`Error: openaiAssistantsVectorStoreController.listAssistantVectorStore - credential not provided!`
)
}
const apiResponse = await openAIAssistantVectorStoreService.listAssistantVectorStore(req.query.credential as string)
return res.json(apiResponse)
} catch (error) {
next(error)
}
}
const createAssistantVectorStore = async (req: Request, res: Response, next: NextFunction) => {
try {
if (!req.body) {
throw new InternalFlowiseError(
StatusCodes.PRECONDITION_FAILED,
`Error: openaiAssistantsVectorStoreController.createAssistantVectorStore - body not provided!`
)
}
if (typeof req.query === 'undefined' || !req.query.credential) {
throw new InternalFlowiseError(
StatusCodes.PRECONDITION_FAILED,
`Error: openaiAssistantsVectorStoreController.createAssistantVectorStore - credential not provided!`
)
}
const apiResponse = await openAIAssistantVectorStoreService.createAssistantVectorStore(req.query.credential as string, req.body)
return res.json(apiResponse)
} catch (error) {
next(error)
}
}
const updateAssistantVectorStore = async (req: Request, res: Response, next: NextFunction) => {
try {
if (typeof req.params === 'undefined' || !req.params.id) {
throw new InternalFlowiseError(
StatusCodes.PRECONDITION_FAILED,
`Error: openaiAssistantsVectorStoreController.updateAssistantVectorStore - id not provided!`
)
}
if (typeof req.query === 'undefined' || !req.query.credential) {
throw new InternalFlowiseError(
StatusCodes.PRECONDITION_FAILED,
`Error: openaiAssistantsVectorStoreController.updateAssistantVectorStore - credential not provided!`
)
}
if (!req.body) {
throw new InternalFlowiseError(
StatusCodes.PRECONDITION_FAILED,
`Error: openaiAssistantsVectorStoreController.updateAssistantVectorStore - body not provided!`
)
}
const apiResponse = await openAIAssistantVectorStoreService.updateAssistantVectorStore(
req.query.credential as string,
req.params.id,
req.body
)
return res.json(apiResponse)
} catch (error) {
next(error)
}
}
const deleteAssistantVectorStore = async (req: Request, res: Response, next: NextFunction) => {
try {
if (typeof req.params === 'undefined' || !req.params.id) {
throw new InternalFlowiseError(
StatusCodes.PRECONDITION_FAILED,
`Error: openaiAssistantsVectorStoreController.deleteAssistantVectorStore - id not provided!`
)
}
if (typeof req.query === 'undefined' || !req.query.credential) {
throw new InternalFlowiseError(
StatusCodes.PRECONDITION_FAILED,
`Error: openaiAssistantsVectorStoreController.updateAssistantVectorStore - credential not provided!`
)
}
const apiResponse = await openAIAssistantVectorStoreService.deleteAssistantVectorStore(
req.query.credential as string,
req.params.id as string
)
return res.json(apiResponse)
} catch (error) {
next(error)
}
}
const uploadFilesToAssistantVectorStore = async (req: Request, res: Response, next: NextFunction) => {
try {
if (!req.body) {
throw new InternalFlowiseError(
StatusCodes.PRECONDITION_FAILED,
`Error: openaiAssistantsVectorStoreController.uploadFilesToAssistantVectorStore - body not provided!`
)
}
if (typeof req.params === 'undefined' || !req.params.id) {
throw new InternalFlowiseError(
StatusCodes.PRECONDITION_FAILED,
`Error: openaiAssistantsVectorStoreController.uploadFilesToAssistantVectorStore - id not provided!`
)
}
if (typeof req.query === 'undefined' || !req.query.credential) {
throw new InternalFlowiseError(
StatusCodes.PRECONDITION_FAILED,
`Error: openaiAssistantsVectorStoreController.uploadFilesToAssistantVectorStore - credential not provided!`
)
}
const files = req.files ?? []
const uploadFiles: { filePath: string; fileName: string }[] = []
if (Array.isArray(files)) {
for (const file of files) {
uploadFiles.push({
filePath: file.path,
fileName: file.originalname
})
}
}
const apiResponse = await openAIAssistantVectorStoreService.uploadFilesToAssistantVectorStore(
req.query.credential as string,
req.params.id as string,
uploadFiles
)
return res.json(apiResponse)
} catch (error) {
next(error)
}
}
const deleteFilesFromAssistantVectorStore = async (req: Request, res: Response, next: NextFunction) => {
try {
if (!req.body) {
throw new InternalFlowiseError(
StatusCodes.PRECONDITION_FAILED,
`Error: openaiAssistantsVectorStoreController.deleteFilesFromAssistantVectorStore - body not provided!`
)
}
if (typeof req.params === 'undefined' || !req.params.id) {
throw new InternalFlowiseError(
StatusCodes.PRECONDITION_FAILED,
`Error: openaiAssistantsVectorStoreController.deleteFilesFromAssistantVectorStore - id not provided!`
)
}
if (typeof req.query === 'undefined' || !req.query.credential) {
throw new InternalFlowiseError(
StatusCodes.PRECONDITION_FAILED,
`Error: openaiAssistantsVectorStoreController.deleteFilesFromAssistantVectorStore - credential not provided!`
)
}
const apiResponse = await openAIAssistantVectorStoreService.deleteFilesFromAssistantVectorStore(
req.query.credential as string,
req.params.id as string,
req.body.file_ids
)
return res.json(apiResponse)
} catch (error) {
next(error)
}
}
export default {
getAssistantVectorStore,
listAssistantVectorStore,
createAssistantVectorStore,
updateAssistantVectorStore,
deleteAssistantVectorStore,
uploadFilesToAssistantVectorStore,
deleteFilesFromAssistantVectorStore
}
@@ -1,11 +1,10 @@
import { Request, Response, NextFunction } from 'express'
import path from 'path'
import * as fs from 'fs'
import openaiAssistantsService from '../../services/openai-assistants'
import { getUserHome } from '../../utils'
import contentDisposition from 'content-disposition'
import { InternalFlowiseError } from '../../errors/internalFlowiseError'
import { StatusCodes } from 'http-status-codes'
import { streamStorageFile } from 'flowise-components'
// List available assistants
const getAllOpenaiAssistants = async (req: Request, res: Response, next: NextFunction) => {
@@ -48,27 +47,57 @@ const getSingleOpenaiAssistant = async (req: Request, res: Response, next: NextF
// Download file from assistant
const getFileFromAssistant = async (req: Request, res: Response, next: NextFunction) => {
try {
const filePath = path.join(getUserHome(), '.flowise', 'openai-assistant', req.body.fileName)
//raise error if file path is not absolute
if (!path.isAbsolute(filePath)) return res.status(500).send(`Invalid file path`)
//raise error if file path contains '..'
if (filePath.includes('..')) return res.status(500).send(`Invalid file path`)
//only return from the .flowise openai-assistant folder
if (!(filePath.includes('.flowise') && filePath.includes('openai-assistant'))) return res.status(500).send(`Invalid file path`)
if (fs.existsSync(filePath)) {
res.setHeader('Content-Disposition', contentDisposition(path.basename(filePath)))
const fileStream = fs.createReadStream(filePath)
if (!req.body.chatflowId || !req.body.chatId || !req.body.fileName) {
return res.status(500).send(`Invalid file path`)
}
const chatflowId = req.body.chatflowId as string
const chatId = req.body.chatId as string
const fileName = req.body.fileName as string
res.setHeader('Content-Disposition', contentDisposition(fileName))
const fileStream = await streamStorageFile(chatflowId, chatId, fileName)
if (!fileStream) throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Error: getFileFromAssistant`)
if (fileStream instanceof fs.ReadStream && fileStream?.pipe) {
fileStream.pipe(res)
} else {
return res.status(404).send(`File ${req.body.fileName} not found`)
res.send(fileStream)
}
} catch (error) {
next(error)
}
}
const uploadAssistantFiles = async (req: Request, res: Response, next: NextFunction) => {
try {
if (typeof req.query === 'undefined' || !req.query.credential) {
throw new InternalFlowiseError(
StatusCodes.PRECONDITION_FAILED,
`Error: openaiAssistantsVectorStoreController.uploadFilesToAssistantVectorStore - credential not provided!`
)
}
const files = req.files ?? []
const uploadFiles: { filePath: string; fileName: string }[] = []
if (Array.isArray(files)) {
for (const file of files) {
uploadFiles.push({
filePath: file.path,
fileName: file.originalname
})
}
}
const apiResponse = await openaiAssistantsService.uploadFilesToAssistant(req.query.credential as string, uploadFiles)
return res.json(apiResponse)
} catch (error) {
next(error)
}
}
export default {
getAllOpenaiAssistants,
getSingleOpenaiAssistant,
getFileFromAssistant
getFileFromAssistant,
uploadAssistantFiles
}