@aj-archipelago/cortex 1.1.25 → 1.1.26

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.
@@ -1,12 +1,13 @@
1
1
  import { selectEndpoint } from './requestExecutor.js';
2
2
 
3
3
  class CortexRequest {
4
- constructor( { url, urlSuffix, data, params, headers, auth, cache, model, pathwayResolver, selectedEndpoint, stream } = {}) {
4
+ constructor( { url, urlSuffix, data, params, headers, auth, cache, model, pathwayResolver, selectedEndpoint, stream, initCallback } = {}) {
5
5
  this._url = url || '';
6
6
  this._urlSuffix = urlSuffix || '';
7
7
  this._data = data || {};
8
8
  this._params = params || {};
9
9
  this._headers = headers || {};
10
+ this._addHeaders = {};
10
11
  this._auth = auth || {};
11
12
  this._cache = cache || {};
12
13
  this._model = model || '';
@@ -14,6 +15,7 @@ class CortexRequest {
14
15
  this._selectedEndpoint = selectedEndpoint || {};
15
16
  this._stream = stream || false;
16
17
  this._method = 'POST';
18
+ this._initCallback = initCallback || null;
17
19
 
18
20
  if (this._pathwayResolver) {
19
21
  this._model = this._pathwayResolver.model;
@@ -24,17 +26,22 @@ class CortexRequest {
24
26
  }
25
27
  }
26
28
 
29
+ initRequest() {
30
+ if (typeof this._initCallback === 'function') {
31
+ this._initCallback(this);
32
+ }
33
+ }
34
+
27
35
  selectNewEndpoint() {
28
36
  const sep = selectEndpoint(this._model);
29
37
  if (sep) {
30
38
  this._selectedEndpoint = sep;
31
39
  this._url = sep.url;
32
- this._data = { ...this._data, ...sep.params };
33
- this._headers = { ...this._headers, ...sep.headers };
40
+ this._data = { ...this._data, ...sep.data, ...sep.params };
34
41
  if (sep.auth) {
35
42
  this._auth = { ...sep.auth };
36
43
  }
37
- this._params = { ...this._params, ...sep.params };
44
+ this.initRequest();
38
45
  }
39
46
  }
40
47
 
@@ -74,9 +81,22 @@ class CortexRequest {
74
81
  this._data = value;
75
82
  }
76
83
 
84
+ // initCallback getter and setter
85
+ get initCallback() {
86
+ return this._initCallback;
87
+ }
88
+
89
+ set initCallback(value) {
90
+ if (typeof value !== 'function') {
91
+ throw new Error('initCallback must be a function');
92
+ }
93
+ this._initCallback = value;
94
+ this.initRequest();
95
+ }
96
+
77
97
  // params getter and setter
78
98
  get params() {
79
- return this._params;
99
+ return {...this._params, ...this._selectedEndpoint.params};
80
100
  }
81
101
 
82
102
  set params(value) {
@@ -85,13 +105,29 @@ class CortexRequest {
85
105
 
86
106
  // headers getter and setter
87
107
  get headers() {
88
- return { ...this._headers, ...this._auth };
108
+ return { ...this._headers, ...this._selectedEndpoint.headers, ...this._auth, ...this._addHeaders };
89
109
  }
90
110
 
91
111
  set headers(value) {
92
112
  this._headers = value;
93
113
  }
94
114
 
115
+ // addheaders getter and setter
116
+ get addHeaders() {
117
+ return this._addHeaders;
118
+ }
119
+
120
+ set addHeaders(value) {
121
+ // Create a new object to store the processed headers
122
+ this._addHeaders = {};
123
+
124
+ // Iterate over the input headers and convert keys to title case
125
+ for (const [key, val] of Object.entries(value)) {
126
+ const titleCaseKey = key.replace(/(^|-)./g, m => m.toUpperCase());
127
+ this._addHeaders[titleCaseKey] = val;
128
+ }
129
+ }
130
+
95
131
  // auth getter and setter
96
132
  get auth() {
97
133
  return this._auth;
@@ -323,6 +323,8 @@ const makeRequest = async (cortexRequest) => {
323
323
  status !== 504) {
324
324
  return { response, duration };
325
325
  }
326
+ // set up for a retry by reinitializing the request
327
+ cortexRequest.initRequest();
326
328
  } else {
327
329
  // if there are multiple endpoints, retry everything by default
328
330
  // as it could be a temporary issue with one endpoint
@@ -331,6 +333,7 @@ const makeRequest = async (cortexRequest) => {
331
333
  if (status == 400) {
332
334
  return { response, duration };
333
335
  }
336
+ // set up for a retry by selecting a new endpoint, which will also reinitialize the request
334
337
  cortexRequest.selectNewEndpoint();
335
338
  }
336
339
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aj-archipelago/cortex",
3
- "version": "1.1.25",
3
+ "version": "1.1.26",
4
4
  "description": "Cortex is a GraphQL API for AI. It provides a simple, extensible interface for using AI services from OpenAI, Azure and others.",
5
5
  "private": false,
6
6
  "repository": {
@@ -117,49 +117,52 @@ class NeuralSpacePlugin extends ModelPlugin {
117
117
  const cortexRequest = new CortexRequest({ pathwayResolver });
118
118
  cortexRequest.url = this.requestUrl();
119
119
 
120
- const formData = new FormData();
121
- formData.append("files", fs.createReadStream(chunk));
122
- const configObj = {
123
- file_transcription: {
124
- mode: "advanced",
125
- },
126
- };
127
-
128
- //phrase/segment level
129
- if ((responseFormat && !wordTimestamped) || maxLineWidth) {
130
- configObj.speaker_diarization = {
131
- // mode: "speakers",
132
- // num_speakers: numSpeakers,
133
- // overrides: {
134
- // clustering: {
135
- // threshold: clusteringThreshold,
136
- // },
137
- // },
120
+ const nsInitCallback = (requestInstance) => {
121
+ const formData = new FormData();
122
+ formData.append("files", fs.createReadStream(chunk));
123
+ const configObj = {
124
+ file_transcription: {
125
+ mode: "advanced",
126
+ },
138
127
  };
139
128
 
140
- configObj.subtitles_guidelines = {
141
- line_count: 1
142
- };
143
- }
144
-
145
- if (maxLineWidth) {
146
- configObj.subtitles_guidelines = {
147
- character_count: maxLineWidth,
129
+ //phrase/segment level
130
+ if ((responseFormat && !wordTimestamped) || maxLineWidth) {
131
+ configObj.speaker_diarization = {
132
+ // mode: "speakers",
133
+ // num_speakers: numSpeakers,
134
+ // overrides: {
135
+ // clustering: {
136
+ // threshold: clusteringThreshold,
137
+ // },
138
+ // },
139
+ };
140
+
141
+ configObj.subtitles_guidelines = {
142
+ line_count: 1,
143
+ };
144
+ }
145
+
146
+ if (maxLineWidth) {
147
+ configObj.subtitles_guidelines = {
148
+ character_count: maxLineWidth,
149
+ };
150
+ }
151
+
152
+ if (language) {
153
+ configObj.file_transcription.language_id = language;
154
+ }
155
+ formData.append("config", JSON.stringify(configObj));
156
+
157
+ requestInstance.data = formData;
158
+ requestInstance.params = {};
159
+ requestInstance.addHeaders = {
160
+ ...formData.getHeaders(),
148
161
  };
149
- }
150
-
151
- if (language) {
152
- configObj.file_transcription.language_id = language;
153
- }
154
- formData.append("config", JSON.stringify(configObj));
155
-
156
- cortexRequest.data = formData;
157
- cortexRequest.params = {};
158
- cortexRequest.headers = {
159
- ...cortexRequest.headers,
160
- ...formData.getHeaders(),
161
162
  };
162
163
 
164
+ cortexRequest.initCallback = nsInitCallback;
165
+
163
166
  const result = await this.executeRequest(cortexRequest);
164
167
 
165
168
  const jobId = result?.data?.jobId;
@@ -37,22 +37,26 @@ class OpenAIWhisperPlugin extends ModelPlugin {
37
37
  chunks.push(chunk);
38
38
 
39
39
  const { language, responseFormat } = parameters;
40
- const params = {};
41
40
  const { modelPromptText } = this.getCompiledPrompt(text, parameters, prompt);
42
41
  const response_format = responseFormat || 'text';
43
42
 
44
- const formData = new FormData();
45
- formData.append('file', fs.createReadStream(chunk));
46
- formData.append('model', cortexRequest.params.model);
47
- formData.append('response_format', response_format);
48
- language && formData.append('language', language);
49
- modelPromptText && formData.append('prompt', modelPromptText);
43
+ const whisperInitCallback = (requestInstance) => {
50
44
 
51
- cortexRequest.data = formData;
52
- cortexRequest.params = params;
53
- cortexRequest.headers = { ...cortexRequest.headers, ...formData.getHeaders() };
45
+ const formData = new FormData();
46
+ formData.append('file', fs.createReadStream(chunk));
47
+ formData.append('model', requestInstance.params.model);
48
+ formData.append('response_format', response_format);
49
+ language && formData.append('language', language);
50
+ modelPromptText && formData.append('prompt', modelPromptText);
54
51
 
52
+ requestInstance.data = formData;
53
+ requestInstance.addHeaders = { ...formData.getHeaders() };
54
+
55
+ };
56
+
57
+ cortexRequest.initCallback = whisperInitCallback;
55
58
  return this.executeRequest(cortexRequest);
59
+
56
60
  } catch (err) {
57
61
  logger.error(`Error getting word timestamped data from api: ${err}`);
58
62
  throw err;