Skip to content

Commit

Permalink
Fix content and title generation
Browse files Browse the repository at this point in the history
  • Loading branch information
AI-Maria committed Jun 2, 2024
1 parent 0806865 commit 4883290
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 15 deletions.
70 changes: 58 additions & 12 deletions src/api/api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {modelTypes} from '@constants/chat';
import { ShareGPTSubmitBodyInterface } from '@type/api';
import { ConfigInterface, ImageContentInterface, MessageInterface, ModelOptions } from '@type/chat';
import { ConfigInterface, ImageContentInterface, MessageInterface, MessageInterfaceText, ModelOptions, TextContentInterface } from '@type/chat';
import { isAzureEndpoint } from '@utils/api';


Expand Down Expand Up @@ -45,14 +46,36 @@ export const getChatCompletion = async (
}
}

let body: BodyInit;

// make content: string if model support only text
if (modelTypes[config.model] == 'text') {
// Convert messages to MessageInterfaceText
const textMessages: MessageInterfaceText[] = messages.map(message => {
// Find the first text and ignore images
const textContent = message.content.find(content => content.type === 'text') as TextContentInterface;
return {
role: message.role,
content: textContent?.text || ''
};
});
body = JSON.stringify({
messages: textMessages,
...config,
max_tokens: undefined,
});
} else {
body = JSON.stringify({
messages,
...config,
max_tokens: undefined,
});
}

const response = await fetch(endpoint, {
method: 'POST',
headers,
body: JSON.stringify({
messages,
...config,
max_tokens: undefined,
}),
body: body,
});
if (!response.ok) throw new Error(await response.text());

Expand Down Expand Up @@ -98,15 +121,38 @@ export const getChatCompletionStream = async (
}
}

let body: BodyInit;

// make content: string if model support only text
if (modelTypes[config.model] == 'text') {
// Convert messages to MessageInterfaceText
const textMessages: MessageInterfaceText[] = messages.map(message => {
// Find the first text and ignore images
const textContent = message.content.find(content => content.type === 'text') as TextContentInterface;
return {
role: message.role,
content: textContent?.text || ''
};
});
body = JSON.stringify({
messages: textMessages,
...config,
max_tokens: config.max_tokens,
stream: true,
});
} else {
body = JSON.stringify({
messages,
...config,
max_tokens: config.max_tokens,
stream: true,
});
}

const response = await fetch(endpoint, {
method: 'POST',
headers,
body: JSON.stringify({
messages,
...config,
max_tokens: config.max_tokens,
stream: true,
}),
body: body,
});
if (response.status === 404 || response.status === 405) {
const text = await response.text();
Expand Down
2 changes: 1 addition & 1 deletion src/constants/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const modelMaxToken = {
'gpt-4-32k-0613': 32768,
'gpt-4-1106-preview': 128000,
'gpt-4-0125-preview': 128000,
'gpt-4-vision-preview': 128000
'gpt-4-vision-preview': 4096
};

export const modelCost = {
Expand Down
17 changes: 15 additions & 2 deletions src/hooks/useSubmit.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import useStore from '@store/store';
import { useTranslation } from 'react-i18next';
import { ChatInterface, MessageInterface, TextContentInterface } from '@type/chat';
import { ChatInterface, ContentInterface, MessageInterface, TextContentInterface } from '@type/chat';
import { getChatCompletion, getChatCompletionStream } from '@api/api';
import { parseEventSource } from '@api/helper';
import { limitMessageTokens, updateTotalTokenUsed } from '@utils/messageUtils';
Expand Down Expand Up @@ -168,17 +168,30 @@ const useSubmit = () => {
currChats &&
!currChats[currentChatIndex]?.titleSet
) {
function extractContentMessages(contents : ContentInterface[]) {
return contents.map(content => {
if (content.type === 'text') {
return content.text;
} else if (content.type === 'image_url') {
return '(image)';
}
return '';
}).join(' ');
}
const messages_length = currChats[currentChatIndex].messages.length;
const assistant_message =
currChats[currentChatIndex].messages[messages_length - 1].content;
const user_message =
currChats[currentChatIndex].messages[messages_length - 2].content;

const assistant_content_string = extractContentMessages(assistant_message);
const user_content_string = extractContentMessages(user_message);

const message: MessageInterface = {
role: 'user',
content: [{
type: 'text',
text: `Generate a title in less than 6 words for the following message (language: ${i18n.language}):\n"""\nUser: ${user_message}\nAssistant: ${assistant_message}\n"""`,
text: `Generate a title in less than 6 words for the following message (language: ${i18n.language}):\n"""\nUser: ${user_content_string}\nAssistant: ${assistant_content_string}\n"""`,
} as TextContentInterface]
};

Expand Down
5 changes: 5 additions & 0 deletions src/types/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ export interface ContentInterface {
type: Content;
}

export interface MessageInterfaceText {
role: Role;
content: String;
}

export interface MessageInterface {
role: Role;
content: ContentInterface[];
Expand Down

0 comments on commit 4883290

Please sign in to comment.