@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 CHANGED
@@ -1,5 +1,7 @@
1
1
  {
2
2
  "name": "@razzium/piece-aimw-api",
3
- "version": "0.0.20",
4
- "dependencies": {}
3
+ "version": "0.0.21",
4
+ "dependencies": {
5
+ "tiktoken": "1.0.11"
6
+ }
5
7
  }
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: any) {
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: errorMessage,
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
- let completion;
223
- try {
224
- completion = await openai.chat.completions.create({
225
- model: model,
226
- messages: [...messageHistory],
227
- // messages: [...roles, ...messageHistory],
228
- // temperature: temperature,
229
- // top_p: topP,
230
- // frequency_penalty: frequencyPenalty,
231
- // presence_penalty: presencePenalty,
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];
@@ -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, _model: string) => {
86
- // Estimation simple: ~4 caractères par token
87
- return Math.round(string.length / 4);
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 (