@aj-archipelago/cortex 1.1.15 → 1.1.17

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,6 +1,6 @@
1
1
  {
2
2
  "name": "@aj-archipelago/cortex",
3
- "version": "1.1.15",
3
+ "version": "1.1.17",
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": {
@@ -30,5 +30,7 @@ export default {
30
30
  executePathway: undefined,
31
31
  // Set the temperature to 0 to favor more deterministic output when generating entity extraction.
32
32
  temperature: 0.9,
33
+ // Require a valid JSON response from the model
34
+ json: false,
33
35
  };
34
36
 
package/server/parser.js CHANGED
@@ -49,6 +49,28 @@ const isNumberedList = (data) => {
49
49
  return numberedListPattern.test(data.trim());
50
50
  }
51
51
 
52
+ function parseJson(str) {
53
+ try {
54
+ const start = Math.min(
55
+ str.indexOf('{') !== -1 ? str.indexOf('{') : Infinity,
56
+ str.indexOf('[') !== -1 ? str.indexOf('[') : Infinity
57
+ );
58
+
59
+ const end = Math.max(
60
+ str.lastIndexOf('}') !== -1 ? str.lastIndexOf('}') + 1 : 0,
61
+ str.lastIndexOf(']') !== -1 ? str.lastIndexOf(']') + 1 : 0
62
+ );
63
+
64
+ const jsonStr = str.slice(start, end);
65
+ // eslint-disable-next-line no-unused-vars
66
+ const json = JSON.parse(jsonStr);
67
+ return jsonStr;
68
+ } catch (error) {
69
+ logger.warn(`Pathway requires JSON format result. Failed to parse JSON: ${error.message}`);
70
+ return null;
71
+ }
72
+ }
73
+
52
74
  export {
53
75
  regexParser,
54
76
  parseNumberedList,
@@ -56,4 +78,5 @@ export {
56
78
  parseCommaSeparatedList,
57
79
  isCommaSeparatedList,
58
80
  isNumberedList,
81
+ parseJson
59
82
  };
@@ -211,8 +211,22 @@ class PathwayResolver {
211
211
  // Save the context before processing the request
212
212
  const savedContextStr = JSON.stringify(this.savedContext);
213
213
 
214
- // Process the request
215
- const data = await this.processRequest(args);
214
+ const MAX_RETRIES = 3;
215
+ let data = null;
216
+
217
+ for (let retries = 0; retries < MAX_RETRIES; retries++) {
218
+ data = await this.processRequest(args);
219
+ if (!data) {
220
+ break;
221
+ }
222
+
223
+ data = this.responseParser.parse(data);
224
+ if (data !== null) {
225
+ break;
226
+ }
227
+
228
+ logger.warn(`Bad pathway result - retrying pathway. Attempt ${retries + 1} of ${MAX_RETRIES}`);
229
+ }
216
230
 
217
231
  // Update saved context if it has changed, generating a new contextId if necessary
218
232
  if (savedContextStr !== JSON.stringify(this.savedContext)) {
@@ -220,8 +234,7 @@ class PathwayResolver {
220
234
  setv && setv(this.savedContextId, this.savedContext);
221
235
  }
222
236
 
223
- // Return the result
224
- return this.responseParser.parse(data);
237
+ return data;
225
238
  }
226
239
 
227
240
  // Add a warning and log it
@@ -1,4 +1,4 @@
1
- import { parseNumberedList, parseNumberedObjectList, parseCommaSeparatedList, isCommaSeparatedList, isNumberedList } from './parser.js';
1
+ import { parseNumberedList, parseNumberedObjectList, parseCommaSeparatedList, isCommaSeparatedList, isNumberedList, parseJson } from './parser.js';
2
2
 
3
3
  class PathwayResponseParser {
4
4
  constructor(pathway) {
@@ -22,6 +22,10 @@ class PathwayResponseParser {
22
22
  return [data];
23
23
  }
24
24
 
25
+ if (this.pathway.json) {
26
+ return parseJson(data);
27
+ }
28
+
25
29
  return data;
26
30
  }
27
31
  }