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

Feature/add disable file download and regex to remove citation
This commit is contained in:
Henry Heng
2023-11-28 16:19:24 +00:00
committed by GitHub
@@ -41,6 +41,15 @@ class OpenAIAssistant_Agents implements INode {
name: 'tools', name: 'tools',
type: 'Tool', type: 'Tool',
list: true list: true
},
{
label: 'Disable File Download',
name: 'disableFileDownload',
type: 'boolean',
description:
'Messages can contain text, images, or files. In some cases, you may want to prevent others from downloading the files. Learn more from OpenAI File Annotation <a target="_blank" href="https://platform.openai.com/docs/assistants/how-it-works/managing-threads-and-messages">docs</a>',
optional: true,
additionalParams: true
} }
] ]
} }
@@ -119,6 +128,8 @@ class OpenAIAssistant_Agents implements INode {
const selectedAssistantId = nodeData.inputs?.selectedAssistant as string const selectedAssistantId = nodeData.inputs?.selectedAssistant as string
const appDataSource = options.appDataSource as DataSource const appDataSource = options.appDataSource as DataSource
const databaseEntities = options.databaseEntities as IDatabaseEntity const databaseEntities = options.databaseEntities as IDatabaseEntity
const disableFileDownload = nodeData.inputs?.disableFileDownload as boolean
let tools = nodeData.inputs?.tools let tools = nodeData.inputs?.tools
tools = flatten(tools) tools = flatten(tools)
const formattedTools = tools?.map((tool: any) => formatToOpenAIAssistantTool(tool)) ?? [] const formattedTools = tools?.map((tool: any) => formatToOpenAIAssistantTool(tool)) ?? []
@@ -310,7 +321,7 @@ class OpenAIAssistant_Agents implements INode {
const dirPath = path.join(getUserHome(), '.flowise', 'openai-assistant') const dirPath = path.join(getUserHome(), '.flowise', 'openai-assistant')
// Iterate over the annotations and add footnotes // Iterate over the annotations
for (let index = 0; index < annotations.length; index++) { for (let index = 0; index < annotations.length; index++) {
const annotation = annotations[index] const annotation = annotations[index]
let filePath = '' let filePath = ''
@@ -323,11 +334,13 @@ class OpenAIAssistant_Agents implements INode {
// eslint-disable-next-line no-useless-escape // eslint-disable-next-line no-useless-escape
const fileName = cited_file.filename.split(/[\/\\]/).pop() ?? cited_file.filename const fileName = cited_file.filename.split(/[\/\\]/).pop() ?? cited_file.filename
filePath = path.join(getUserHome(), '.flowise', 'openai-assistant', fileName) filePath = path.join(getUserHome(), '.flowise', 'openai-assistant', fileName)
if (!disableFileDownload) {
await downloadFile(cited_file, filePath, dirPath, openAIApiKey) await downloadFile(cited_file, filePath, dirPath, openAIApiKey)
fileAnnotations.push({ fileAnnotations.push({
filePath, filePath,
fileName fileName
}) })
}
} else { } else {
const file_path = (annotation as OpenAI.Beta.Threads.Messages.MessageContentText.Text.FilePath).file_path const file_path = (annotation as OpenAI.Beta.Threads.Messages.MessageContentText.Text.FilePath).file_path
if (file_path) { if (file_path) {
@@ -335,6 +348,7 @@ class OpenAIAssistant_Agents implements INode {
// eslint-disable-next-line no-useless-escape // eslint-disable-next-line no-useless-escape
const fileName = cited_file.filename.split(/[\/\\]/).pop() ?? cited_file.filename const fileName = cited_file.filename.split(/[\/\\]/).pop() ?? cited_file.filename
filePath = path.join(getUserHome(), '.flowise', 'openai-assistant', fileName) filePath = path.join(getUserHome(), '.flowise', 'openai-assistant', fileName)
if (!disableFileDownload) {
await downloadFile(cited_file, filePath, dirPath, openAIApiKey) await downloadFile(cited_file, filePath, dirPath, openAIApiKey)
fileAnnotations.push({ fileAnnotations.push({
filePath, filePath,
@@ -342,6 +356,7 @@ class OpenAIAssistant_Agents implements INode {
}) })
} }
} }
}
// Replace the text with a footnote // Replace the text with a footnote
message_content.value = message_content.value.replace(`${annotation.text}`, `${filePath}`) message_content.value = message_content.value.replace(`${annotation.text}`, `${filePath}`)
@@ -351,6 +366,9 @@ class OpenAIAssistant_Agents implements INode {
} else { } else {
returnVal += content.text.value returnVal += content.text.value
} }
const lenticularBracketRegex = /【[^】]*】/g
returnVal = returnVal.replace(lenticularBracketRegex, '')
} 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