@activepieces/piece-azure-openai 0.0.1
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 +7 -0
- package/package.json +20 -0
- package/src/index.d.ts +10 -0
- package/src/index.js +32 -0
- package/src/index.js.map +1 -0
- package/src/lib/actions/ask-gpt.d.ts +11 -0
- package/src/lib/actions/ask-gpt.js +106 -0
- package/src/lib/actions/ask-gpt.js.map +1 -0
- package/src/lib/common/index.d.ts +6 -0
- package/src/lib/common/index.js +57 -0
- package/src/lib/common/index.js.map +1 -0
package/README.md
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@activepieces/piece-azure-openai",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"dependencies": {
|
|
5
|
+
"@azure/openai": "1.0.0-beta.11",
|
|
6
|
+
"@sinclair/typebox": "0.26.8",
|
|
7
|
+
"axios": "^1.6.3",
|
|
8
|
+
"dayjs": "1.11.9",
|
|
9
|
+
"is-base64": "1.1.0",
|
|
10
|
+
"lodash": "4.17.21",
|
|
11
|
+
"nanoid": "3.3.6",
|
|
12
|
+
"semver": "7.6.0",
|
|
13
|
+
"tiktoken": "1.0.11",
|
|
14
|
+
"@activepieces/pieces-framework": "0.7.19",
|
|
15
|
+
"@activepieces/shared": "0.10.82",
|
|
16
|
+
"tslib": "1.14.1"
|
|
17
|
+
},
|
|
18
|
+
"main": "./src/index.js",
|
|
19
|
+
"type": "commonjs"
|
|
20
|
+
}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export declare const azureOpenaiAuth: import("@activepieces/pieces-framework").CustomAuthProperty<{
|
|
2
|
+
endpoint: import("@activepieces/pieces-framework").ShortTextProperty<true>;
|
|
3
|
+
apiKey: import("@activepieces/pieces-framework").SecretTextProperty<true>;
|
|
4
|
+
}>;
|
|
5
|
+
export type AzureOpenAIAuth = {
|
|
6
|
+
endpoint: string;
|
|
7
|
+
apiKey: string;
|
|
8
|
+
deploymentId: string;
|
|
9
|
+
};
|
|
10
|
+
export declare const azureOpenai: import("@activepieces/pieces-framework").Piece<import("@activepieces/pieces-framework").PieceAuthProperty>;
|
package/src/index.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.azureOpenai = exports.azureOpenaiAuth = void 0;
|
|
4
|
+
const pieces_framework_1 = require("@activepieces/pieces-framework");
|
|
5
|
+
const ask_gpt_1 = require("./lib/actions/ask-gpt");
|
|
6
|
+
exports.azureOpenaiAuth = pieces_framework_1.PieceAuth.CustomAuth({
|
|
7
|
+
props: {
|
|
8
|
+
endpoint: pieces_framework_1.Property.ShortText({
|
|
9
|
+
displayName: 'Endpoint',
|
|
10
|
+
description: 'https://<resource name>.openai.azure.com/',
|
|
11
|
+
required: true
|
|
12
|
+
}),
|
|
13
|
+
apiKey: pieces_framework_1.PieceAuth.SecretText({
|
|
14
|
+
displayName: 'API Key',
|
|
15
|
+
description: 'Use the Azure Portal to browse to your OpenAI resource and retrieve an API key',
|
|
16
|
+
required: true,
|
|
17
|
+
}),
|
|
18
|
+
},
|
|
19
|
+
required: true,
|
|
20
|
+
});
|
|
21
|
+
exports.azureOpenai = (0, pieces_framework_1.createPiece)({
|
|
22
|
+
displayName: 'Azure OpenAI',
|
|
23
|
+
auth: exports.azureOpenaiAuth,
|
|
24
|
+
minimumSupportedRelease: '0.9.0',
|
|
25
|
+
logoUrl: 'https://cdn.activepieces.com/pieces/azure-openai.png',
|
|
26
|
+
authors: ['MoShizzle'],
|
|
27
|
+
actions: [
|
|
28
|
+
ask_gpt_1.askGpt,
|
|
29
|
+
],
|
|
30
|
+
triggers: [],
|
|
31
|
+
});
|
|
32
|
+
//# sourceMappingURL=index.js.map
|
package/src/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../packages/pieces/community/azure-openai/src/index.ts"],"names":[],"mappings":";;;AAAA,qEAIwC;AACxC,mDAA+C;AAElC,QAAA,eAAe,GAAG,4BAAS,CAAC,UAAU,CAAC;IAClD,KAAK,EAAE;QACL,QAAQ,EAAE,2BAAQ,CAAC,SAAS,CAAC;YACzB,WAAW,EAAE,UAAU;YACvB,WAAW,EAAE,2CAA2C;YACxD,QAAQ,EAAE,IAAI;SACjB,CAAC;QACF,MAAM,EAAE,4BAAS,CAAC,UAAU,CAAC;YACzB,WAAW,EAAE,SAAS;YACtB,WAAW,EAAE,gFAAgF;YAC7F,QAAQ,EAAE,IAAI;SACjB,CAAC;KACH;IACD,QAAQ,EAAE,IAAI;CACf,CAAC,CAAC;AAQU,QAAA,WAAW,GAAG,IAAA,8BAAW,EAAC;IACrC,WAAW,EAAE,cAAc;IAC3B,IAAI,EAAE,uBAAe;IACrB,uBAAuB,EAAE,OAAO;IAChC,OAAO,EAAE,sDAAsD;IAC/D,OAAO,EAAE,CAAC,WAAW,CAAC;IACtB,OAAO,EAAE;QACP,gBAAM;KACP;IACD,QAAQ,EAAE,EAAE;CACb,CAAC,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare const askGpt: import("@activepieces/pieces-framework").IAction<import("@activepieces/pieces-framework").PieceAuthProperty, {
|
|
2
|
+
deploymentId: import("@activepieces/pieces-framework").ShortTextProperty<true>;
|
|
3
|
+
prompt: import("@activepieces/pieces-framework").LongTextProperty<true>;
|
|
4
|
+
temperature: import("@activepieces/pieces-framework").NumberProperty<false>;
|
|
5
|
+
maxTokens: import("@activepieces/pieces-framework").NumberProperty<true>;
|
|
6
|
+
topP: import("@activepieces/pieces-framework").NumberProperty<false>;
|
|
7
|
+
frequencyPenalty: import("@activepieces/pieces-framework").NumberProperty<false>;
|
|
8
|
+
presencePenalty: import("@activepieces/pieces-framework").NumberProperty<false>;
|
|
9
|
+
memoryKey: import("@activepieces/pieces-framework").ShortTextProperty<false>;
|
|
10
|
+
roles: import("@activepieces/pieces-framework").ArrayProperty<false>;
|
|
11
|
+
}>;
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.askGpt = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const pieces_framework_1 = require("@activepieces/pieces-framework");
|
|
6
|
+
const openai_1 = require("@azure/openai");
|
|
7
|
+
const common_1 = require("../common");
|
|
8
|
+
exports.askGpt = (0, pieces_framework_1.createAction)({
|
|
9
|
+
name: 'ask_gpt',
|
|
10
|
+
displayName: 'Ask GPT',
|
|
11
|
+
description: 'Ask ChatGPT anything you want!',
|
|
12
|
+
props: {
|
|
13
|
+
deploymentId: pieces_framework_1.Property.ShortText({
|
|
14
|
+
displayName: 'Deployment ID',
|
|
15
|
+
description: 'The ID of your model deployment or model to use.',
|
|
16
|
+
defaultValue: 'gpt-3.5-turbo',
|
|
17
|
+
required: true,
|
|
18
|
+
}),
|
|
19
|
+
prompt: pieces_framework_1.Property.LongText({
|
|
20
|
+
displayName: 'Question',
|
|
21
|
+
required: true,
|
|
22
|
+
}),
|
|
23
|
+
temperature: pieces_framework_1.Property.Number({
|
|
24
|
+
displayName: 'Temperature',
|
|
25
|
+
required: false,
|
|
26
|
+
description: 'Controls randomness: Lowering results in less random completions. As the temperature approaches zero, the model will become deterministic and repetitive.',
|
|
27
|
+
validators: [pieces_framework_1.Validators.minValue(0), pieces_framework_1.Validators.maxValue(1.0)],
|
|
28
|
+
defaultValue: 0.9,
|
|
29
|
+
}),
|
|
30
|
+
maxTokens: pieces_framework_1.Property.Number({
|
|
31
|
+
displayName: 'Maximum Tokens',
|
|
32
|
+
required: true,
|
|
33
|
+
description: "The maximum number of tokens to generate. Requests can use up to 2,048 or 4,096 tokens shared between prompt and completion depending on the model. Don't set the value to maximum and leave some tokens for the input. (One token is roughly 4 characters for normal English text)",
|
|
34
|
+
defaultValue: 2048,
|
|
35
|
+
}),
|
|
36
|
+
topP: pieces_framework_1.Property.Number({
|
|
37
|
+
displayName: 'Top P',
|
|
38
|
+
required: false,
|
|
39
|
+
description: 'An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered.',
|
|
40
|
+
defaultValue: 1,
|
|
41
|
+
}),
|
|
42
|
+
frequencyPenalty: pieces_framework_1.Property.Number({
|
|
43
|
+
displayName: 'Frequency penalty',
|
|
44
|
+
required: false,
|
|
45
|
+
description: "Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim.",
|
|
46
|
+
defaultValue: 0,
|
|
47
|
+
}),
|
|
48
|
+
presencePenalty: pieces_framework_1.Property.Number({
|
|
49
|
+
displayName: 'Presence penalty',
|
|
50
|
+
required: false,
|
|
51
|
+
description: "Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the mode's likelihood to talk about new topics.",
|
|
52
|
+
defaultValue: 0.6,
|
|
53
|
+
}),
|
|
54
|
+
memoryKey: pieces_framework_1.Property.ShortText({
|
|
55
|
+
displayName: 'Memory Key',
|
|
56
|
+
description: 'A memory key that will keep the chat history shared across runs and flows. Keep it empty to leave ChatGPT without memory of previous messages.',
|
|
57
|
+
required: false,
|
|
58
|
+
}),
|
|
59
|
+
roles: pieces_framework_1.Property.Array({
|
|
60
|
+
displayName: 'Roles',
|
|
61
|
+
required: false,
|
|
62
|
+
description: 'Array of roles to specify more accurate response',
|
|
63
|
+
defaultValue: ['You are a helpful assistant.'],
|
|
64
|
+
}),
|
|
65
|
+
},
|
|
66
|
+
run(context) {
|
|
67
|
+
var _a;
|
|
68
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
69
|
+
const { propsValue, store } = context;
|
|
70
|
+
const auth = context.auth;
|
|
71
|
+
const openai = new openai_1.OpenAIClient(auth.endpoint, new openai_1.AzureKeyCredential(auth.apiKey));
|
|
72
|
+
let messageHistory = [];
|
|
73
|
+
// If memory key is set, retrieve messages stored in history
|
|
74
|
+
if (propsValue.memoryKey) {
|
|
75
|
+
messageHistory = (_a = (yield store.get(propsValue.memoryKey, pieces_framework_1.StoreScope.PROJECT))) !== null && _a !== void 0 ? _a : [];
|
|
76
|
+
}
|
|
77
|
+
// Add user prompt to message history
|
|
78
|
+
messageHistory.push(propsValue.prompt);
|
|
79
|
+
// Add system instructions if set by user
|
|
80
|
+
const roles = propsValue.roles ? propsValue.roles : [];
|
|
81
|
+
const completion = yield openai.getCompletions(propsValue.deploymentId, [...roles, ...messageHistory], {
|
|
82
|
+
maxTokens: propsValue.maxTokens,
|
|
83
|
+
temperature: propsValue.temperature,
|
|
84
|
+
frequencyPenalty: propsValue.frequencyPenalty,
|
|
85
|
+
presencePenalty: propsValue.presencePenalty,
|
|
86
|
+
topP: propsValue.topP,
|
|
87
|
+
});
|
|
88
|
+
const responseText = completion.choices[0].text;
|
|
89
|
+
// Add response to message history
|
|
90
|
+
messageHistory = [...messageHistory, responseText];
|
|
91
|
+
// Check message history token size
|
|
92
|
+
// System limit is 32K tokens, we can probably make it bigger but this is a safe spot
|
|
93
|
+
const tokenLength = yield (0, common_1.calculateMessagesTokenSize)(messageHistory, '');
|
|
94
|
+
if (propsValue.memoryKey) {
|
|
95
|
+
// If tokens exceed 90% system limit or 90% of model limit - maxTokens, reduce history token size
|
|
96
|
+
if ((0, common_1.exceedsHistoryLimit)(tokenLength, '', propsValue.maxTokens)) {
|
|
97
|
+
messageHistory = yield (0, common_1.reduceContextSize)(messageHistory, '', propsValue.maxTokens);
|
|
98
|
+
}
|
|
99
|
+
// Store history
|
|
100
|
+
yield store.put(propsValue.memoryKey, messageHistory, pieces_framework_1.StoreScope.PROJECT);
|
|
101
|
+
}
|
|
102
|
+
return completion.choices[0].text;
|
|
103
|
+
});
|
|
104
|
+
},
|
|
105
|
+
});
|
|
106
|
+
//# sourceMappingURL=ask-gpt.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ask-gpt.js","sourceRoot":"","sources":["../../../../../../../../packages/pieces/community/azure-openai/src/lib/actions/ask-gpt.ts"],"names":[],"mappings":";;;;AACA,qEAKwC;AACxC,0CAAiE;AACjE,sCAA+F;AAElF,QAAA,MAAM,GAAG,IAAA,+BAAY,EAAC;IAC/B,IAAI,EAAE,SAAS;IACf,WAAW,EAAE,SAAS;IACtB,WAAW,EAAE,gCAAgC;IAC7C,KAAK,EAAE;QACH,YAAY,EAAE,2BAAQ,CAAC,SAAS,CAAC;YAC7B,WAAW,EAAE,eAAe;YAC5B,WAAW,EAAE,kDAAkD;YAC/D,YAAY,EAAE,eAAe;YAC7B,QAAQ,EAAE,IAAI;SACjB,CAAC;QACF,MAAM,EAAE,2BAAQ,CAAC,QAAQ,CAAC;YACtB,WAAW,EAAE,UAAU;YACvB,QAAQ,EAAE,IAAI;SACjB,CAAC;QACF,WAAW,EAAE,2BAAQ,CAAC,MAAM,CAAC;YACzB,WAAW,EAAE,aAAa;YAC1B,QAAQ,EAAE,KAAK;YACf,WAAW,EACP,2JAA2J;YAC/J,UAAU,EAAE,CAAC,6BAAU,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,6BAAU,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;YAC9D,YAAY,EAAE,GAAG;SACpB,CAAC;QACF,SAAS,EAAE,2BAAQ,CAAC,MAAM,CAAC;YACvB,WAAW,EAAE,gBAAgB;YAC7B,QAAQ,EAAE,IAAI;YACd,WAAW,EACP,qRAAqR;YACzR,YAAY,EAAE,IAAI;SACrB,CAAC;QACF,IAAI,EAAE,2BAAQ,CAAC,MAAM,CAAC;YAClB,WAAW,EAAE,OAAO;YACpB,QAAQ,EAAE,KAAK;YACf,WAAW,EACP,6OAA6O;YACjP,YAAY,EAAE,CAAC;SAClB,CAAC;QACF,gBAAgB,EAAE,2BAAQ,CAAC,MAAM,CAAC;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,KAAkB,CAAC,CAAC,CAAC,EAAE,CAAC;YAErE,MAAM,UAAU,GAAG,MAAM,MAAM,CAAC,cAAc,CAAC,UAAU,CAAC,YAAY,EAAE,CAAC,GAAG,KAAK,EAAE,GAAG,cAAc,CAAC,EAAE;gBACnG,SAAS,EAAE,UAAU,CAAC,SAAS;gBAC/B,WAAW,EAAE,UAAU,CAAC,WAAW;gBACnC,gBAAgB,EAAE,UAAU,CAAC,gBAAgB;gBAC7C,eAAe,EAAE,UAAU,CAAC,eAAe;gBAC3C,IAAI,EAAE,UAAU,CAAC,IAAI;aACxB,CAAC,CAAC;YAEH,MAAM,YAAY,GAAG,UAAU,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;YAEhD,kCAAkC;YAClC,cAAc,GAAG,CAAC,GAAG,cAAc,EAAE,YAAY,CAAC,CAAC;YAEnD,mCAAmC;YACnC,qFAAqF;YACrF,MAAM,WAAW,GAAG,MAAM,IAAA,mCAA0B,EAAC,cAAc,EAAE,EAAE,CAAC,CAAC;YACzE,IAAI,UAAU,CAAC,SAAS,EAAE;gBACxB,iGAAiG;gBACjG,IAAI,IAAA,4BAAmB,EAAC,WAAW,EAAE,EAAE,EAAE,UAAU,CAAC,SAAS,CAAC,EAAE;oBAC9D,cAAc,GAAG,MAAM,IAAA,0BAAiB,EACtC,cAAc,EACd,EAAE,EACF,UAAU,CAAC,SAAS,CACrB,CAAC;iBACH;gBACD,gBAAgB;gBAChB,MAAM,KAAK,CAAC,GAAG,CAAC,UAAU,CAAC,SAAS,EAAE,cAAc,EAAE,6BAAU,CAAC,OAAO,CAAC,CAAC;aAC3E;YAED,OAAO,UAAU,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;;KACrC;CACJ,CAAC,CAAC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export declare const calculateTokensFromString: (string: string, model: string) => number;
|
|
2
|
+
export declare const calculateMessagesTokenSize: (messages: string[], model: string) => Promise<number>;
|
|
3
|
+
export declare const reduceContextSize: (messages: string[], model: string, maxTokens: number) => Promise<string[]>;
|
|
4
|
+
export declare const exceedsHistoryLimit: (tokenLength: number, model: string, maxTokens: number) => boolean;
|
|
5
|
+
export declare const tokenLimit = 32000;
|
|
6
|
+
export declare const modelTokenLimit: (model: string) => number;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.modelTokenLimit = exports.tokenLimit = exports.exceedsHistoryLimit = exports.reduceContextSize = exports.calculateMessagesTokenSize = exports.calculateTokensFromString = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const tiktoken_1 = require("tiktoken");
|
|
6
|
+
const calculateTokensFromString = (string, model) => {
|
|
7
|
+
try {
|
|
8
|
+
const encoder = (0, tiktoken_1.encoding_for_model)(model);
|
|
9
|
+
const tokens = encoder.encode(string);
|
|
10
|
+
encoder.free();
|
|
11
|
+
return tokens.length;
|
|
12
|
+
}
|
|
13
|
+
catch (e) {
|
|
14
|
+
// Model not supported by tiktoken, every 4 chars is a token
|
|
15
|
+
return Math.round(string.length / 4);
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
exports.calculateTokensFromString = calculateTokensFromString;
|
|
19
|
+
const calculateMessagesTokenSize = (messages, model) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
20
|
+
let tokenLength = 0;
|
|
21
|
+
yield Promise.all(messages.map((message) => {
|
|
22
|
+
return new Promise((resolve) => {
|
|
23
|
+
tokenLength += (0, exports.calculateTokensFromString)(message, model);
|
|
24
|
+
resolve(tokenLength);
|
|
25
|
+
});
|
|
26
|
+
}));
|
|
27
|
+
return tokenLength;
|
|
28
|
+
});
|
|
29
|
+
exports.calculateMessagesTokenSize = calculateMessagesTokenSize;
|
|
30
|
+
const reduceContextSize = (messages, model, maxTokens) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
31
|
+
// TODO: Summarize context instead of cutoff
|
|
32
|
+
const cutoffSize = Math.round(messages.length * 0.1);
|
|
33
|
+
const cutoffMessages = messages.splice(cutoffSize, messages.length - 1);
|
|
34
|
+
if ((yield (0, exports.calculateMessagesTokenSize)(cutoffMessages, model)) >
|
|
35
|
+
maxTokens / 1.5) {
|
|
36
|
+
(0, exports.reduceContextSize)(cutoffMessages, model, maxTokens);
|
|
37
|
+
}
|
|
38
|
+
return cutoffMessages;
|
|
39
|
+
});
|
|
40
|
+
exports.reduceContextSize = reduceContextSize;
|
|
41
|
+
const exceedsHistoryLimit = (tokenLength, model, maxTokens) => {
|
|
42
|
+
if (tokenLength >= exports.tokenLimit / 1.1 ||
|
|
43
|
+
tokenLength >= ((0, exports.modelTokenLimit)(model) - maxTokens) / 1.1) {
|
|
44
|
+
return true;
|
|
45
|
+
}
|
|
46
|
+
return false;
|
|
47
|
+
};
|
|
48
|
+
exports.exceedsHistoryLimit = exceedsHistoryLimit;
|
|
49
|
+
exports.tokenLimit = 32000;
|
|
50
|
+
const modelTokenLimit = (model) => {
|
|
51
|
+
switch (model) {
|
|
52
|
+
default:
|
|
53
|
+
return 2048;
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
exports.modelTokenLimit = modelTokenLimit;
|
|
57
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../../../packages/pieces/community/azure-openai/src/lib/common/index.ts"],"names":[],"mappings":";;;;AAAA,uCAA8C;AAEvC,MAAM,yBAAyB,GAAG,CAAC,MAAc,EAAE,KAAa,EAAE,EAAE;IACzE,IAAI;QACF,MAAM,OAAO,GAAG,IAAA,6BAAkB,EAAC,KAAY,CAAC,CAAC;QACjD,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;QACtC,OAAO,CAAC,IAAI,EAAE,CAAC;QAEf,OAAO,MAAM,CAAC,MAAM,CAA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