Skip to content

Commit

Permalink
Merge pull request #1302 from FlowiseAI/feature/OpenAI-Assistant
Browse files Browse the repository at this point in the history
Feature/add disable file download and regex to remove citation
  • Loading branch information
HenryHengZJ authored Nov 28, 2023
2 parents b6a9d44 + 7362dec commit 28daa5b
Showing 1 changed file with 29 additions and 11 deletions.
40 changes: 29 additions & 11 deletions packages/components/nodes/agents/OpenAIAssistant/OpenAIAssistant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ class OpenAIAssistant_Agents implements INode {
name: 'tools',
type: 'Tool',
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
}
]
}
Expand Down Expand Up @@ -119,6 +128,8 @@ class OpenAIAssistant_Agents implements INode {
const selectedAssistantId = nodeData.inputs?.selectedAssistant as string
const appDataSource = options.appDataSource as DataSource
const databaseEntities = options.databaseEntities as IDatabaseEntity
const disableFileDownload = nodeData.inputs?.disableFileDownload as boolean

let tools = nodeData.inputs?.tools
tools = flatten(tools)
const formattedTools = tools?.map((tool: any) => formatToOpenAIAssistantTool(tool)) ?? []
Expand Down Expand Up @@ -310,7 +321,7 @@ class OpenAIAssistant_Agents implements INode {

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++) {
const annotation = annotations[index]
let filePath = ''
Expand All @@ -323,23 +334,27 @@ class OpenAIAssistant_Agents implements INode {
// 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
})
if (!disableFileDownload) {
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
})
if (!disableFileDownload) {
await downloadFile(cited_file, filePath, dirPath, openAIApiKey)
fileAnnotations.push({
filePath,
fileName
})
}
}
}

Expand All @@ -351,6 +366,9 @@ class OpenAIAssistant_Agents implements INode {
} else {
returnVal += content.text.value
}

const lenticularBracketRegex = /[^]*/g
returnVal = returnVal.replace(lenticularBracketRegex, '')
} else {
const content = assistantMessages[0].content[i] as MessageContentImageFile
const fileId = content.image_file.file_id
Expand Down

0 comments on commit 28daa5b

Please sign in to comment.