@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.
- package/CHANGELOG.md +25 -0
- package/README.md +58 -17
- package/dist/cjs/BaseClient.js +242 -182
- package/dist/cjs/anthropic.js +115 -93
- package/dist/cjs/google.js +74 -80
- package/dist/cjs/index.js +23 -75
- package/dist/cjs/openai.js +114 -72
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/utils/code.js +11 -0
- package/dist/cjs/utils/json.js +53 -0
- package/dist/cjs/utils/templates.js +83 -0
- package/dist/cjs/xai.js +11 -20
- package/dist/esm/BaseClient.js +243 -0
- package/dist/esm/anthropic.js +116 -0
- package/dist/esm/google.js +75 -0
- package/dist/esm/index.js +25 -0
- package/dist/esm/openai.js +113 -0
- package/dist/esm/utils/code.js +8 -0
- package/dist/esm/utils/json.js +50 -0
- package/dist/esm/utils/templates.js +76 -0
- package/dist/esm/xai.js +10 -0
- package/package.json +25 -18
- package/types/BaseClient.d.ts +67 -26
- package/types/BaseClient.d.ts.map +1 -1
- package/types/anthropic.d.ts +26 -2
- package/types/anthropic.d.ts.map +1 -1
- package/types/google.d.ts.map +1 -1
- package/types/index.d.ts +4 -11
- package/types/index.d.ts.map +1 -1
- package/types/openai.d.ts +45 -2
- package/types/openai.d.ts.map +1 -1
- package/types/utils/code.d.ts +2 -0
- package/types/utils/code.d.ts.map +1 -0
- package/types/utils/json.d.ts +2 -0
- package/types/utils/json.d.ts.map +1 -0
- package/types/utils/templates.d.ts +3 -0
- package/types/utils/templates.d.ts.map +1 -0
- package/types/utils.d.ts +4 -0
- package/types/utils.d.ts.map +1 -0
- package/types/xai.d.ts.map +1 -1
- package/.prettierignore +0 -1
- package/.prettierrc.cjs +0 -1
- package/__mocks__/@anthropic-ai/sdk.js +0 -43
- package/__mocks__/@google/generative-ai.js +0 -59
- package/__mocks__/openai.js +0 -48
- package/dist/cjs/util.js +0 -62
- package/src/BaseClient.js +0 -195
- package/src/anthropic.js +0 -97
- package/src/google.js +0 -91
- package/src/index.js +0 -72
- package/src/openai.js +0 -71
- package/src/util.js +0 -60
- 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
|
|
23
|
+
import yd from '@bedrockio/yada';
|
|
24
|
+
import { createClient } from '@bedrockio/ai';
|
|
24
25
|
|
|
25
|
-
const 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
|
|
37
|
-
|
|
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
|
-
|
|
42
|
-
|
|
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
|
-
|
|
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
|
|
63
|
-
console.info(
|
|
71
|
+
for await (const event of stream) {
|
|
72
|
+
console.info(event.text);
|
|
64
73
|
}
|
|
65
74
|
```
|
|
66
75
|
|
|
67
|
-
|
|
76
|
+
Event types:
|
|
68
77
|
|
|
69
|
-
|
|
70
|
-
|
|
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
|
-
|
|
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
|
-
|
|
96
|
+
Note that in addition to streaming partial data above, there are 2 other valid
|
|
97
|
+
approaches:
|
|
76
98
|
|
|
77
|
-
|
|
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
|
package/dist/cjs/BaseClient.js
CHANGED
|
@@ -1,186 +1,246 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
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
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
const
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
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
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
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
|
+
*/
|