Skip to content

Commit

Permalink
🧠 feat: Implement O1 Model Support for Max Tokens Handling (danny-avi…
Browse files Browse the repository at this point in the history
  • Loading branch information
danny-avila authored and olivierhub committed Oct 14, 2024
1 parent 24750b3 commit d36ef94
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions api/app/clients/OpenAIClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ class OpenAIClient extends BaseClient {

/** @type {OpenAIUsageMetadata | undefined} */
this.usage;
/** @type {boolean|undefined} */
this.isO1Model;
}

// TODO: PluginsClient calls this 3x, unneeded
Expand Down Expand Up @@ -99,6 +101,8 @@ class OpenAIClient extends BaseClient {
this.options.modelOptions,
);

this.isO1Model = /\bo1\b/i.test(this.modelOptions.model);

this.defaultVisionModel = this.options.visionModel ?? 'gpt-4-vision-preview';
if (typeof this.options.attachments?.then === 'function') {
this.options.attachments.then((attachments) => this.checkVisionRequest(attachments));
Expand Down Expand Up @@ -546,8 +550,7 @@ class OpenAIClient extends BaseClient {
promptPrefix = this.augmentedPrompt + promptPrefix;
}

const isO1Model = /\bo1\b/i.test(this.modelOptions.model);
if (promptPrefix && !isO1Model) {
if (promptPrefix && this.isO1Model !== true) {
promptPrefix = `Instructions:\n${replaceSpecialVars(promptPrefix.trim())}`;
instructions = {
role: 'system',
Expand Down Expand Up @@ -576,7 +579,7 @@ class OpenAIClient extends BaseClient {
};

/** EXPERIMENTAL */
if (promptPrefix && isO1Model) {
if (promptPrefix && this.isO1Model === true) {
const lastUserMessageIndex = payload.findLastIndex((message) => message.role === 'user');
if (lastUserMessageIndex !== -1) {
payload[
Expand Down Expand Up @@ -1228,6 +1231,11 @@ ${convo}
opts.defaultHeaders = { ...opts.defaultHeaders, 'api-key': this.apiKey };
}

if (this.isO1Model === true && modelOptions.max_tokens != null) {
modelOptions.max_completion_tokens = modelOptions.max_tokens;
delete modelOptions.max_tokens;
}

if (process.env.OPENAI_ORGANIZATION) {
opts.organization = process.env.OPENAI_ORGANIZATION;
}
Expand Down

0 comments on commit d36ef94

Please sign in to comment.