Merge pull request #1252 from FlowiseAI/feature/OpenAI-Assistant

Feature/Add file annotations, sync and delete assistant
This commit is contained in:
Henry Heng 2023-11-20 12:14:52 +00:00 committed by GitHub
commit 10c3066a91
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 436 additions and 82 deletions

View File

@ -111,7 +111,7 @@ class OpenAIAssistant_Agents implements INode {
const openai = new OpenAI({ apiKey: openAIApiKey }) const openai = new OpenAI({ apiKey: openAIApiKey })
options.logger.info(`Clearing OpenAI Thread ${sessionId}`) options.logger.info(`Clearing OpenAI Thread ${sessionId}`)
await openai.beta.threads.del(sessionId) if (sessionId) await openai.beta.threads.del(sessionId)
options.logger.info(`Successfully cleared OpenAI Thread ${sessionId}`) options.logger.info(`Successfully cleared OpenAI Thread ${sessionId}`)
} }
@ -135,16 +135,25 @@ class OpenAIAssistant_Agents implements INode {
const openai = new OpenAI({ apiKey: openAIApiKey }) const openai = new OpenAI({ apiKey: openAIApiKey })
// Retrieve assistant
try { try {
const assistantDetails = JSON.parse(assistant.details) const assistantDetails = JSON.parse(assistant.details)
const openAIAssistantId = assistantDetails.id const openAIAssistantId = assistantDetails.id
// Retrieve assistant
const retrievedAssistant = await openai.beta.assistants.retrieve(openAIAssistantId) const retrievedAssistant = await openai.beta.assistants.retrieve(openAIAssistantId)
if (formattedTools.length) { if (formattedTools.length) {
let filteredTools = uniqWith([...retrievedAssistant.tools, ...formattedTools], isEqual) let filteredTools = []
for (const tool of retrievedAssistant.tools) {
if (tool.type === 'code_interpreter' || tool.type === 'retrieval') filteredTools.push(tool)
}
filteredTools = uniqWith([...filteredTools, ...formattedTools], isEqual)
// filter out tool with empty function
filteredTools = filteredTools.filter((tool) => !(tool.type === 'function' && !(tool as any).function)) filteredTools = filteredTools.filter((tool) => !(tool.type === 'function' && !(tool as any).function))
await openai.beta.assistants.update(openAIAssistantId, { tools: filteredTools }) await openai.beta.assistants.update(openAIAssistantId, { tools: filteredTools })
} else {
let filteredTools = retrievedAssistant.tools.filter((tool) => tool.type !== 'function')
await openai.beta.assistants.update(openAIAssistantId, { tools: filteredTools })
} }
const chatmessage = await appDataSource.getRepository(databaseEntities['ChatMessage']).findOneBy({ const chatmessage = await appDataSource.getRepository(databaseEntities['ChatMessage']).findOneBy({
@ -152,14 +161,45 @@ class OpenAIAssistant_Agents implements INode {
}) })
let threadId = '' let threadId = ''
let isNewThread = false
if (!chatmessage) { if (!chatmessage) {
const thread = await openai.beta.threads.create({}) const thread = await openai.beta.threads.create({})
threadId = thread.id threadId = thread.id
isNewThread = true
} else { } else {
const thread = await openai.beta.threads.retrieve(chatmessage.sessionId) const thread = await openai.beta.threads.retrieve(chatmessage.sessionId)
threadId = thread.id threadId = thread.id
} }
// List all runs
if (!isNewThread) {
const promise = (threadId: string) => {
return new Promise<void>((resolve) => {
const timeout = setInterval(async () => {
const allRuns = await openai.beta.threads.runs.list(threadId)
if (allRuns.data && allRuns.data.length) {
const firstRunId = allRuns.data[0].id
const runStatus = allRuns.data.find((run) => run.id === firstRunId)?.status
if (
runStatus &&
(runStatus === 'cancelled' ||
runStatus === 'completed' ||
runStatus === 'expired' ||
runStatus === 'failed')
) {
clearInterval(timeout)
resolve()
}
} else {
clearInterval(timeout)
resolve()
}
}, 500)
})
}
await promise(threadId)
}
// Add message to thread // Add message to thread
await openai.beta.threads.messages.create(threadId, { await openai.beta.threads.messages.create(threadId, {
role: 'user', role: 'user',
@ -217,27 +257,41 @@ class OpenAIAssistant_Agents implements INode {
}) })
resolve(state) resolve(state)
} else { } else {
reject( await openai.beta.threads.runs.cancel(threadId, runId)
new Error( resolve('requires_action_retry')
`Error processing thread: ${state}, Thread ID: ${threadId}, Run ID: ${runId}. submit_tool_outputs.tool_calls are empty`
)
)
} }
} }
} else if (state === 'cancelled' || state === 'expired' || state === 'failed') { } else if (state === 'cancelled' || state === 'expired' || state === 'failed') {
clearInterval(timeout) clearInterval(timeout)
reject(new Error(`Error processing thread: ${state}, Thread ID: ${threadId}, Run ID: ${runId}`)) reject(
new Error(`Error processing thread: ${state}, Thread ID: ${threadId}, Run ID: ${runId}, Status: ${state}`)
)
} }
}, 500) }, 500)
}) })
} }
// Polling run status // Polling run status
let runThreadId = runThread.id
let state = await promise(threadId, runThread.id) let state = await promise(threadId, runThread.id)
while (state === 'requires_action') { while (state === 'requires_action') {
state = await promise(threadId, runThread.id) state = await promise(threadId, runThread.id)
} }
let retries = 3
while (state === 'requires_action_retry') {
if (retries > 0) {
retries -= 1
const newRunThread = await openai.beta.threads.runs.create(threadId, {
assistant_id: retrievedAssistant.id
})
runThreadId = newRunThread.id
state = await promise(threadId, newRunThread.id)
} else {
throw new Error(`Error processing thread: ${state}, Thread ID: ${threadId}`)
}
}
// List messages // List messages
const messages = await openai.beta.threads.messages.list(threadId) const messages = await openai.beta.threads.messages.list(threadId)
const messageData = messages.data ?? [] const messageData = messages.data ?? []
@ -245,12 +299,58 @@ class OpenAIAssistant_Agents implements INode {
if (!assistantMessages.length) return '' if (!assistantMessages.length) return ''
let returnVal = '' let returnVal = ''
const fileAnnotations = []
for (let i = 0; i < assistantMessages[0].content.length; i += 1) { for (let i = 0; i < assistantMessages[0].content.length; i += 1) {
if (assistantMessages[0].content[i].type === 'text') { if (assistantMessages[0].content[i].type === 'text') {
const content = assistantMessages[0].content[i] as MessageContentText const content = assistantMessages[0].content[i] as MessageContentText
returnVal += content.text.value
//TODO: handle annotations if (content.text.annotations) {
const message_content = content.text
const annotations = message_content.annotations
const dirPath = path.join(getUserHome(), '.flowise', 'openai-assistant')
// Iterate over the annotations and add footnotes
for (let index = 0; index < annotations.length; index++) {
const annotation = annotations[index]
let filePath = ''
// Gather citations based on annotation attributes
const file_citation = (annotation as OpenAI.Beta.Threads.Messages.MessageContentText.Text.FileCitation)
.file_citation
if (file_citation) {
const cited_file = await openai.files.retrieve(file_citation.file_id)
// eslint-disable-next-line no-useless-escape
const fileName = cited_file.filename.split(/[\/\\]/).pop() ?? cited_file.filename
filePath = path.join(getUserHome(), '.flowise', 'openai-assistant', fileName)
await downloadFile(cited_file, filePath, dirPath, openAIApiKey)
fileAnnotations.push({
filePath,
fileName
})
} else {
const file_path = (annotation as OpenAI.Beta.Threads.Messages.MessageContentText.Text.FilePath).file_path
if (file_path) {
const cited_file = await openai.files.retrieve(file_path.file_id)
// eslint-disable-next-line no-useless-escape
const fileName = cited_file.filename.split(/[\/\\]/).pop() ?? cited_file.filename
filePath = path.join(getUserHome(), '.flowise', 'openai-assistant', fileName)
await downloadFile(cited_file, filePath, dirPath, openAIApiKey)
fileAnnotations.push({
filePath,
fileName
})
}
}
// Replace the text with a footnote
message_content.value = message_content.value.replace(`${annotation.text}`, `${filePath}`)
}
returnVal += message_content.value
} else {
returnVal += content.text.value
}
} else { } else {
const content = assistantMessages[0].content[i] as MessageContentImageFile const content = assistantMessages[0].content[i] as MessageContentImageFile
const fileId = content.image_file.file_id const fileId = content.image_file.file_id
@ -271,7 +371,8 @@ class OpenAIAssistant_Agents implements INode {
return { return {
text: returnVal, text: returnVal,
usedTools, usedTools,
assistant: { assistantId: openAIAssistantId, threadId, runId: runThread.id, messages: messageData } fileAnnotations,
assistant: { assistantId: openAIAssistantId, threadId, runId: runThreadId, messages: messageData }
} }
} catch (error) { } catch (error) {
throw new Error(error) throw new Error(error)

View File

@ -30,6 +30,7 @@ export interface IChatMessage {
chatflowid: string chatflowid: string
sourceDocuments?: string sourceDocuments?: string
usedTools?: string usedTools?: string
fileAnnotations?: string
chatType: string chatType: string
chatId: string chatId: string
memoryType?: string memoryType?: string

View File

@ -23,6 +23,9 @@ export class ChatMessage implements IChatMessage {
@Column({ nullable: true, type: 'text' }) @Column({ nullable: true, type: 'text' })
usedTools?: string usedTools?: string
@Column({ nullable: true, type: 'text' })
fileAnnotations?: string
@Column() @Column()
chatType: string chatType: string

View File

@ -0,0 +1,12 @@
import { MigrationInterface, QueryRunner } from 'typeorm'
export class AddFileAnnotationsToChatMessage1700271021237 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
const columnExists = await queryRunner.hasColumn('chat_message', 'fileAnnotations')
if (!columnExists) queryRunner.query(`ALTER TABLE \`chat_message\` ADD COLUMN \`fileAnnotations\` TEXT;`)
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE \`chat_message\` DROP COLUMN \`fileAnnotations\`;`)
}
}

View File

@ -8,6 +8,7 @@ import { AddAnalytic1694432361423 } from './1694432361423-AddAnalytic'
import { AddChatHistory1694658767766 } from './1694658767766-AddChatHistory' import { AddChatHistory1694658767766 } from './1694658767766-AddChatHistory'
import { AddAssistantEntity1699325775451 } from './1699325775451-AddAssistantEntity' import { AddAssistantEntity1699325775451 } from './1699325775451-AddAssistantEntity'
import { AddUsedToolsToChatMessage1699481607341 } from './1699481607341-AddUsedToolsToChatMessage' import { AddUsedToolsToChatMessage1699481607341 } from './1699481607341-AddUsedToolsToChatMessage'
import { AddFileAnnotationsToChatMessage1700271021237 } from './1700271021237-AddFileAnnotationsToChatMessage'
export const mysqlMigrations = [ export const mysqlMigrations = [
Init1693840429259, Init1693840429259,
@ -19,5 +20,6 @@ export const mysqlMigrations = [
AddAnalytic1694432361423, AddAnalytic1694432361423,
AddChatHistory1694658767766, AddChatHistory1694658767766,
AddAssistantEntity1699325775451, AddAssistantEntity1699325775451,
AddUsedToolsToChatMessage1699481607341 AddUsedToolsToChatMessage1699481607341,
AddFileAnnotationsToChatMessage1700271021237
] ]

View File

@ -6,6 +6,6 @@ export class AddUsedToolsToChatMessage1699481607341 implements MigrationInterfac
} }
public async down(queryRunner: QueryRunner): Promise<void> { public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "chat_flow" DROP COLUMN "usedTools";`) await queryRunner.query(`ALTER TABLE "chat_message" DROP COLUMN "usedTools";`)
} }
} }

View File

@ -0,0 +1,11 @@
import { MigrationInterface, QueryRunner } from 'typeorm'
export class AddFileAnnotationsToChatMessage1700271021237 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "chat_message" ADD COLUMN IF NOT EXISTS "fileAnnotations" TEXT;`)
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "chat_message" DROP COLUMN "fileAnnotations";`)
}
}

View File

@ -8,6 +8,7 @@ import { AddAnalytic1694432361423 } from './1694432361423-AddAnalytic'
import { AddChatHistory1694658756136 } from './1694658756136-AddChatHistory' import { AddChatHistory1694658756136 } from './1694658756136-AddChatHistory'
import { AddAssistantEntity1699325775451 } from './1699325775451-AddAssistantEntity' import { AddAssistantEntity1699325775451 } from './1699325775451-AddAssistantEntity'
import { AddUsedToolsToChatMessage1699481607341 } from './1699481607341-AddUsedToolsToChatMessage' import { AddUsedToolsToChatMessage1699481607341 } from './1699481607341-AddUsedToolsToChatMessage'
import { AddFileAnnotationsToChatMessage1700271021237 } from './1700271021237-AddFileAnnotationsToChatMessage'
export const postgresMigrations = [ export const postgresMigrations = [
Init1693891895163, Init1693891895163,
@ -19,5 +20,6 @@ export const postgresMigrations = [
AddAnalytic1694432361423, AddAnalytic1694432361423,
AddChatHistory1694658756136, AddChatHistory1694658756136,
AddAssistantEntity1699325775451, AddAssistantEntity1699325775451,
AddUsedToolsToChatMessage1699481607341 AddUsedToolsToChatMessage1699481607341,
AddFileAnnotationsToChatMessage1700271021237
] ]

View File

@ -0,0 +1,20 @@
import { MigrationInterface, QueryRunner } from 'typeorm'
export class AddFileAnnotationsToChatMessage1700271021237 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`CREATE TABLE "temp_chat_message" ("id" varchar PRIMARY KEY NOT NULL, "role" varchar NOT NULL, "chatflowid" varchar NOT NULL, "content" text NOT NULL, "sourceDocuments" text, "usedTools" text, "fileAnnotations" text, "createdDate" datetime NOT NULL DEFAULT (datetime('now')), "chatType" VARCHAR NOT NULL DEFAULT 'INTERNAL', "chatId" VARCHAR NOT NULL, "memoryType" VARCHAR, "sessionId" VARCHAR);`
)
await queryRunner.query(
`INSERT INTO "temp_chat_message" ("id", "role", "chatflowid", "content", "sourceDocuments", "usedTools", "createdDate", "chatType", "chatId", "memoryType", "sessionId") SELECT "id", "role", "chatflowid", "content", "sourceDocuments", "usedTools", "createdDate", "chatType", "chatId", "memoryType", "sessionId" FROM "chat_message";`
)
await queryRunner.query(`DROP TABLE "chat_message";`)
await queryRunner.query(`ALTER TABLE "temp_chat_message" RENAME TO "chat_message";`)
await queryRunner.query(`CREATE INDEX "IDX_e574527322272fd838f4f0f3d3" ON "chat_message" ("chatflowid") ;`)
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`DROP TABLE IF EXISTS "temp_chat_message";`)
await queryRunner.query(`ALTER TABLE "chat_message" DROP COLUMN "fileAnnotations";`)
}
}

View File

@ -8,6 +8,7 @@ import { AddAnalytic1694432361423 } from './1694432361423-AddAnalytic'
import { AddChatHistory1694657778173 } from './1694657778173-AddChatHistory' import { AddChatHistory1694657778173 } from './1694657778173-AddChatHistory'
import { AddAssistantEntity1699325775451 } from './1699325775451-AddAssistantEntity' import { AddAssistantEntity1699325775451 } from './1699325775451-AddAssistantEntity'
import { AddUsedToolsToChatMessage1699481607341 } from './1699481607341-AddUsedToolsToChatMessage' import { AddUsedToolsToChatMessage1699481607341 } from './1699481607341-AddUsedToolsToChatMessage'
import { AddFileAnnotationsToChatMessage1700271021237 } from './1700271021237-AddFileAnnotationsToChatMessage'
export const sqliteMigrations = [ export const sqliteMigrations = [
Init1693835579790, Init1693835579790,
@ -19,5 +20,6 @@ export const sqliteMigrations = [
AddAnalytic1694432361423, AddAnalytic1694432361423,
AddChatHistory1694657778173, AddChatHistory1694657778173,
AddAssistantEntity1699325775451, AddAssistantEntity1699325775451,
AddUsedToolsToChatMessage1699481607341 AddUsedToolsToChatMessage1699481607341,
AddFileAnnotationsToChatMessage1700271021237
] ]

View File

@ -138,6 +138,7 @@ export class App {
'/api/v1/node-icon/', '/api/v1/node-icon/',
'/api/v1/components-credentials-icon/', '/api/v1/components-credentials-icon/',
'/api/v1/chatflows-streaming', '/api/v1/chatflows-streaming',
'/api/v1/openai-assistants-file',
'/api/v1/ip' '/api/v1/ip'
] ]
this.app.use((req, res, next) => { this.app.use((req, res, next) => {
@ -782,8 +783,8 @@ export class App {
await openai.beta.assistants.update(assistantDetails.id, { await openai.beta.assistants.update(assistantDetails.id, {
name: assistantDetails.name, name: assistantDetails.name,
description: assistantDetails.description, description: assistantDetails.description ?? '',
instructions: assistantDetails.instructions, instructions: assistantDetails.instructions ?? '',
model: assistantDetails.model, model: assistantDetails.model,
tools: filteredTools, tools: filteredTools,
file_ids: uniqWith( file_ids: uniqWith(
@ -952,7 +953,7 @@ export class App {
const results = await this.AppDataSource.getRepository(Assistant).delete({ id: req.params.id }) const results = await this.AppDataSource.getRepository(Assistant).delete({ id: req.params.id })
await openai.beta.assistants.del(assistantDetails.id) if (req.query.isDeleteBoth) await openai.beta.assistants.del(assistantDetails.id)
return res.json(results) return res.json(results)
} catch (error: any) { } catch (error: any) {
@ -961,6 +962,14 @@ export class App {
} }
}) })
// Download file from assistant
this.app.post('/api/v1/openai-assistants-file', async (req: Request, res: Response) => {
const filePath = path.join(getUserHome(), '.flowise', 'openai-assistant', req.body.fileName)
res.setHeader('Content-Disposition', 'attachment; filename=' + path.basename(filePath))
const fileStream = fs.createReadStream(filePath)
fileStream.pipe(res)
})
// ---------------------------------------- // ----------------------------------------
// Configuration // Configuration
// ---------------------------------------- // ----------------------------------------
@ -1499,6 +1508,7 @@ export class App {
} }
if (result?.sourceDocuments) apiMessage.sourceDocuments = JSON.stringify(result.sourceDocuments) if (result?.sourceDocuments) apiMessage.sourceDocuments = JSON.stringify(result.sourceDocuments)
if (result?.usedTools) apiMessage.usedTools = JSON.stringify(result.usedTools) if (result?.usedTools) apiMessage.usedTools = JSON.stringify(result.usedTools)
if (result?.fileAnnotations) apiMessage.fileAnnotations = JSON.stringify(result.fileAnnotations)
await this.addChatMessage(apiMessage) await this.addChatMessage(apiMessage)
logger.debug(`[server]: Finished running ${nodeToExecuteData.label} (${nodeToExecuteData.id})`) logger.debug(`[server]: Finished running ${nodeToExecuteData.label} (${nodeToExecuteData.id})`)

View File

@ -12,7 +12,8 @@ const createNewAssistant = (body) => client.post(`/assistants`, body)
const updateAssistant = (id, body) => client.put(`/assistants/${id}`, body) const updateAssistant = (id, body) => client.put(`/assistants/${id}`, body)
const deleteAssistant = (id) => client.delete(`/assistants/${id}`) const deleteAssistant = (id, isDeleteBoth) =>
isDeleteBoth ? client.delete(`/assistants/${id}?isDeleteBoth=true`) : client.delete(`/assistants/${id}`)
export default { export default {
getAllAssistants, getAllAssistants,

View File

@ -7,6 +7,7 @@ import rehypeMathjax from 'rehype-mathjax'
import rehypeRaw from 'rehype-raw' import rehypeRaw from 'rehype-raw'
import remarkGfm from 'remark-gfm' import remarkGfm from 'remark-gfm'
import remarkMath from 'remark-math' import remarkMath from 'remark-math'
import axios from 'axios'
// material-ui // material-ui
import { import {
@ -28,7 +29,7 @@ import DatePicker from 'react-datepicker'
import robotPNG from 'assets/images/robot.png' import robotPNG from 'assets/images/robot.png'
import userPNG from 'assets/images/account.png' import userPNG from 'assets/images/account.png'
import msgEmptySVG from 'assets/images/message_empty.svg' import msgEmptySVG from 'assets/images/message_empty.svg'
import { IconFileExport, IconEraser, IconX } from '@tabler/icons' import { IconFileExport, IconEraser, IconX, IconDownload } from '@tabler/icons'
// Project import // Project import
import { MemoizedReactMarkdown } from 'ui-component/markdown/MemoizedReactMarkdown' import { MemoizedReactMarkdown } from 'ui-component/markdown/MemoizedReactMarkdown'
@ -48,6 +49,7 @@ import useConfirm from 'hooks/useConfirm'
// Utils // Utils
import { isValidURL, removeDuplicateURL } from 'utils/genericHelper' import { isValidURL, removeDuplicateURL } from 'utils/genericHelper'
import useNotifier from 'utils/useNotifier' import useNotifier from 'utils/useNotifier'
import { baseURL } from 'store/constant'
import { enqueueSnackbar as enqueueSnackbarAction, closeSnackbar as closeSnackbarAction } from 'store/actions' import { enqueueSnackbar as enqueueSnackbarAction, closeSnackbar as closeSnackbarAction } from 'store/actions'
@ -130,6 +132,7 @@ const ViewMessagesDialog = ({ show, dialogProps, onCancel }) => {
} }
if (chatmsg.sourceDocuments) msg.sourceDocuments = JSON.parse(chatmsg.sourceDocuments) if (chatmsg.sourceDocuments) msg.sourceDocuments = JSON.parse(chatmsg.sourceDocuments)
if (chatmsg.usedTools) msg.usedTools = JSON.parse(chatmsg.usedTools) if (chatmsg.usedTools) msg.usedTools = JSON.parse(chatmsg.usedTools)
if (chatmsg.fileAnnotations) msg.fileAnnotations = JSON.parse(chatmsg.fileAnnotations)
if (!Object.prototype.hasOwnProperty.call(obj, chatPK)) { if (!Object.prototype.hasOwnProperty.call(obj, chatPK)) {
obj[chatPK] = { obj[chatPK] = {
@ -253,6 +256,7 @@ const ViewMessagesDialog = ({ show, dialogProps, onCancel }) => {
} }
if (chatmsg.sourceDocuments) obj.sourceDocuments = JSON.parse(chatmsg.sourceDocuments) if (chatmsg.sourceDocuments) obj.sourceDocuments = JSON.parse(chatmsg.sourceDocuments)
if (chatmsg.usedTools) obj.usedTools = JSON.parse(chatmsg.usedTools) if (chatmsg.usedTools) obj.usedTools = JSON.parse(chatmsg.usedTools)
if (chatmsg.fileAnnotations) obj.fileAnnotations = JSON.parse(chatmsg.fileAnnotations)
loadedMessages.push(obj) loadedMessages.push(obj)
} }
@ -318,6 +322,26 @@ const ViewMessagesDialog = ({ show, dialogProps, onCancel }) => {
window.open(data, '_blank') window.open(data, '_blank')
} }
const downloadFile = async (fileAnnotation) => {
try {
const response = await axios.post(
`${baseURL}/api/v1/openai-assistants-file`,
{ fileName: fileAnnotation.fileName },
{ responseType: 'blob' }
)
const blob = new Blob([response.data], { type: response.headers['content-type'] })
const downloadUrl = window.URL.createObjectURL(blob)
const link = document.createElement('a')
link.href = downloadUrl
link.download = fileAnnotation.fileName
document.body.appendChild(link)
link.click()
link.remove()
} catch (error) {
console.error('Download failed:', error)
}
}
const onSourceDialogClick = (data, title) => { const onSourceDialogClick = (data, title) => {
setSourceDialogProps({ data, title }) setSourceDialogProps({ data, title })
setSourceDialogOpen(true) setSourceDialogOpen(true)
@ -648,6 +672,30 @@ const ViewMessagesDialog = ({ show, dialogProps, onCancel }) => {
{message.message} {message.message}
</MemoizedReactMarkdown> </MemoizedReactMarkdown>
</div> </div>
{message.fileAnnotations && (
<div style={{ display: 'block', flexDirection: 'row', width: '100%' }}>
{message.fileAnnotations.map((fileAnnotation, index) => {
return (
<Button
sx={{
fontSize: '0.85rem',
textTransform: 'none',
mb: 1,
mr: 1
}}
key={index}
variant='outlined'
onClick={() => downloadFile(fileAnnotation)}
endIcon={
<IconDownload color={theme.palette.primary.main} />
}
>
{fileAnnotation.fileName}
</Button>
)
})}
</div>
)}
{message.sourceDocuments && ( {message.sourceDocuments && (
<div style={{ display: 'block', flexDirection: 'row', width: '100%' }}> <div style={{ display: 'block', flexDirection: 'row', width: '100%' }}>
{removeDuplicateURL(message).map((source, index) => { {removeDuplicateURL(message).map((source, index) => {

View File

@ -9,12 +9,12 @@ import { Box, Typography, Button, IconButton, Dialog, DialogActions, DialogConte
import { StyledButton } from 'ui-component/button/StyledButton' import { StyledButton } from 'ui-component/button/StyledButton'
import { TooltipWithParser } from 'ui-component/tooltip/TooltipWithParser' import { TooltipWithParser } from 'ui-component/tooltip/TooltipWithParser'
import ConfirmDialog from 'ui-component/dialog/ConfirmDialog'
import { Dropdown } from 'ui-component/dropdown/Dropdown' import { Dropdown } from 'ui-component/dropdown/Dropdown'
import { MultiDropdown } from 'ui-component/dropdown/MultiDropdown' import { MultiDropdown } from 'ui-component/dropdown/MultiDropdown'
import CredentialInputHandler from 'views/canvas/CredentialInputHandler' import CredentialInputHandler from 'views/canvas/CredentialInputHandler'
import { File } from 'ui-component/file/File' import { File } from 'ui-component/file/File'
import { BackdropLoader } from 'ui-component/loading/BackdropLoader' import { BackdropLoader } from 'ui-component/loading/BackdropLoader'
import DeleteConfirmDialog from './DeleteConfirmDialog'
// Icons // Icons
import { IconX } from '@tabler/icons' import { IconX } from '@tabler/icons'
@ -23,7 +23,6 @@ import { IconX } from '@tabler/icons'
import assistantsApi from 'api/assistants' import assistantsApi from 'api/assistants'
// Hooks // Hooks
import useConfirm from 'hooks/useConfirm'
import useApi from 'hooks/useApi' import useApi from 'hooks/useApi'
// utils // utils
@ -71,14 +70,8 @@ const assistantAvailableModels = [
const AssistantDialog = ({ show, dialogProps, onCancel, onConfirm }) => { const AssistantDialog = ({ show, dialogProps, onCancel, onConfirm }) => {
const portalElement = document.getElementById('portal') const portalElement = document.getElementById('portal')
const dispatch = useDispatch()
// ==============================|| Snackbar ||============================== //
useNotifier() useNotifier()
const { confirm } = useConfirm() const dispatch = useDispatch()
const enqueueSnackbar = (...args) => dispatch(enqueueSnackbarAction(...args)) const enqueueSnackbar = (...args) => dispatch(enqueueSnackbarAction(...args))
const closeSnackbar = (...args) => dispatch(closeSnackbarAction(...args)) const closeSnackbar = (...args) => dispatch(closeSnackbarAction(...args))
@ -97,6 +90,8 @@ const AssistantDialog = ({ show, dialogProps, onCancel, onConfirm }) => {
const [assistantFiles, setAssistantFiles] = useState([]) const [assistantFiles, setAssistantFiles] = useState([])
const [uploadAssistantFiles, setUploadAssistantFiles] = useState('') const [uploadAssistantFiles, setUploadAssistantFiles] = useState('')
const [loading, setLoading] = useState(false) const [loading, setLoading] = useState(false)
const [deleteDialogOpen, setDeleteDialogOpen] = useState(false)
const [deleteDialogProps, setDeleteDialogProps] = useState({})
useEffect(() => { useEffect(() => {
if (show) dispatch({ type: SHOW_CANVAS_DIALOG }) if (show) dispatch({ type: SHOW_CANVAS_DIALOG })
@ -123,20 +118,7 @@ const AssistantDialog = ({ show, dialogProps, onCancel, onConfirm }) => {
useEffect(() => { useEffect(() => {
if (getAssistantObjApi.data) { if (getAssistantObjApi.data) {
setOpenAIAssistantId(getAssistantObjApi.data.id) syncData(getAssistantObjApi.data)
setAssistantName(getAssistantObjApi.data.name)
setAssistantDesc(getAssistantObjApi.data.description)
setAssistantModel(getAssistantObjApi.data.model)
setAssistantInstructions(getAssistantObjApi.data.instructions)
setAssistantFiles(getAssistantObjApi.data.files ?? [])
let tools = []
if (getAssistantObjApi.data.tools && getAssistantObjApi.data.tools.length) {
for (const tool of getAssistantObjApi.data.tools) {
tools.push(tool.type)
}
}
setAssistantTools(tools)
} }
}, [getAssistantObjApi.data]) }, [getAssistantObjApi.data])
@ -199,6 +181,23 @@ const AssistantDialog = ({ show, dialogProps, onCancel, onConfirm }) => {
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
}, [dialogProps]) }, [dialogProps])
const syncData = (data) => {
setOpenAIAssistantId(data.id)
setAssistantName(data.name)
setAssistantDesc(data.description)
setAssistantModel(data.model)
setAssistantInstructions(data.instructions)
setAssistantFiles(data.files ?? [])
let tools = []
if (data.tools && data.tools.length) {
for (const tool of data.tools) {
tools.push(tool.type)
}
}
setAssistantTools(tools)
}
const addNewAssistant = async () => { const addNewAssistant = async () => {
setLoading(true) setLoading(true)
try { try {
@ -309,41 +308,17 @@ const AssistantDialog = ({ show, dialogProps, onCancel, onConfirm }) => {
} }
} }
const deleteAssistant = async () => { const onSyncClick = async () => {
const confirmPayload = { setLoading(true)
title: `Delete Assistant`, try {
description: `Delete Assistant ${assistantName}?`, const getResp = await assistantsApi.getAssistantObj(openAIAssistantId, assistantCredential)
confirmButtonName: 'Delete', if (getResp.data) {
cancelButtonName: 'Cancel' syncData(getResp.data)
}
const isConfirmed = await confirm(confirmPayload)
if (isConfirmed) {
try {
const delResp = await assistantsApi.deleteAssistant(assistantId)
if (delResp.data) {
enqueueSnackbar({
message: 'Assistant deleted',
options: {
key: new Date().getTime() + Math.random(),
variant: 'success',
action: (key) => (
<Button style={{ color: 'white' }} onClick={() => closeSnackbar(key)}>
<IconX />
</Button>
)
}
})
onConfirm()
}
} catch (error) {
const errorData = error.response.data || `${error.response.status}: ${error.response.statusText}`
enqueueSnackbar({ enqueueSnackbar({
message: `Failed to delete Assistant: ${errorData}`, message: 'Assistant successfully synced!',
options: { options: {
key: new Date().getTime() + Math.random(), key: new Date().getTime() + Math.random(),
variant: 'error', variant: 'success',
persist: true,
action: (key) => ( action: (key) => (
<Button style={{ color: 'white' }} onClick={() => closeSnackbar(key)}> <Button style={{ color: 'white' }} onClick={() => closeSnackbar(key)}>
<IconX /> <IconX />
@ -351,8 +326,71 @@ const AssistantDialog = ({ show, dialogProps, onCancel, onConfirm }) => {
) )
} }
}) })
onCancel()
} }
setLoading(false)
} catch (error) {
const errorData = error.response.data || `${error.response.status}: ${error.response.statusText}`
enqueueSnackbar({
message: `Failed to sync Assistant: ${errorData}`,
options: {
key: new Date().getTime() + Math.random(),
variant: 'error',
persist: true,
action: (key) => (
<Button style={{ color: 'white' }} onClick={() => closeSnackbar(key)}>
<IconX />
</Button>
)
}
})
setLoading(false)
}
}
const onDeleteClick = () => {
setDeleteDialogProps({
title: `Delete Assistant`,
description: `Delete Assistant ${assistantName}?`,
cancelButtonName: 'Cancel'
})
setDeleteDialogOpen(true)
}
const deleteAssistant = async (isDeleteBoth) => {
setDeleteDialogOpen(false)
try {
const delResp = await assistantsApi.deleteAssistant(assistantId, isDeleteBoth)
if (delResp.data) {
enqueueSnackbar({
message: 'Assistant deleted',
options: {
key: new Date().getTime() + Math.random(),
variant: 'success',
action: (key) => (
<Button style={{ color: 'white' }} onClick={() => closeSnackbar(key)}>
<IconX />
</Button>
)
}
})
onConfirm()
}
} catch (error) {
const errorData = error.response.data || `${error.response.status}: ${error.response.statusText}`
enqueueSnackbar({
message: `Failed to delete Assistant: ${errorData}`,
options: {
key: new Date().getTime() + Math.random(),
variant: 'error',
persist: true,
action: (key) => (
<Button style={{ color: 'white' }} onClick={() => closeSnackbar(key)}>
<IconX />
</Button>
)
}
})
onCancel()
} }
} }
@ -578,7 +616,12 @@ const AssistantDialog = ({ show, dialogProps, onCancel, onConfirm }) => {
</DialogContent> </DialogContent>
<DialogActions> <DialogActions>
{dialogProps.type === 'EDIT' && ( {dialogProps.type === 'EDIT' && (
<StyledButton color='error' variant='contained' onClick={() => deleteAssistant()}> <StyledButton color='secondary' variant='contained' onClick={() => onSyncClick()}>
Sync
</StyledButton>
)}
{dialogProps.type === 'EDIT' && (
<StyledButton color='error' variant='contained' onClick={() => onDeleteClick()}>
Delete Delete
</StyledButton> </StyledButton>
)} )}
@ -590,7 +633,13 @@ const AssistantDialog = ({ show, dialogProps, onCancel, onConfirm }) => {
{dialogProps.confirmButtonName} {dialogProps.confirmButtonName}
</StyledButton> </StyledButton>
</DialogActions> </DialogActions>
<ConfirmDialog /> <DeleteConfirmDialog
show={deleteDialogOpen}
dialogProps={deleteDialogProps}
onCancel={() => setDeleteDialogOpen(false)}
onDelete={() => deleteAssistant()}
onDeleteBoth={() => deleteAssistant(true)}
/>
{loading && <BackdropLoader open={loading} />} {loading && <BackdropLoader open={loading} />}
</Dialog> </Dialog>
) : null ) : null

View File

@ -0,0 +1,47 @@
import { createPortal } from 'react-dom'
import PropTypes from 'prop-types'
import { Button, Dialog, DialogContent, DialogTitle } from '@mui/material'
import { StyledButton } from 'ui-component/button/StyledButton'
const DeleteConfirmDialog = ({ show, dialogProps, onCancel, onDelete, onDeleteBoth }) => {
const portalElement = document.getElementById('portal')
const component = show ? (
<Dialog
fullWidth
maxWidth='xs'
open={show}
onClose={onCancel}
aria-labelledby='alert-dialog-title'
aria-describedby='alert-dialog-description'
>
<DialogTitle sx={{ fontSize: '1rem' }} id='alert-dialog-title'>
{dialogProps.title}
</DialogTitle>
<DialogContent>
<span>{dialogProps.description}</span>
<div style={{ display: 'flex', flexDirection: 'column', marginTop: 20 }}>
<StyledButton sx={{ mb: 1 }} color='orange' variant='contained' onClick={onDelete}>
Delete only from Flowise
</StyledButton>
<StyledButton sx={{ mb: 1 }} color='error' variant='contained' onClick={onDeleteBoth}>
Delete from both OpenAI and Flowise
</StyledButton>
<Button onClick={onCancel}>{dialogProps.cancelButtonName}</Button>
</div>
</DialogContent>
</Dialog>
) : null
return createPortal(component, portalElement)
}
DeleteConfirmDialog.propTypes = {
show: PropTypes.bool,
dialogProps: PropTypes.object,
onDeleteBoth: PropTypes.func,
onDelete: PropTypes.func,
onCancel: PropTypes.func
}
export default DeleteConfirmDialog

View File

@ -7,10 +7,11 @@ import rehypeMathjax from 'rehype-mathjax'
import rehypeRaw from 'rehype-raw' import rehypeRaw from 'rehype-raw'
import remarkGfm from 'remark-gfm' import remarkGfm from 'remark-gfm'
import remarkMath from 'remark-math' import remarkMath from 'remark-math'
import axios from 'axios'
import { CircularProgress, OutlinedInput, Divider, InputAdornment, IconButton, Box, Chip } from '@mui/material' import { CircularProgress, OutlinedInput, Divider, InputAdornment, IconButton, Box, Chip, Button } from '@mui/material'
import { useTheme } from '@mui/material/styles' import { useTheme } from '@mui/material/styles'
import { IconSend } from '@tabler/icons' import { IconSend, IconDownload } from '@tabler/icons'
// project import // project import
import { CodeBlock } from 'ui-component/markdown/CodeBlock' import { CodeBlock } from 'ui-component/markdown/CodeBlock'
@ -139,7 +140,13 @@ export const ChatMessage = ({ open, chatflowid, isDialog }) => {
setMessages((prevMessages) => [ setMessages((prevMessages) => [
...prevMessages, ...prevMessages,
{ message: text, sourceDocuments: data?.sourceDocuments, usedTools: data?.usedTools, type: 'apiMessage' } {
message: text,
sourceDocuments: data?.sourceDocuments,
usedTools: data?.usedTools,
fileAnnotations: data?.fileAnnotations,
type: 'apiMessage'
}
]) ])
} }
@ -170,6 +177,26 @@ export const ChatMessage = ({ open, chatflowid, isDialog }) => {
} }
} }
const downloadFile = async (fileAnnotation) => {
try {
const response = await axios.post(
`${baseURL}/api/v1/openai-assistants-file`,
{ fileName: fileAnnotation.fileName },
{ responseType: 'blob' }
)
const blob = new Blob([response.data], { type: response.headers['content-type'] })
const downloadUrl = window.URL.createObjectURL(blob)
const link = document.createElement('a')
link.href = downloadUrl
link.download = fileAnnotation.fileName
document.body.appendChild(link)
link.click()
link.remove()
} catch (error) {
console.error('Download failed:', error)
}
}
// Get chatmessages successful // Get chatmessages successful
useEffect(() => { useEffect(() => {
if (getChatmessageApi.data?.length) { if (getChatmessageApi.data?.length) {
@ -183,6 +210,7 @@ export const ChatMessage = ({ open, chatflowid, isDialog }) => {
} }
if (message.sourceDocuments) obj.sourceDocuments = JSON.parse(message.sourceDocuments) if (message.sourceDocuments) obj.sourceDocuments = JSON.parse(message.sourceDocuments)
if (message.usedTools) obj.usedTools = JSON.parse(message.usedTools) if (message.usedTools) obj.usedTools = JSON.parse(message.usedTools)
if (message.fileAnnotations) obj.fileAnnotations = JSON.parse(message.fileAnnotations)
return obj return obj
}) })
setMessages((prevMessages) => [...prevMessages, ...loadedMessages]) setMessages((prevMessages) => [...prevMessages, ...loadedMessages])
@ -331,6 +359,23 @@ export const ChatMessage = ({ open, chatflowid, isDialog }) => {
{message.message} {message.message}
</MemoizedReactMarkdown> </MemoizedReactMarkdown>
</div> </div>
{message.fileAnnotations && (
<div style={{ display: 'block', flexDirection: 'row', width: '100%' }}>
{message.fileAnnotations.map((fileAnnotation, index) => {
return (
<Button
sx={{ fontSize: '0.85rem', textTransform: 'none', mb: 1 }}
key={index}
variant='outlined'
onClick={() => downloadFile(fileAnnotation)}
endIcon={<IconDownload color={theme.palette.primary.main} />}
>
{fileAnnotation.fileName}
</Button>
)
})}
</div>
)}
{message.sourceDocuments && ( {message.sourceDocuments && (
<div style={{ display: 'block', flexDirection: 'row', width: '100%' }}> <div style={{ display: 'block', flexDirection: 'row', width: '100%' }}>
{removeDuplicateURL(message).map((source, index) => { {removeDuplicateURL(message).map((source, index) => {