@avalant/piece-promptx-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 +40 -0
- package/src/index.d.ts +10 -0
- package/src/index.js +99 -0
- package/src/index.js.map +1 -0
- package/src/lib/actions/ask-assistant.d.ts +9 -0
- package/src/lib/actions/ask-assistant.js +150 -0
- package/src/lib/actions/ask-assistant.js.map +1 -0
- package/src/lib/actions/ask-chatgpt.d.ts +15 -0
- package/src/lib/actions/ask-chatgpt.js +191 -0
- package/src/lib/actions/ask-chatgpt.js.map +1 -0
- package/src/lib/actions/extract-structured-data-from-text.d.ts +9 -0
- package/src/lib/actions/extract-structured-data-from-text.js +174 -0
- package/src/lib/actions/extract-structured-data-from-text.js.map +1 -0
- package/src/lib/actions/generate-image.d.ts +10 -0
- package/src/lib/actions/generate-image.js +123 -0
- package/src/lib/actions/generate-image.js.map +1 -0
- package/src/lib/actions/text-to-speech.d.ts +12 -0
- package/src/lib/actions/text-to-speech.js +110 -0
- package/src/lib/actions/text-to-speech.js.map +1 -0
- package/src/lib/actions/transcribe-audio.d.ts +8 -0
- package/src/lib/actions/transcribe-audio.js +65 -0
- package/src/lib/actions/transcribe-audio.js.map +1 -0
- package/src/lib/actions/translate-audio.d.ts +7 -0
- package/src/lib/actions/translate-audio.js +50 -0
- package/src/lib/actions/translate-audio.js.map +1 -0
- package/src/lib/actions/vision-prompt.d.ts +15 -0
- package/src/lib/actions/vision-prompt.js +165 -0
- package/src/lib/actions/vision-prompt.js.map +1 -0
- package/src/lib/common/common.d.ts +16 -0
- package/src/lib/common/common.js +193 -0
- package/src/lib/common/common.js.map +1 -0
- package/src/lib/common/pmtx-api.d.ts +50 -0
- package/src/lib/common/pmtx-api.js +123 -0
- package/src/lib/common/pmtx-api.js.map +1 -0
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.visionPrompt = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const pieces_framework_1 = require("@activepieces/pieces-framework");
|
|
6
|
+
const openai_1 = tslib_1.__importDefault(require("openai"));
|
|
7
|
+
const __1 = require("../..");
|
|
8
|
+
const zod_1 = require("zod");
|
|
9
|
+
const pieces_common_1 = require("@activepieces/pieces-common");
|
|
10
|
+
const pmtx_api_1 = require("../common/pmtx-api");
|
|
11
|
+
const common_1 = require("../common/common");
|
|
12
|
+
exports.visionPrompt = (0, pieces_framework_1.createAction)({
|
|
13
|
+
auth: __1.promptxAuth,
|
|
14
|
+
name: 'vision_prompt',
|
|
15
|
+
displayName: 'Vision Prompt',
|
|
16
|
+
description: 'Ask GPT a question about an image',
|
|
17
|
+
props: {
|
|
18
|
+
image: pieces_framework_1.Property.File({
|
|
19
|
+
displayName: 'Image',
|
|
20
|
+
description: "The image URL or file you want GPT's vision to read.",
|
|
21
|
+
required: true,
|
|
22
|
+
}),
|
|
23
|
+
prompt: pieces_framework_1.Property.LongText({
|
|
24
|
+
displayName: 'Question',
|
|
25
|
+
description: 'What do you want ChatGPT to tell you about the image?',
|
|
26
|
+
required: true,
|
|
27
|
+
}),
|
|
28
|
+
detail: pieces_framework_1.Property.Dropdown({
|
|
29
|
+
displayName: 'Detail',
|
|
30
|
+
required: false,
|
|
31
|
+
description: 'Control how the model processes the image and generates textual understanding.',
|
|
32
|
+
defaultValue: 'auto',
|
|
33
|
+
refreshers: [],
|
|
34
|
+
options: () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
35
|
+
return {
|
|
36
|
+
options: [
|
|
37
|
+
{
|
|
38
|
+
label: 'low',
|
|
39
|
+
value: 'low',
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
label: 'high',
|
|
43
|
+
value: 'high',
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
label: 'auto',
|
|
47
|
+
value: 'auto',
|
|
48
|
+
},
|
|
49
|
+
],
|
|
50
|
+
};
|
|
51
|
+
}),
|
|
52
|
+
}),
|
|
53
|
+
temperature: pieces_framework_1.Property.Number({
|
|
54
|
+
displayName: 'Temperature',
|
|
55
|
+
required: false,
|
|
56
|
+
description: 'Controls randomness: Lowering results in less random completions. As the temperature approaches zero, the model will become deterministic and repetitive.',
|
|
57
|
+
defaultValue: 0.9,
|
|
58
|
+
}),
|
|
59
|
+
maxTokens: pieces_framework_1.Property.Number({
|
|
60
|
+
displayName: 'Maximum Tokens',
|
|
61
|
+
required: false,
|
|
62
|
+
description: "The maximum number of tokens to generate. Requests can use up to 2,048 or 4,096 tokens shared between prompt and completion, don't set the value to maximum and leave some tokens for the input. The exact limit varies by model. (One token is roughly 4 characters for normal English text)",
|
|
63
|
+
defaultValue: 2048,
|
|
64
|
+
}),
|
|
65
|
+
topP: pieces_framework_1.Property.Number({
|
|
66
|
+
displayName: 'Top P',
|
|
67
|
+
required: false,
|
|
68
|
+
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.',
|
|
69
|
+
defaultValue: 1,
|
|
70
|
+
}),
|
|
71
|
+
frequencyPenalty: pieces_framework_1.Property.Number({
|
|
72
|
+
displayName: 'Frequency penalty',
|
|
73
|
+
required: false,
|
|
74
|
+
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.",
|
|
75
|
+
defaultValue: 0,
|
|
76
|
+
}),
|
|
77
|
+
presencePenalty: pieces_framework_1.Property.Number({
|
|
78
|
+
displayName: 'Presence penalty',
|
|
79
|
+
required: false,
|
|
80
|
+
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.",
|
|
81
|
+
defaultValue: 0.6,
|
|
82
|
+
}),
|
|
83
|
+
roles: pieces_framework_1.Property.Json({
|
|
84
|
+
displayName: 'Roles',
|
|
85
|
+
required: false,
|
|
86
|
+
description: 'Array of roles to specify more accurate response',
|
|
87
|
+
defaultValue: [
|
|
88
|
+
{ role: 'system', content: 'You are a helpful assistant.' },
|
|
89
|
+
],
|
|
90
|
+
}),
|
|
91
|
+
},
|
|
92
|
+
run(_a) {
|
|
93
|
+
return tslib_1.__awaiter(this, arguments, void 0, function* ({ auth, propsValue, project, flows, store }) {
|
|
94
|
+
var _b, _c, _d, _e, _f, _g;
|
|
95
|
+
const { server, username, password } = auth;
|
|
96
|
+
const accessToken = yield (0, pmtx_api_1.getAccessToken)(server, username, password);
|
|
97
|
+
//get store data
|
|
98
|
+
const { userId, apiKey } = yield (0, pmtx_api_1.getStoreData)(store, server, accessToken);
|
|
99
|
+
const { temperature, maxTokens, topP, frequencyPenalty, presencePenalty } = propsValue;
|
|
100
|
+
const usage = yield (0, pmtx_api_1.getUsagePlan)(server, accessToken);
|
|
101
|
+
//check token is available
|
|
102
|
+
if (maxTokens && maxTokens > usage.token_available) {
|
|
103
|
+
throw new Error(common_1.billingIssueMessage);
|
|
104
|
+
}
|
|
105
|
+
yield pieces_common_1.propsValidation.validateZod(propsValue, {
|
|
106
|
+
temperature: zod_1.z.number().min(0).max(1),
|
|
107
|
+
});
|
|
108
|
+
const openai = new openai_1.default({
|
|
109
|
+
apiKey: apiKey,
|
|
110
|
+
});
|
|
111
|
+
const rolesArray = propsValue.roles ? propsValue.roles : [];
|
|
112
|
+
const roles = rolesArray.map((item) => {
|
|
113
|
+
const rolesEnum = ['system', 'user', 'assistant'];
|
|
114
|
+
if (!rolesEnum.includes(item.role)) {
|
|
115
|
+
throw new Error('The only available roles are: [system, user, assistant]');
|
|
116
|
+
}
|
|
117
|
+
return {
|
|
118
|
+
role: item.role,
|
|
119
|
+
content: item.content,
|
|
120
|
+
};
|
|
121
|
+
});
|
|
122
|
+
const completion = yield openai.chat.completions.create({
|
|
123
|
+
model: 'gpt-4o',
|
|
124
|
+
messages: [
|
|
125
|
+
...roles,
|
|
126
|
+
{
|
|
127
|
+
role: 'user',
|
|
128
|
+
content: [
|
|
129
|
+
{
|
|
130
|
+
type: 'text',
|
|
131
|
+
text: propsValue['prompt'],
|
|
132
|
+
},
|
|
133
|
+
{
|
|
134
|
+
type: 'image_url',
|
|
135
|
+
image_url: {
|
|
136
|
+
url: `data:image/${propsValue.image.extension};base64,${propsValue.image.base64}`,
|
|
137
|
+
},
|
|
138
|
+
},
|
|
139
|
+
],
|
|
140
|
+
},
|
|
141
|
+
],
|
|
142
|
+
temperature: temperature,
|
|
143
|
+
top_p: topP,
|
|
144
|
+
frequency_penalty: frequencyPenalty,
|
|
145
|
+
presence_penalty: presencePenalty,
|
|
146
|
+
max_completion_tokens: maxTokens,
|
|
147
|
+
});
|
|
148
|
+
// update token usage data for the user
|
|
149
|
+
yield (0, pmtx_api_1.addTokenUsage)({
|
|
150
|
+
userId: `${userId}`,
|
|
151
|
+
model: 'gpt-4o',
|
|
152
|
+
projectId: project.id,
|
|
153
|
+
flowId: flows.current.id,
|
|
154
|
+
component: 'Automationx',
|
|
155
|
+
usage: {
|
|
156
|
+
inputTokens: (_c = (_b = completion.usage) === null || _b === void 0 ? void 0 : _b.prompt_tokens) !== null && _c !== void 0 ? _c : 0,
|
|
157
|
+
outputTokens: (_e = (_d = completion.usage) === null || _d === void 0 ? void 0 : _d.completion_tokens) !== null && _e !== void 0 ? _e : 0,
|
|
158
|
+
totalTokens: (_g = (_f = completion.usage) === null || _f === void 0 ? void 0 : _f.total_tokens) !== null && _g !== void 0 ? _g : 0,
|
|
159
|
+
},
|
|
160
|
+
}, server, accessToken);
|
|
161
|
+
return completion.choices[0].message.content;
|
|
162
|
+
});
|
|
163
|
+
},
|
|
164
|
+
});
|
|
165
|
+
//# sourceMappingURL=vision-prompt.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vision-prompt.js","sourceRoot":"","sources":["../../../../../../../../packages/pieces/community/promptx-openai/src/lib/actions/vision-prompt.ts"],"names":[],"mappings":";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E,MAAM;gCACZ,IAAI,EAAE,UAAU,CAAC,QAAQ,CAAC;6BAC3B;4BACD;gCACE,IAAI,EAAE,WAAW;gCACjB,SAAS,EAAE;oCACT,GAAG,EAAE,cAAc,UAAU,CAAC,KAAK,CAAC,SAAS,WAAW,UAAU,CAAC,KAAK,CAAC,MAAM,EAAE;iCAClF;6BACF;yBACF;qBACF;iBACF;gBACD,WAAW,EAAE,WAAW;gBACxB,KAAK,EAAE,IAAI;gBACX,iBAAiB,EAAE,gBAAgB;gBACnC,gBAAgB,EAAE,eAAe;gBACjC,qBAAqB,EAAE,SAAS;aACjC,CAAC,CAAC;YAEH,uCAAuC;YACvC,MAAM,IAAA,wBAAa,EACjB;gBACE,MAAM,EAAE,GAAG,MAAM,EAAE;gBACnB,KAAK,EAAE,QAAQ;gBACf,SAAS,EAAE,OAAO,CAAC,EAAE;gBACrB,MAAM,EAAE,KAAK,CAAC,OAAO,CAAC,EAAE;gBACxB,SAAS,EAAE,aAAa;gBACxB,KAAK,EAAE;oBACL,WAAW,EAAE,MAAA,MAAA,UAAU,CAAC,KAAK,0CAAE,aAAa,mCAAI,CAAC;oBACjD,YAAY,EAAE,MAAA,MAAA,UAAU,CAAC,KAAK,0CAAE,iBAAiB,mCAAI,CAAC;oBACtD,WAAW,EAAE,MAAA,MAAA,UAAU,CAAC,KAAK,0CAAE,YAAY,mCAAI,CAAC;iBACjD;aACF,EACD,MAAM,EACN,WAAqB,CACtB,CAAC;YAEF,OAAO,UAAU,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC;QAC/C,CAAC;KAAA;CACF,CAAC,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export declare const baseUrl = "https://api.openai.com/v1";
|
|
2
|
+
export declare const Languages: {
|
|
3
|
+
value: string;
|
|
4
|
+
label: string;
|
|
5
|
+
}[];
|
|
6
|
+
export declare const billingIssueMessage = "Error Occurred: 429 \n\n1. Ensure that billing is enabled on your OpenAI platform. \n\n2. Generate a new API key. \n\n3. Attempt the process again. \n\nFor guidance, visit: https://beta.openai.com/account/billing";
|
|
7
|
+
export declare const unauthorizedMessage = "Error Occurred: 401 \n\nEnsure that your API key is valid. \n";
|
|
8
|
+
export declare const sleep: (ms: number) => Promise<unknown>;
|
|
9
|
+
export declare const streamToBuffer: (stream: any) => Promise<unknown>;
|
|
10
|
+
export declare const calculateTokensFromString: (string: string, model: string) => number;
|
|
11
|
+
export declare const calculateMessagesTokenSize: (messages: any[], model: string) => Promise<number>;
|
|
12
|
+
export declare const reduceContextSize: (messages: any[], model: string, maxTokens: number) => Promise<any[]>;
|
|
13
|
+
export declare const exceedsHistoryLimit: (tokenLength: number, model: string, maxTokens: number) => boolean;
|
|
14
|
+
export declare const tokenLimit = 32000;
|
|
15
|
+
export declare const modelTokenLimit: (model: string) => 128000 | 8192 | 32768 | 16385 | 4096 | 8001 | 2048;
|
|
16
|
+
export declare const notLLMs: string[];
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.notLLMs = exports.modelTokenLimit = exports.tokenLimit = exports.exceedsHistoryLimit = exports.reduceContextSize = exports.calculateMessagesTokenSize = exports.calculateTokensFromString = exports.streamToBuffer = exports.sleep = exports.unauthorizedMessage = exports.billingIssueMessage = exports.Languages = exports.baseUrl = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const tiktoken_1 = require("tiktoken");
|
|
6
|
+
exports.baseUrl = 'https://api.openai.com/v1';
|
|
7
|
+
exports.Languages = [
|
|
8
|
+
{ value: 'es', label: 'Spanish' },
|
|
9
|
+
{ value: 'it', label: 'Italian' },
|
|
10
|
+
{ value: 'en', label: 'English' },
|
|
11
|
+
{ value: 'pt', label: 'Portuguese' },
|
|
12
|
+
{ value: 'de', label: 'German' },
|
|
13
|
+
{ value: 'ja', label: 'Japanese' },
|
|
14
|
+
{ value: 'pl', label: 'Polish' },
|
|
15
|
+
{ value: 'ar', label: 'Arabic' },
|
|
16
|
+
{ value: 'af', label: 'Afrikaans' },
|
|
17
|
+
{ value: 'az', label: 'Azerbaijani' },
|
|
18
|
+
{ value: 'bg', label: 'Bulgarian' },
|
|
19
|
+
{ value: 'bs', label: 'Bosnian' },
|
|
20
|
+
{ value: 'ca', label: 'Catalan' },
|
|
21
|
+
{ value: 'cs', label: 'Czech' },
|
|
22
|
+
{ value: 'da', label: 'Danish' },
|
|
23
|
+
{ value: 'el', label: 'Greek' },
|
|
24
|
+
{ value: 'et', label: 'Estonian' },
|
|
25
|
+
{ value: 'fa', label: 'Persian' },
|
|
26
|
+
{ value: 'fi', label: 'Finnish' },
|
|
27
|
+
{ value: 'tl', label: 'Tagalog' },
|
|
28
|
+
{ value: 'fr', label: 'French' },
|
|
29
|
+
{ value: 'gl', label: 'Galician' },
|
|
30
|
+
{ value: 'he', label: 'Hebrew' },
|
|
31
|
+
{ value: 'hi', label: 'Hindi' },
|
|
32
|
+
{ value: 'hr', label: 'Croatian' },
|
|
33
|
+
{ value: 'hu', label: 'Hungarian' },
|
|
34
|
+
{ value: 'hy', label: 'Armenian' },
|
|
35
|
+
{ value: 'id', label: 'Indonesian' },
|
|
36
|
+
{ value: 'is', label: 'Icelandic' },
|
|
37
|
+
{ value: 'kk', label: 'Kazakh' },
|
|
38
|
+
{ value: 'kn', label: 'Kannada' },
|
|
39
|
+
{ value: 'ko', label: 'Korean' },
|
|
40
|
+
{ value: 'lt', label: 'Lithuanian' },
|
|
41
|
+
{ value: 'lv', label: 'Latvian' },
|
|
42
|
+
{ value: 'ma', label: 'Maori' },
|
|
43
|
+
{ value: 'mk', label: 'Macedonian' },
|
|
44
|
+
{ value: 'mr', label: 'Marathi' },
|
|
45
|
+
{ value: 'ms', label: 'Malay' },
|
|
46
|
+
{ value: 'ne', label: 'Nepali' },
|
|
47
|
+
{ value: 'nl', label: 'Dutch' },
|
|
48
|
+
{ value: 'no', label: 'Norwegian' },
|
|
49
|
+
{ value: 'ro', label: 'Romanian' },
|
|
50
|
+
{ value: 'ru', label: 'Russian' },
|
|
51
|
+
{ value: 'sk', label: 'Slovak' },
|
|
52
|
+
{ value: 'sl', label: 'Slovenian' },
|
|
53
|
+
{ value: 'sr', label: 'Serbian' },
|
|
54
|
+
{ value: 'sv', label: 'Swedish' },
|
|
55
|
+
{ value: 'sw', label: 'Swahili' },
|
|
56
|
+
{ value: 'ta', label: 'Tamil' },
|
|
57
|
+
{ value: 'th', label: 'Thai' },
|
|
58
|
+
{ value: 'tr', label: 'Turkish' },
|
|
59
|
+
{ value: 'uk', label: 'Ukrainian' },
|
|
60
|
+
{ value: 'ur', label: 'Urdu' },
|
|
61
|
+
{ value: 'vi', label: 'Vietnamese' },
|
|
62
|
+
{ value: 'zh', label: 'Chinese (Simplified)' },
|
|
63
|
+
{ value: 'cy', label: 'Welsh' },
|
|
64
|
+
{ value: 'be', label: 'Belarusian' },
|
|
65
|
+
];
|
|
66
|
+
exports.billingIssueMessage = `Error Occurred: 429 \n
|
|
67
|
+
1. Ensure that billing is enabled on your OpenAI platform. \n
|
|
68
|
+
2. Generate a new API key. \n
|
|
69
|
+
3. Attempt the process again. \n
|
|
70
|
+
For guidance, visit: https://beta.openai.com/account/billing`;
|
|
71
|
+
exports.unauthorizedMessage = `Error Occurred: 401 \n
|
|
72
|
+
Ensure that your API key is valid. \n`;
|
|
73
|
+
const sleep = (ms) => {
|
|
74
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
75
|
+
};
|
|
76
|
+
exports.sleep = sleep;
|
|
77
|
+
const streamToBuffer = (stream) => {
|
|
78
|
+
const chunks = [];
|
|
79
|
+
return new Promise((resolve, reject) => {
|
|
80
|
+
stream.on('data', (chunk) => chunks.push(Buffer.from(chunk)));
|
|
81
|
+
stream.on('error', (err) => reject(err));
|
|
82
|
+
stream.on('end', () => resolve(Buffer.concat(chunks)));
|
|
83
|
+
});
|
|
84
|
+
};
|
|
85
|
+
exports.streamToBuffer = streamToBuffer;
|
|
86
|
+
const calculateTokensFromString = (string, model) => {
|
|
87
|
+
try {
|
|
88
|
+
const encoder = (0, tiktoken_1.encoding_for_model)(model);
|
|
89
|
+
const tokens = encoder.encode(string);
|
|
90
|
+
encoder.free();
|
|
91
|
+
return tokens.length;
|
|
92
|
+
}
|
|
93
|
+
catch (e) {
|
|
94
|
+
// Model not supported by tiktoken, every 4 chars is a token
|
|
95
|
+
return Math.round(string.length / 4);
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
exports.calculateTokensFromString = calculateTokensFromString;
|
|
99
|
+
const calculateMessagesTokenSize = (messages, model) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
100
|
+
let tokenLength = 0;
|
|
101
|
+
yield Promise.all(messages.map((message) => {
|
|
102
|
+
return new Promise((resolve) => {
|
|
103
|
+
tokenLength += (0, exports.calculateTokensFromString)(message.content, model);
|
|
104
|
+
resolve(tokenLength);
|
|
105
|
+
});
|
|
106
|
+
}));
|
|
107
|
+
return tokenLength;
|
|
108
|
+
});
|
|
109
|
+
exports.calculateMessagesTokenSize = calculateMessagesTokenSize;
|
|
110
|
+
const reduceContextSize = (messages, model, maxTokens) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
111
|
+
// TODO: Summarize context instead of cutoff
|
|
112
|
+
const cutoffSize = Math.round(messages.length * 0.1);
|
|
113
|
+
const cutoffMessages = messages.splice(cutoffSize, messages.length - 1);
|
|
114
|
+
if ((yield (0, exports.calculateMessagesTokenSize)(cutoffMessages, model)) >
|
|
115
|
+
maxTokens / 1.5) {
|
|
116
|
+
(0, exports.reduceContextSize)(cutoffMessages, model, maxTokens);
|
|
117
|
+
}
|
|
118
|
+
return cutoffMessages;
|
|
119
|
+
});
|
|
120
|
+
exports.reduceContextSize = reduceContextSize;
|
|
121
|
+
const exceedsHistoryLimit = (tokenLength, model, maxTokens) => {
|
|
122
|
+
if (tokenLength >= exports.tokenLimit / 1.1 ||
|
|
123
|
+
tokenLength >= ((0, exports.modelTokenLimit)(model) - maxTokens) / 1.1) {
|
|
124
|
+
return true;
|
|
125
|
+
}
|
|
126
|
+
return false;
|
|
127
|
+
};
|
|
128
|
+
exports.exceedsHistoryLimit = exceedsHistoryLimit;
|
|
129
|
+
exports.tokenLimit = 32000;
|
|
130
|
+
const modelTokenLimit = (model) => {
|
|
131
|
+
switch (model) {
|
|
132
|
+
case 'gpt-4-1106-preview':
|
|
133
|
+
return 128000;
|
|
134
|
+
case 'gpt-4-vision-preview':
|
|
135
|
+
return 128000;
|
|
136
|
+
case 'gpt-4':
|
|
137
|
+
return 8192;
|
|
138
|
+
case 'gpt-4-32k':
|
|
139
|
+
return 32768;
|
|
140
|
+
case 'gpt-4-0613':
|
|
141
|
+
return 8192;
|
|
142
|
+
case 'gpt-4-32k-0613':
|
|
143
|
+
return 32768;
|
|
144
|
+
case 'gpt-4-0314':
|
|
145
|
+
return 8192;
|
|
146
|
+
case 'gpt-4-32k-0314':
|
|
147
|
+
return 32768;
|
|
148
|
+
case 'gpt-3.5-turbo-1106':
|
|
149
|
+
return 16385;
|
|
150
|
+
case 'gpt-3.5-turbo':
|
|
151
|
+
return 4096;
|
|
152
|
+
case 'gpt-3.5-turbo-16k':
|
|
153
|
+
return 16385;
|
|
154
|
+
case 'gpt-3.5-turbo-instruct':
|
|
155
|
+
return 4096;
|
|
156
|
+
case 'gpt-3.5-turbo-0613':
|
|
157
|
+
return 4096;
|
|
158
|
+
case 'gpt-3.5-turbo-16k-0613':
|
|
159
|
+
return 16385;
|
|
160
|
+
case 'gpt-3.5-turbo-0301':
|
|
161
|
+
return 4096;
|
|
162
|
+
case 'text-davinci-003':
|
|
163
|
+
return 4096;
|
|
164
|
+
case 'text-davinci-002':
|
|
165
|
+
return 4096;
|
|
166
|
+
case 'code-davinci-002':
|
|
167
|
+
return 8001;
|
|
168
|
+
case 'text-moderation-latest':
|
|
169
|
+
return 32768;
|
|
170
|
+
case 'text-moderation-stable':
|
|
171
|
+
return 32768;
|
|
172
|
+
default:
|
|
173
|
+
return 2048;
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
exports.modelTokenLimit = modelTokenLimit;
|
|
177
|
+
// List of non-text models to filter out in Ask GPT action
|
|
178
|
+
exports.notLLMs = [
|
|
179
|
+
'gpt-4o-realtime-preview-2024-10-01',
|
|
180
|
+
'gpt-4o-realtime-preview',
|
|
181
|
+
'babbage-002',
|
|
182
|
+
'davinci-002',
|
|
183
|
+
'tts-1-hd-1106',
|
|
184
|
+
'whisper-1',
|
|
185
|
+
'canary-whisper',
|
|
186
|
+
'canary-tts',
|
|
187
|
+
'tts-1',
|
|
188
|
+
'tts-1-hd',
|
|
189
|
+
'tts-1-1106',
|
|
190
|
+
'dall-e-3',
|
|
191
|
+
'dall-e-2',
|
|
192
|
+
];
|
|
193
|
+
//# sourceMappingURL=common.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"common.js","sourceRoot":"","sources":["../../../../../../../../packages/pieces/community/promptx-openai/src/lib/common/common.ts"],"names":[],"mappings":";;;;AAAA,uCAA8C;AAEjC,QAAA,OAAO,GAAG,2BAA2B,CAAC;AAEtC,QAAA,SAAS,GAAG;IACvB,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,SAAS,EAAE;IACjC,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,SAAS,EAAE;IACjC,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,SAAS,EAAE;IACjC,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,YAAY,EAAE;IACpC,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,QAAQ,EAAE;IAChC,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,UAAU,EAAE;IAClC,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,QAAQ,EAAE;IAChC,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,QAAQ,EA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AAK,oBAAoB;YACvB,OAAO,KAAK,CAAC;QACf,KAAK,eAAe;YAClB,OAAO,IAAI,CAAC;QACd,KAAK,mBAAmB;YACtB,OAAO,KAAK,CAAC;QACf,KAAK,wBAAwB;YAC3B,OAAO,IAAI,CAAC;QACd,KAAK,oBAAoB;YACvB,OAAO,IAAI,CAAC;QACd,KAAK,wBAAwB;YAC3B,OAAO,KAAK,CAAC;QACf,KAAK,oBAAoB;YACvB,OAAO,IAAI,CAAC;QACd,KAAK,kBAAkB;YACrB,OAAO,IAAI,CAAC;QACd,KAAK,kBAAkB;YACrB,OAAO,IAAI,CAAC;QACd,KAAK,kBAAkB;YACrB,OAAO,IAAI,CAAC;QACd,KAAK,wBAAwB;YAC3B,OAAO,KAAK,CAAC;QACf,KAAK,wBAAwB;YAC3B,OAAO,KAAK,CAAC;QACf;YACE,OAAO,IAAI,CAAC;IAChB,CAAC;AACH,CAAC,CAAC;AA7CW,QAAA,eAAe,mBA6C1B;AAEF,0DAA0D;AAC7C,QAAA,OAAO,GAAG;IACrB,oCAAoC;IACpC,yBAAyB;IACzB,aAAa;IACb,aAAa;IACb,eAAe;IACf,WAAW;IACX,gBAAgB;IAChB,YAAY;IACZ,OAAO;IACP,UAAU;IACV,YAAY;IACZ,UAAU;IACV,UAAU;CACX,CAAC"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { Store } from '@activepieces/pieces-framework';
|
|
2
|
+
type UrlConfig = {
|
|
3
|
+
loginUrl: string;
|
|
4
|
+
quotaCheckUrl: string;
|
|
5
|
+
addTokenUrl: string;
|
|
6
|
+
myProfileUrl: string;
|
|
7
|
+
getAIKeyUrl: string;
|
|
8
|
+
};
|
|
9
|
+
type UsagePackage = {
|
|
10
|
+
package_name: string;
|
|
11
|
+
total_tokens_used: number;
|
|
12
|
+
limit_token_usage: number;
|
|
13
|
+
token_available: number;
|
|
14
|
+
total_credit_used: number;
|
|
15
|
+
limit_credit_usage: number;
|
|
16
|
+
credit_available: number;
|
|
17
|
+
};
|
|
18
|
+
type UserInfo = {
|
|
19
|
+
userIAM2ID: string;
|
|
20
|
+
email: string;
|
|
21
|
+
username: string;
|
|
22
|
+
};
|
|
23
|
+
interface Usage {
|
|
24
|
+
inputTokens: number;
|
|
25
|
+
outputTokens: number;
|
|
26
|
+
totalTokens: number;
|
|
27
|
+
}
|
|
28
|
+
interface AppUsageData {
|
|
29
|
+
appId?: string;
|
|
30
|
+
userId: string;
|
|
31
|
+
model: string;
|
|
32
|
+
projectId: string;
|
|
33
|
+
flowId: string;
|
|
34
|
+
agentId?: string;
|
|
35
|
+
component: string;
|
|
36
|
+
usage: Usage;
|
|
37
|
+
}
|
|
38
|
+
export declare const Production = "PromptX";
|
|
39
|
+
export declare const Test = "Staging";
|
|
40
|
+
export declare const baseUrlMap: Record<string, UrlConfig>;
|
|
41
|
+
export declare const getAccessToken: (server: string, username: string, password: string) => Promise<string | null>;
|
|
42
|
+
export declare const addTokenUsage: (data: AppUsageData, server: string, access_token: string) => Promise<any>;
|
|
43
|
+
export declare const getUsagePlan: (server: string, access_token: string) => Promise<UsagePackage>;
|
|
44
|
+
export declare const getUserProfile: (server: string, access_token: string) => Promise<UserInfo>;
|
|
45
|
+
export declare const getStoreData: (store: Store, server: string, access_token: string) => Promise<{
|
|
46
|
+
userId: any;
|
|
47
|
+
apiKey: any;
|
|
48
|
+
}>;
|
|
49
|
+
export declare const getAiApiKey: (server: string, access_token: string) => Promise<any>;
|
|
50
|
+
export {};
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getAiApiKey = exports.getStoreData = exports.getUserProfile = exports.getUsagePlan = exports.addTokenUsage = exports.getAccessToken = exports.baseUrlMap = exports.Test = exports.Production = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
exports.Production = 'PromptX';
|
|
6
|
+
exports.Test = 'Staging';
|
|
7
|
+
exports.baseUrlMap = {
|
|
8
|
+
[exports.Production]: {
|
|
9
|
+
loginUrl: 'https://centerapp.io/center/auth/login',
|
|
10
|
+
quotaCheckUrl: 'https://promptxai.com/zero-service/pmtx-ai-token-api/v1/quota-check',
|
|
11
|
+
addTokenUrl: 'https://promptxai.com/zero-service/pmtx-ai-token-api/v1/token-used',
|
|
12
|
+
myProfileUrl: 'https://centerapp.io/center//api/v1/users/me',
|
|
13
|
+
getAIKeyUrl: 'https://promptxai.com/zero-service/pmtx-ai-token-api/v1/api-key?key=openAIKey',
|
|
14
|
+
},
|
|
15
|
+
[exports.Test]: {
|
|
16
|
+
loginUrl: 'https://test.oneweb.tech/zero-service/pmtx/login',
|
|
17
|
+
quotaCheckUrl: 'https://test.oneweb.tech/zero-service/pmtx-ai-token-api/v1/quota-check',
|
|
18
|
+
addTokenUrl: 'https://test.oneweb.tech/zero-service/pmtx-ai-token-api/v1/token-used',
|
|
19
|
+
myProfileUrl: 'https://mocha.centerapp.io/center//api/v1/users/me',
|
|
20
|
+
getAIKeyUrl: 'https://test.oneweb.tech/zero-service/pmtx-ai-token-api/v1/api-key?key=openAIKey',
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
const getAccessToken = (server, username, password) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
24
|
+
const response = yield fetch(exports.baseUrlMap[server].loginUrl, {
|
|
25
|
+
method: 'POST',
|
|
26
|
+
body: new URLSearchParams({
|
|
27
|
+
username: username,
|
|
28
|
+
password: password,
|
|
29
|
+
}).toString(),
|
|
30
|
+
headers: {
|
|
31
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
const data = yield response.json();
|
|
35
|
+
if (response.status !== 200) {
|
|
36
|
+
throw new Error((data === null || data === void 0 ? void 0 : data.error) || (data === null || data === void 0 ? void 0 : data.message));
|
|
37
|
+
}
|
|
38
|
+
return data === null || data === void 0 ? void 0 : data.access_token;
|
|
39
|
+
});
|
|
40
|
+
exports.getAccessToken = getAccessToken;
|
|
41
|
+
const addTokenUsage = (data, server, access_token) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
42
|
+
try {
|
|
43
|
+
const response = yield fetch(exports.baseUrlMap[server]['addTokenUrl'], {
|
|
44
|
+
method: 'POST',
|
|
45
|
+
headers: {
|
|
46
|
+
'Content-Type': 'application/json',
|
|
47
|
+
Authorization: `Bearer ${access_token}`,
|
|
48
|
+
},
|
|
49
|
+
body: JSON.stringify(data),
|
|
50
|
+
});
|
|
51
|
+
if (response.status !== 200) {
|
|
52
|
+
throw new Error(`API error: ${response.statusText}`);
|
|
53
|
+
}
|
|
54
|
+
const result = yield response.json();
|
|
55
|
+
return result;
|
|
56
|
+
}
|
|
57
|
+
catch (error) {
|
|
58
|
+
console.error('Failed to send token usage:', error);
|
|
59
|
+
throw error;
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
exports.addTokenUsage = addTokenUsage;
|
|
63
|
+
const getUsagePlan = (server, access_token) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
64
|
+
const response = yield fetch(exports.baseUrlMap[server]['quotaCheckUrl'], {
|
|
65
|
+
headers: {
|
|
66
|
+
'Content-Type': 'application/json',
|
|
67
|
+
Authorization: `Bearer ${access_token}`,
|
|
68
|
+
},
|
|
69
|
+
});
|
|
70
|
+
if (response.status !== 201) {
|
|
71
|
+
throw new Error(`API error: ${response.statusText}`);
|
|
72
|
+
}
|
|
73
|
+
const result = yield response.json();
|
|
74
|
+
return result;
|
|
75
|
+
});
|
|
76
|
+
exports.getUsagePlan = getUsagePlan;
|
|
77
|
+
const getUserProfile = (server, access_token) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
78
|
+
const response = yield fetch(exports.baseUrlMap[server]['myProfileUrl'], {
|
|
79
|
+
headers: {
|
|
80
|
+
'Content-Type': 'application/json',
|
|
81
|
+
Authorization: `Bearer ${access_token}`,
|
|
82
|
+
},
|
|
83
|
+
});
|
|
84
|
+
if (response.status !== 200) {
|
|
85
|
+
throw new Error(`API error: ${response.statusText}`);
|
|
86
|
+
}
|
|
87
|
+
const result = yield response.json();
|
|
88
|
+
return result;
|
|
89
|
+
});
|
|
90
|
+
exports.getUserProfile = getUserProfile;
|
|
91
|
+
const getStoreData = (store, server, access_token) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
92
|
+
//get store data
|
|
93
|
+
let userId = yield store.get('userId');
|
|
94
|
+
let apiKey = yield (0, exports.getAiApiKey)(server, access_token);
|
|
95
|
+
if (!userId) {
|
|
96
|
+
const userInfo = yield (0, exports.getUserProfile)(server, access_token);
|
|
97
|
+
store.put('userId', userInfo.userIAM2ID);
|
|
98
|
+
userId = userInfo.userIAM2ID;
|
|
99
|
+
}
|
|
100
|
+
return {
|
|
101
|
+
userId,
|
|
102
|
+
apiKey,
|
|
103
|
+
};
|
|
104
|
+
});
|
|
105
|
+
exports.getStoreData = getStoreData;
|
|
106
|
+
const getAiApiKey = (server, access_token) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
107
|
+
const response = yield fetch(exports.baseUrlMap[server]['getAIKeyUrl'], {
|
|
108
|
+
headers: {
|
|
109
|
+
'Content-Type': 'application/json',
|
|
110
|
+
Authorization: `Bearer ${access_token}`,
|
|
111
|
+
},
|
|
112
|
+
});
|
|
113
|
+
if (response.status !== 201) {
|
|
114
|
+
throw new Error(`API error: ${response.statusText}`);
|
|
115
|
+
}
|
|
116
|
+
const result = yield response.json();
|
|
117
|
+
if (!(result === null || result === void 0 ? void 0 : result.openAIKey)) {
|
|
118
|
+
throw new Error('No AI Api Key found for Avalant OpenAI');
|
|
119
|
+
}
|
|
120
|
+
return result === null || result === void 0 ? void 0 : result.openAIKey;
|
|
121
|
+
});
|
|
122
|
+
exports.getAiApiKey = getAiApiKey;
|
|
123
|
+
//# sourceMappingURL=pmtx-api.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pmtx-api.js","sourceR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