@bedrockio/ai 0.3.0 → 0.4.4

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.
Files changed (53) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/README.md +58 -17
  3. package/dist/cjs/BaseClient.js +242 -182
  4. package/dist/cjs/anthropic.js +115 -93
  5. package/dist/cjs/google.js +74 -80
  6. package/dist/cjs/index.js +23 -75
  7. package/dist/cjs/openai.js +114 -72
  8. package/dist/cjs/package.json +1 -0
  9. package/dist/cjs/utils/code.js +11 -0
  10. package/dist/cjs/utils/json.js +53 -0
  11. package/dist/cjs/utils/templates.js +83 -0
  12. package/dist/cjs/xai.js +11 -20
  13. package/dist/esm/BaseClient.js +243 -0
  14. package/dist/esm/anthropic.js +116 -0
  15. package/dist/esm/google.js +75 -0
  16. package/dist/esm/index.js +25 -0
  17. package/dist/esm/openai.js +113 -0
  18. package/dist/esm/utils/code.js +8 -0
  19. package/dist/esm/utils/json.js +50 -0
  20. package/dist/esm/utils/templates.js +76 -0
  21. package/dist/esm/xai.js +10 -0
  22. package/package.json +25 -18
  23. package/types/BaseClient.d.ts +67 -26
  24. package/types/BaseClient.d.ts.map +1 -1
  25. package/types/anthropic.d.ts +26 -2
  26. package/types/anthropic.d.ts.map +1 -1
  27. package/types/google.d.ts.map +1 -1
  28. package/types/index.d.ts +4 -11
  29. package/types/index.d.ts.map +1 -1
  30. package/types/openai.d.ts +45 -2
  31. package/types/openai.d.ts.map +1 -1
  32. package/types/utils/code.d.ts +2 -0
  33. package/types/utils/code.d.ts.map +1 -0
  34. package/types/utils/json.d.ts +2 -0
  35. package/types/utils/json.d.ts.map +1 -0
  36. package/types/utils/templates.d.ts +3 -0
  37. package/types/utils/templates.d.ts.map +1 -0
  38. package/types/utils.d.ts +4 -0
  39. package/types/utils.d.ts.map +1 -0
  40. package/types/xai.d.ts.map +1 -1
  41. package/.prettierignore +0 -1
  42. package/.prettierrc.cjs +0 -1
  43. package/__mocks__/@anthropic-ai/sdk.js +0 -43
  44. package/__mocks__/@google/generative-ai.js +0 -59
  45. package/__mocks__/openai.js +0 -48
  46. package/dist/cjs/util.js +0 -62
  47. package/src/BaseClient.js +0 -195
  48. package/src/anthropic.js +0 -97
  49. package/src/google.js +0 -91
  50. package/src/index.js +0 -72
  51. package/src/openai.js +0 -71
  52. package/src/util.js +0 -60
  53. package/src/xai.js +0 -19
package/CHANGELOG.md CHANGED
@@ -1,3 +1,28 @@
1
+ ## 0.4.3
2
+
3
+ - Moved to files whitelist.
4
+
5
+ ## 0.4.2
6
+
7
+ - Exclude keys from tarball.
8
+
9
+ ## 0.4.1
10
+
11
+ - No error on missing API key.
12
+
13
+ ## 0.4.0
14
+
15
+ - Rewrote OpenAI to use new responses format.
16
+ - Allow structured responses.
17
+ - Allow yada or JSON schema.
18
+ - Allow extracting partial JSON when streaming.
19
+ - Using `template` option only now.
20
+ - Allow specifying model in client options.
21
+ - Store the previous response id for OpenAI.
22
+ - Removed MultiClient for now.
23
+ - Client -> createClient.
24
+ - Added debug feature.
25
+
1
26
  ## 0.3.0
2
27
 
3
28
  - Added MultiClient.
package/README.md CHANGED
@@ -20,9 +20,10 @@ yarn install @bedrockio/ai
20
20
  ## Usage
21
21
 
