Skip to content

Commit

Permalink
ability to determine which OpenAI Assistant tool to use based on uplo…
Browse files Browse the repository at this point in the history
…aded files
  • Loading branch information
OvidijusParsiunas committed May 27, 2024
1 parent 69c043a commit e2c6a87
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
14 changes: 12 additions & 2 deletions component/src/services/openAI/openAIAssistantIO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,20 @@ export class OpenAIAssistantIO extends DirectServiceIO {
const processedMessage = MessageLimitUtils.getCharacterLimitMessages(pMessages, totalMessagesMaxCharLength)[0];
// https://platform.openai.com/docs/api-reference/messages/createMessage
if (uploadedFiles && uploadedFiles.length > 0) {
if (this.filesToolType === 'file_search') {
let toolType = this.filesToolType;
// OpenAI also allows multiple tool types for a message but not doing it here as we don't process multiple responses.
// https://platform.openai.com/docs/assistants/migration/what-has-changed
// "tools": [
// { "type": "file_search" },
// { "type": "code_interpreter" }
// ]
if (typeof this.filesToolType === 'function') {
toolType = this.filesToolType(uploadedFiles.map(({name}) => name));
}
if (toolType === 'file_search') {
return OpenAIAssistantIO.processAttachmentsMessage(processedMessage, uploadedFiles, 'file_search');
}
if (this.filesToolType === 'code_interpreter') {
if (toolType === 'code_interpreter') {
return OpenAIAssistantIO.processAttachmentsMessage(processedMessage, uploadedFiles, 'code_interpreter');
}
const imageMessage = OpenAIAssistantIO.processImageMessage(processedMessage, uploadedFiles);
Expand Down
5 changes: 4 additions & 1 deletion component/src/types/openAI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,17 @@ export interface OpenAINewAssistant {
};
}

export type FileToolTypes = 'code_interpreter' | 'file_search' | 'images';

// https://platform.openai.com/docs/api-reference/assistants
export interface OpenAIAssistant {
assistant_id?: string;
thread_id?: string;
load_thread_history?: boolean;
new_assistant?: OpenAINewAssistant;
// if image is uploaded and this is undefined, it will default to images
files_tool_type?: 'code_interpreter' | 'file_search' | 'images'; // images can be used without a file tool type
// images can be used without a file tool type
files_tool_type?: FileToolTypes | ((fileNames: string[]) => FileToolTypes);
function_handler?: AssistantFunctionHandler;
}

Expand Down

0 comments on commit e2c6a87

Please sign in to comment.