@razzium/piece-aimw-api 0.0.20 → 0.0.21
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/package.json +4 -2
- package/src/index.ts +2 -4
- package/src/lib/actions/send-prompt.ts +10 -22
- package/src/lib/common/common.ts +13 -3
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -62,12 +62,10 @@ export const openaiAuth = PieceAuth.SecretText({
|
|
|
62
62
|
return {
|
|
63
63
|
valid: true,
|
|
64
64
|
};
|
|
65
|
-
} catch (e
|
|
66
|
-
console.error('Auth validation error:', e?.message || e);
|
|
67
|
-
const errorMessage = e?.message || e?.response?.data?.message || 'Invalid API key';
|
|
65
|
+
} catch (e) {
|
|
68
66
|
return {
|
|
69
67
|
valid: false,
|
|
70
|
-
error:
|
|
68
|
+
error: 'Invalid API key',
|
|
71
69
|
};
|
|
72
70
|
}
|
|
73
71
|
},
|
|
@@ -219,28 +219,16 @@ export const askOpenAI = createAction({
|
|
|
219
219
|
// });
|
|
220
220
|
|
|
221
221
|
// Send prompt
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
// max_completion_tokens: maxTokens,
|
|
233
|
-
});
|
|
234
|
-
} catch (error: any) {
|
|
235
|
-
const errorMessage = error?.message || error?.error?.message || JSON.stringify(error);
|
|
236
|
-
const errorStatus = error?.status || error?.response?.status || 'unknown';
|
|
237
|
-
throw new Error(`AIMW API Error (${errorStatus}): ${errorMessage}`);
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
// Validate API response
|
|
241
|
-
if (!completion?.choices?.[0]?.message) {
|
|
242
|
-
throw new Error('Invalid API response: no completion message returned');
|
|
243
|
-
}
|
|
222
|
+
const completion = await openai.chat.completions.create({
|
|
223
|
+
model: model,
|
|
224
|
+
messages: [...messageHistory],
|
|
225
|
+
// messages: [...roles, ...messageHistory],
|
|
226
|
+
// temperature: temperature,
|
|
227
|
+
// top_p: topP,
|
|
228
|
+
// frequency_penalty: frequencyPenalty,
|
|
229
|
+
// presence_penalty: presencePenalty,
|
|
230
|
+
// max_completion_tokens: maxTokens,
|
|
231
|
+
});
|
|
244
232
|
|
|
245
233
|
// Add response to message history
|
|
246
234
|
messageHistory = [...messageHistory, completion.choices[0].message];
|
package/src/lib/common/common.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { encoding_for_model } from 'tiktoken';
|
|
2
|
+
|
|
1
3
|
export const baseUrl = 'https://rekto.dashboard.aimw.ai/api';
|
|
2
4
|
|
|
3
5
|
export const Languages = [
|
|
@@ -82,9 +84,17 @@ export const streamToBuffer = (stream: any) => {
|
|
|
82
84
|
});
|
|
83
85
|
};
|
|
84
86
|
|
|
85
|
-
export const calculateTokensFromString = (string: string,
|
|
86
|
-
|
|
87
|
-
|
|
87
|
+
export const calculateTokensFromString = (string: string, model: string) => {
|
|
88
|
+
try {
|
|
89
|
+
const encoder = encoding_for_model(model as any);
|
|
90
|
+
const tokens = encoder.encode(string);
|
|
91
|
+
encoder.free();
|
|
92
|
+
|
|
93
|
+
return tokens.length;
|
|
94
|
+
} catch (e) {
|
|
95
|
+
// Model not supported by tiktoken, every 4 chars is a token
|
|
96
|
+
return Math.round(string.length / 4);
|
|
97
|
+
}
|
|
88
98
|
};
|
|
89
99
|
|
|
90
100
|
export const calculateMessagesTokenSize = async (
|