@ampt/ai 1.0.0 → 1.0.2

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/dist/index.cjs CHANGED
@@ -15137,9 +15137,13 @@ var init_dist_es53 = __esm({
15137
15137
  // src/index.ts
15138
15138
  var src_exports = {};
15139
15139
  __export(src_exports, {
15140
+ ChatMessage: () => ChatMessage,
15141
+ bedrock: () => bedrock,
15140
15142
  buildPrompt: () => buildPrompt,
15141
15143
  chat: () => chat,
15142
- invokeModel: () => invokeModel
15144
+ invoke: () => invoke2,
15145
+ models: () => models,
15146
+ render: () => render
15143
15147
  });
15144
15148
  module.exports = __toCommonJS(src_exports);
15145
15149
 
@@ -24887,41 +24891,49 @@ function credentialsProvider() {
24887
24891
  };
24888
24892
  }
24889
24893
 
24890
- // src/index.ts
24894
+ // src/bedrock.ts
24891
24895
  var region = process.env.AWS_REGION ?? "us-east-1";
24892
24896
  var bedrockRegion = region.startsWith("us-west") ? "us-west-2" : region.startsWith("ap-south") ? "ap-southeast-1" : region.startsWith("ap-north") ? "ap-northeast-1" : region.startsWith("eu-") ? "eu-central-1" : "us-east-1";
24893
24897
  var bedrock = new BedrockRuntime({ credentialDefaultProvider: credentialsProvider, region: bedrockRegion });
24894
- async function invokeModel(options) {
24895
- const body = {
24896
- prompt: options.prompt,
24897
- max_tokens_to_sample: options.maxTokens ?? 50
24898
+
24899
+ // src/invoke.ts
24900
+ async function invoke2(options) {
24901
+ const streaming = options.streaming ?? true;
24902
+ const contentType = options.contentType ?? "application/json";
24903
+ const accept = options.accept ?? "application/json";
24904
+ const body = contentType === "application/json" ? JSON.stringify(options.body) : options.body;
24905
+ const args = {
24906
+ body,
24907
+ contentType,
24908
+ accept,
24909
+ modelId: options.modelId
24898
24910
  };
24899
- const result = await bedrock.invokeModelWithResponseStream({
24900
- body: JSON.stringify(body),
24901
- contentType: "application/json",
24902
- accept: "application/json",
24903
- modelId: options.modelId ?? "anthropic.claude-instant-v1"
24904
- });
24911
+ const result = streaming ? await bedrock.invokeModelWithResponseStream(args) : await bedrock.invokeModel(args);
24905
24912
  if (!result.body) {
24906
24913
  throw new Error("Failed to invoke model");
24907
24914
  }
24908
- const decoder = new TextDecoder();
24909
- const iterator = result.body[Symbol.asyncIterator]();
24910
- return new ReadableStream({
24911
- async pull(controller) {
24912
- const { value, done } = await iterator.next();
24913
- if (done) {
24914
- controller.close();
24915
- } else {
24916
- if (!value.chunk?.bytes) {
24917
- console.warn("Error in LLM response stream:", value);
24918
- return;
24915
+ if (Symbol.asyncIterator in result.body) {
24916
+ const iterator = result.body[Symbol.asyncIterator]();
24917
+ return new ReadableStream({
24918
+ async pull(controller) {
24919
+ const { value, done } = await iterator.next();
24920
+ if (done) {
24921
+ controller.close();
24922
+ } else {
24923
+ if (!value.chunk?.bytes) {
24924
+ console.warn("Error in LLM response stream:", value);
24925
+ return;
24926
+ }
24927
+ controller.enqueue(value.chunk.bytes);
24919
24928
  }
24920
- controller.enqueue(JSON.parse(decoder.decode(value.chunk.bytes)));
24921
24929
  }
24922
- }
24923
- });
24930
+ });
24931
+ } else {
24932
+ return result.body.transformToString();
24933
+ }
24924
24934
  }
24935
+
24936
+ // src/chat.ts
24925
24937
  var ChatMessage = class {
24926
24938
  role;
24927
24939
  content;
@@ -24942,24 +24954,70 @@ function buildPrompt(messages) {
24942
24954
 
24943
24955
  ${titleCase(message.role)}: ${message.content}`).join("");
24944
24956
  }
24957
+ var decoder = new TextDecoder();
24945
24958
  async function chat(messages, options = {}) {
24946
24959
  const transformer = new TransformStream({
24947
- transform(event, controller) {
24960
+ transform(chunk, controller) {
24961
+ const event = JSON.parse(decoder.decode(chunk));
24948
24962
  if (event.completion) {
24949
24963
  controller.enqueue(event.completion);
24950
24964
  }
24951
24965
  }
24952
24966
  });
24953
24967
  const prompt = buildPrompt(messages);
24954
- const response = await invokeModel({ prompt, maxTokens: 1e3, modelId: options.modelId });
24968
+ const body = {
24969
+ prompt,
24970
+ max_tokens_to_sample: options.maxTokens ?? 50
24971
+ };
24972
+ const modelId = options.modelId ?? "anthropic.claude-instant-v1";
24973
+ const response = await invoke2({ body, modelId, streaming: true });
24955
24974
  return response.pipeThrough(transformer);
24956
24975
  }
24957
- chat.models = ["anthropic.claude-v1", "anthropic.claude-instant-v1", "anthropic.claude-v2"];
24976
+ chat.models = async () => {
24977
+ return ["anthropic.claude-v1", "anthropic.claude-instant-v1", "anthropic.claude-v2"];
24978
+ };
24979
+
24980
+ // src/models.ts
24981
+ async function models() {
24982
+ return [
24983
+ "anthropic.claude-v1",
24984
+ "anthropic.claude-instant-v1",
24985
+ "anthropic.claude-v2",
24986
+ "stability.stable-diffusion-xl-v0"
24987
+ ];
24988
+ }
24989
+
24990
+ // src/render.ts
24991
+ async function render(prompt, options = {}) {
24992
+ const modelId = options.modelId ?? "stability.stable-diffusion-xl-v0";
24993
+ const body = {
24994
+ text_prompts: [
24995
+ {
24996
+ text: prompt
24997
+ }
24998
+ ],
24999
+ cfg_scale: options.scale ?? 10,
25000
+ seed: options.seed ?? 20,
25001
+ steps: options.steps ?? 50,
25002
+ width: options.width ?? 512,
25003
+ height: options.height ?? 512
25004
+ };
25005
+ const json = await invoke2({ body, modelId, streaming: false });
25006
+ const result = JSON.parse(json);
25007
+ return Buffer.from(result.artifacts[0].base64, "base64");
25008
+ }
25009
+ render.models = async () => {
25010
+ return ["stability.stable-diffusion-xl-v0"];
25011
+ };
24958
25012
  // Annotate the CommonJS export names for ESM import in node:
24959
25013
  0 && (module.exports = {
25014
+ ChatMessage,
25015
+ bedrock,
24960
25016
  buildPrompt,
24961
25017
  chat,
24962
- invokeModel
25018
+ invoke,
25019
+ models,
25020
+ render
24963
25021
  });
24964
25022
  /*! Bundled license information:
24965
25023
 
package/dist/index.d.ts CHANGED
@@ -1,15 +1,29 @@
1
- interface InvokeModelOptions {
2
- prompt: string;
3
- maxTokens?: number;
4
- modelId?: string;
1
+ import { BedrockRuntime } from '@aws-sdk/client-bedrock-runtime';
2
+
3
+ interface InvokeOptions {
4
+ body: any;
5
+ contentType?: string;
6
+ accept?: string;
7
+ streaming?: boolean;
8
+ modelId: string;
9
+ }
10
+ interface InvokeStreamingDisabledOptions extends InvokeOptions {
11
+ streaming: false;
12
+ }
13
+ interface InvokeStreamingEnabledOptions extends InvokeOptions {
14
+ streaming?: true;
5
15
  }
6
- type ModelId = 'anthropic.claude-v2' | 'anthropic.claude-instant-v1' | 'anthropic.claude-v1';
7
16
  /**
8
17
  * Invoke model and return events as a ReadableStream.
9
18
  * @param options
10
19
  * @returns
11
20
  */
12
- declare function invokeModel(options: InvokeModelOptions): Promise<ReadableStream<any>>;
21
+ declare function invoke(options: InvokeStreamingDisabledOptions): Promise<string>;
22
+ declare function invoke(options: InvokeStreamingEnabledOptions): Promise<ReadableStream<Uint8Array>>;
23
+
24
+ type ModelId = 'anthropic.claude-v2' | 'anthropic.claude-instant-v1' | 'anthropic.claude-v1' | 'stability.stable-diffusion-xl-v0';
25
+ declare function models(): Promise<ModelId[]>;
26
+
13
27
  type MessageRole = 'assistant' | 'human';
14
28
  /**
15
29
  * A message to send to the model
@@ -27,6 +41,7 @@ declare class ChatMessage {
27
41
  declare function buildPrompt(messages: ChatMessage[]): string;
28
42
  interface ChatOptions {
29
43
  modelId?: ModelId;
44
+ maxTokens?: number;
30
45
  }
31
46
  /**
32
47
  * Send messages to the model and return the completion
@@ -35,7 +50,22 @@ interface ChatOptions {
35
50
  */
36
51
  declare function chat(messages: ChatMessage[], options?: ChatOptions): Promise<ReadableStream<any>>;
37
52
  declare namespace chat {
38
- var models: ModelId[];
53
+ var models: () => Promise<ModelId[]>;
54
+ }
55
+
56
+ declare const bedrock: BedrockRuntime;
57
+
58
+ interface RenderOptions {
59
+ width?: number;
60
+ height?: number;
61
+ modelId?: ModelId;
62
+ steps?: number;
63
+ seed?: number;
64
+ scale?: number;
65
+ }
66
+ declare function render(prompt: string, options?: RenderOptions): Promise<Buffer>;
67
+ declare namespace render {
68
+ var models: () => Promise<string[]>;
39
69
  }
40
70
 
41
- export { ChatOptions, InvokeModelOptions, ModelId, buildPrompt, chat, invokeModel };
71
+ export { ChatMessage, ChatOptions, InvokeOptions, InvokeStreamingDisabledOptions, InvokeStreamingEnabledOptions, MessageRole, ModelId, RenderOptions, bedrock, buildPrompt, chat, invoke, models, render };
package/dist/index.js CHANGED
@@ -12223,41 +12223,49 @@ function credentialsProvider() {
12223
12223
  };
12224
12224
  }
12225
12225
 
12226
- // src/index.ts
12226
+ // src/bedrock.ts
12227
12227
  var region = process.env.AWS_REGION ?? "us-east-1";
12228
12228
  var bedrockRegion = region.startsWith("us-west") ? "us-west-2" : region.startsWith("ap-south") ? "ap-southeast-1" : region.startsWith("ap-north") ? "ap-northeast-1" : region.startsWith("eu-") ? "eu-central-1" : "us-east-1";
12229
12229
  var bedrock = new BedrockRuntime({ credentialDefaultProvider: credentialsProvider, region: bedrockRegion });
12230
- async function invokeModel(options) {
12231
- const body = {
12232
- prompt: options.prompt,
12233
- max_tokens_to_sample: options.maxTokens ?? 50
12230
+
12231
+ // src/invoke.ts
12232
+ async function invoke2(options) {
12233
+ const streaming = options.streaming ?? true;
12234
+ const contentType = options.contentType ?? "application/json";
12235
+ const accept = options.accept ?? "application/json";
12236
+ const body = contentType === "application/json" ? JSON.stringify(options.body) : options.body;
12237
+ const args = {
12238
+ body,
12239
+ contentType,
12240
+ accept,
12241
+ modelId: options.modelId
12234
12242
  };
12235
- const result = await bedrock.invokeModelWithResponseStream({
12236
- body: JSON.stringify(body),
12237
- contentType: "application/json",
12238
- accept: "application/json",
12239
- modelId: options.modelId ?? "anthropic.claude-instant-v1"
12240
- });
12243
+ const result = streaming ? await bedrock.invokeModelWithResponseStream(args) : await bedrock.invokeModel(args);
12241
12244
  if (!result.body) {
12242
12245
  throw new Error("Failed to invoke model");
12243
12246
  }
12244
- const decoder = new TextDecoder();
12245
- const iterator = result.body[Symbol.asyncIterator]();
12246
- return new ReadableStream({
12247
- async pull(controller) {
12248
- const { value, done } = await iterator.next();
12249
- if (done) {
12250
- controller.close();
12251
- } else {
12252
- if (!value.chunk?.bytes) {
12253
- console.warn("Error in LLM response stream:", value);
12254
- return;
12247
+ if (Symbol.asyncIterator in result.body) {
12248
+ const iterator = result.body[Symbol.asyncIterator]();
12249
+ return new ReadableStream({
12250
+ async pull(controller) {
12251
+ const { value, done } = await iterator.next();
12252
+ if (done) {
12253
+ controller.close();
12254
+ } else {
12255
+ if (!value.chunk?.bytes) {
12256
+ console.warn("Error in LLM response stream:", value);
12257
+ return;
12258
+ }
12259
+ controller.enqueue(value.chunk.bytes);
12255
12260
  }
12256
- controller.enqueue(JSON.parse(decoder.decode(value.chunk.bytes)));
12257
12261
  }
12258
- }
12259
- });
12262
+ });
12263
+ } else {
12264
+ return result.body.transformToString();
12265
+ }
12260
12266
  }
12267
+
12268
+ // src/chat.ts
12261
12269
  var ChatMessage = class {
12262
12270
  role;
12263
12271
  content;
@@ -12278,23 +12286,69 @@ function buildPrompt(messages) {
12278
12286
 
12279
12287
  ${titleCase(message.role)}: ${message.content}`).join("");
12280
12288
  }
12289
+ var decoder = new TextDecoder();
12281
12290
  async function chat(messages, options = {}) {
12282
12291
  const transformer = new TransformStream({
12283
- transform(event, controller) {
12292
+ transform(chunk, controller) {
12293
+ const event = JSON.parse(decoder.decode(chunk));
12284
12294
  if (event.completion) {
12285
12295
  controller.enqueue(event.completion);
12286
12296
  }
12287
12297
  }
12288
12298
  });
12289
12299
  const prompt = buildPrompt(messages);
12290
- const response = await invokeModel({ prompt, maxTokens: 1e3, modelId: options.modelId });
12300
+ const body = {
12301
+ prompt,
12302
+ max_tokens_to_sample: options.maxTokens ?? 50
12303
+ };
12304
+ const modelId = options.modelId ?? "anthropic.claude-instant-v1";
12305
+ const response = await invoke2({ body, modelId, streaming: true });
12291
12306
  return response.pipeThrough(transformer);
12292
12307
  }
12293
- chat.models = ["anthropic.claude-v1", "anthropic.claude-instant-v1", "anthropic.claude-v2"];
12308
+ chat.models = async () => {
12309
+ return ["anthropic.claude-v1", "anthropic.claude-instant-v1", "anthropic.claude-v2"];
12310
+ };
12311
+
12312
+ // src/models.ts
12313
+ async function models() {
12314
+ return [
12315
+ "anthropic.claude-v1",
12316
+ "anthropic.claude-instant-v1",
12317
+ "anthropic.claude-v2",
12318
+ "stability.stable-diffusion-xl-v0"
12319
+ ];
12320
+ }
12321
+
12322
+ // src/render.ts
12323
+ async function render(prompt, options = {}) {
12324
+ const modelId = options.modelId ?? "stability.stable-diffusion-xl-v0";
12325
+ const body = {
12326
+ text_prompts: [
12327
+ {
12328
+ text: prompt
12329
+ }
12330
+ ],
12331
+ cfg_scale: options.scale ?? 10,
12332
+ seed: options.seed ?? 20,
12333
+ steps: options.steps ?? 50,
12334
+ width: options.width ?? 512,
12335
+ height: options.height ?? 512
12336
+ };
12337
+ const json = await invoke2({ body, modelId, streaming: false });
12338
+ const result = JSON.parse(json);
12339
+ return Buffer.from(result.artifacts[0].base64, "base64");
12340
+ }
12341
+ render.models = async () => {
12342
+ return ["stability.stable-diffusion-xl-v0"];
12343
+ };
12294
12344
  export {
12345
+ ChatMessage,
12346
+ bedrock,
12295
12347
  buildPrompt,
12296
12348
  chat,
12297
- invokeModel
12349
+ invoke2 as invoke,
12350
+ models,
12351
+ render
12298
12352
  };
12299
12353
  /*! Bundled license information:
12300
12354
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ampt/ai",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "type": "module",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",