Skip to content

Commit c89f3dc

Browse files
committed
fix: OpenAI models not supporting system messages
* Consistently handle OpenAI LLMs that don't support system messages in OpenAI model provider * Remove O1-specific agent as it isn't needed anymore * Update to latest OpenAI library * Change "system" to "developer" message role (https://cdn.openai.com/spec/model-spec-2024-05-08.html#follow-the-chain-of-command) * Add 'o1-mini' model Fixes #14686
1 parent 02f4192 commit c89f3dc

File tree

7 files changed

+64
-93
lines changed

7 files changed

+64
-93
lines changed

packages/ai-chat/src/browser/ai-chat-frontend-module.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import { aiChatPreferences } from './ai-chat-preferences';
3737
import { AICustomAgentsFrontendApplicationContribution } from './custom-agent-frontend-application-contribution';
3838
import { FrontendChatServiceImpl } from './frontend-chat-service';
3939
import { CustomAgentFactory } from './custom-agent-factory';
40-
import { O1ChatAgent } from '../common/o1-chat-agent';
4140

4241
export default new ContainerModule(bind => {
4342
bindContributionProvider(bind, Agent);
@@ -64,10 +63,6 @@ export default new ContainerModule(bind => {
6463
bind(Agent).toService(OrchestratorChatAgent);
6564
bind(ChatAgent).toService(OrchestratorChatAgent);
6665

67-
bind(O1ChatAgent).toSelf().inSingletonScope();
68-
bind(Agent).toService(O1ChatAgent);
69-
bind(ChatAgent).toService(O1ChatAgent);
70-
7166
bind(UniversalChatAgent).toSelf().inSingletonScope();
7267
bind(Agent).toService(UniversalChatAgent);
7368
bind(ChatAgent).toService(UniversalChatAgent);

packages/ai-chat/src/common/o1-chat-agent.ts

Lines changed: 0 additions & 51 deletions
This file was deleted.

packages/ai-openai/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"@theia/workspace": "1.57.0",
99
"minimatch": "^5.1.0",
1010
"tslib": "^2.6.2",
11-
"openai": "^4.55.7",
11+
"openai": "^4.77.0",
1212
"@theia/ai-core": "1.57.0"
1313
},
1414
"publishConfig": {

packages/ai-openai/src/browser/openai-frontend-application-contribution.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,4 @@ export class OpenAiFrontendApplicationContribution implements FrontendApplicatio
159159
}
160160
}
161161

162-
const openAIModelsWithDisabledStreaming = ['o1-preview'];
162+
const openAIModelsWithDisabledStreaming = ['o1-preview', 'o1-mini'];

packages/ai-openai/src/browser/openai-preferences.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export const OpenAiPreferencesSchema: PreferenceSchema = {
3434
type: 'array',
3535
description: 'Official OpenAI models to use',
3636
title: AI_CORE_PREFERENCES_TITLE,
37-
default: ['gpt-4o', 'gpt-4o-2024-08-06', 'gpt-4o-2024-05-13', 'gpt-4o-mini', 'gpt-4-turbo', 'gpt-4', 'gpt-3.5-turbo', 'o1-preview'],
37+
default: ['gpt-4o', 'gpt-4o-2024-08-06', 'gpt-4o-2024-05-13', 'gpt-4o-mini', 'gpt-4-turbo', 'gpt-4', 'gpt-3.5-turbo', 'o1-preview', 'o1-mini'],
3838
items: {
3939
type: 'string'
4040
}

packages/ai-openai/src/node/openai-language-model.ts

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,6 @@ import { ChatCompletionMessageParam } from 'openai/resources';
3131

3232
export const OpenAiModelIdentifier = Symbol('OpenAiModelIdentifier');
3333

34-
function toOpenAIMessage(message: LanguageModelRequestMessage): ChatCompletionMessageParam {
35-
return {
36-
role: toOpenAiRole(message),
37-
content: message.query || ''
38-
};
39-
}
40-
41-
function toOpenAiRole(message: LanguageModelRequestMessage): 'system' | 'user' | 'assistant' {
42-
switch (message.actor) {
43-
case 'system':
44-
return 'system';
45-
case 'ai':
46-
return 'assistant';
47-
default:
48-
return 'user';
49-
}
50-
}
51-
5234
export class OpenAiModel implements LanguageModel {
5335

5436
/**
@@ -93,7 +75,7 @@ export class OpenAiModel implements LanguageModel {
9375
if (tools) {
9476
runner = openai.beta.chat.completions.runTools({
9577
model: this.model,
96-
messages: request.messages.map(toOpenAIMessage),
78+
messages: request.messages.map(this.toOpenAIMessage.bind(this)),
9779
stream: true,
9880
tools: tools,
9981
tool_choice: 'auto',
@@ -102,7 +84,7 @@ export class OpenAiModel implements LanguageModel {
10284
} else {
10385
runner = openai.beta.chat.completions.stream({
10486
model: this.model,
105-
messages: request.messages.map(toOpenAIMessage),
87+
messages: request.messages.map(this.toOpenAIMessage.bind(this)),
10688
stream: true,
10789
...settings
10890
});
@@ -161,7 +143,7 @@ export class OpenAiModel implements LanguageModel {
161143
const settings = this.getSettings(request);
162144
const response = await openai.chat.completions.create({
163145
model: this.model,
164-
messages: request.messages.map(toOpenAIMessage),
146+
messages: request.messages.map(this.toOpenAIMessage.bind(this)),
165147
...settings
166148
});
167149

@@ -172,6 +154,24 @@ export class OpenAiModel implements LanguageModel {
172154
};
173155
}
174156

157+
protected toOpenAIMessage(message: LanguageModelRequestMessage): ChatCompletionMessageParam {
158+
return {
159+
role: this.toOpenAiRole(message),
160+
content: message.query || ''
161+
};
162+
}
163+
164+
protected toOpenAiRole(message: LanguageModelRequestMessage): 'developer' | 'user' | 'assistant' {
165+
switch (message.actor) {
166+
case 'system':
167+
return this.supportsDeveloperMessage() ? 'developer' : 'user';
168+
case 'ai':
169+
return 'assistant';
170+
default:
171+
return 'user';
172+
}
173+
}
174+
175175
protected isNonStreamingModel(_model: string): boolean {
176176
return !this.enableStreaming;
177177
}
@@ -185,12 +185,19 @@ export class OpenAiModel implements LanguageModel {
185185
].includes(this.model);
186186
}
187187

188+
protected supportsDeveloperMessage(): boolean {
189+
return ![
190+
'o1-preview',
191+
'o1-mini'
192+
].includes(this.model);
193+
}
194+
188195
protected async handleStructuredOutputRequest(openai: OpenAI, request: LanguageModelRequest): Promise<LanguageModelParsedResponse> {
189196
const settings = this.getSettings(request);
190197
// TODO implement tool support for structured output (parse() seems to require different tool format)
191198
const result = await openai.beta.chat.completions.parse({
192199
model: this.model,
193-
messages: request.messages.map(toOpenAIMessage),
200+
messages: request.messages.map(this.toOpenAIMessage.bind(this)),
194201
response_format: request.response_format,
195202
...settings
196203
});

yarn.lock

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5213,11 +5213,6 @@ detect-libc@^1.0.3:
52135213
resolved "https://registry.yarnpkg.com/detect-libc/-/detect-libc-1.0.3.tgz#fa137c4bd698edf55cd5cd02ac559f91a4c4ba9b"
52145214
integrity sha512-pGjwhsmsp4kL2RTz08wcOlGN83otlqHeD/Z5T8GXZB+/YcpQ/dgo+lbU8ZsGxV0HIvqqxo9l7mqYwyYMD9bKDg==
52155215

5216-
detect-libc@^2.0.0, detect-libc@^2.0.1:
5217-
version "2.0.2"
5218-
resolved "https://registry.yarnpkg.com/detect-libc/-/detect-libc-2.0.2.tgz#8ccf2ba9315350e1241b88d0ac3b0e1fbd99605d"
5219-
integrity sha512-UX6sGumvvqSaXgdKGUsgZWqcUyIXZ/vZTrlRT/iobiKhGL0zL4d3osHj3uqllWJK+i+sixDS/3COVEOFbupFyw==
5220-
52215216
detect-libc@^2.0.0, detect-libc@^2.0.1, detect-libc@^2.0.2:
52225217
version "2.0.3"
52235218
resolved "https://registry.yarnpkg.com/detect-libc/-/detect-libc-2.0.3.tgz#f0cd503b40f9939b894697d19ad50895e30cf700"
@@ -9806,10 +9801,10 @@ open@^8.0.0, open@^8.4.0:
98069801
is-docker "^2.1.1"
98079802
is-wsl "^2.2.0"
98089803

9809-
openai@^4.55.7:
9810-
version "4.55.7"
9811-
resolved "https://registry.yarnpkg.com/openai/-/openai-4.55.7.tgz#2bba4ae9224ad205c0d087d1412fe95421397dff"
9812-
integrity sha512-I2dpHTINt0Zk+Wlns6KzkKu77MmNW3VfIIQf5qYziEUI6t7WciG1zTobfKqdPzBmZi3TTM+3DtjPumxQdcvzwA==
9804+
openai@^4.77.0:
9805+
version "4.77.0"
9806+
resolved "https://registry.yarnpkg.com/openai/-/openai-4.77.0.tgz#228f2d43ffa79ae9d8b5d4155e965da82e5ac330"
9807+
integrity sha512-WWacavtns/7pCUkOWvQIjyOfcdr9X+9n9Vvb0zFeKVDAqwCMDHB+iSr24SVaBAhplvSG6JrRXFpcNM9gWhOGIw==
98139808
dependencies:
98149809
"@types/node" "^18.11.18"
98159810
"@types/node-fetch" "^2.6.4"
@@ -11896,7 +11891,16 @@ string-argv@^0.1.1:
1189611891
resolved "https://registry.yarnpkg.com/string-argv/-/string-argv-0.1.2.tgz#c5b7bc03fb2b11983ba3a72333dd0559e77e4738"
1189711892
integrity sha512-mBqPGEOMNJKXRo7z0keX0wlAhbBAjilUdPW13nN0PecVryZxdHIeM7TqbsSUA7VYuS00HGC6mojP7DlQzfa9ZA==
1189811893

11899-
"string-width-cjs@npm:string-width@^4.2.0", "string-width@^1.0.2 || 2 || 3 || 4", string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
11894+
"string-width-cjs@npm:string-width@^4.2.0":
11895+
version "4.2.3"
11896+
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
11897+
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
11898+
dependencies:
11899+
emoji-regex "^8.0.0"
11900+
is-fullwidth-code-point "^3.0.0"
11901+
strip-ansi "^6.0.1"
11902+
11903+
"string-width@^1.0.2 || 2 || 3 || 4", string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
1190011904
version "4.2.3"
1190111905
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
1190211906
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
@@ -11982,7 +11986,7 @@ string_decoder@~1.1.1:
1198211986
dependencies:
1198311987
safe-buffer "~5.1.0"
1198411988

11985-
"strip-ansi-cjs@npm:strip-ansi@^6.0.1", strip-ansi@^6.0.0, strip-ansi@^6.0.1:
11989+
"strip-ansi-cjs@npm:strip-ansi@^6.0.1":
1198611990
version "6.0.1"
1198711991
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
1198811992
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
@@ -12003,6 +12007,13 @@ strip-ansi@^5.2.0:
1200312007
dependencies:
1200412008
ansi-regex "^4.1.0"
1200512009

12010+
strip-ansi@^6.0.0, strip-ansi@^6.0.1:
12011+
version "6.0.1"
12012+
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
12013+
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
12014+
dependencies:
12015+
ansi-regex "^5.0.1"
12016+
1200612017
strip-ansi@^7.0.1:
1200712018
version "7.1.0"
1200812019
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-7.1.0.tgz#d5b6568ca689d8561370b0707685d22434faff45"
@@ -13288,7 +13299,7 @@ [email protected]:
1328813299
resolved "https://registry.yarnpkg.com/workerpool/-/workerpool-6.2.1.tgz#46fc150c17d826b86a008e5a4508656777e9c343"
1328913300
integrity sha512-ILEIE97kDZvF9Wb9f6h5aXK4swSlKGUcOEGiIYb2OOu/IrDU9iwj0fD//SsA6E5ibwJxpEvhullJY4Sl4GcpAw==
1329013301

13291-
"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0", wrap-ansi@^7.0.0:
13302+
"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0":
1329213303
version "7.0.0"
1329313304
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
1329413305
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
@@ -13306,6 +13317,15 @@ wrap-ansi@^6.0.1, wrap-ansi@^6.2.0:
1330613317
string-width "^4.1.0"
1330713318
strip-ansi "^6.0.0"
1330813319

13320+
wrap-ansi@^7.0.0:
13321+
version "7.0.0"
13322+
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
13323+
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
13324+
dependencies:
13325+
ansi-styles "^4.0.0"
13326+
string-width "^4.1.0"
13327+
strip-ansi "^6.0.0"
13328+
1330913329
wrap-ansi@^8.1.0:
1331013330
version "8.1.0"
1331113331
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-8.1.0.tgz#56dc22368ee570face1b49819975d9b9a5ead214"

0 commit comments

Comments
 (0)