@theia/ai-openai 1.55.0-next.4 → 1.55.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +19 -1
- package/lib/browser/openai-frontend-application-contribution.js +7 -6
- package/lib/browser/openai-frontend-application-contribution.js.map +1 -1
- package/lib/browser/openai-preferences.d.ts.map +1 -1
- package/lib/browser/openai-preferences.js +14 -3
- package/lib/browser/openai-preferences.js.map +1 -1
- package/lib/common/openai-language-models-manager.d.ts +4 -0
- package/lib/common/openai-language-models-manager.d.ts.map +1 -1
- package/lib/node/openai-language-model.d.ts +5 -3
- package/lib/node/openai-language-model.d.ts.map +1 -1
- package/lib/node/openai-language-model.js +27 -6
- package/lib/node/openai-language-model.js.map +1 -1
- package/lib/node/openai-language-models-manager-impl.d.ts.map +1 -1
- package/lib/node/openai-language-models-manager-impl.js +13 -9
- package/lib/node/openai-language-models-manager-impl.js.map +1 -1
- package/package.json +7 -7
- package/src/browser/openai-frontend-application-contribution.ts +7 -5
- package/src/browser/openai-preferences.ts +14 -3
- package/src/common/openai-language-models-manager.ts +4 -0
- package/src/node/openai-language-model.ts +34 -8
- package/src/node/openai-language-models-manager-impl.ts +13 -8
package/README.md
CHANGED
|
@@ -14,7 +14,25 @@
|
|
|
14
14
|
|
|
15
15
|
The `@theia/ai-openai` integrates OpenAI's models with Theia AI.
|
|
16
16
|
The OpenAI API key and the models to use can be configured via preferences.
|
|
17
|
-
Alternatively the OpenAI API key can also be handed in via
|
|
17
|
+
Alternatively the OpenAI API key can also be handed in via the `OPENAI_API_KEY` variable.
|
|
18
|
+
|
|
19
|
+
### Custom models
|
|
20
|
+
|
|
21
|
+
The extension also supports OpenAI compatible models hosted on different end points.
|
|
22
|
+
You can configure the end points via the `ai-features.openAiCustom.customOpenAiModels` preference:
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
{
|
|
26
|
+
model: string
|
|
27
|
+
url: string
|
|
28
|
+
id?: string
|
|
29
|
+
apiKey?: string | true
|
|
30
|
+
}
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
- `model` and `url` are mandatory attributes, indicating the end point and model to use
|
|
34
|
+
- `id` is an optional attribute which is used in the UI to refer to this configuration
|
|
35
|
+
- `apiKey` is either the key to access the API served at the given URL or `true` to use the global OpenAI API key. If not given 'no-key' will be used.
|
|
18
36
|
|
|
19
37
|
## Additional Information
|
|
20
38
|
|
|
@@ -54,7 +54,7 @@ let OpenAiFrontendApplicationContribution = class OpenAiFrontendApplicationContr
|
|
|
54
54
|
const oldModels = createCustomModelDescriptionsFromPreferences(this.prevCustomModels);
|
|
55
55
|
const newModels = createCustomModelDescriptionsFromPreferences(event.newValue);
|
|
56
56
|
const modelsToRemove = oldModels.filter(model => !newModels.some(newModel => newModel.id === model.id));
|
|
57
|
-
const modelsToAddOrUpdate = newModels.filter(newModel => !oldModels.some(model => model.id === newModel.id && model.model === newModel.model && model.url === newModel.url));
|
|
57
|
+
const modelsToAddOrUpdate = newModels.filter(newModel => !oldModels.some(model => model.id === newModel.id && model.model === newModel.model && model.url === newModel.url && model.apiKey === newModel.apiKey));
|
|
58
58
|
this.manager.removeLanguageModels(...modelsToRemove.map(model => model.id));
|
|
59
59
|
this.manager.createOrUpdateLanguageModels(...modelsToAddOrUpdate);
|
|
60
60
|
}
|
|
@@ -77,21 +77,22 @@ exports.OpenAiFrontendApplicationContribution = OpenAiFrontendApplicationContrib
|
|
|
77
77
|
function createOpenAIModelDescription(modelId) {
|
|
78
78
|
return {
|
|
79
79
|
id: `openai/${modelId}`,
|
|
80
|
-
model: modelId
|
|
80
|
+
model: modelId,
|
|
81
|
+
apiKey: true
|
|
81
82
|
};
|
|
82
83
|
}
|
|
83
84
|
function createCustomModelDescriptionsFromPreferences(preferences) {
|
|
84
85
|
return preferences.reduce((acc, pref) => {
|
|
85
|
-
|
|
86
|
-
if (!pref.model || !pref.url) {
|
|
86
|
+
if (!pref.model || !pref.url || typeof pref.model !== 'string' || typeof pref.url !== 'string') {
|
|
87
87
|
return acc;
|
|
88
88
|
}
|
|
89
89
|
return [
|
|
90
90
|
...acc,
|
|
91
91
|
{
|
|
92
|
-
id:
|
|
92
|
+
id: pref.id && typeof pref.id === 'string' ? pref.id : pref.model,
|
|
93
93
|
model: pref.model,
|
|
94
|
-
url: pref.url
|
|
94
|
+
url: pref.url,
|
|
95
|
+
apiKey: typeof pref.apiKey === 'string' || pref.apiKey === true ? pref.apiKey : undefined
|
|
95
96
|
}
|
|
96
97
|
];
|
|
97
98
|
}, []);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openai-frontend-application-contribution.js","sourceRoot":"","sources":["../../src/browser/openai-frontend-application-contribution.ts"],"names":[],"mappings":";AAAA,gFAAgF;AAChF,yCAAyC;AACzC,EAAE;AACF,2EAA2E;AAC3E,mEAAmE;AACnE,wCAAwC;AACxC,EAAE;AACF,4EAA4E;AAC5E,8EAA8E;AAC9E,6EAA6E;AAC7E,yDAAyD;AACzD,uDAAuD;AACvD,EAAE;AACF,gFAAgF;AAChF,gFAAgF;;;;AAEhF,qDAA6F;AAC7F,4DAAkE;AAClE,sCAAgF;AAChF,6DAAwF;AAGjF,IAAM,qCAAqC,GAA3C,MAAM,qCAAqC;IAA3C;QAQH,oEAAoE;QAC1D,eAAU,GAAa,EAAE,CAAC;QAC1B,qBAAgB,GAAsC,EAAE,CAAC;IA0CvE,CAAC;IAxCG,OAAO;QACH,IAAI,CAAC,iBAAiB,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,EAAE;YACnC,MAAM,MAAM,GAAG,IAAI,CAAC,iBAAiB,CAAC,GAAG,CAAS,iCAAY,EAAE,SAAS,CAAC,CAAC;YAC3E,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC;YAE/B,MAAM,MAAM,GAAG,IAAI,CAAC,iBAAiB,CAAC,GAAG,CAAW,gCAAW,EAAE,EAAE,CAAC,CAAC;YACrE,IAAI,CAAC,OAAO,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CAAC;oBACrF,IAAI,CAAC,OAAO,CAAC,4BAA4B,CAAC,GAAG,WAAW,CAAC,GAAG,CAAC,4BAA4B,CAAC,CAAC,CAAC;oBAC5F,IAAI,CAAC,UAAU,GAAG,CAAC,GAAG,KAAK,CAAC,QAAQ,CAAC,CAAC;gBAC1C,CAAC;qBAAM,IAAI,KAAK,CAAC,cAAc,KAAK,0CAAqB,EAAE,CAAC;oBACxD,MAAM,SAAS,GAAG,4CAA4C,CAAC,IAAI,CAAC,gBAAgB,CAAC,CAAC;oBACtF,MAAM,SAAS,GAAG,4CAA4C,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;oBAE/E,MAAM,cAAc,GAAG,SAAS,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,SAAS,CAAC,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,QAAQ,CAAC,EAAE,KAAK,KAAK,CAAC,EAAE,CAAC,CAAC,CAAC;oBACxG,MAAM,mBAAmB,GAAG,SAAS,CAAC,MAAM,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,SAAS,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAC7E,KAAK,CAAC,EAAE,KAAK,QAAQ,CAAC,EAAE,IAAI,KAAK,CAAC,KAAK,KAAK,QAAQ,CAAC,KAAK,IAAI,KAAK,CAAC,GAAG,KAAK,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"openai-frontend-application-contribution.js","sourceRoot":"","sources":["../../src/browser/openai-frontend-application-contribution.ts"],"names":[],"mappings":";AAAA,gFAAgF;AAChF,yCAAyC;AACzC,EAAE;AACF,2EAA2E;AAC3E,mEAAmE;AACnE,wCAAwC;AACxC,EAAE;AACF,4EAA4E;AAC5E,8EAA8E;AAC9E,6EAA6E;AAC7E,yDAAyD;AACzD,uDAAuD;AACvD,EAAE;AACF,gFAAgF;AAChF,gFAAgF;;;;AAEhF,qDAA6F;AAC7F,4DAAkE;AA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AAO,IAAI,CAAC,EAAE,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK;gBACjE,KAAK,EAAE,IAAI,CAAC,KAAK;gBACjB,GAAG,EAAE,IAAI,CAAC,GAAG;gBACb,MAAM,EAAE,OAAO,IAAI,CAAC,MAAM,KAAK,QAAQ,IAAI,IAAI,CAAC,MAAM,KAAK,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS;aAC5F;SACJ,CAAC;IACN,CAAC,EAAE,EAAE,CAAC,CAAC;AACX,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openai-preferences.d.ts","sourceRoot":"","sources":["../../src/browser/openai-preferences.ts"],"names":[],"mappings":"AAgBA,OAAO,EAAE,gBAAgB,EAAE,MAAM,6DAA6D,CAAC;AAG/F,eAAO,MAAM,YAAY,4CAA4C,CAAC;AACtE,eAAO,MAAM,WAAW,oDAAoD,CAAC;AAC7E,eAAO,MAAM,qBAAqB,gDAAgD,CAAC;AAEnF,eAAO,MAAM,uBAAuB,EAAE,
|
|
1
|
+
{"version":3,"file":"openai-preferences.d.ts","sourceRoot":"","sources":["../../src/browser/openai-preferences.ts"],"names":[],"mappings":"AAgBA,OAAO,EAAE,gBAAgB,EAAE,MAAM,6DAA6D,CAAC;AAG/F,eAAO,MAAM,YAAY,4CAA4C,CAAC;AACtE,eAAO,MAAM,WAAW,oDAAoD,CAAC;AAC7E,eAAO,MAAM,qBAAqB,gDAAgD,CAAC;AAEnF,eAAO,MAAM,uBAAuB,EAAE,gBAsDrC,CAAC"}
|
|
@@ -33,7 +33,7 @@ exports.OpenAiPreferencesSchema = {
|
|
|
33
33
|
type: 'array',
|
|
34
34
|
description: 'Official OpenAI models to use',
|
|
35
35
|
title: ai_core_preferences_1.AI_CORE_PREFERENCES_TITLE,
|
|
36
|
-
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'],
|
|
36
|
+
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
37
|
items: {
|
|
38
38
|
type: 'string'
|
|
39
39
|
}
|
|
@@ -42,7 +42,14 @@ exports.OpenAiPreferencesSchema = {
|
|
|
42
42
|
type: 'array',
|
|
43
43
|
title: ai_core_preferences_1.AI_CORE_PREFERENCES_TITLE,
|
|
44
44
|
markdownDescription: 'Integrate custom models compatible with the OpenAI API, for example via `vllm`. The required attributes are `model` and `url`.\
|
|
45
|
-
|
|
45
|
+
\n\
|
|
46
|
+
Optionally, you can\
|
|
47
|
+
\n\
|
|
48
|
+
- specify a unique `id` to identify the custom model in the UI. If none is given `model` will be used as `id`.\
|
|
49
|
+
\n\
|
|
50
|
+
- provide an `apiKey` to access the API served at the given url. Use `true` to indicate the use of the global OpenAI API key.\
|
|
51
|
+
\n\
|
|
52
|
+
Refer to [our documentation](https://theia-ide.org/docs/user_ai/#openai-compatible-models-eg-via-vllm) for more information.',
|
|
46
53
|
default: [],
|
|
47
54
|
items: {
|
|
48
55
|
type: 'object',
|
|
@@ -58,7 +65,11 @@ exports.OpenAiPreferencesSchema = {
|
|
|
58
65
|
id: {
|
|
59
66
|
type: 'string',
|
|
60
67
|
title: 'A unique identifier which is used in the UI to identify the custom model',
|
|
61
|
-
}
|
|
68
|
+
},
|
|
69
|
+
apiKey: {
|
|
70
|
+
type: ['string', 'boolean'],
|
|
71
|
+
title: 'Either the key to access the API served at the given url or `true` to use the global OpenAI API key',
|
|
72
|
+
},
|
|
62
73
|
}
|
|
63
74
|
}
|
|
64
75
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openai-preferences.js","sourceRoot":"","sources":["../../src/browser/openai-preferences.ts"],"names":[],"mappings":";AAAA,gFAAgF;AAChF,yCAAyC;AACzC,EAAE;AACF,2EAA2E;AAC3E,mEAAmE;AACnE,wCAAwC;AACxC,EAAE;AACF,4EAA4E;AAC5E,8EAA8E;AAC9E,6EAA6E;AAC7E,yDAAyD;AACzD,uDAAuD;AACvD,EAAE;AACF,gFAAgF;AAChF,gFAAgF;;;AAGhF,wFAA2F;AAE9E,QAAA,YAAY,GAAG,yCAAyC,CAAC;AACzD,QAAA,WAAW,GAAG,iDAAiD,CAAC;AAChE,QAAA,qBAAqB,GAAG,6CAA6C,CAAC;AAEtE,QAAA,uBAAuB,GAAqB;IACrD,IAAI,EAAE,QAAQ;IACd,UAAU,EAAE;QACR,CAAC,oBAAY,CAAC,EAAE;YACZ,IAAI,EAAE,QAAQ;YACd,mBAAmB,EAAE;iHACgF;YACrG,KAAK,EAAE,+CAAyB;SACnC;QACD,CAAC,mBAAW,CAAC,EAAE;YACX,IAAI,EAAE,OAAO;YACb,WAAW,EAAE,+BAA+B;YAC5C,KAAK,EAAE,+CAAyB;YAChC,OAAO,EAAE,CAAC,QAAQ,EAAE,mBAAmB,EAAE,mBAAmB,EAAE,aAAa,EAAE,aAAa,EAAE,OAAO,EAAE,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"openai-preferences.js","sourceRoot":"","sources":["../../src/browser/openai-preferences.ts"],"names":[],"mappings":";AAAA,gFAAgF;AAChF,yCAAyC;AACzC,EAAE;AACF,2EAA2E;AAC3E,mEAAmE;AACnE,wCAAwC;AACxC,EAAE;AACF,4EAA4E;AAC5E,8EAA8E;AAC9E,6EAA6E;AAC7E,yDAAyD;AACzD,uDAAuD;AACvD,EAAE;AACF,gFAAgF;AAChF,gFAAgF;;;AAGhF,wFAA2F;AAE9E,QAAA,YAAY,GAAG,yCAAyC,CAAC;AACzD,QAAA,WAAW,GAAG,iDAAiD,CAAC;AAChE,QAAA,qBAAqB,GAAG,6CAA6C,CAAC;AAEtE,QAAA,uBAAuB,GAAqB;IACrD,IAAI,EAAE,QAAQ;IACd,UAAU,EAAE;QACR,CAAC,oBAAY,CAAC,EAAE;YACZ,IAAI,EAAE,QAAQ;YACd,mBAAmB,EAAE;iHACgF;YACrG,KAAK,EAAE,+CAAyB;SACnC;QACD,CAAC,mBAAW,CAAC,EAAE;YACX,IAAI,EAAE,OAAO;YACb,WAAW,EAAE,+BAA+B;YAC5C,KAAK,EAAE,+CAAyB;YAChC,OAAO,EAAE,CAAC,QAAQ,EAAE,mBAAmB,EAAE,mBAAmB,EAAE,aAAa,EAAE,aAAa,EAAE,OAAO,EAAE,eAAe,EAAE,YAAY,CAAC;YACnI,KAAK,EAAE;gBACH,IAAI,EAAE,QAAQ;aACjB;SACJ;QACD,CAAC,6BAAqB,CAAC,EAAE;YACrB,IAAI,EAAE,OAAO;YACb,KAAK,EAAE,+CAAyB;YAChC,mBAAmB,EAAE;;;;;;;;yIAQwG;YAC7H,OAAO,EAAE,EAAE;YACX,KAAK,EAAE;gBACH,IAAI,EAAE,QAAQ;gBACd,UAAU,EAAE;oBACR,KAAK,EAAE;wBACH,IAAI,EAAE,QAAQ;wBACd,KAAK,EAAE,UAAU;qBACpB;oBACD,GAAG,EAAE;wBACD,IAAI,EAAE,QAAQ;wBACd,KAAK,EAAE,+DAA+D;qBACzE;oBACD,EAAE,EAAE;wBACA,IAAI,EAAE,QAAQ;wBACd,KAAK,EAAE,0EAA0E;qBACpF;oBACD,MAAM,EAAE;wBACJ,IAAI,EAAE,CAAC,QAAQ,EAAE,SAAS,CAAC;wBAC3B,KAAK,EAAE,qGAAqG;qBAC/G;iBACJ;aACJ;SACJ;KACJ;CACJ,CAAC"}
|
|
@@ -13,6 +13,10 @@ export interface OpenAiModelDescription {
|
|
|
13
13
|
* The OpenAI API compatible endpoint where the model is hosted. If not provided the default OpenAI endpoint will be used.
|
|
14
14
|
*/
|
|
15
15
|
url?: string;
|
|
16
|
+
/**
|
|
17
|
+
* The key for the model. If 'true' is provided the global OpenAI API key will be used.
|
|
18
|
+
*/
|
|
19
|
+
apiKey: string | true | undefined;
|
|
16
20
|
}
|
|
17
21
|
export interface OpenAiLanguageModelsManager {
|
|
18
22
|
apiKey: string | undefined;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openai-language-models-manager.d.ts","sourceRoot":"","sources":["../../src/common/openai-language-models-manager.ts"],"names":[],"mappings":"AAeA,eAAO,MAAM,mCAAmC,6CAA6C,CAAC;AAC9F,eAAO,MAAM,2BAA2B,eAAwC,CAAC;AACjF,MAAM,WAAW,sBAAsB;IACnC;;OAEG;IACH,EAAE,EAAE,MAAM,CAAC;IACX;;OAEG;IACH,KAAK,EAAE,MAAM,CAAC;IACd;;OAEG;IACH,GAAG,CAAC,EAAE,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"openai-language-models-manager.d.ts","sourceRoot":"","sources":["../../src/common/openai-language-models-manager.ts"],"names":[],"mappings":"AAeA,eAAO,MAAM,mCAAmC,6CAA6C,CAAC;AAC9F,eAAO,MAAM,2BAA2B,eAAwC,CAAC;AACjF,MAAM,WAAW,sBAAsB;IACnC;;OAEG;IACH,EAAE,EAAE,MAAM,CAAC;IACX;;OAEG;IACH,KAAK,EAAE,MAAM,CAAC;IACd;;OAEG;IACH,GAAG,CAAC,EAAE,MAAM,CAAC;IACb;;OAEG;IACH,MAAM,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC;CACrC;AACD,MAAM,WAAW,2BAA2B;IACxC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC;IAC3B,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,GAAG,IAAI,CAAC;IACzC,4BAA4B,CAAC,GAAG,MAAM,EAAE,sBAAsB,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACjF,oBAAoB,CAAC,GAAG,QAAQ,EAAE,MAAM,EAAE,GAAG,IAAI,CAAA;CACpD"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { LanguageModel, LanguageModelParsedResponse, LanguageModelRequest, LanguageModelResponse } from '@theia/ai-core';
|
|
1
|
+
import { LanguageModel, LanguageModelParsedResponse, LanguageModelRequest, LanguageModelResponse, LanguageModelTextResponse } from '@theia/ai-core';
|
|
2
2
|
import { CancellationToken } from '@theia/core';
|
|
3
3
|
import OpenAI from 'openai';
|
|
4
4
|
import { RunnableToolFunctionWithoutParse } from 'openai/lib/RunnableFunction';
|
|
@@ -6,15 +6,17 @@ export declare const OpenAiModelIdentifier: unique symbol;
|
|
|
6
6
|
export declare class OpenAiModel implements LanguageModel {
|
|
7
7
|
readonly id: string;
|
|
8
8
|
model: string;
|
|
9
|
-
|
|
9
|
+
apiKey: () => string | undefined;
|
|
10
10
|
url: string | undefined;
|
|
11
11
|
/**
|
|
12
12
|
* @param id the unique id for this language model. It will be used to identify the model in the UI.
|
|
13
13
|
* @param model the model id as it is used by the OpenAI API
|
|
14
14
|
* @param openAIInitializer initializer for the OpenAI client, used for each request.
|
|
15
15
|
*/
|
|
16
|
-
constructor(id: string, model: string, apiKey: (
|
|
16
|
+
constructor(id: string, model: string, apiKey: () => string | undefined, url: string | undefined);
|
|
17
17
|
request(request: LanguageModelRequest, cancellationToken?: CancellationToken): Promise<LanguageModelResponse>;
|
|
18
|
+
protected handleNonStreamingRequest(openai: OpenAI, request: LanguageModelRequest): Promise<LanguageModelTextResponse>;
|
|
19
|
+
protected isNonStreamingModel(model: string): boolean;
|
|
18
20
|
protected supportsStructuredOutput(): boolean;
|
|
19
21
|
protected handleStructuredOutputRequest(openai: OpenAI, request: LanguageModelRequest): Promise<LanguageModelParsedResponse>;
|
|
20
22
|
private getCompletionContent;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openai-language-model.d.ts","sourceRoot":"","sources":["../../src/node/openai-language-model.ts"],"names":[],"mappings":"AAgBA,OAAO,EACH,aAAa,EACb,2BAA2B,EAC3B,oBAAoB,EAEpB,qBAAqB,
|
|
1
|
+
{"version":3,"file":"openai-language-model.d.ts","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 +49,9 @@ class OpenAiModel {
|
|
|
49
49
|
async request(request, cancellationToken) {
|
|
50
50
|
var _a;
|
|
51
51
|
const openai = this.initializeOpenAi();
|
|
52
|
+
if (this.isNonStreamingModel(this.model)) {
|
|
53
|
+
return this.handleNonStreamingRequest(openai, request);
|
|
54
|
+
}
|
|
52
55
|
if (((_a = request.response_format) === null || _a === void 0 ? void 0 : _a.type) === 'json_schema' && this.supportsStructuredOutput()) {
|
|
53
56
|
return this.handleStructuredOutputRequest(openai, request);
|
|
54
57
|
}
|
|
@@ -120,10 +123,28 @@ class OpenAiModel {
|
|
|
120
123
|
};
|
|
121
124
|
return { stream: asyncIterator };
|
|
122
125
|
}
|
|
126
|
+
async handleNonStreamingRequest(openai, request) {
|
|
127
|
+
var _a;
|
|
128
|
+
const response = await openai.chat.completions.create({
|
|
129
|
+
model: this.model,
|
|
130
|
+
messages: request.messages.map(toOpenAIMessage),
|
|
131
|
+
...request.settings
|
|
132
|
+
});
|
|
133
|
+
const message = response.choices[0].message;
|
|
134
|
+
return {
|
|
135
|
+
text: (_a = message.content) !== null && _a !== void 0 ? _a : ''
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
isNonStreamingModel(model) {
|
|
139
|
+
return ['o1-preview'].includes(model);
|
|
140
|
+
}
|
|
123
141
|
supportsStructuredOutput() {
|
|
124
|
-
//
|
|
125
|
-
|
|
126
|
-
|
|
142
|
+
// see https://platform.openai.com/docs/models/gpt-4o
|
|
143
|
+
return [
|
|
144
|
+
'gpt-4o',
|
|
145
|
+
'gpt-4o-2024-08-06',
|
|
146
|
+
'gpt-4o-mini'
|
|
147
|
+
].includes(this.model);
|
|
127
148
|
}
|
|
128
149
|
async handleStructuredOutputRequest(openai, request) {
|
|
129
150
|
var _a;
|
|
@@ -162,12 +183,12 @@ class OpenAiModel {
|
|
|
162
183
|
}));
|
|
163
184
|
}
|
|
164
185
|
initializeOpenAi() {
|
|
165
|
-
const apiKey = this.apiKey
|
|
186
|
+
const apiKey = this.apiKey();
|
|
166
187
|
if (!apiKey && !(this.url)) {
|
|
167
188
|
throw new Error('Please provide OPENAI_API_KEY in preferences or via environment variable');
|
|
168
189
|
}
|
|
169
|
-
//
|
|
170
|
-
return new openai_1.default({ apiKey:
|
|
190
|
+
// We need to hand over "some" key, even if a custom url is not key protected as otherwise the OpenAI client will throw an error
|
|
191
|
+
return new openai_1.default({ apiKey: apiKey !== null && apiKey !== void 0 ? apiKey : 'no-key', baseURL: this.url });
|
|
171
192
|
}
|
|
172
193
|
}
|
|
173
194
|
exports.OpenAiModel = OpenAiModel;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openai-language-model.js","sourceRoot":"","sources":["../../src/node/openai-language-model.ts"],"names":[],"mappings":";AAAA,gFAAgF;AAChF,yCAAyC;AACzC,EAAE;AACF,2EAA2E;AAC3E,mEAAmE;AACnE,wCAAwC;AACxC,EAAE;AACF,4EAA4E;AAC5E,8EAA8E;AAC9E,6EAA6E;AAC7E,yDAAyD;AACzD,uDAAuD;AACvD,EAAE;AACF,gFAAgF;AAChF,gFAAgF;;;
|
|
1
|
+
{"version":3,"file":"openai-language-model.js","sourceRoot":"","sources":["../../src/node/openai-language-model.ts"],"names":[],"mappings":";AAAA,gFAAgF;AAChF,yCAAyC;AACzC,EAAE;AACF,2EAA2E;AAC3E,mEAAmE;AACnE,wCAAwC;AACxC,EAAE;AACF,4EAA4E;AAC5E,8EAA8E;AAC9E,6EAA6E;AAC7E,yDAAyD;AACzD,uDAAuD;AACvD,EAAE;AACF,gFAAgF;AAChF,gFAAgF;;;AAYhF,mCAA4B;AAKf,QAAA,qBAAqB,GAAG,MAAM,CAAC,uBAAuB,CAAC,CAAC;AAErE,SAAS,eAAe,CAAC,OAAoC;IACzD,OAAO;QACH,IAAI,EAAE,YAAY,CAAC,OAAO,CAAC;QAC3B,OAAO,EAAE,OAAO,CAAC,KAAK,IAAI,EAAE;KAC/B,CAAC;AACN,CAAC;AAED,SAAS,YAAY,CAAC,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E,CAAC;CACJ;AAjKD,kCAiKC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openai-language-models-manager-impl.d.ts","sourceRoot":"","sources":["../../src/node/openai-language-models-manager-impl.ts"],"names":[],"mappings":"AAgBA,OAAO,EAAE,qBAAqB,EAAE,MAAM,gBAAgB,CAAC;AAGvD,OAAO,EAAE,2BAA2B,EAAE,sBAAsB,EAAE,MAAM,WAAW,CAAC;AAEhF,qBACa,+BAAgC,YAAW,2BAA2B;IAE/E,SAAS,CAAC,OAAO,EAAE,MAAM,GAAG,SAAS,CAAC;IAGtC,SAAS,CAAC,QAAQ,CAAC,qBAAqB,EAAE,qBAAqB,CAAC;IAEhE,IAAI,MAAM,IAAI,MAAM,GAAG,SAAS,CAE/B;IAIK,4BAA4B,CAAC,GAAG,iBAAiB,EAAE,sBAAsB,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;
|
|
1
|
+
{"version":3,"file":"openai-language-models-manager-impl.d.ts","sourceRoot":"","sources":["../../src/node/openai-language-models-manager-impl.ts"],"names":[],"mappings":"AAgBA,OAAO,EAAE,qBAAqB,EAAE,MAAM,gBAAgB,CAAC;AAGvD,OAAO,EAAE,2BAA2B,EAAE,sBAAsB,EAAE,MAAM,WAAW,CAAC;AAEhF,qBACa,+BAAgC,YAAW,2BAA2B;IAE/E,SAAS,CAAC,OAAO,EAAE,MAAM,GAAG,SAAS,CAAC;IAGtC,SAAS,CAAC,QAAQ,CAAC,qBAAqB,EAAE,qBAAqB,CAAC;IAEhE,IAAI,MAAM,IAAI,MAAM,GAAG,SAAS,CAE/B;IAIK,4BAA4B,CAAC,GAAG,iBAAiB,EAAE,sBAAsB,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IA+BjG,oBAAoB,CAAC,GAAG,QAAQ,EAAE,MAAM,EAAE,GAAG,IAAI;IAIjD,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,GAAG,IAAI;CAO9C"}
|
|
@@ -30,6 +30,15 @@ let OpenAiLanguageModelsManagerImpl = class OpenAiLanguageModelsManagerImpl {
|
|
|
30
30
|
async createOrUpdateLanguageModels(...modelDescriptions) {
|
|
31
31
|
for (const modelDescription of modelDescriptions) {
|
|
32
32
|
const model = await this.languageModelRegistry.getLanguageModel(modelDescription.id);
|
|
33
|
+
const apiKeyProvider = () => {
|
|
34
|
+
if (modelDescription.apiKey === true) {
|
|
35
|
+
return this.apiKey;
|
|
36
|
+
}
|
|
37
|
+
if (modelDescription.apiKey) {
|
|
38
|
+
return modelDescription.apiKey;
|
|
39
|
+
}
|
|
40
|
+
return undefined;
|
|
41
|
+
};
|
|
33
42
|
if (model) {
|
|
34
43
|
if (!(model instanceof openai_language_model_1.OpenAiModel)) {
|
|
35
44
|
console.warn(`Open AI: model ${modelDescription.id} is not an OpenAI model`);
|
|
@@ -40,17 +49,12 @@ let OpenAiLanguageModelsManagerImpl = class OpenAiLanguageModelsManagerImpl {
|
|
|
40
49
|
console.info(`Open AI: skip creating model ${modelDescription.id} because it already exists`);
|
|
41
50
|
continue;
|
|
42
51
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
}
|
|
47
|
-
else {
|
|
48
|
-
// This can happen during the initializing of more than one frontends.
|
|
49
|
-
console.info(`Open AI: skip creating or updating model ${modelDescription.id} because it already exists and is up to date`);
|
|
50
|
-
}
|
|
52
|
+
model.url = modelDescription.url;
|
|
53
|
+
model.model = modelDescription.model;
|
|
54
|
+
model.apiKey = apiKeyProvider;
|
|
51
55
|
}
|
|
52
56
|
else {
|
|
53
|
-
this.languageModelRegistry.addLanguageModels([new openai_language_model_1.OpenAiModel(modelDescription.id, modelDescription.model,
|
|
57
|
+
this.languageModelRegistry.addLanguageModels([new openai_language_model_1.OpenAiModel(modelDescription.id, modelDescription.model, apiKeyProvider, modelDescription.url)]);
|
|
54
58
|
}
|
|
55
59
|
}
|
|
56
60
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openai-language-models-manager-impl.js","sourceRoot":"","sources":["../../src/node/openai-language-models-manager-impl.ts"],"names":[],"mappings":";AAAA,gFAAgF;AAChF,yCAAyC;AACzC,EAAE;AACF,2EAA2E;AAC3E,mEAAmE;AACnE,wCAAwC;AACxC,EAAE;AACF,4EAA4E;AAC5E,8EAA8E;AAC9E,6EAA6E;AAC7E,yDAAyD;AACzD,uDAAuD;AACvD,EAAE;AACF,gFAAgF;AAChF,gFAAgF;;;;AAEhF,4CAAuD;AACvD,4DAAkE;AAClE,mEAAsD;AAI/C,IAAM,+BAA+B,GAArC,MAAM,+BAA+B;IAOxC,IAAI,MAAM;;QACN,OAAO,MAAA,IAAI,CAAC,OAAO,mCAAI,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC;IACtD,CAAC;IAED,6EAA6E;IAC7E,6CAA6C;IAC7C,KAAK,CAAC,4BAA4B,CAAC,GAAG,iBAA2C;QAC7E,KAAK,MAAM,gBAAgB,IAAI,iBAAiB,EAAE,CAAC;YAC/C,MAAM,KAAK,GAAG,MAAM,IAAI,CAAC,qBAAqB,CAAC,gBAAgB,CAAC,gBAAgB,CAAC,EAAE,CAAC,CAAC;YACrF,IAAI,KAAK,EAAE,CAAC;gBACR,IAAI,CAAC,CAAC,KAAK,YAAY,mCAAW,CAAC,EAAE,CAAC;oBAClC,OAAO,CAAC,IAAI,CAAC,kBAAkB,gBAAgB,CAAC,EAAE,yBAAyB,CAAC,CAAC;oBAC7E,SAAS;gBACb,CAAC;gBACD,IAAI,CAAC,gBAAgB,CAAC,GAAG,EAAE,CAAC;oBACxB,qIAAqI;oBACrI,OAAO,CAAC,IAAI,CAAC,gCAAgC,gBAAgB,CAAC,EAAE,4BAA4B,CAAC,CAAC;oBAC9F,SAAS;gBACb,CAAC;gBACD,
|
|
1
|
+
{"version":3,"file":"openai-language-models-manager-impl.js","sourceRoot":"","sources":["../../src/node/openai-language-models-manager-impl.ts"],"names":[],"mappings":";AAAA,gFAAgF;AAChF,yCAAyC;AACzC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AAC,CAAC;oBAC9F,SAAS;gBACb,CAAC;gBACD,KAAK,CAAC,GAAG,GAAG,gBAAgB,CAAC,GAAG,CAAC;gBACjC,KAAK,CAAC,KAAK,GAAG,gBAAgB,CAAC,KAAK,CAAC;gBACrC,KAAK,CAAC,MAAM,GAAG,cAAc,CAAC;YAClC,CAAC;iBAAM,CAAC;gBACJ,IAAI,CAAC,qBAAqB,CAAC,iBAAiB,CAAC,CAAC,IAAI,mCAAW,CAAC,gBAAgB,CAAC,EAAE,EAAE,gBAAgB,CAAC,KAAK,EAAE,cAAc,EAAE,gBAAgB,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;YACvJ,CAAC;QACL,CAAC;IACL,CAAC;IAED,oBAAoB,CAAC,GAAG,QAAkB;QACtC,IAAI,CAAC,qBAAqB,CAAC,oBAAoB,CAAC,QAAQ,CAAC,CAAC;IAC9D,CAAC;IAED,SAAS,CAAC,MAA0B;QAChC,IAAI,MAAM,EAAE,CAAC;YACT,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC;QAC1B,CAAC;aAAM,CAAC;YACJ,IAAI,CAAC,OAAO,GAAG,SAAS,CAAC;QAC7B,CAAC;IACL,CAAC;CACJ,CAAA;AAvDY,0EAA+B;AAKrB;IADlB,IAAA,kBAAM,EAAC,+BAAqB,CAAC;;8EACkC;0CALvD,+BAA+B;IAD3C,IAAA,sBAAU,GAAE;GACA,+BAA+B,CAuD3C"}
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@theia/ai-openai",
|
|
3
|
-
"version": "1.55.0
|
|
3
|
+
"version": "1.55.0",
|
|
4
4
|
"description": "Theia - OpenAI Integration",
|
|
5
5
|
"dependencies": {
|
|
6
|
-
"@theia/ai-core": "1.55.0
|
|
7
|
-
"@theia/core": "1.55.0
|
|
8
|
-
"@theia/filesystem": "1.55.0
|
|
9
|
-
"@theia/workspace": "1.55.0
|
|
6
|
+
"@theia/ai-core": "1.55.0",
|
|
7
|
+
"@theia/core": "1.55.0",
|
|
8
|
+
"@theia/filesystem": "1.55.0",
|
|
9
|
+
"@theia/workspace": "1.55.0",
|
|
10
10
|
"minimatch": "^5.1.0",
|
|
11
11
|
"openai": "^4.55.7",
|
|
12
12
|
"tslib": "^2.6.2"
|
|
@@ -45,10 +45,10 @@
|
|
|
45
45
|
"watch": "theiaext watch"
|
|
46
46
|
},
|
|
47
47
|
"devDependencies": {
|
|
48
|
-
"@theia/ext-scripts": "1.
|
|
48
|
+
"@theia/ext-scripts": "1.55.0"
|
|
49
49
|
},
|
|
50
50
|
"nyc": {
|
|
51
51
|
"extends": "../../configs/nyc.json"
|
|
52
52
|
},
|
|
53
|
-
"gitHead": "
|
|
53
|
+
"gitHead": "0e7a523b8e798679d2e098709c63bd7060e54c8a"
|
|
54
54
|
}
|
|
@@ -64,7 +64,7 @@ export class OpenAiFrontendApplicationContribution implements FrontendApplicatio
|
|
|
64
64
|
|
|
65
65
|
const modelsToRemove = oldModels.filter(model => !newModels.some(newModel => newModel.id === model.id));
|
|
66
66
|
const modelsToAddOrUpdate = newModels.filter(newModel => !oldModels.some(model =>
|
|
67
|
-
model.id === newModel.id && model.model === newModel.model && model.url === newModel.url));
|
|
67
|
+
model.id === newModel.id && model.model === newModel.model && model.url === newModel.url && model.apiKey === newModel.apiKey));
|
|
68
68
|
|
|
69
69
|
this.manager.removeLanguageModels(...modelsToRemove.map(model => model.id));
|
|
70
70
|
this.manager.createOrUpdateLanguageModels(...modelsToAddOrUpdate);
|
|
@@ -77,21 +77,23 @@ export class OpenAiFrontendApplicationContribution implements FrontendApplicatio
|
|
|
77
77
|
function createOpenAIModelDescription(modelId: string): OpenAiModelDescription {
|
|
78
78
|
return {
|
|
79
79
|
id: `openai/${modelId}`,
|
|
80
|
-
model: modelId
|
|
80
|
+
model: modelId,
|
|
81
|
+
apiKey: true
|
|
81
82
|
};
|
|
82
83
|
}
|
|
83
84
|
|
|
84
85
|
function createCustomModelDescriptionsFromPreferences(preferences: Partial<OpenAiModelDescription>[]): OpenAiModelDescription[] {
|
|
85
86
|
return preferences.reduce((acc, pref) => {
|
|
86
|
-
if (!pref.model || !pref.url) {
|
|
87
|
+
if (!pref.model || !pref.url || typeof pref.model !== 'string' || typeof pref.url !== 'string') {
|
|
87
88
|
return acc;
|
|
88
89
|
}
|
|
89
90
|
return [
|
|
90
91
|
...acc,
|
|
91
92
|
{
|
|
92
|
-
id: pref.id
|
|
93
|
+
id: pref.id && typeof pref.id === 'string' ? pref.id : pref.model,
|
|
93
94
|
model: pref.model,
|
|
94
|
-
url: pref.url
|
|
95
|
+
url: pref.url,
|
|
96
|
+
apiKey: typeof pref.apiKey === 'string' || pref.apiKey === true ? pref.apiKey : undefined
|
|
95
97
|
}
|
|
96
98
|
];
|
|
97
99
|
}, []);
|
|
@@ -34,7 +34,7 @@ export const OpenAiPreferencesSchema: PreferenceSchema = {
|
|
|
34
34
|
type: 'array',
|
|
35
35
|
description: 'Official OpenAI models to use',
|
|
36
36
|
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'],
|
|
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'],
|
|
38
38
|
items: {
|
|
39
39
|
type: 'string'
|
|
40
40
|
}
|
|
@@ -43,7 +43,14 @@ export const OpenAiPreferencesSchema: PreferenceSchema = {
|
|
|
43
43
|
type: 'array',
|
|
44
44
|
title: AI_CORE_PREFERENCES_TITLE,
|
|
45
45
|
markdownDescription: 'Integrate custom models compatible with the OpenAI API, for example via `vllm`. The required attributes are `model` and `url`.\
|
|
46
|
-
|
|
46
|
+
\n\
|
|
47
|
+
Optionally, you can\
|
|
48
|
+
\n\
|
|
49
|
+
- specify a unique `id` to identify the custom model in the UI. If none is given `model` will be used as `id`.\
|
|
50
|
+
\n\
|
|
51
|
+
- provide an `apiKey` to access the API served at the given url. Use `true` to indicate the use of the global OpenAI API key.\
|
|
52
|
+
\n\
|
|
53
|
+
Refer to [our documentation](https://theia-ide.org/docs/user_ai/#openai-compatible-models-eg-via-vllm) for more information.',
|
|
47
54
|
default: [],
|
|
48
55
|
items: {
|
|
49
56
|
type: 'object',
|
|
@@ -59,7 +66,11 @@ export const OpenAiPreferencesSchema: PreferenceSchema = {
|
|
|
59
66
|
id: {
|
|
60
67
|
type: 'string',
|
|
61
68
|
title: 'A unique identifier which is used in the UI to identify the custom model',
|
|
62
|
-
}
|
|
69
|
+
},
|
|
70
|
+
apiKey: {
|
|
71
|
+
type: ['string', 'boolean'],
|
|
72
|
+
title: 'Either the key to access the API served at the given url or `true` to use the global OpenAI API key',
|
|
73
|
+
},
|
|
63
74
|
}
|
|
64
75
|
}
|
|
65
76
|
}
|
|
@@ -28,6 +28,10 @@ export interface OpenAiModelDescription {
|
|
|
28
28
|
* The OpenAI API compatible endpoint where the model is hosted. If not provided the default OpenAI endpoint will be used.
|
|
29
29
|
*/
|
|
30
30
|
url?: string;
|
|
31
|
+
/**
|
|
32
|
+
* The key for the model. If 'true' is provided the global OpenAI API key will be used.
|
|
33
|
+
*/
|
|
34
|
+
apiKey: string | true | undefined;
|
|
31
35
|
}
|
|
32
36
|
export interface OpenAiLanguageModelsManager {
|
|
33
37
|
apiKey: string | undefined;
|
|
@@ -20,7 +20,8 @@ import {
|
|
|
20
20
|
LanguageModelRequest,
|
|
21
21
|
LanguageModelRequestMessage,
|
|
22
22
|
LanguageModelResponse,
|
|
23
|
-
LanguageModelStreamResponsePart
|
|
23
|
+
LanguageModelStreamResponsePart,
|
|
24
|
+
LanguageModelTextResponse
|
|
24
25
|
} from '@theia/ai-core';
|
|
25
26
|
import { CancellationToken } from '@theia/core';
|
|
26
27
|
import OpenAI from 'openai';
|
|
@@ -55,11 +56,15 @@ export class OpenAiModel implements LanguageModel {
|
|
|
55
56
|
* @param model the model id as it is used by the OpenAI API
|
|
56
57
|
* @param openAIInitializer initializer for the OpenAI client, used for each request.
|
|
57
58
|
*/
|
|
58
|
-
constructor(public readonly id: string, public model: string,
|
|
59
|
+
constructor(public readonly id: string, public model: string, public apiKey: () => string | undefined, public url: string | undefined) { }
|
|
59
60
|
|
|
60
61
|
async request(request: LanguageModelRequest, cancellationToken?: CancellationToken): Promise<LanguageModelResponse> {
|
|
61
62
|
const openai = this.initializeOpenAi();
|
|
62
63
|
|
|
64
|
+
if (this.isNonStreamingModel(this.model)) {
|
|
65
|
+
return this.handleNonStreamingRequest(openai, request);
|
|
66
|
+
}
|
|
67
|
+
|
|
63
68
|
if (request.response_format?.type === 'json_schema' && this.supportsStructuredOutput()) {
|
|
64
69
|
return this.handleStructuredOutputRequest(openai, request);
|
|
65
70
|
}
|
|
@@ -133,10 +138,31 @@ export class OpenAiModel implements LanguageModel {
|
|
|
133
138
|
return { stream: asyncIterator };
|
|
134
139
|
}
|
|
135
140
|
|
|
141
|
+
protected async handleNonStreamingRequest(openai: OpenAI, request: LanguageModelRequest): Promise<LanguageModelTextResponse> {
|
|
142
|
+
const response = await openai.chat.completions.create({
|
|
143
|
+
model: this.model,
|
|
144
|
+
messages: request.messages.map(toOpenAIMessage),
|
|
145
|
+
...request.settings
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
const message = response.choices[0].message;
|
|
149
|
+
|
|
150
|
+
return {
|
|
151
|
+
text: message.content ?? ''
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
protected isNonStreamingModel(model: string): boolean {
|
|
156
|
+
return ['o1-preview'].includes(model);
|
|
157
|
+
}
|
|
158
|
+
|
|
136
159
|
protected supportsStructuredOutput(): boolean {
|
|
137
|
-
//
|
|
138
|
-
|
|
139
|
-
|
|
160
|
+
// see https://platform.openai.com/docs/models/gpt-4o
|
|
161
|
+
return [
|
|
162
|
+
'gpt-4o',
|
|
163
|
+
'gpt-4o-2024-08-06',
|
|
164
|
+
'gpt-4o-mini'
|
|
165
|
+
].includes(this.model);
|
|
140
166
|
}
|
|
141
167
|
|
|
142
168
|
protected async handleStructuredOutputRequest(openai: OpenAI, request: LanguageModelRequest): Promise<LanguageModelParsedResponse> {
|
|
@@ -177,11 +203,11 @@ export class OpenAiModel implements LanguageModel {
|
|
|
177
203
|
}
|
|
178
204
|
|
|
179
205
|
protected initializeOpenAi(): OpenAI {
|
|
180
|
-
const apiKey = this.apiKey
|
|
206
|
+
const apiKey = this.apiKey();
|
|
181
207
|
if (!apiKey && !(this.url)) {
|
|
182
208
|
throw new Error('Please provide OPENAI_API_KEY in preferences or via environment variable');
|
|
183
209
|
}
|
|
184
|
-
//
|
|
185
|
-
return new OpenAI({ apiKey:
|
|
210
|
+
// We need to hand over "some" key, even if a custom url is not key protected as otherwise the OpenAI client will throw an error
|
|
211
|
+
return new OpenAI({ apiKey: apiKey ?? 'no-key', baseURL: this.url });
|
|
186
212
|
}
|
|
187
213
|
}
|
|
@@ -36,6 +36,15 @@ export class OpenAiLanguageModelsManagerImpl implements OpenAiLanguageModelsMana
|
|
|
36
36
|
async createOrUpdateLanguageModels(...modelDescriptions: OpenAiModelDescription[]): Promise<void> {
|
|
37
37
|
for (const modelDescription of modelDescriptions) {
|
|
38
38
|
const model = await this.languageModelRegistry.getLanguageModel(modelDescription.id);
|
|
39
|
+
const apiKeyProvider = () => {
|
|
40
|
+
if (modelDescription.apiKey === true) {
|
|
41
|
+
return this.apiKey;
|
|
42
|
+
}
|
|
43
|
+
if (modelDescription.apiKey) {
|
|
44
|
+
return modelDescription.apiKey;
|
|
45
|
+
}
|
|
46
|
+
return undefined;
|
|
47
|
+
};
|
|
39
48
|
if (model) {
|
|
40
49
|
if (!(model instanceof OpenAiModel)) {
|
|
41
50
|
console.warn(`Open AI: model ${modelDescription.id} is not an OpenAI model`);
|
|
@@ -46,15 +55,11 @@ export class OpenAiLanguageModelsManagerImpl implements OpenAiLanguageModelsMana
|
|
|
46
55
|
console.info(`Open AI: skip creating model ${modelDescription.id} because it already exists`);
|
|
47
56
|
continue;
|
|
48
57
|
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
} else {
|
|
53
|
-
// This can happen during the initializing of more than one frontends.
|
|
54
|
-
console.info(`Open AI: skip creating or updating model ${modelDescription.id} because it already exists and is up to date`);
|
|
55
|
-
}
|
|
58
|
+
model.url = modelDescription.url;
|
|
59
|
+
model.model = modelDescription.model;
|
|
60
|
+
model.apiKey = apiKeyProvider;
|
|
56
61
|
} else {
|
|
57
|
-
this.languageModelRegistry.addLanguageModels([new OpenAiModel(modelDescription.id, modelDescription.model,
|
|
62
|
+
this.languageModelRegistry.addLanguageModels([new OpenAiModel(modelDescription.id, modelDescription.model, apiKeyProvider, modelDescription.url)]);
|
|
58
63
|
}
|
|
59
64
|
}
|
|
60
65
|
}
|