@ampt/ai 1.0.1 → 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
@@ -15142,7 +15142,8 @@ __export(src_exports, {
15142
15142
  buildPrompt: () => buildPrompt,
15143
15143
  chat: () => chat,
15144
15144
  invoke: () => invoke2,
15145
- models: () => models
15145
+ models: () => models,
15146
+ render: () => render
15146
15147
  });
15147
15148
  module.exports = __toCommonJS(src_exports);
15148
15149
 
@@ -24897,34 +24898,39 @@ var bedrock = new BedrockRuntime({ credentialDefaultProvider: credentialsProvide
24897
24898
 
24898
24899
  // src/invoke.ts
24899
24900
  async function invoke2(options) {
24900
- const body = {
24901
- prompt: options.prompt,
24902
- max_tokens_to_sample: options.maxTokens ?? 50
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
24903
24910
  };
24904
- const result = await bedrock.invokeModelWithResponseStream({
24905
- body: JSON.stringify(body),
24906
- contentType: "application/json",
24907
- accept: "application/json",
24908
- modelId: options.modelId ?? "anthropic.claude-instant-v1"
24909
- });
24911
+ const result = streaming ? await bedrock.invokeModelWithResponseStream(args) : await bedrock.invokeModel(args);
24910
24912
  if (!result.body) {
24911
24913
  throw new Error("Failed to invoke model");
24912
24914
  }
24913
- const iterator = result.body[Symbol.asyncIterator]();
24914
- return new ReadableStream({
24915
- async pull(controller) {
24916
- const { value, done } = await iterator.next();
24917
- if (done) {
24918
- controller.close();
24919
- } else {
24920
- if (!value.chunk?.bytes) {
24921
- console.warn("Error in LLM response stream:", value);
24922
- 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);
24923
24928
  }
24924
- controller.enqueue(value.chunk.bytes);
24925
24929
  }
24926
- }
24927
- });
24930
+ });
24931
+ } else {
24932
+ return result.body.transformToString();
24933
+ }
24928
24934
  }
24929
24935
 
24930
24936
  // src/chat.ts
@@ -24959,7 +24965,12 @@ async function chat(messages, options = {}) {
24959
24965
  }
24960
24966
  });
24961
24967
  const prompt = buildPrompt(messages);
24962
- const response = await invoke2({ prompt, maxTokens: options.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 });
24963
24974
  return response.pipeThrough(transformer);
24964
24975
  }