22
22
  ```js
23
- import { Client } from '@bedrockio/ai';
23
+ import yd from '@bedrockio/yada';
24
+ import { createClient } from '@bedrockio/ai';
24
25
 
25
- const client = new Client({
26
+ const client = createClient({
26
27
  // Directory to templates
27
28
  templates: './test/templates',
28
29
  // Platform: openai|gpt|anthopic|claude
@@ -33,13 +34,18 @@ const client = new Client({
33
34
 
34
35
  // Get a one time response.
35
36
  const response = await client.prompt({
36
- // The template file to use.
37
- file: 'classify-fruits',
37
+ // The template to use. If no template is found will
38
+ // use this string as the template.
39
+ template: 'classify-fruits',
38
40
  // The form of output. May be raw|text|messages|json.
39
41
  // Default is "text".
40
42
  output: 'json',
41
- // A custom template may be passed if "file" is not.
42
- template: 'custom',
43
+
44
+ // Aa yada schema (or any JSON schema) may be passed
45
+ // here to define structured output.
46
+ schema: yd.object({
47
+ name: yd.string(),
48
+ })
43
49
 
44
50
  // All other variables will be
45
51
  // interpolated into the template.
@@ -55,27 +61,63 @@ Responses may be streamed:
55
61
  ```js
56
62
  // Stream the results
57
63
  const stream = await client.stream({
58
- file: 'classify-fruits',
64
+ template: 'classify-fruits',
65
+
66
+ // See below.
67
+ extractMessages: 'text',
59
68
  });
60
69
 
61
70
  // Will return an AsyncIterator
