@ckeditor/ckeditor5-ai 40.1.0 → 41.0.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/LICENSE.md +14 -6
- package/README.md +10 -11
- package/build/ai.js +78 -3
- package/build/translations/af.js +1 -1
- package/build/translations/ar.js +1 -1
- package/build/translations/bg.js +1 -1
- package/build/translations/bn.js +1 -1
- package/build/translations/bs.js +1 -1
- package/build/translations/ca.js +1 -1
- package/build/translations/cs.js +1 -1
- package/build/translations/da.js +1 -1
- package/build/translations/de-ch.js +1 -1
- package/build/translations/de.js +1 -1
- package/build/translations/el.js +1 -1
- package/build/translations/en-au.js +1 -1
- package/build/translations/es-co.js +1 -1
- package/build/translations/es.js +1 -1
- package/build/translations/et.js +1 -1
- package/build/translations/fa.js +1 -1
- package/build/translations/fi.js +1 -1
- package/build/translations/fr.js +1 -1
- package/build/translations/gl.js +1 -1
- package/build/translations/he.js +1 -1
- package/build/translations/hi.js +1 -1
- package/build/translations/hr.js +1 -1
- package/build/translations/hu.js +1 -1
- package/build/translations/id.js +1 -1
- package/build/translations/it.js +1 -1
- package/build/translations/ja.js +1 -1
- package/build/translations/jv.js +1 -1
- package/build/translations/ko.js +1 -1
- package/build/translations/lt.js +1 -1
- package/build/translations/lv.js +1 -1
- package/build/translations/ms.js +1 -1
- package/build/translations/nl.js +1 -1
- package/build/translations/no.js +1 -1
- package/build/translations/pl.js +1 -1
- package/build/translations/pt-br.js +1 -1
- package/build/translations/pt.js +1 -1
- package/build/translations/ro.js +1 -1
- package/build/translations/ru.js +1 -1
- package/build/translations/sk.js +1 -1
- package/build/translations/sr-latn.js +1 -1
- package/build/translations/sr.js +1 -1
- package/build/translations/sv.js +1 -1
- package/build/translations/th.js +1 -1
- package/build/translations/tk.js +1 -1
- package/build/translations/tr.js +1 -1
- package/build/translations/ug.js +1 -1
- package/build/translations/uk.js +1 -1
- package/build/translations/ur.js +1 -1
- package/build/translations/uz.js +1 -1
- package/build/translations/vi.js +1 -1
- package/build/translations/zh-cn.js +1 -1
- package/build/translations/zh.js +1 -1
- package/ckeditor5-metadata.json +2 -2
- package/lang/contexts.json +6 -4
- package/lang/translations/af.po +14 -6
- package/lang/translations/ar.po +15 -7
- package/lang/translations/bg.po +15 -7
- package/lang/translations/bn.po +15 -7
- package/lang/translations/bs.po +14 -6
- package/lang/translations/ca.po +15 -7
- package/lang/translations/cs.po +15 -7
- package/lang/translations/da.po +15 -7
- package/lang/translations/de-ch.po +14 -6
- package/lang/translations/de.po +15 -7
- package/lang/translations/el.po +15 -7
- package/lang/translations/en-au.po +14 -6
- package/lang/translations/en.po +15 -7
- package/lang/translations/es-co.po +14 -6
- package/lang/translations/es.po +15 -7
- package/lang/translations/et.po +15 -7
- package/lang/translations/fa.po +14 -6
- package/lang/translations/fi.po +15 -7
- package/lang/translations/fr.po +15 -7
- package/lang/translations/gl.po +62 -54
- package/lang/translations/he.po +15 -7
- package/lang/translations/hi.po +15 -7
- package/lang/translations/hr.po +14 -6
- package/lang/translations/hu.po +15 -7
- package/lang/translations/id.po +15 -7
- package/lang/translations/it.po +15 -7
- package/lang/translations/ja.po +15 -7
- package/lang/translations/jv.po +14 -6
- package/lang/translations/ko.po +15 -7
- package/lang/translations/lt.po +15 -7
- package/lang/translations/lv.po +15 -7
- package/lang/translations/ms.po +15 -7
- package/lang/translations/nl.po +15 -7
- package/lang/translations/no.po +15 -7
- package/lang/translations/pl.po +15 -7
- package/lang/translations/pt-br.po +15 -7
- package/lang/translations/pt.po +15 -7
- package/lang/translations/ro.po +15 -7
- package/lang/translations/ru.po +15 -7
- package/lang/translations/sk.po +15 -7
- package/lang/translations/sr-latn.po +62 -54
- package/lang/translations/sr.po +15 -7
- package/lang/translations/sv.po +15 -7
- package/lang/translations/th.po +15 -7
- package/lang/translations/tk.po +14 -6
- package/lang/translations/tr.po +15 -7
- package/lang/translations/ug.po +26 -18
- package/lang/translations/uk.po +15 -7
- package/lang/translations/ur.po +14 -6
- package/lang/translations/uz.po +14 -6
- package/lang/translations/vi.po +15 -7
- package/lang/translations/zh-cn.po +15 -7
- package/lang/translations/zh.po +15 -7
- package/package.json +8 -5
- package/src/adapters/aiadapter.d.ts +48 -0
- package/src/adapters/aiadapter.js +23 -0
- package/src/adapters/aitextadapter.d.ts +146 -0
- package/src/adapters/aitextadapter.js +23 -0
- package/src/adapters/awstextadapter.d.ts +178 -0
- package/src/adapters/awstextadapter.js +23 -0
- package/src/adapters/openaitextadapter.d.ts +181 -0
- package/src/adapters/openaitextadapter.js +23 -0
- package/src/aiassistant.d.ts +49 -121
- package/src/aiassistant.js +2 -2
- package/src/aiassistantediting.d.ts +13 -12
- package/src/aiassistantediting.js +2 -2
- package/src/aiassistantui.d.ts +16 -8
- package/src/aiassistantui.js +2 -2
- package/src/aiconfig.d.ts +69 -0
- package/src/aiconfig.js +23 -0
- package/src/augmentation.d.ts +10 -9
- package/src/augmentation.js +1 -1
- package/src/index.d.ts +7 -3
- package/src/index.js +2 -2
- package/src/ui/aiassistantcontroller.d.ts +40 -27
- package/src/ui/aiassistantcontroller.js +2 -2
- package/src/ui/dropdown/aicommandslistview.d.ts +2 -2
- package/src/ui/dropdown/aicommandslistview.js +2 -2
- package/src/ui/dropdown/buttonlabelwithhighlightview.d.ts +2 -2
- package/src/ui/dropdown/buttonlabelwithhighlightview.js +2 -2
- package/src/ui/dropdown/labelwithhighlightview.d.ts +2 -2
- package/src/ui/dropdown/labelwithhighlightview.js +2 -2
- package/src/ui/form/aiformcontentarea.d.ts +3 -3
- package/src/ui/form/aiformcontentarea.js +2 -2
- package/src/ui/form/aiformerrorview.d.ts +3 -3
- package/src/ui/form/aiformerrorview.js +2 -2
- package/src/ui/form/aiformpromptview.d.ts +3 -3
- package/src/ui/form/aiformpromptview.js +2 -2
- package/src/ui/form/aiformtoolbarview.d.ts +12 -8
- package/src/ui/form/aiformtoolbarview.js +2 -2
- package/src/ui/form/aiformview.d.ts +13 -14
- package/src/ui/form/aiformview.js +2 -2
- package/src/ui/form/aihistorylistitemview.d.ts +3 -3
- package/src/ui/form/aihistorylistitemview.js +2 -2
- package/src/ui/form/aihistorylistview.d.ts +3 -3
- package/src/ui/form/aihistorylistview.js +2 -2
- package/src/ui/form/prompthistory.d.ts +2 -2
- package/src/ui/form/prompthistory.js +2 -2
- package/src/ui/showaiassistantcommand.d.ts +8 -5
- package/src/ui/showaiassistantcommand.js +2 -2
- package/src/utils/common-translations.d.ts +2 -2
- package/src/utils/common-translations.js +2 -2
- package/theme/ai-dropdown.css +1 -1
- package/theme/ai-form.css +16 -4
- package/theme/ai.css +17 -4
- package/src/aiconnector.d.ts +0 -45
- package/src/aiconnector.js +0 -23
- package/theme/icons/robot-pencil.svg +0 -1
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* @module ai/adapters/aitextadapter
|
|
7
|
+
* @publicApi
|
|
8
|
+
*/
|
|
9
|
+
import { Plugin } from 'ckeditor5/src/core.js';
|
|
10
|
+
import { default as AIAdapter } from './aiadapter.js';
|
|
11
|
+
/**
|
|
12
|
+
* Abstract base class for AI adapters that perform text-related requests.
|
|
13
|
+
*
|
|
14
|
+
* Concrete AI text adapter implementation plugin, after loaded by the editor, will set itself as
|
|
15
|
+
* {@link module:ai/adapters/aiadapter~AIAdapter#textAdapter `AIAdapter#textAdapter`}.
|
|
16
|
+
*
|
|
17
|
+
* Then, features making text-related AI requests will use that concrete adapter through
|
|
18
|
+
* {@link module:ai/adapters/aitextadapter~AITextAdapter#makeRequest `AIAdapter#textAdapter#makeRequest()`}.
|
|
19
|
+
*
|
|
20
|
+
* You can create your own adapter that may connect to any model by extending this abstract class.
|
|
21
|
+
*/
|
|
22
|
+
export default abstract class AITextAdapter extends Plugin {
|
|
23
|
+
/**
|
|
24
|
+
* Abort controller instance.
|
|
25
|
+
*
|
|
26
|
+
* It is used by {@link ~AITextAdapter#abort `abort()`}.
|
|
27
|
+
*
|
|
28
|
+
* It should be passed to the mechanism that performs the request (e.g. `fetch()`).
|
|
29
|
+
*/
|
|
30
|
+
abortController: AbortController;
|
|
31
|
+
/**
|
|
32
|
+
* @inheritDoc
|
|
33
|
+
*/
|
|
34
|
+
static get requires(): readonly [typeof AIAdapter];
|
|
35
|
+
/**
|
|
36
|
+
* @inheritDoc
|
|
37
|
+
*/
|
|
38
|
+
init(): void;
|
|
39
|
+
/**
|
|
40
|
+
* The method that stops the current request and then recreates the abort controller instance.
|
|
41
|
+
*
|
|
42
|
+
* It should be called when the currently processed request should be aborted (e.g. when the user closes the UI as the request is
|
|
43
|
+
* processed).
|
|
44
|
+
*/
|
|
45
|
+
abort(): void;
|
|
46
|
+
/**
|
|
47
|
+
* Makes the AI request to the AI API service and processes the response from the service.
|
|
48
|
+
*
|
|
49
|
+
* Internally, it uses {@link ~AITextAdapter#sendRequest `sendRequest()`}.
|
|
50
|
+
*
|
|
51
|
+
* If you are creating a custom adapter, implement `sendRequest()` rather than extending this method.
|
|
52
|
+
*
|
|
53
|
+
* Implements basic error handling for errors thrown by `sendRequest()`:
|
|
54
|
+
*
|
|
55
|
+
* * {@link module:ai/adapters/aiadapter~AIRequestError `AIRequestError`} is re-thrown, and it is expected to be handled by the feature
|
|
56
|
+
* that made the request (e.g. by displaying some error notification).
|
|
57
|
+
* * {@link module:utils/ckeditorerror~CKEditorError `CKEditorError`} is unexpected, is logged to the console, and re-thrown.
|
|
58
|
+
* * `'AbortError'` is expected and is silenced. It is thrown when the request is aborted and does not need to be handled.
|
|
59
|
+
*
|
|
60
|
+
* @returns A Promise that resolves with the AI-generated response.
|
|
61
|
+
*/
|
|
62
|
+
makeRequest(requestData: AITextAdapterRequestData): Promise<void>;
|
|
63
|
+
/**
|
|
64
|
+
* Abstract method.
|
|
65
|
+
*
|
|
66
|
+
* Creates the request using provided `requestData`. Makes the request to the AI service endpoint. Handles the AI service response.
|
|
67
|
+
*
|
|
68
|
+
* If you want to extend an existing adapter, you can overload this method to do some additional processing or make an external call.
|
|
69
|
+
*
|
|
70
|
+
* If you want to create a custom adapter, the main logic related to making and handling the request should be implemented here.
|
|
71
|
+
* Make sure to properly implement {@link ~AITextAdapterRequestData#onData `onData()`} callback handling.
|
|
72
|
+
*/
|
|
73
|
+
abstract sendRequest(requestData: AITextAdapterRequestData): Promise<void>;
|
|
74
|
+
}
|
|
75
|
+
export type AITextAdapterRequestData = {
|
|
76
|
+
/**
|
|
77
|
+
* The user's query.
|
|
78
|
+
*/
|
|
79
|
+
query: string;
|
|
80
|
+
/**
|
|
81
|
+
* The context on which the query is used.
|
|
82
|
+
*
|
|
83
|
+
* May be empty. In such case, it is assumed that the user wanted to create new textual content.
|
|
84
|
+
*/
|
|
85
|
+
context: string;
|
|
86
|
+
/**
|
|
87
|
+
* A callback that is called whenever the data received by the adapter is updated.
|
|
88
|
+
*
|
|
89
|
+
* If the adapter does not use streaming, and receives the whole answer at once, it should call `onData()` once, when the data is
|
|
90
|
+
* received.
|
|
91
|
+
*
|
|
92
|
+
* If the adapter uses streaming, the adapter should internally build the whole received data and fire `onData()` callback with the
|
|
93
|
+
* full update data each time. For example, if during streaming, the adapter receives following chunks: `'<p>T'`, `'his is an'`, and
|
|
94
|
+
* `' example</p>'`, then it should fire `onData()` with: `'<p>T'`, `'<p>This is an'`, and `'<p>This is an example</p>'`.
|
|
95
|
+
*/
|
|
96
|
+
onData: AITextAdapterDataCallback;
|
|
97
|
+
/**
|
|
98
|
+
* ID of the performed action. This is a unique ID for each feature and each action that triggers the AI request.
|
|
99
|
+
*
|
|
100
|
+
* The main role for `actionId` is to distinguish which feature made the call. The action ID can be used in extended and custom adapters
|
|
101
|
+
* to customize the request.
|
|
102
|
+
*
|
|
103
|
+
* Each AI feature describes what action IDs it creates and uses. For example, the AI Assistant feature uses `actionId` values like
|
|
104
|
+
* `'aiAssistant:custom'`, or `'aiAssistant:command:improveWriting'`.
|
|
105
|
+
*/
|
|
106
|
+
actionId: string;
|
|
107
|
+
};
|
|
108
|
+
/**
|
|
109
|
+
* Object with headers to set in the request to the AI service.
|
|
110
|
+
*
|
|
111
|
+
* If the provided value is an `object`, it is simply used as provided.
|
|
112
|
+
*
|
|
113
|
+
* If the provided value is a function, it should be a function that returns a `Promise` which resolves with the headers object.
|
|
114
|
+
* This way, you can perform an authorization request to your application and receive the authorization token (and also implement
|
|
115
|
+
* any custom logic on the back-end side). The headers object is then used to make the actual call to the AI service.
|
|
116
|
+
*
|
|
117
|
+
* The function is passed {@link module:ai/adapters/aitextadapter~AITextAdapterRequestData#actionId `actionId`} parameter to make it
|
|
118
|
+
* possible to further customize the headers.
|
|
119
|
+
*
|
|
120
|
+
* If the function fails for any reason, the promise should be rejected. In this case, a feature that made the request should display
|
|
121
|
+
* an error notification.
|
|
122
|
+
*/
|
|
123
|
+
export type RequestHeaders = Record<string, string> | ((actionId: string) => Promise<Record<string, string>>);
|
|
124
|
+
/**
|
|
125
|
+
* Additional configuration parameters for the AI request. Use it to customize how the AI service generates responses.
|
|
126
|
+
*
|
|
127
|
+
* See the reference for the AI service of your choice to learn what parameters can be used with that service or model.
|
|
128
|
+
*
|
|
129
|
+
* If the provided value is an `object`, it is passed to the request as provided.
|
|
130
|
+
*
|
|
131
|
+
* If the provided value is a function, it should be a function that returns a `Promise` which resolves with the request parameters
|
|
132
|
+
* object. This gives you more flexibility to provide parameters for the AI model.
|
|
133
|
+
*
|
|
134
|
+
* The function is passed {@link module:ai/adapters/aitextadapter~AITextAdapterRequestData#actionId `actionId`} parameter to make it
|
|
135
|
+
* possible to further customize the parameters.
|
|
136
|
+
*
|
|
137
|
+
* If the function fails for any reason, the promise should be rejected. In this case, the feature that made the request should display
|
|
138
|
+
* an error notification.
|
|
139
|
+
*/
|
|
140
|
+
export type RequestParameters = Record<string, any> | ((actionId: string) => Promise<Record<string, any>>);
|
|
141
|
+
/**
|
|
142
|
+
* Callback called each time the AI text adapter receives data.
|
|
143
|
+
*
|
|
144
|
+
* @params content The received data.
|
|
145
|
+
*/
|
|
146
|
+
export type AITextAdapterDataCallback = (content: string) => any;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
+
*
|
|
4
|
+
*
|
|
5
|
+
*
|
|
6
|
+
*
|
|
7
|
+
* +---------------------------------------------------------------------------------+
|
|
8
|
+
* | |
|
|
9
|
+
* | Hello stranger! |
|
|
10
|
+
* | |
|
|
11
|
+
* | |
|
|
12
|
+
* | What you're currently looking at is the source code of a legally protected, |
|
|
13
|
+
* | proprietary software. Any attempts to deobfuscate / disassemble this code |
|
|
14
|
+
* | are forbidden and will result in legal consequences. |
|
|
15
|
+
* | |
|
|
16
|
+
* | |
|
|
17
|
+
* +---------------------------------------------------------------------------------+
|
|
18
|
+
*
|
|
19
|
+
*
|
|
20
|
+
*
|
|
21
|
+
*
|
|
22
|
+
*/
|
|
23
|
+
const _0x1ba8=['makeRequest','_prepareRequestParameters','init','aitextadapter-send-request-failed','sendRequest','locale','resolve','_prepareRequestHeaders','abortController','catch','object','application/json','reject','name','error','assign','plugins','signal','editor','aborted','get','AI_ERROR_GET_PARAMETERS','abort'];(function(_0x2e5222,_0x1ba8a1){const _0x5a7f64=function(_0x5b77dd){while(--_0x5b77dd){_0x2e5222['push'](_0x2e5222['shift']());}};_0x5a7f64(++_0x1ba8a1);}(_0x1ba8,0x126));const _0x5a7f=function(_0x2e5222,_0x1ba8a1){_0x2e5222=_0x2e5222-0x0;let _0x5a7f64=_0x1ba8[_0x2e5222];return _0x5a7f64;};import{Plugin as _0x35e9db}from'ckeditor5/src/core.js';import{CKEditorError as _0x8d0012}from'ckeditor5/src/utils.js';import{default as _0x5f2cc2,AIRequestError as _0x2f8655}from'./aiadapter.js';import{getTranslation as _0xea8bf0}from'../utils/common-translations.js';export default class _ extends _0x35e9db{static get['requires'](){return[_0x5f2cc2];}constructor(_0x4de12b){super(_0x4de12b),this[_0x5a7f('0xd')]=new AbortController();}[_0x5a7f('0x7')](){this[_0x5a7f('0x0')][_0x5a7f('0x15')][_0x5a7f('0x2')](_0x5f2cc2)['textAdapter']=this;}[_0x5a7f('0x4')](){this[_0x5a7f('0xd')][_0x5a7f('0x16')][_0x5a7f('0x1')]||(this[_0x5a7f('0xd')][_0x5a7f('0x4')](),this[_0x5a7f('0xd')]=new AbortController());}async[_0x5a7f('0x5')](_0x43eddd){return this[_0x5a7f('0x9')](_0x43eddd)[_0x5a7f('0xe')](_0x1cdf7a=>{if(_0x1cdf7a instanceof _0x2f8655)return Promise['reject'](_0x1cdf7a);if('AbortError'==_0x1cdf7a[_0x5a7f('0x12')])return Promise[_0x5a7f('0xb')]();throw console[_0x5a7f('0x13')](_0x1cdf7a),new _0x8d0012(_0x5a7f('0x8'),null);});}async[_0x5a7f('0xc')](_0x46ba52,_0x43d4cc){const _0x62c49a=this[_0x5a7f('0x0')][_0x5a7f('0xa')],_0x3b60d2={'Content-Type':_0x5a7f('0x10')};if(_0x5a7f('0xf')==typeof _0x46ba52)Object[_0x5a7f('0x14')](_0x3b60d2,_0x46ba52);else{if(_0x46ba52)try{const _0x398bda=await _0x46ba52(_0x43d4cc);Object[_0x5a7f('0x14')](_0x3b60d2,_0x398bda);}catch(_0x375d30){return Promise['reject'](new _0x2f8655(_0xea8bf0(_0x62c49a,'AI_ERROR_GET_HEADERS')));}}return _0x3b60d2;}async[_0x5a7f('0x6')](_0x164509,_0x409b60){const _0x205343=this[_0x5a7f('0x0')][_0x5a7f('0xa')];if(_0x5a7f('0xf')==typeof _0x164509)return _0x164509;try{return await _0x164509(_0x409b60);}catch(_0x3ba33c){return Promise[_0x5a7f('0x11')](new _0x2f8655(_0xea8bf0(_0x205343,_0x5a7f('0x3'))));}}}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* @module ai/adapters/awstextadapter
|
|
7
|
+
* @publicApi
|
|
8
|
+
*/
|
|
9
|
+
import { type Editor } from 'ckeditor5/src/core.js';
|
|
10
|
+
import { default as AITextAdapter, type AITextAdapterRequestData, type RequestHeaders, type RequestParameters } from './aitextadapter.js';
|
|
11
|
+
import { type BedrockRuntimeClientConfig } from '@aws-sdk/client-bedrock-runtime';
|
|
12
|
+
/**
|
|
13
|
+
* Adapter for AI text-related requests that supports the Amazon Bedrock service.
|
|
14
|
+
*
|
|
15
|
+
* See also {@link module:ai/adapters/aitextadapter~AITextAdapter}.
|
|
16
|
+
*/
|
|
17
|
+
export default class AWSTextAdapter extends AITextAdapter {
|
|
18
|
+
/**
|
|
19
|
+
* @inheritDoc
|
|
20
|
+
*/
|
|
21
|
+
static get pluginName(): "AWSTextAdapter";
|
|
22
|
+
/**
|
|
23
|
+
* @inheritDoc
|
|
24
|
+
*/
|
|
25
|
+
constructor(editor: Editor);
|
|
26
|
+
/**
|
|
27
|
+
* Performs the request to the Amazon Bedrock service or to the endpoint provided in the editor configuration.
|
|
28
|
+
*
|
|
29
|
+
* If you want to extend this adapter, you can overload this method to do some additional processing or make an external call.
|
|
30
|
+
*/
|
|
31
|
+
sendRequest({ query, context, onData, actionId }: AITextAdapterRequestData): Promise<void>;
|
|
32
|
+
/**
|
|
33
|
+
* Prepares the actual full prompt for the request to the Amazon Bedrock API.
|
|
34
|
+
*
|
|
35
|
+
* The prompt may differ depending on what model is used.
|
|
36
|
+
*
|
|
37
|
+
* You can overload this method to customize the prompts that are sent to the AI service.
|
|
38
|
+
*
|
|
39
|
+
* @param query The user's query. The instruction which the model should perform.
|
|
40
|
+
* @param context The context for the instruction. Usually this will be a part of the editor content. Can be empty.
|
|
41
|
+
* @param model Model that is used to perform the request.
|
|
42
|
+
* @param actionId ID of the performed action. See {@link module:ai/adapters/aitextadapter~AITextAdapterRequestData#actionId}.
|
|
43
|
+
*/
|
|
44
|
+
preparePrompt(query: string, context: string, model: string, actionId: string): Promise<string>;
|
|
45
|
+
/**
|
|
46
|
+
* For a given model, it returns the family to which this model belongs.
|
|
47
|
+
*
|
|
48
|
+
* For example, for `anthropic.claude-instant-v1` it will return `anthropic.claude`.
|
|
49
|
+
*
|
|
50
|
+
* Throws {@link module:ai/adapters/aiadapter~AIRequestError `AIRequestError`} if a given model is not supported.
|
|
51
|
+
*/
|
|
52
|
+
getModelFamily(model: string): AWSModelFamily;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* The configuration for the Amazon Bedrock adapter.
|
|
56
|
+
*
|
|
57
|
+
* The properties defined in this configuration are set in the `config.ai.aws` namespace.
|
|
58
|
+
*
|
|
59
|
+
* ```ts
|
|
60
|
+
* ClassicEditor
|
|
61
|
+
* .create( editorElement, {
|
|
62
|
+
* ai: {
|
|
63
|
+
* aws: {
|
|
64
|
+
* apiUrl: 'https://url.to.your.application/ai'
|
|
65
|
+
* }
|
|
66
|
+
* }
|
|
67
|
+
* } )
|
|
68
|
+
* .then( ... )
|
|
69
|
+
* .catch( ... );
|
|
70
|
+
* ```
|
|
71
|
+
*
|
|
72
|
+
* See {@link module:ai/aiconfig~AIConfig the full AI configuration}.
|
|
73
|
+
*
|
|
74
|
+
* See {@link module:core/editor/editorconfig~EditorConfig all editor options}.
|
|
75
|
+
*/
|
|
76
|
+
export interface AWSTextAdapterConfig {
|
|
77
|
+
/**
|
|
78
|
+
* The URL to which the AI service request will be sent.
|
|
79
|
+
*
|
|
80
|
+
* Set this value only if you want to connect to Amazon Bedrock using a proxy endpoint.
|
|
81
|
+
*
|
|
82
|
+
* If `apiUrl` is not provided, the AWS-SDK library will be used to process the request. It will set a proper URL on its own.
|
|
83
|
+
*/
|
|
84
|
+
apiUrl?: string;
|
|
85
|
+
/**
|
|
86
|
+
* An object with headers to set in the request to the AI service.
|
|
87
|
+
*
|
|
88
|
+
* **These headers are only used when connecting to AWS through the proxy endpoint (that is, when `apiUrl` is set).**
|
|
89
|
+
*
|
|
90
|
+
* Otherwise, the AWS-SDK library is used, and it sets the request headers on its own. In this mode, the request headers cannot be
|
|
91
|
+
* customized.
|
|
92
|
+
*
|
|
93
|
+
* If the provided value is an object, it is simply used as provided.
|
|
94
|
+
*
|
|
95
|
+
* ```js
|
|
96
|
+
* {
|
|
97
|
+
* ai: {
|
|
98
|
+
* aws: {
|
|
99
|
+
* requestHeaders: {
|
|
100
|
+
* 'Authorization': 'Bearer YOUR_API_KEY'
|
|
101
|
+
* }
|
|
102
|
+
* }
|
|
103
|
+
* // ...
|
|
104
|
+
* }
|
|
105
|
+
* }
|
|
106
|
+
* ```
|
|
107
|
+
*
|
|
108
|
+
* If the provided value is a function, it should be a function that returns a promise which resolves with the headers object.
|
|
109
|
+
* This way, you can perform an authorization request to your application and receive the authorization token (and also implement
|
|
110
|
+
* any custom logic on the backend side). The headers object is then used to make the actual call to the AI service.
|
|
111
|
+
*
|
|
112
|
+
* ```js
|
|
113
|
+
* {
|
|
114
|
+
* ai: {
|
|
115
|
+
* aws: {
|
|
116
|
+
* requestHeaders: async () => {
|
|
117
|
+
* const jwt = await fetch( 'https://example.com/jwt-endpoint' );
|
|
118
|
+
*
|
|
119
|
+
* return {
|
|
120
|
+
* 'Authorization': 'Bearer ' + jwt
|
|
121
|
+
* }
|
|
122
|
+
* }
|
|
123
|
+
* }
|
|
124
|
+
* // ...
|
|
125
|
+
* }
|
|
126
|
+
* }
|
|
127
|
+
* ```
|
|
128
|
+
*
|
|
129
|
+
* The function is passed the {@link module:ai/adapters/aitextadapter~AITextAdapterRequestData#actionId `actionId`} parameter to make it
|
|
130
|
+
* possible to further customize the headers.
|
|
131
|
+
*
|
|
132
|
+
* If the function fails for any reason, the promise should be rejected. In this case, a feature that made the request should display
|
|
133
|
+
* an error notification.
|
|
134
|
+
*/
|
|
135
|
+
requestHeaders?: RequestHeaders;
|
|
136
|
+
/**
|
|
137
|
+
* Additional configuration parameters for the AI service request. Use it to customize how the AI service generates responses.
|
|
138
|
+
*
|
|
139
|
+
* The exact configuration (available parameters) depends on the used model. Keep in mind that some properties are not supported by some
|
|
140
|
+
* models.
|
|
141
|
+
*
|
|
142
|
+
* See [AWS model parameters reference](https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters.html) to learn more.
|
|
143
|
+
*
|
|
144
|
+
* If the provided value is an object, it is passed to the request as provided.
|
|
145
|
+
*
|
|
146
|
+
* If the provided value is a function, it should be a function that returns a promise which resolves with the request parameters
|
|
147
|
+
* object. This gives you more flexibility to provide parameters for the AI model.
|
|
148
|
+
*
|
|
149
|
+
* The function is passed the {@link module:ai/adapters/aitextadapter~AITextAdapterRequestData#actionId `actionId`} parameter to make it
|
|
150
|
+
* possible to further customize the parameters.
|
|
151
|
+
*
|
|
152
|
+
* If the function fails for any reason, the promise should be rejected. In this case, the feature that made the request should display
|
|
153
|
+
* an error notification.
|
|
154
|
+
*
|
|
155
|
+
* Defaults to:
|
|
156
|
+
*
|
|
157
|
+
* ```json
|
|
158
|
+
* {
|
|
159
|
+
* model: 'anthropic.claude-v2',
|
|
160
|
+
* max_tokens_to_sample: 500,
|
|
161
|
+
* temperature: 1,
|
|
162
|
+
* top_k: 250,
|
|
163
|
+
* top_p: 1,
|
|
164
|
+
* anthropic_version: 'bedrock-2023-05-31',
|
|
165
|
+
* stream: true
|
|
166
|
+
* }
|
|
167
|
+
* ```
|
|
168
|
+
*/
|
|
169
|
+
requestParameters?: RequestParameters;
|
|
170
|
+
/**
|
|
171
|
+
* The configuration for AWS-SDK Bedrock Runtime Client.
|
|
172
|
+
*
|
|
173
|
+
* If `apiUrl` is not provided, this configuration will be used to initialize the Bedrock Runtime Client which will be used to process
|
|
174
|
+
* the requests. The adapter will make requests directly to the Amazon Bedrock service.
|
|
175
|
+
*/
|
|
176
|
+
bedrockClientConfig?: BedrockRuntimeClientConfig;
|
|
177
|
+
}
|
|
178
|
+
export type AWSModelFamily = 'anthropic.claude' | 'ai21.j2' | 'cohere.command' | 'meta.llama';
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
+
*
|
|
4
|
+
*
|
|
5
|
+
*
|
|
6
|
+
*
|
|
7
|
+
* +---------------------------------------------------------------------------------+
|
|
8
|
+
* | |
|
|
9
|
+
* | Hello stranger! |
|
|
10
|
+
* | |
|
|
11
|
+
* | |
|
|
12
|
+
* | What you're currently looking at is the source code of a legally protected, |
|
|
13
|
+
* | proprietary software. Any attempts to deobfuscate / disassemble this code |
|
|
14
|
+
* | are forbidden and will result in legal consequences. |
|
|
15
|
+
* | |
|
|
16
|
+
* | |
|
|
17
|
+
* +---------------------------------------------------------------------------------+
|
|
18
|
+
*
|
|
19
|
+
*
|
|
20
|
+
*
|
|
21
|
+
*
|
|
22
|
+
*/
|
|
23
|
+
const _0x49e3=['cohere.command','_apiUrl','split','ai.aws.requestParameters.anthropic_version','parse','preparePrompt','completions','read','json','meta.llama','_prepareRequestParameters','body','decode','join','pluginName','\x0a\x0aAssistant:','map','bytes','application/json','from','<<SYS>>\x0a','ai.aws.requestParameters.max_tokens_to_sample','chunk','bedrock-2023-05-31','editor','resolve','Instruction:\x0a','AWSTextAdapter','_processAWSRequest','data','startsWith','stringify','_readStreamableResponse','get','\x0aYou\x20must\x20keep\x20the\x20text\x20formatting.\x0a\x0aContent:\x0a','ai.aws.requestParameters.top_p','utf-8','_requestHeaders','_readStaticResponse','completion','_readAsyncIterableResponse','signal','utf8','text','_getGeneratedText','filter','getReader','\x0a<</SYS>>\x0a[INST]\x0a','test','getModelFamily','sendRequest','generations','_bedrockClientConfig','Human:\x0a','define','ai.aws.requestParameters.stream','config','POST','_requestParameters','anthropic.claude-v2','ai.aws.requestParameters.model','Your\x20task\x20is\x20to\x20generate\x20HTML\x20content\x20accordingly\x20to\x20the\x20given\x20instruction.\x20Never\x20include\x20<img>\x20tag\x20in\x20your\x20response\x20even\x20if\x20asked\x20for.\x20Your\x20answer\x20must\x20be\x20a\x20well-structured\x20and\x20properly\x20formatted\x20HTML\x20code.\x20Answer\x20only\x20with\x20the\x20generated\x20HTML\x20content.\x20Do\x20not\x20add\x20any\x20additional\x20remarks\x20or\x20notes.\x20Do\x20not\x20act\x20like\x20a\x20chatbot\x20or\x20a\x20real\x20person.','_processProxyRequest','ai.aws.requestParameters.temperature','length','ai21.j2','locale','ai.aws.bedrockClientConfig','\x0a[/INST]','_prepareRequestHeaders','ai.aws.requestParameters','abortController','ai.aws.apiUrl'];(function(_0x829635,_0x49e348){const _0x12e164=function(_0x3af88b){while(--_0x3af88b){_0x829635['push'](_0x829635['shift']());}};_0x12e164(++_0x49e348);}(_0x49e3,0x86));const _0x12e1=function(_0x829635,_0x49e348){_0x829635=_0x829635-0x0;let _0x12e164=_0x49e3[_0x829635];return _0x12e164;};import{default as _0x4059f9}from'./aitextadapter.js';import{AIRequestError as _0x5dc193}from'./aiadapter.js';import{BedrockRuntimeClient as _0x359174,InvokeModelCommand as _0x32f221,InvokeModelWithResponseStreamCommand as _0x354837}from'@aws-sdk/client-bedrock-runtime';import{getTranslation as _0x4eaf34}from'../utils/common-translations.js';export default class H extends _0x4059f9{static get[_0x12e1('0x1a')](){return _0x12e1('0x27');}constructor(_0x14a070){super(_0x14a070),_0x14a070[_0x12e1('0x44')]['get'](_0x12e1('0x9'))||(_0x14a070[_0x12e1('0x44')][_0x12e1('0x42')](_0x12e1('0x48'),_0x12e1('0x47')),_0x14a070[_0x12e1('0x44')]['define'](_0x12e1('0x21'),0x7d0),_0x14a070[_0x12e1('0x44')]['define'](_0x12e1('0x2'),0x1),_0x14a070['config'][_0x12e1('0x42')](_0x12e1('0x2f'),0x1),_0x14a070[_0x12e1('0x44')][_0x12e1('0x42')]('ai.aws.requestParameters.top_k',0xfa),_0x14a070['config'][_0x12e1('0x42')](_0x12e1('0xf'),_0x12e1('0x23')),_0x14a070[_0x12e1('0x44')][_0x12e1('0x42')](_0x12e1('0x43'),!0x0)),this[_0x12e1('0xd')]=_0x14a070[_0x12e1('0x44')][_0x12e1('0x2d')](_0x12e1('0xb')),this['_bedrockClientConfig']=_0x14a070[_0x12e1('0x44')]['get'](_0x12e1('0x6')),this[_0x12e1('0x31')]=_0x14a070[_0x12e1('0x44')][_0x12e1('0x2d')]('ai.aws.requestHeaders'),this[_0x12e1('0x46')]=_0x14a070[_0x12e1('0x44')]['get']('ai.aws.requestParameters');}async[_0x12e1('0x3e')]({query:_0x59c7b6,context:_0x4d2be0,onData:_0xf86a97,actionId:_0x32a69d}){const {model:_0x2de079,stream:_0x17641d,..._0x4fd126}=await this[_0x12e1('0x16')](this[_0x12e1('0x46')],_0x32a69d),_0x5a6709={'prompt':await this[_0x12e1('0x11')](_0x59c7b6,_0x4d2be0,_0x2de079,_0x32a69d),..._0x4fd126};this[_0x12e1('0xd')]?await this[_0x12e1('0x1')](_0x5a6709,_0x2de079,_0x17641d,_0xf86a97,_0x32a69d):await this['_processAWSRequest'](_0x5a6709,_0x2de079,_0x17641d,_0xf86a97);}async[_0x12e1('0x11')](_0x4d6bc5,_0x3a5dfe,_0x1c5f5e,_0x5b8fb3){let _0x1e2579,_0x2bb547;return _0x3a5dfe?(_0x1e2579='Your\x20task\x20is\x20to\x20execute\x20the\x20instruction\x20using\x20the\x20provided\x20HTML\x20content.\x20Follow\x20the\x20instruction\x20closely.\x20Your\x20answer\x20must\x20be\x20a\x20properly\x20formatted\x20HTML\x20code.\x20Do\x20not\x20add\x20any\x20additional\x20remarks\x20or\x20notes.\x20Do\x20not\x20act\x20like\x20a\x20chatbot\x20or\x20a\x20real\x20person.',_0x2bb547=_0x12e1('0x26')+_0x4d6bc5+_0x12e1('0x2e')+(_0x3a5dfe[_0x12e1('0x2a')]('<')?_0x3a5dfe:'<span>'+_0x3a5dfe+'</span>')):(_0x1e2579=_0x12e1('0x0'),_0x2bb547=_0x4d6bc5),'meta.llama'==this[_0x12e1('0x3d')](_0x1c5f5e)?_0x12e1('0x20')+_0x1e2579+_0x12e1('0x3b')+_0x2bb547+_0x12e1('0x7'):_0x12e1('0x41')+_0x1e2579+'\x0a'+_0x2bb547+_0x12e1('0x1b');}[_0x12e1('0x3d')](_0x566303){if(/^anthropic\.claude/[_0x12e1('0x3c')](_0x566303))return'anthropic.claude';if(/^ai21\.j2/[_0x12e1('0x3c')](_0x566303))return'ai21.j2';if(/^cohere\.command/[_0x12e1('0x3c')](_0x566303))return _0x12e1('0xc');if(/^meta\.llama/['test'](_0x566303))return _0x12e1('0x15');throw new _0x5dc193(_0x4eaf34(this[_0x12e1('0x24')][_0x12e1('0x5')],'AI_ERROR_UNSUPPORTED_MODEL',_0x566303));}async[_0x12e1('0x1')](_0x4c8dd5,_0x3b2d1b,_0xa5ec4a,_0x4b1161,_0xafca01){const _0x59a577={'method':_0x12e1('0x45'),'headers':await this[_0x12e1('0x8')](this[_0x12e1('0x31')],_0xafca01),'body':JSON[_0x12e1('0x2b')]({..._0x4c8dd5,'model':_0x3b2d1b,'stream':_0xa5ec4a}),'signal':this['abortController'][_0x12e1('0x35')]},_0x4f466b=await fetch(this['_apiUrl'],_0x59a577);_0xa5ec4a?await this[_0x12e1('0x2c')](_0x4f466b,_0x3b2d1b,_0x4b1161):await this[_0x12e1('0x32')](_0x4f466b,_0x3b2d1b,_0x4b1161);}async[_0x12e1('0x28')](_0x177038,_0x425a5e,_0x4ee445,_0x46ae24){const _0x51ab2a=new _0x359174(this[_0x12e1('0x40')]),_0x7ab5d={'body':JSON['stringify'](_0x177038),'contentType':_0x12e1('0x1e'),'accept':'application/json','modelId':_0x425a5e};let _0x3fbf4c;if(_0x4ee445){const _0x298afe=new _0x354837(_0x7ab5d);_0x3fbf4c=await _0x51ab2a['send'](_0x298afe,{'abortSignal':this['abortController']['signal']}),await this[_0x12e1('0x34')](_0x3fbf4c,_0x425a5e,_0x46ae24);}else{const _0x17a166=new _0x32f221(_0x7ab5d);_0x3fbf4c=await _0x51ab2a['send'](_0x17a166,{'abortSignal':this[_0x12e1('0xa')][_0x12e1('0x35')]}),await this[_0x12e1('0x32')](_0x3fbf4c,_0x425a5e,_0x46ae24);}}async[_0x12e1('0x34')](_0x3752ed,_0x489084,_0x35eadd){const _0x18a614=_0x3752ed[_0x12e1('0x17')],_0x565f39=new TextDecoder('utf-8');let _0x3cd51b='';for await(const _0x5cbff5 of _0x18a614)if(_0x5cbff5[_0x12e1('0x22')]){const _0x2b7f70=_0x565f39[_0x12e1('0x18')](_0x5cbff5[_0x12e1('0x22')][_0x12e1('0x1d')]),_0xbd7b90=JSON[_0x12e1('0x10')](_0x2b7f70);_0x3cd51b+=this[_0x12e1('0x38')](_0xbd7b90,_0x489084),_0x35eadd(_0x3cd51b);}return Promise[_0x12e1('0x25')]();}async[_0x12e1('0x2c')](_0x2fd4e9,_0x59a888,_0x2c92a8){const _0x4af78c=_0x2fd4e9[_0x12e1('0x17')],_0x87be23=new TextDecoder(_0x12e1('0x30')),_0x5e6abb=_0x4af78c[_0x12e1('0x3a')]();let _0x134601='',_0xa07a='',_0x229092=!0x1;for(;!_0x229092;){const {done:_0x2e04c2,value:_0x555071}=await _0x5e6abb[_0x12e1('0x13')]();if(_0x229092=_0x2e04c2,_0x229092)break;const _0x2dd88e=(_0x134601+_0x87be23[_0x12e1('0x18')](_0x555071))[_0x12e1('0xe')]('\x0a')[_0x12e1('0x1c')](_0x93d769=>{try{return _0x134601='',JSON[_0x12e1('0x10')](_0x93d769);}catch(_0x5d8926){return _0x134601=_0x93d769,null;}})[_0x12e1('0x39')](_0x2895ba=>null!==_0x2895ba);_0x2dd88e[_0x12e1('0x3')]&&(_0xa07a+=_0x2dd88e['map'](_0x577b1b=>this[_0x12e1('0x38')](_0x577b1b,_0x59a888))[_0x12e1('0x19')](''),_0x2c92a8(_0xa07a));}return Promise['resolve']();}async[_0x12e1('0x32')](_0x569107,_0x268273,_0x4dbd20){let _0x52b6ef;return _0x52b6ef=_0x569107 instanceof Response?await _0x569107[_0x12e1('0x14')]():JSON['parse'](Buffer[_0x12e1('0x1f')](_0x569107[_0x12e1('0x17')])['toString'](_0x12e1('0x36'))),(_0x4dbd20(this['_getGeneratedText'](_0x52b6ef,_0x268273)),Promise[_0x12e1('0x25')]());}[_0x12e1('0x38')](_0x3f63c6,_0x58e20b){switch(this[_0x12e1('0x3d')](_0x58e20b)){case'anthropic.claude':return _0x3f63c6[_0x12e1('0x33')];case _0x12e1('0x4'):return _0x3f63c6[_0x12e1('0x12')][0x0][_0x12e1('0x29')][_0x12e1('0x37')];case _0x12e1('0xc'):return _0x3f63c6[_0x12e1('0x3f')][0x0][_0x12e1('0x37')];case _0x12e1('0x15'):return _0x3f63c6['generation'];}}}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* @module ai/adapters/openaitextadapter
|
|
7
|
+
* @publicApi
|
|
8
|
+
*/
|
|
9
|
+
import { type Editor } from 'ckeditor5/src/core.js';
|
|
10
|
+
import { default as AITextAdapter, type AITextAdapterRequestData, type RequestHeaders, type RequestParameters } from './aitextadapter.js';
|
|
11
|
+
/**
|
|
12
|
+
* Adapter for AI text-related requests that supports OpenAI and Azure OpenAI services.
|
|
13
|
+
*
|
|
14
|
+
* See also {@link module:ai/adapters/aitextadapter~AITextAdapter}.
|
|
15
|
+
*/
|
|
16
|
+
export default class OpenAITextAdapter extends AITextAdapter {
|
|
17
|
+
/**
|
|
18
|
+
* @inheritDoc
|
|
19
|
+
*/
|
|
20
|
+
static get pluginName(): "OpenAITextAdapter";
|
|
21
|
+
/**
|
|
22
|
+
* @inheritDoc
|
|
23
|
+
*/
|
|
24
|
+
constructor(editor: Editor);
|
|
25
|
+
/**
|
|
26
|
+
* Performs the request to the OpenAI service, Azure OpenAI service, or to the endpoint provided in the editor configuration.
|
|
27
|
+
*
|
|
28
|
+
* If you want to extend this adapter, you can overload this method to do some additional processing or make an external call.
|
|
29
|
+
*/
|
|
30
|
+
sendRequest({ query, context, onData, actionId }: AITextAdapterRequestData): Promise<void>;
|
|
31
|
+
/**
|
|
32
|
+
* Prepares the actual request messages to be sent as a part of the request to the AI service.
|
|
33
|
+
*
|
|
34
|
+
* See [OpenAI API reference](https://platform.openai.com/docs/api-reference/chat/create) to learn more.
|
|
35
|
+
*
|
|
36
|
+
* This method returns two messages, one "system message" with general instructions for the model and one "user message"
|
|
37
|
+
* with the actual instruction to perform.
|
|
38
|
+
*
|
|
39
|
+
* The messages are different, based on whether there is any `context` provided. If no `context` is provided, it is assumed that
|
|
40
|
+
* the user wants to generate new content. If `context` is provided, it is assumed that the user wants to process this `context`
|
|
41
|
+
* (e.g. translate) or generate something based on `context` (e.g. summary).
|
|
42
|
+
*
|
|
43
|
+
* You can overload this method to customize the messages that are sent to the AI service.
|
|
44
|
+
*
|
|
45
|
+
* @param query The user's query. The instruction which the model should perform.
|
|
46
|
+
* @param context The context for the instruction. Usually this will be a part of the editor content. Can be empty.
|
|
47
|
+
* @param actionId ID of the performed action. See {@link module:ai/adapters/aitextadapter~AITextAdapterRequestData#actionId}.
|
|
48
|
+
*/
|
|
49
|
+
prepareMessages(query: string, context: string, actionId: string): Promise<Array<RequestMessageItem>>;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* The configuration for the OpenAI adapter.
|
|
53
|
+
*
|
|
54
|
+
* The properties defined in this config are set in the `config.ai.openAI` namespace.
|
|
55
|
+
*
|
|
56
|
+
* ```ts
|
|
57
|
+
* ClassicEditor
|
|
58
|
+
* .create( editorElement, {
|
|
59
|
+
* ai: {
|
|
60
|
+
* openAI: {
|
|
61
|
+
* requestHeaders: {
|
|
62
|
+
* Authorization: 'Bearer OPENAI_API_KEY'
|
|
63
|
+
* }
|
|
64
|
+
* }
|
|
65
|
+
* }
|
|
66
|
+
* } )
|
|
67
|
+
* .then( ... )
|
|
68
|
+
* .catch( ... );
|
|
69
|
+
* ```
|
|
70
|
+
*
|
|
71
|
+
* See {@link module:ai/aiconfig~AIConfig the full AI configuration}.
|
|
72
|
+
*
|
|
73
|
+
* See {@link module:core/editor/editorconfig~EditorConfig all editor options}.
|
|
74
|
+
*/
|
|
75
|
+
export interface OpenAITextAdapterConfig {
|
|
76
|
+
/**
|
|
77
|
+
* The URL to which the AI service request will be sent.
|
|
78
|
+
*
|
|
79
|
+
* By default, requests are sent to the OpenAI API service. Change this value to make requests to Azure OpenAI API or use proxy endpoint
|
|
80
|
+
* in your application instead.
|
|
81
|
+
*
|
|
82
|
+
* Defaults to `'https://api.openai.com/v1/chat/completions'`.
|
|
83
|
+
*/
|
|
84
|
+
apiUrl?: string;
|
|
85
|
+
/**
|
|
86
|
+
* Object with headers to set in the request to the AI service API.
|
|
87
|
+
*
|
|
88
|
+
* If the provided value is an `object`, it is simply used as provided.
|
|
89
|
+
*
|
|
90
|
+
* If you are connecting directly to the OpenAI API, use your OpenAI API key in the following way:
|
|
91
|
+
*
|
|
92
|
+
* ```js
|
|
93
|
+
* {
|
|
94
|
+
* ai: {
|
|
95
|
+
* openAI: {
|
|
96
|
+
* requestHeaders: {
|
|
97
|
+
* 'Authorization': 'Bearer YOUR_API_KEY'
|
|
98
|
+
* }
|
|
99
|
+
* // ...
|
|
100
|
+
* }
|
|
101
|
+
* // ..
|
|
102
|
+
* }
|
|
103
|
+
* }
|
|
104
|
+
* ```
|
|
105
|
+
*
|
|
106
|
+
* **Important: use your API key ONLY in a development environment or for testing purposes!**
|
|
107
|
+
* In the production environment, pass your request through a proxy endpoint.
|
|
108
|
+
*
|
|
109
|
+
* If you are using a proxy service to send requests to the OpenAI API, `requestHeaders` can be used to implement authorization for your
|
|
110
|
+
* requests.
|
|
111
|
+
*
|
|
112
|
+
* If the provided value is a function, it should be a function that returns a `Promise` which resolves with the headers object.
|
|
113
|
+
* This way, you can perform an authorization request to your application and receive the authorization token (and also implement
|
|
114
|
+
* any custom logic on the back-end side). The headers object is then used to make the actual call to the AI service.
|
|
115
|
+
*
|
|
116
|
+
* The function is passed {@link module:ai/adapters/aitextadapter~AITextAdapterRequestData#actionId `actionId`} parameter to make it
|
|
117
|
+
* possible to further customize the headers.
|
|
118
|
+
*
|
|
119
|
+
* ```js
|
|
120
|
+
* {
|
|
121
|
+
* ai: {
|
|
122
|
+
* openAI: {
|
|
123
|
+
* requestHeaders: async () => {
|
|
124
|
+
* const jwt = await fetch( 'https://example.com/jwt-endpoint' );
|
|
125
|
+
*
|
|
126
|
+
* return {
|
|
127
|
+
* 'Authorization': 'Bearer ' + jwt
|
|
128
|
+
* };
|
|
129
|
+
* }
|
|
130
|
+
* // ...
|
|
131
|
+
* }
|
|
132
|
+
* }
|
|
133
|
+
* }
|
|
134
|
+
* ```
|
|
135
|
+
*
|
|
136
|
+
* If the function fails for any reason, the promise should be rejected. In this case, a feature that made the request should display
|
|
137
|
+
* an error notification.
|
|
138
|
+
*/
|
|
139
|
+
requestHeaders?: RequestHeaders;
|
|
140
|
+
/**
|
|
141
|
+
* Additional configuration parameters for the AI service request. Use it to customize how the AI service generates responses.
|
|
142
|
+
*
|
|
143
|
+
* See [OpenAI API reference](https://platform.openai.com/docs/api-reference/chat/create) to learn more about available parameters.
|
|
144
|
+
*
|
|
145
|
+
* If the provided value is an `object`, it is passed to the request as provided.
|
|
146
|
+
*
|
|
147
|
+
* If the provided value is a function, it should be a function that returns a `Promise` which resolves with the request parameters
|
|
148
|
+
* object. This gives you more flexibility to provide parameters for the AI model.
|
|
149
|
+
*
|
|
150
|
+
* The function is passed {@link module:ai/adapters/aitextadapter~AITextAdapterRequestData#actionId `actionId`} parameter to make it
|
|
151
|
+
* possible to further customize the parameters.
|
|
152
|
+
*
|
|
153
|
+
* If the function fails for any reason, the promise should be rejected. In this case, the feature that made the request should display
|
|
154
|
+
* an error notification.
|
|
155
|
+
*
|
|
156
|
+
* Defaults to:
|
|
157
|
+
*
|
|
158
|
+
* ```json
|
|
159
|
+
* {
|
|
160
|
+
* model: 'gpt-3.5-turbo',
|
|
161
|
+
* max_tokens: 2000,
|
|
162
|
+
* temperature: 1,
|
|
163
|
+
* top_p: 1
|
|
164
|
+
* }
|
|
165
|
+
* ```
|
|
166
|
+
*/
|
|
167
|
+
requestParameters?: RequestParameters;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Single message item, in the format required for the OpenAI request.
|
|
171
|
+
*
|
|
172
|
+
* Example:
|
|
173
|
+
*
|
|
174
|
+
* ```json
|
|
175
|
+
* { role: 'system', content: 'You are a helpful assistant.' }
|
|
176
|
+
* ```
|
|
177
|
+
*
|
|
178
|
+
* See [OpenAI API reference](https://platform.openai.com/docs/api-reference/chat/create) to learn more.
|
|
179
|
+
*/
|
|
180
|
+
type RequestMessageItem = Record<string, string>;
|
|
181
|
+
export {};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
+
*
|
|
4
|
+
*
|
|
5
|
+
*
|
|
6
|
+
*
|
|
7
|
+
* +---------------------------------------------------------------------------------+
|
|
8
|
+
* | |
|
|
9
|
+
* | Hello stranger! |
|
|
10
|
+
* | |
|
|
11
|
+
* | |
|
|
12
|
+
* | What you're currently looking at is the source code of a legally protected, |
|
|
13
|
+
* | proprietary software. Any attempts to deobfuscate / disassemble this code |
|
|
14
|
+
* | are forbidden and will result in legal consequences. |
|
|
15
|
+
* | |
|
|
16
|
+
* | |
|
|
17
|
+
* +---------------------------------------------------------------------------------+
|
|
18
|
+
*
|
|
19
|
+
*
|
|
20
|
+
*
|
|
21
|
+
*
|
|
22
|
+
*/
|
|
23
|
+
const _0x5718=['ai.openAI.apiUrl','context_length_exceeded','define','parse','replace','Instruction:\x0a','json','ai.openAI.requestParameters.top_p','Moderation','_apiUrl','[DONE]','_readStreamableResponse','text','_requestParameters','editor','ai.openAI.requestHeaders','_isBufferIncomplete','prepareMessages','split','ai.openAI.requestParameters.max_tokens','Your\x20task\x20is\x20to\x20execute\x20the\x20instruction\x20using\x20the\x20provided\x20HTML\x20content.\x20Follow\x20the\x20instruction\x20closely.\x20Your\x20answer\x20must\x20be\x20a\x20properly\x20formatted\x20HTML\x20code.\x20Do\x20not\x20add\x20any\x20additional\x20remarks\x20or\x20notes.\x20Do\x20not\x20act\x20like\x20a\x20chatbot\x20or\x20a\x20real\x20person.','</span>','ai.openAI.requestParameters.temperature','config','includes','length','delta','_requestHeaders','locale','gpt-3.5-turbo','filter','decode','join','map','AI_ERROR_MODERATION','OpenAITextAdapter','pluginName','stream','content','resolve','ai.openAI.requestParameters.stream','body','AI_ERROR_CONTEXT_LENGTH','\x0aYou\x20must\x20keep\x20the\x20text\x20formatting.\x0a\x0aContent:\x0a','stringify','choices','<span>','get','ai.openAI.requestParameters.model','_cleanUpData','POST','system','reject','user','sendRequest','read','data:\x20','_prepareRequestHeaders'];(function(_0x18e373,_0x571871){const _0x30e599=function(_0x3bb0db){while(--_0x3bb0db){_0x18e373['push'](_0x18e373['shift']());}};_0x30e599(++_0x571871);}(_0x5718,0xcc));const _0x30e5=function(_0x18e373,_0x571871){_0x18e373=_0x18e373-0x0;let _0x30e599=_0x5718[_0x18e373];return _0x30e599;};import{default as _0x1d87fc}from'./aitextadapter.js';import{AIRequestError as _0x38f10d}from'./aiadapter.js';import{getTranslation as _0x2c3f16}from'../utils/common-translations.js';export default class M extends _0x1d87fc{static get[_0x30e5('0x6')](){return _0x30e5('0x5');}constructor(_0x3a2ed2){super(_0x3a2ed2),_0x3a2ed2['config'][_0x30e5('0x1e')](_0x30e5('0x1c'),'https://api.openai.com/v1/chat/completions'),_0x3a2ed2[_0x30e5('0x33')]['define'](_0x30e5('0x12'),_0x30e5('0x39')),_0x3a2ed2['config'][_0x30e5('0x1e')](_0x30e5('0x2f'),0x7d0),_0x3a2ed2[_0x30e5('0x33')]['define'](_0x30e5('0x32'),0x1),_0x3a2ed2[_0x30e5('0x33')]['define'](_0x30e5('0x23'),0x1),_0x3a2ed2[_0x30e5('0x33')][_0x30e5('0x1e')](_0x30e5('0xa'),!0x0),this[_0x30e5('0x25')]=_0x3a2ed2[_0x30e5('0x33')][_0x30e5('0x11')](_0x30e5('0x1c')),this[_0x30e5('0x37')]=_0x3a2ed2[_0x30e5('0x33')][_0x30e5('0x11')](_0x30e5('0x2b'))||{},this[_0x30e5('0x29')]=_0x3a2ed2[_0x30e5('0x33')][_0x30e5('0x11')]('ai.openAI.requestParameters');}async[_0x30e5('0x18')]({query:_0x73ba9d,context:_0x391999,onData:_0x346e8c,actionId:_0x2ef330}){const _0x4bfc8e=this[_0x30e5('0x2a')][_0x30e5('0x38')],_0x22376d=await this[_0x30e5('0x2d')](_0x73ba9d,_0x391999,_0x2ef330),_0x3c3d5e=await this[_0x30e5('0x1b')](this[_0x30e5('0x37')],_0x2ef330),_0x223608=await this['_prepareRequestParameters'](this[_0x30e5('0x29')],_0x2ef330),_0x53ae95={'messages':_0x22376d,..._0x223608},_0x2fcebf={'method':_0x30e5('0x14'),'headers':_0x3c3d5e,'body':JSON[_0x30e5('0xe')](_0x53ae95),'signal':this['abortController']['signal']},_0x1bf028=await fetch(this[_0x30e5('0x25')],_0x2fcebf);if(_0x1bf028['ok'])return _0x223608[_0x30e5('0x7')]?await this[_0x30e5('0x27')](_0x1bf028,_0x346e8c):await this['_readStaticResponse'](_0x1bf028,_0x346e8c),Promise[_0x30e5('0x9')]();{const _0x276108=await _0x1bf028[_0x30e5('0x28')]();return _0x276108[_0x30e5('0x34')](_0x30e5('0x1d'))?Promise['reject'](new _0x38f10d(_0x2c3f16(_0x4bfc8e,_0x30e5('0xc')))):_0x276108['includes'](_0x30e5('0x24'))?Promise[_0x30e5('0x16')](new _0x38f10d(_0x2c3f16(_0x4bfc8e,_0x30e5('0x4')))):Promise[_0x30e5('0x16')](new Error(_0x276108));}}async[_0x30e5('0x2d')](_0x19b7c3,_0x20d7bd,_0x418ed9){let _0x37d4f3,_0x18f4bb;return _0x20d7bd?(_0x37d4f3=_0x30e5('0x30'),_0x18f4bb=_0x30e5('0x21')+_0x19b7c3+_0x30e5('0xd')+(_0x20d7bd['startsWith']('<')?_0x20d7bd:_0x30e5('0x10')+_0x20d7bd+_0x30e5('0x31'))):(_0x37d4f3='Your\x20task\x20is\x20to\x20generate\x20HTML\x20content\x20accordingly\x20to\x20the\x20given\x20instruction.\x20Never\x20include\x20<img>\x20tag\x20in\x20your\x20response\x20even\x20if\x20asked\x20for.\x20Your\x20answer\x20must\x20be\x20a\x20well-structured\x20and\x20properly\x20formatted\x20HTML\x20code.\x20Answer\x20only\x20with\x20the\x20generated\x20HTML\x20content.\x20Do\x20not\x20add\x20any\x20additional\x20remarks\x20or\x20notes.\x20Do\x20not\x20act\x20like\x20a\x20chatbot\x20or\x20a\x20real\x20person.',_0x18f4bb=_0x19b7c3),[{'role':_0x30e5('0x15'),'content':_0x37d4f3},{'role':_0x30e5('0x17'),'content':_0x18f4bb}];}async[_0x30e5('0x27')](_0x551182,_0x1c0f16){const _0x1b2390=_0x551182[_0x30e5('0xb')]['getReader'](),_0x1121ae=new TextDecoder();let _0x3e4941='',_0x412a19='',_0x2a3af7='',_0x147bec=!0x1;for(;!_0x147bec;){const {done:_0x1ebafd,value:_0x59dcf7}=await _0x1b2390[_0x30e5('0x19')](),_0x1ffea7=_0x3e4941+_0x1121ae[_0x30e5('0x1')](_0x59dcf7);let _0x3d5a23='';_0x147bec=_0x1ebafd;try{_0x3d5a23=this['_cleanUpData'](_0x1ffea7),_0x3e4941='';}catch(_0x1c6949){_0x3e4941=_0x1ffea7;continue;}_0x412a19+=_0x3d5a23,!_0x147bec&&this['_isBufferIncomplete'](_0x412a19)||_0x412a19&&(_0x2a3af7+=_0x412a19,_0x412a19='',_0x1c0f16(_0x2a3af7));}return Promise[_0x30e5('0x9')]();}async['_readStaticResponse'](_0x3049e8,_0xbd6705){return _0xbd6705((await _0x3049e8[_0x30e5('0x22')]())[_0x30e5('0xf')][0x0]['message'][_0x30e5('0x8')]),Promise['resolve']();}[_0x30e5('0x2c')](_0x2a993e){const _0x13f403=_0x2a993e['split']('<')[_0x30e5('0x35')]!==_0x2a993e[_0x30e5('0x2e')]('>')[_0x30e5('0x35')],_0x3f2678=_0x2a993e[_0x30e5('0x35')]<0x1e;return _0x13f403&&_0x3f2678;}[_0x30e5('0x13')](_0x2f9436){return _0x2f9436[_0x30e5('0x2e')](/\n(?![^{]*\})/g)[_0x30e5('0x3')](_0x3cad8b=>_0x3cad8b[_0x30e5('0x20')](_0x30e5('0x1a'),''))[_0x30e5('0x0')](_0x4a862e=>_0x4a862e&&_0x4a862e['length']>0x0&&_0x30e5('0x26')!==_0x4a862e)['map'](_0xf39593=>{const {choices:_0x4b6b68}=JSON[_0x30e5('0x1f')](_0xf39593);if(_0x4b6b68[0x0]&&_0x4b6b68[0x0][_0x30e5('0x36')]&&_0x4b6b68[0x0][_0x30e5('0x36')]['content'])return _0x4b6b68[0x0]['delta']['content'];})[_0x30e5('0x0')](_0x655317=>_0x655317)[_0x30e5('0x2')]('');}}
|