24965
24976
  chat.models = async () => {
@@ -24968,8 +24979,36 @@ chat.models = async () => {
24968
24979
 
24969
24980
  // src/models.ts
24970
24981
  async function models() {
24971
- return ["anthropic.claude-v1", "anthropic.claude-instant-v1", "anthropic.claude-v2"];
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");
24972
25008
  }
25009
+ render.models = async () => {
25010
+ return ["stability.stable-diffusion-xl-v0"];
25011
+ };
24973
25012
  // Annotate the CommonJS export names for ESM import in node:
24974
25013
  0 && (module.exports = {
24975
25014
  ChatMessage,
@@ -24977,7 +25016,8 @@ async function models() {
24977
25016
  buildPrompt,
24978
25017
  chat,
24979
25018
  invoke,
24980
- models
25019
+ models,
25020
+ render
24981
25021
  });
24982
25022
  /*! Bundled license information:
24983
25023
 
package/dist/index.d.ts CHANGED
@@ -1,18 +1,27 @@
1
1
  import { BedrockRuntime } from '@aws-sdk/client-bedrock-runtime';
2
2
 
3
3
  interface InvokeOptions {
4
- prompt: string;
5
- maxTokens?: number;
6
- modelId?: string;
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;
7
15
  }
8
16
  /**
9
17
  * Invoke model and return events as a ReadableStream.
10
18
  * @param options
11
19
  * @returns
12
20
  */
13
- declare function invoke(options: InvokeOptions): Promise<ReadableStream<any>>;
21
+ declare function invoke(options: InvokeStreamingDisabledOptions): Promise<string>;
22
+ declare function invoke(options: InvokeStreamingEnabledOptions): Promise<ReadableStream<Uint8Array>>;
14
23
 
15
- type ModelId = 'anthropic.claude-v2' | 'anthropic.claude-instant-v1' | 'anthropic.claude-v1';
24
+ type ModelId = 'anthropic.claude-v2' | 'anthropic.claude-instant-v1' | 'anthropic.claude-v1' | 'stability.stable-diffusion-xl-v0';
16
25
  declare function models(): Promise<ModelId[]>;
17
26
 
18
27
  type MessageRole = 'assistant' | 'human';
@@ -46,4 +55,17 @@ declare namespace chat {
46
55
 
47
56
  declare const bedrock: BedrockRuntime;
48
57
 
49
- export { ChatMessage, ChatOptions, InvokeOptions, MessageRole, ModelId, bedrock, buildPrompt, chat, invoke, models };
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[]>;
69
+ }
70
+
71
+ export { ChatMessage, ChatOptions, InvokeOptions, InvokeStreamingDisabledOptions, InvokeStreamingEnabledOptions, MessageRole, ModelId, RenderOptions, bedrock, buildPrompt, chat, invoke, models, render };
package/dist/index.js CHANGED
@@ -12230,34 +12230,39 @@ var bedrock = new BedrockRuntime({ credentialDefaultProvider: credentialsProvide
12230
12230
 
12231
12231
  // src/invoke.ts
12232
12232
  async function invoke2(options) {
12233
- const body = {
12234
- prompt: options.prompt,
12235
- max_tokens_to_sample: options.maxTokens ?? 50
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
12236
12242
  };
12237
- const result = await bedrock.invokeModelWithResponseStream({
12238
- body: JSON.stringify(body),
12239
- contentType: "application/json",
12240
- accept: "application/json",
12241
- modelId: options.modelId ?? "anthropic.claude-instant-v1"
12242
- });
12243
+ const result = streaming ? await bedrock.invokeModelWithResponseStream(args) : await bedrock.invokeModel(args);
12243
12244
  if (!result.body) {
12244
12245
  throw new Error("Failed to invoke model");
12245
12246
  }
12246
- const iterator = result.body[Symbol.asyncIterator]();
12247
- return new ReadableStream({
12248
- async pull(controller) {
12249
- const { value, done } = await iterator.next();
12250
- if (done) {
12251
- controller.close();
12252
- } else {
12253
- if (!value.chunk?.bytes) {
12254
- console.warn("Error in LLM response stream:", value);
12255
- 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);
12256
12260
  }
12257
- controller.enqueue(value.chunk.bytes);
12258
12261
  }
12259
- }
12260
- });
12262
+ });
12263
+ } else {
12264
+ return result.body.transformToString();
12265
+ }
12261
12266
  }
12262
12267
 
12263
12268
  // src/chat.ts
@@ -12292,7 +12297,12 @@ async function chat(messages, options = {}) {
12292
12297
  }
12293
12298
  });
12294
12299
  const prompt = buildPrompt(messages);
12295
- const response = await invoke2({ prompt, maxTokens: options.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 });
12296
12306
  return response.pipeThrough(transformer);
12297
12307
  }
12298
12308
  chat.models = async () => {
@@ -12301,15 +12311,44 @@ chat.models = async () => {
12301
12311
 
12302
12312
  // src/models.ts
12303
12313
  async function models() {
12304
- return ["anthropic.claude-v1", "anthropic.claude-instant-v1", "anthropic.claude-v2"];
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");
12305
12340
  }
12341
+ render.models = async () => {
12342
+ return ["stability.stable-diffusion-xl-v0"];
12343
+ };
12306
12344
  export {
12307
12345
  ChatMessage,
12308
12346
  bedrock,
12309
12347
  buildPrompt,
12310
12348
  chat,
12311
12349
  invoke2 as invoke,
12312
- models
12350
+ models,
12351
+ render
12313
12352
  };
12314
12353
  /*! Bundled license information:
12315
12354
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ampt/ai",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "type": "module",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",