Skip to content

Commit

Permalink
fix for OpenAI Assistants v2 message annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
OvidijusParsiunas committed Jun 10, 2024
1 parent 49f511a commit d3ab5f6
Showing 1 changed file with 25 additions and 22 deletions.
47 changes: 25 additions & 22 deletions component/src/services/openAI/utils/openAIAssistantUtils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {OpenAIAssistantData, OpenAIAssistantContent, OpenAIAssistantMessagesResult} from '../../../types/openAIResult';
import {FileMessageUtils} from '../../../views/chat/messages/fileMessageUtils';
import {MessageFileType, MessageFile} from '../../../types/messageFile';
import {Messages} from '../../../views/chat/messages/messages';
import {RequestUtils} from '../../../utils/HTTP/requestUtils';
Expand Down Expand Up @@ -81,14 +80,37 @@ export class OpenAIAssistantUtils {
return parts[parts.length - 1];
}

// prettier-ignore
private static async getFilesAndNewText(io: ServiceIO, fileDetails: FileDetails,
role?: string, content?: OpenAIAssistantContent) {
let files: MessageFile[] | undefined;
if (fileDetails.length > 0) {
files = await OpenAIAssistantUtils.getFiles(io, fileDetails);
if (content?.text?.value) {
files.forEach((file, index) => {
if (!file.src) return;
const path = fileDetails[index].path;
if (content?.text?.value && path) {
content.text.value = content.text.value.replace(path, file.src);
}
});
}
}
// not displaying a separate file if annotated
return content?.text?.value ? {text: content.text.value, role} : {files, role};
}

// Noticed an issue where text contains a sandbox hyperlink to a csv, but no annotation provided
// To reproduce use the following text:
// give example data for a csv and create a suitable bar chart for it with a link
// Don't think it can be fixed and it is something on OpenAI side of things
// prettier-ignore
private static getFileDetails(lastMessage: OpenAIAssistantData, content?: OpenAIAssistantContent) {
const fileDetails: FileDetails = [];
if (content?.text?.value) {
lastMessage.content.forEach((content) => {
content.text?.annotations?.forEach((annotation) => {
if (annotation.text && annotation.text.startsWith('sandbox:')
&& !FileMessageUtils.isImageFile({src: annotation.text}) && annotation.file_path?.file_id) {
if (annotation.text && annotation.text.startsWith('sandbox:') && annotation.file_path?.file_id) {
fileDetails.push({
path: annotation.text,
fileId: annotation.file_path.file_id,
Expand All @@ -106,25 +128,6 @@ export class OpenAIAssistantUtils {
return fileDetails;
}

// prettier-ignore
private static async getFilesAndNewText(io: ServiceIO, fileDetails: FileDetails,
role?: string, content?: OpenAIAssistantContent) {
let files: MessageFile[] | undefined;
if (fileDetails.length > 0) {
files = await OpenAIAssistantUtils.getFiles(io, fileDetails);
if (content?.text?.value) {
files.forEach((file, index) => {
if (!file.src) return;
const path = fileDetails[index].path;
if (content?.text?.value && path) {
content.text.value = content.text.value.replace(path, file.src);
}
});
}
}
return {files, text: content?.text?.value, role};
}

public static async getFilesAndText(io: ServiceIO, message: OpenAIAssistantData, content?: OpenAIAssistantContent) {
const fileDetails = OpenAIAssistantUtils.getFileDetails(message, content);
// gets files and replaces hyperlinks with base64 file encodings
Expand Down

0 comments on commit d3ab5f6

Please sign in to comment.