62
- for await (const chunk of stream) {
63
- console.info(chunk.text);
71
+ for await (const event of stream) {
72
+ console.info(event.text);
64
73
  }
65
74
  ```
66
75
 
67
- ## Templates
76
+ Event types:
68
77
 
69
- Template files must be markdown (`.md`) and live in your templates directory.
70
- They may be a simple text prompt or delineated roles:
78
+ - `start` - Response has been initiated. This event also contains an `id` field.
79
+ that can be passsed back in as `prevResponseId` (OpenAI/Grok only).
80
+ - `stop` - Response has finished. Contains the `id` field and usage data.
81
+ - `delta`- Main text delta event when a new token is output.
82
+ - `done` - Text has stopped.
83
+ - `extract:delta` - Used with `extractMessages` (see below).
84
+ - `extract:done` - Used with `extractMessages` (see below).
71
85
 
72
- ````
73
- --- SYSTEM ---
86
+ ### Streaming Structured Data
87
+
88
+ Often you want prompt responses to be structured JSON, however you still want to
89
+ stream the user-facing message. In this case use the `extractMessages` option to
90
+ define the key of the structured output you want to stream. When this is defined
91
+ you receive additional `extract:delta` and `extract:done` events. These will
92
+ stream even as the partial JSON data comes in.
93
+
94
+ ### Streaming Notes
74
95
 
75
- This is a list of fruits: {{fruits}}
96
+ Note that in addition to streaming partial data above, there are 2 other valid
97
+ approaches:
76
98
 
77
- --- USER ---
99
+ 1. Send two prompts, one for the message and one for the extracted data. This
100
+ works, however there are edge cases when there needs to correlation between
101
+ the responses. For example when asking the user a "next question" in text but
102
+ extracting the type of question in data, the results may not match depending
103
+ on the LLM temperament. This also will increase token usage.
78
104
 
105
+ 2. Use function calls, ie "tools". This approach seems more appropriate as
106
+ function calls stream separately to text output and can easily be
107
+ multiplexed, however at the time of this writing there seem to me issues with
108
+ ensuring tht the LLM actually uses the correct tools and results have been
109
+ flaky. Depending on the approach this may also increase token usage.
110
+
111
+ For the reasons above currently the most reliable approach to streaming
112
+ structured data is using `extractMessage` to stream the partial JSON response.
113
+
114
+ ## Templates
115
+
116
+ Template files must be markdown (`.md`) and live in your templates directory.
117
+ These will be passed as `instructions`, or the equivalent to the `developer`
118
+ role.
119
+
120
+ ````
79
121
  Which fruit do you think the following input most closely resembles?
80
122
 
81
123
  Please provide your response as a JSON object containing:
@@ -95,7 +137,6 @@ Currently supported platforms:
95
137
 
96
138
  - OpenAI (ChatGPT)
97
139
  - Anthropic (Claude)
98
- - Google (Gemini).
99
140
  - xAi (Grok).
100
141
 
101
142
  ## Models
@@ -1,186 +1,246 @@
1
1
  "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
- var _mustache = _interopRequireDefault(require("mustache"));
8
- var _util = require("./util.js");
9
- function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
10
- const MESSAGES_REG = /(?:^|\n)-{3,}\s*(\w+)\s*-{3,}(.*?)(?=\n-{3,}|$)/gs;
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const code_js_1 = require("./utils/code.js");
4
+ const json_js_1 = require("./utils/json.js");
5
+ const templates_js_1 = require("./utils/templates.js");
11
6
  class BaseClient {
12
- constructor(options) {
13
- this.options = options;
14
- this.templates = null;
15
- }
16
-
17
- /**
18
- * Interpolates vars into the provided template and
19
- * runs the chat completion. The "output" option may
20
- * be omitted and will default to `"text"`.
21
- * {@link https://github.com/bedrockio/ai?tab=readme-ov-file#bedrockioai Documentation}
22
- *
23
- * @param {object} options
24
- * @param {string} options.model - The model to use.
25
- * @param {"raw" | "text" | "json" | "messages"} [options.output] - The output to use.
26
- * @param {Object.<string, any>} [options.other] - Additional props
27
- * will be interpolated in the template.
28
- */
29
- async prompt(options) {
30
- options = {
31
- ...this.options,
32
- ...options
33
- };
34
- const messages = await this.getMessages(options);
35
- return await this.getCompletion({
36
- ...options,
37
- messages
38
- });
39
- }
40
-
41
- /**
42
- * Streams the prompt response.
43
- * @returns {AsyncIterator}
44
- */
45
- async *stream(options) {
46
- const stream = await this.getStream(options);
47
- let started = false;
48
-
49
- // @ts-ignore
50
- for await (const chunk of stream) {
51
- const resolved = this.getStreamedChunk(chunk, started);
52
- started = true;
53
-
54
- // @ts-ignore
55
- if (resolved) {
56
- yield resolved;
57
- }
58
- }
59
- }
60
- async getMessages(options) {
61
- const {
62
- text
63
- } = options;
64
- const template = await this.resolveTemplate(options);
65
- if (template) {
66
- const raw = render(template, options);
67
- const messages = [];
68
- for (let match of raw.matchAll(MESSAGES_REG)) {
69
- const [, role, content] = match;
70
- messages.push({
71
- role: role.toLowerCase(),
72
- content: content.trim()
73
- });
74
- }
75
- if (!messages.length) {
76
- messages.push({
77
- role: 'user',
78
- content: raw.trim()
79
- });
80
- }
81
- return messages;
82
- } else if (text) {
83
- return [{
84
- role: 'user',
85
- content: text
86
- }];
87
- } else {
88
- throw new Error('No input provided.');
89
- }
90
- }
91
- async buildTemplate(options) {
92
- const template = await this.resolveTemplate(options);
93
- return render(template, options);
94
- }
95
- async loadTemplates() {
96
- const {
97
- templates
98
- } = this.options;
99
- this.templates ||= await (0, _util.loadTemplates)(templates);
100
- }
101
- async resolveTemplate(options) {
102
- const {
103
- template,
104
- file
105
- } = options;
106
- if (template) {
107
- return template;
108
- } else if (file?.endsWith('.md')) {
109
- return await (0, _util.loadTemplate)(file);
110
- } else if (file) {
111
- await this.loadTemplates();
112
- return this.templates[file];
113
- }
114
- }
115
- async getStream(options) {
116
- return await this.prompt({
117
- ...options,
118
- output: 'raw',
119
- stream: true
120
- });
121
- }
122
- getCompletion(options) {
123
- void options;
124
- new Error('Method not implemented.');
125
- }
126
- getStreamedChunk(chunk, started) {
127
- void chunk;
128
- void started;
129
- new Error('Method not implemented.');
130
- }
7
+ constructor(options) {
8
+ this.options = {
9
+ // @ts-ignore
10
+ model: this.constructor.DEFAULT_MODEL,
11
+ ...options,
12
+ };
13
+ this.templates = null;
14
+ }
15
+ // Public
16
+ /**
17
+ * Interpolates vars into the provided template as instructions and runs the
18
+ * prompt.
19
+ *
20
+ * @param {PromptOptions} options
21
+ */
22
+ async prompt(options) {
23
+ options = await this.normalizeOptions(options);
24
+ const { input, output, stream, schema } = options;
25
+ const response = await this.runPrompt(options);
26
+ if (!stream) {
27
+ this.debug('Response:', response);
28
+ }
29
+ if (output === 'raw') {
30
+ return response;
31
+ }
32
+ let result;
33
+ if (schema) {
34
+ result = this.getStructuredResponse(response);
35
+ // @ts-ignore
36
+ if (options.hasWrappedSchema) {
37
+ result = result.items;
38
+ }
39
+ }
40
+ else if (output === 'json') {
41
+ result = JSON.parse((0, code_js_1.parseCode)(this.getTextResponse(response)));
42
+ }
43
+ else {
44
+ result = (0, code_js_1.parseCode)(this.getTextResponse(response));
45
+ }
46
+ if (output === 'messages') {
47
+ return {
48
+ result,
49
+ ...this.getMessagesResponse(input, response),
50
+ };
51
+ }
52
+ else {
53
+ return result;
54
+ }
55
+ }
56
+ /**
57
+ * Streams the prompt response.
58
+ *
59
+ * @param {PromptOptions & StreamOptions} options
60
+ * @returns {AsyncIterator}
61
+ */
62
+ async *stream(options) {
63
+ options = await this.normalizeOptions(options);
64
+ const extractor = this.getMessageExtractor(options);
65
+ try {
66
+ const stream = await this.runStream(options);
67
+ // @ts-ignore
68
+ for await (let event of stream) {
69
+ this.debug('Event:', event);
70
+ event = this.normalizeStreamEvent(event);
71
+ if (event) {
72
+ yield event;
73
+ }
74
+ const extractedMessages = extractor?.(event) || [];
75
+ for (let message of extractedMessages) {
76
+ const { key, delta, text, done } = message;
77
+ let extractEvent;
78
+ if (done) {
79
+ extractEvent = {
80
+ type: 'extract:done',
81
+ text,
82
+ key,
83
+ };
84
+ }
85
+ else {
86
+ extractEvent = {
87
+ type: 'extract:delta',
88
+ delta,
89
+ key,
90
+ };
91
+ }
92
+ this.debug('Extract:', extractEvent);
93
+ yield extractEvent;
94
+ }
95
+ }
96
+ }
97
+ catch (error) {
98
+ const { message, code } = error;
99
+ yield {
100
+ type: 'error',
101
+ code,
102
+ message,
103
+ };
104
+ }
105
+ }
106
+ async buildTemplate(options) {
107
+ const template = await this.resolveTemplate(options);
108
+ return (0, templates_js_1.renderTemplate)(template, options);
109
+ }
110
+ // Protected
111
+ runPrompt(options) {
112
+ void options;
113
+ throw new Error('Method not implemented.');
114
+ }
115
+ runStream(options) {
116
+ void options;
117
+ throw new Error('Method not implemented.');
118
+ }
119
+ getTextResponse(response) {
120
+ void response;
121
+ throw new Error('Method not implemented.');
122
+ }
123
+ /**
124
+ * @returns {Object}
125
+ */
126
+ getStructuredResponse(response) {
127
+ void response;
128
+ throw new Error('Method not implemented.');
129
+ }
130
+ /**
131
+ * @returns {Object}
132
+ */
133
+ getMessagesResponse(input, response) {
134
+ void response;
135
+ throw new Error('Method not implemented.');
136
+ }
137
+ /**
138
+ * @returns {Object}
139
+ */
140
+ normalizeStreamEvent(event) {
141
+ void event;
142
+ throw new Error('Method not implemented.');
143
+ }
144
+ // Private
145
+ async normalizeOptions(options) {
146
+ options = {
147
+ input: '',
148
+ output: 'text',
149
+ ...this.options,
150
+ ...options,
151
+ };
152
+ options.input = this.normalizeInput(options);
153
+ options.schema = this.normalizeSchema(options);
154
+ options.instructions ||= await this.resolveInstructions(options);
155
+ return options;
156
+ }
157
+ normalizeInput(options) {
158
+ let { input = '', output } = options;
159
+ if (typeof input === 'string') {
160
+ if (output === 'json') {
161
+ input += '\nOutput only valid JSON.';
162
+ }
163
+ input = [
164
+ {
165
+ role: 'user',
166
+ content: input,
167
+ },
168
+ ];
169
+ }
170
+ return input;
171
+ }
172
+ normalizeSchema(options) {
173
+ let { schema } = options;
174
+ if (!schema) {
175
+ return;
176
+ }
177
+ // Convert to JSON schema.
178
+ schema = schema.toJSON?.() || schema;
179
+ if (schema?.type === 'array') {
180
+ schema = {
181
+ type: 'object',
182
+ properties: {
183
+ items: schema,
184
+ },
185
+ required: ['items'],
186
+ additionalProperties: false,
187
+ };
188
+ options.hasWrappedSchema = true;
189
+ }
190
+ return schema;
191
+ }
192
+ getMessageExtractor(options) {
193
+ const { extractMessages } = options;
194
+ if (!extractMessages) {
195
+ return;
196
+ }
197
+ const messageExtractor = (0, json_js_1.createMessageExtractor)([extractMessages]);
198
+ return (event) => {
199
+ if (event?.type === 'delta') {
200
+ return messageExtractor(event.delta);
201
+ }
202
+ };
203
+ }
204
+ debug(message, arg) {
205
+ if (this.options.debug) {
206
+ // TODO: replace with logger when opentelemetry is removed
207
+ // eslint-disable-next-line
208
+ console.debug(`${message}\n${JSON.stringify(arg, null, 2)}\n`);
209
+ }
210
+ }
211
+ async resolveInstructions(options) {
212
+ if (options.template) {
213
+ const template = await this.resolveTemplate(options);
214
+ return (0, templates_js_1.renderTemplate)(template, options);
215
+ }
216
+ }
217
+ async resolveTemplate(options) {
218
+ const { template } = options;
219
+ await this.loadTemplates();
220
+ return this.templates[template] || template;
221
+ }
222
+ async loadTemplates() {
223
+ const { templates } = this.options;
224
+ this.templates ||= await (0, templates_js_1.loadTemplates)(templates);
225
+ }
131
226
  }
132
227
  exports.default = BaseClient;
133
- function render(template, options) {
134
- let params = {
135
- ...options,
136
- ...options.params
137
- };
138
- params = mapObjects(params);
139
- params = wrapProxy(params);
140
- return _mustache.default.render(template, params);
141
- }
142
-
143
- // Transform arrays and object to versions
144
- // that are more understandable in the context
145
- // of a template that may have meaningful whitespace.
146
- function mapObjects(params) {
147
- const result = {};
148
- for (let [key, value] of Object.entries(params)) {
149
- if (Array.isArray(value)) {
150
- value = mapArray(value);
151
- } else if (typeof value === 'object') {
152
- value = JSON.stringify(value, null, 2);
153
- }
154
- result[key] = value;
155
- }
156
- return result;
157
- }
158
- function mapArray(arr) {
159
- // Only map simple arrays of primitives.
160
- if (typeof arr[0] === 'string') {
161
- arr = arr.map(el => {
162
- return `- ${el}`;
163
- }).join('\n');
164
- }
165
- return arr;
166
- }
167
-
168
- // Wrap params with a proxy object that reports
169
- // as having all properties. If one is accessed
170
- // that does not exist then return the original
171
- // token. This way templates can be partially
172
- // interpolated and re-interpolated later.
173
- function wrapProxy(params) {
174
- return new Proxy(params, {
175
- has() {
176
- return true;
177
- },
178
- get(target, prop) {
179
- if (prop in target) {
180
- return target[prop];
181
- } else {
182
- return `{{{${prop.toString()}}}}`;
183
- }
184
- }
185
- });
186
- }
228
+ /**
229
+ * @typedef {Object} PromptOptions
230
+ * @property {string|PromptMessage[]} input - Input to use.
231
+ * @property {string} [model] - The model to use.
232
+ * @property {boolean} stream - Stream response.
233
+ * @property {Object} [schema] - A JSON schema compatible object that defines the output shape.
234
+ * @property {"raw" | "text" | "json" | "messages"} [output] - The return value type.
235
+ * @property {Object} [params] - Params to be interpolated into the template.
236
+ * May also be passed as additional props to options.
237
+ */
238
+ /**
239
+ * @typedef {Object} StreamOptions
240
+ * @property {string} [extractMessages] - Key in JSON response to extract a message stream from.
241
+ */
242
+ /**
243
+ * @typedef {Object} PromptMessage
244
+ * @property {"system" | "user" | "assistant"} role
245
+ * @property {string} content
246
+ */