forked from open-webui/open-webui
Add validation for chatGPT imports, stopping any breaking issues when imports are corrupted/not compatible
This commit is contained in:
parent
f079cb6b56
commit
4d85e2cb15
1 changed files with 57 additions and 17 deletions
|
@ -206,25 +206,32 @@ const convertOpenAIMessages = (convo) => {
|
||||||
const mapping = convo['mapping'];
|
const mapping = convo['mapping'];
|
||||||
const messages = [];
|
const messages = [];
|
||||||
let currentId = '';
|
let currentId = '';
|
||||||
|
let lastId = null;
|
||||||
|
|
||||||
for (let message_id in mapping) {
|
for (let message_id in mapping) {
|
||||||
const message = mapping[message_id];
|
const message = mapping[message_id];
|
||||||
currentId = message_id;
|
currentId = message_id;
|
||||||
if (message['message'] == null || message['message']['content']['parts'][0] == '') {
|
try {
|
||||||
|
if (messages.length == 0 && (message['message'] == null ||
|
||||||
|
(message['message']['content']['parts']?.[0] == '' && message['message']['content']['text'] == null))) {
|
||||||
// Skip chat messages with no content
|
// Skip chat messages with no content
|
||||||
continue;
|
continue;
|
||||||
} else {
|
} else {
|
||||||
const new_chat = {
|
const new_chat = {
|
||||||
id: message_id,
|
id: message_id,
|
||||||
parentId: messages.length > 0 && message['parent'] in mapping ? message['parent'] : null,
|
parentId: lastId,
|
||||||
childrenIds: message['children'] || [],
|
childrenIds: message['children'] || [],
|
||||||
role: message['message']?.['author']?.['role'] !== 'user' ? 'assistant' : 'user',
|
role: message['message']?.['author']?.['role'] !== 'user' ? 'assistant' : 'user',
|
||||||
content: message['message']?.['content']?.['parts']?.[0] || '',
|
content: message['message']?.['content']?.['parts']?.[0] || message['message']?.['content']?.['text'] || '',
|
||||||
model: 'gpt-3.5-turbo',
|
model: 'gpt-3.5-turbo',
|
||||||
done: true,
|
done: true,
|
||||||
context: null
|
context: null
|
||||||
};
|
};
|
||||||
messages.push(new_chat);
|
messages.push(new_chat);
|
||||||
|
lastId = currentId;
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.log("Error with", message, "\nError:", error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -245,13 +252,45 @@ const convertOpenAIMessages = (convo) => {
|
||||||
return chat;
|
return chat;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const validateChat = (chat) => {
|
||||||
|
// Because ChatGPT sometimes has features we can't use like DALL-E or migh have corrupted messages, need to validate
|
||||||
|
const messages = chat.messages;
|
||||||
|
|
||||||
|
// Check if messages array is empty
|
||||||
|
if (messages.length === 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Last message's children should be an empty array
|
||||||
|
const lastMessage = messages[messages.length - 1];
|
||||||
|
if (lastMessage.childrenIds.length !== 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// First message's parent should be null
|
||||||
|
const firstMessage = messages[0];
|
||||||
|
if (firstMessage.parentId !== null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Every message's content should be a string
|
||||||
|
for (let message of messages) {
|
||||||
|
if (typeof message.content !== 'string') {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
export const convertOpenAIChats = (_chats) => {
|
export const convertOpenAIChats = (_chats) => {
|
||||||
// Create a list of dictionaries with each conversation from import
|
// Create a list of dictionaries with each conversation from import
|
||||||
const chats = [];
|
const chats = [];
|
||||||
|
let failed = 0;
|
||||||
for (let convo of _chats) {
|
for (let convo of _chats) {
|
||||||
const chat = convertOpenAIMessages(convo);
|
const chat = convertOpenAIMessages(convo);
|
||||||
|
|
||||||
if (Object.keys(chat.history.messages).length > 0) {
|
if (validateChat(chat)) {
|
||||||
chats.push({
|
chats.push({
|
||||||
id: convo['id'],
|
id: convo['id'],
|
||||||
user_id: '',
|
user_id: '',
|
||||||
|
@ -259,7 +298,8 @@ export const convertOpenAIChats = (_chats) => {
|
||||||
chat: chat,
|
chat: chat,
|
||||||
timestamp: convo['timestamp']
|
timestamp: convo['timestamp']
|
||||||
});
|
});
|
||||||
|
} else { failed ++}
|
||||||
}
|
}
|
||||||
}
|
console.log(failed, "Conversations could not be imported");
|
||||||
return chats;
|
return chats;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue