@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/src/openai.js
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
import OpenAI from 'openai';
|
|
2
|
-
|
|
3
|
-
import BaseClient from './BaseClient.js';
|
|
4
|
-
import { transformResponse } from './util.js';
|
|
5
|
-
|
|
6
|
-
const DEFAULT_MODEL = 'gpt-4o-mini';
|
|
7
|
-
|
|
8
|
-
export class OpenAiClient extends BaseClient {
|
|
9
|
-
constructor(options) {
|
|
10
|
-
super(options);
|
|
11
|
-
this.client = new OpenAI({
|
|
12
|
-
...options,
|
|
13
|
-
});
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Lists available models.
|
|
18
|
-
* {@link https://platform.openai.com/docs/models Documentation}
|
|
19
|
-
*/
|
|
20
|
-
async models() {
|
|
21
|
-
const { data } = await this.client.models.list();
|
|
22
|
-
return data.map((o) => o.id);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
async getCompletion(options) {
|
|
26
|
-
const { model = DEFAULT_MODEL, output = 'text', stream = false } = options;
|
|
27
|
-
const { client } = this;
|
|
28
|
-
|
|
29
|
-
const messages = await this.getMessages(options);
|
|
30
|
-
const response = await client.chat.completions.create({
|
|
31
|
-
model,
|
|
32
|
-
messages,
|
|
33
|
-
stream,
|
|
34
|
-
response_format: {
|
|
35
|
-
type: output === 'json' ? 'json_object' : 'text',
|
|
36
|
-
},
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
if (output === 'raw') {
|
|
40
|
-
return response;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const { message } = response.choices[0];
|
|
44
|
-
|
|
45
|
-
return transformResponse({
|
|
46
|
-
...options,
|
|
47
|
-
messages,
|
|
48
|
-
message,
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
getStreamedChunk(chunk, started) {
|
|
53
|
-
const [choice] = chunk.choices;
|
|
54
|
-
|
|
55
|
-
let type;
|
|
56
|
-
if (!started) {
|
|
57
|
-
type = 'start';
|
|
58
|
-
} else if (choice.finish_reason === 'stop') {
|
|
59
|
-
type = 'stop';
|
|
60
|
-
} else {
|
|
61
|
-
type = 'chunk';
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
if (type) {
|
|
65
|
-
return {
|
|
66
|
-
type,
|
|
67
|
-
text: choice.delta.content || '',
|
|
68
|
-
};
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
}
|
package/src/util.js
DELETED
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
import fs from 'fs/promises';
|
|
2
|
-
|
|
3
|
-
import path from 'path';
|
|
4
|
-
|
|
5
|
-
import { glob } from 'glob';
|
|
6
|
-
|
|
7
|
-
const CODE_REG = /^```\w*$(.+)```/ms;
|
|
8
|
-
const JSON_REG = /([{[].+[}\]])/s;
|
|
9
|
-
|
|
10
|
-
export async function loadTemplates(dir) {
|
|
11
|
-
const result = {};
|
|
12
|
-
const files = await glob(path.join(dir, '*.md'));
|
|
13
|
-
|
|
14
|
-
if (!files.length) {
|
|
15
|
-
throw new Error(`No templates found in: ${dir}.`);
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
for (let file of files) {
|
|
19
|
-
const base = path.basename(file, '.md');
|
|
20
|
-
result[base] = await loadTemplate(file);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
return result;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export async function loadTemplate(file) {
|
|
27
|
-
return await fs.readFile(file, 'utf-8');
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export function transformResponse(options) {
|
|
31
|
-
const { output = 'text', messages, message } = options;
|
|
32
|
-
const content = message.content || message.text;
|
|
33
|
-
if (output === 'text') {
|
|
34
|
-
return content;
|
|
35
|
-
} else if (output === 'messages') {
|
|
36
|
-
return [...messages, message];
|
|
37
|
-
} else if (output === 'json') {
|
|
38
|
-
return parseJson(content);
|
|
39
|
-
} else if (output === 'code') {
|
|
40
|
-
return parseCode(content);
|
|
41
|
-
} else {
|
|
42
|
-
throw new Error(`No output type provided.`);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
function parseJson(content) {
|
|
47
|
-
try {
|
|
48
|
-
return JSON.parse(content.match(JSON_REG)[0]);
|
|
49
|
-
} catch (error) {
|
|
50
|
-
throw new Error(`Unable to derive JSON from response:\n\n${content}`);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
function parseCode(content) {
|
|
55
|
-
try {
|
|
56
|
-
return content.match(CODE_REG)[1].trim();
|
|
57
|
-
} catch (error) {
|
|
58
|
-
throw new Error(`Unable to derive code from response:\n\n${content}`);
|
|
59
|
-
}
|
|
60
|
-
}
|
package/src/xai.js
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { OpenAiClient } from './openai.js';
|
|
2
|
-
|
|
3
|
-
const DEFAULT_MODEL = 'grok-2-1212';
|
|
4
|
-
|
|
5
|
-
export class XAiClient extends OpenAiClient {
|
|
6
|
-
constructor(options) {
|
|
7
|
-
super({
|
|
8
|
-
...options,
|
|
9
|
-
baseURL: 'https://api.x.ai/v1',
|
|
10
|
-
});
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
async getCompletion(options) {
|
|
14
|
-
return super.getCompletion({
|
|
15
|
-
model: DEFAULT_MODEL,
|
|
16
|
-
...options,
|
|
17
|
-
});
|
|
18
|
-
}
|
|
19
|
-
}
|