Skip to content

Fix max tokens and add model options #436

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/api/api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { ShareGPTSubmitBodyInterface } from '@type/api';
import { ConfigInterface, MessageInterface } from '@type/chat';
import { isAzureEndpoint } from '@utils/api';
import { modelMaxToken } from '@constants/chat';
import countTokens from '@utils/messageUtils';

export const getChatCompletion = async (
endpoint: string,
Expand Down Expand Up @@ -32,13 +34,16 @@ export const getChatCompletion = async (
}
}

const tokenCount = countTokens(messages, config.model);
const maxTokens = Math.min(config.max_tokens, modelMaxToken[config.model] - tokenCount-1);

const response = await fetch(endpoint, {
method: 'POST',
headers,
body: JSON.stringify({
messages,
...config,
max_tokens: undefined,
max_tokens: maxTokens,
}),
});
if (!response.ok) throw new Error(await response.text());
Expand Down Expand Up @@ -77,13 +82,16 @@ export const getChatCompletionStream = async (
}
}

const tokenCount = countTokens(messages, config.model);
const maxTokens = Math.min(config.max_tokens, modelMaxToken[config.model] - tokenCount-1);

const response = await fetch(endpoint, {
method: 'POST',
headers,
body: JSON.stringify({
messages,
...config,
max_tokens: undefined,
max_tokens: maxTokens,
stream: true,
}),
});
Expand Down
16 changes: 12 additions & 4 deletions src/constants/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ export const modelOptions: ModelOptions[] = [
'gpt-3.5-turbo-16k',
'gpt-4',
'gpt-4-32k',
// 'gpt-3.5-turbo-0301',
// 'gpt-4-0314',
// 'gpt-4-32k-0314',
'gpt-4-0314',
'gpt-4-32k-0314',
'gpt-3.5-turbo-0301',
'gpt-3.5-turbo-0613',
];

export const defaultModel = 'gpt-3.5-turbo';
Expand Down Expand Up @@ -100,7 +101,14 @@ export const _defaultChatConfig: ConfigInterface = {
top_p: 1,
frequency_penalty: 0,
};

export const _generateTitleConfig:ConfigInterface = {
model: defaultModel,
max_tokens: 200,//Only 6 words in title according to the prompt
temperature: 0,//for stable titles
presence_penalty: 0,
top_p: 1,
frequency_penalty: 0,
};
export const generateDefaultChat = (
title?: string,
folder?: string
Expand Down
5 changes: 1 addition & 4 deletions src/types/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,7 @@ export interface Folder {
color?: string;
}

export type ModelOptions = 'gpt-4' | 'gpt-4-32k' | 'gpt-3.5-turbo' | 'gpt-3.5-turbo-16k' ;
// | 'gpt-3.5-turbo-0301';
// | 'gpt-4-0314'
// | 'gpt-4-32k-0314'
export type ModelOptions = 'gpt-4' | 'gpt-4-32k' | 'gpt-3.5-turbo' | 'gpt-3.5-turbo-16k' | 'gpt-3.5-turbo-0301'|'gpt-3.5-turbo-0613'| 'gpt-4-0314'| 'gpt-4-32k-0314'

export type TotalTokenUsed = {
[model in ModelOptions]?: {
Expand Down