@stacksjs/ai 0.64.6 → 0.66.0

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/text.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ export interface AiOptions {
2
+ maxTokenCount?: number;
3
+ temperature?: number;
4
+ topP?: number;
5
+ }
6
+ export interface SummarizeOptions extends AiOptions {}
7
+ export interface AskOptions extends AiOptions {}
8
+ export declare function summarize(text: string, options?: SummarizeOptions): Promise<string>;
9
+ export declare function ask(question: string, options?: AskOptions): Promise<string>;
@@ -0,0 +1,7 @@
1
+ import type { InvokeModelCommandInput, InvokeModelCommandOutput, InvokeModelWithResponseStreamCommandInput, InvokeModelWithResponseStreamCommandOutput } from "@aws-sdk/client-bedrock-runtime";
2
+ import { BedrockRuntimeClient, InvokeModelCommand } from "@aws-sdk/client-bedrock-runtime";
3
+ export declare const client: BedrockRuntimeClient;
4
+ export declare function invokeModel(params: InvokeModelCommandInput): Promise<InvokeModelCommandOutput>;
5
+ export declare function invokeModelWithResponseStream(params: InvokeModelWithResponseStreamCommandInput): Promise<InvokeModelWithResponseStreamCommandOutput>;
6
+ export { InvokeModelCommand };
7
+ export type { InvokeModelCommandInput, InvokeModelWithResponseStreamCommandInput };
@@ -0,0 +1,5 @@
1
+ import type { CreateModelCustomizationJobCommandInput, CreateModelCustomizationJobCommandOutput, GetModelCustomizationJobCommandInput, GetModelCustomizationJobCommandOutput, ListFoundationModelsCommandInput, ListFoundationModelsCommandOutput } from "@aws-sdk/client-bedrock";
2
+ export declare function createModelCustomizationJob(param: CreateModelCustomizationJobCommandInput): Promise<CreateModelCustomizationJobCommandOutput>;
3
+ export declare function getModelCustomizationJob(params: GetModelCustomizationJobCommandInput): Promise<GetModelCustomizationJobCommandOutput>;
4
+ export declare function listFoundationModels(params: ListFoundationModelsCommandInput): Promise<ListFoundationModelsCommandOutput>;
5
+ export type { CreateModelCustomizationJobCommandInput, GetModelCustomizationJobCommandInput, ListFoundationModelsCommandInput };
@@ -0,0 +1 @@
1
+ export declare function requestModelAccess(): Promise<void>;
package/package.json CHANGED
@@ -1,9 +1,10 @@
1
1
  {
2
2
  "name": "@stacksjs/ai",
3
3
  "type": "module",
4
- "version": "0.64.6",
4
+ "version": "0.66.0",
5
5
  "description": "Stacks Artificial Intelligence.",
6
6
  "author": "Chris Breuer",
7
+ "contributors": ["Chris Breuer <chris@stacksjs.org>"],
7
8
  "license": "MIT",
8
9
  "funding": "https://github.com/sponsors/chrisbbreuer",
9
10
  "homepage": "https://github.com/stacksjs/stacks/tree/main/storage/framework/core/ai#readme",
@@ -35,23 +36,17 @@
35
36
  },
36
37
  "module": "dist/index.js",
37
38
  "types": "dist/index.d.ts",
38
- "contributors": [
39
- "Chris Breuer <chris@stacksjs.org>"
40
- ],
41
- "files": [
42
- "README.md",
43
- "dist",
44
- "src"
45
- ],
39
+ "files": ["README.md", "dist", "src"],
46
40
  "scripts": {
47
- "build": "bun --bun build.ts",
48
- "typecheck": "bun --bun tsc --noEmit",
41
+ "build": "bun build.ts",
42
+ "typecheck": "bun tsc --noEmit",
43
+ "test": "bun test",
49
44
  "prepublishOnly": "bun run build"
50
45
  },
51
- "dependencies": {
52
- "@aws-sdk/client-bedrock-runtime": "^3.637.0"
53
- },
54
46
  "devDependencies": {
55
- "@stacksjs/development": "latest"
47
+ "@aws-sdk/client-bedrock-runtime": "^3.668.0",
48
+ "@aws-sdk/credential-providers": "^3.668.0",
49
+ "@stacksjs/development": "0.65.1",
50
+ "aws-sdk-client-mock": "^4.0.2"
56
51
  }
57
52
  }
package/src/index.ts CHANGED
@@ -1,8 +1,8 @@
1
+ export * from './text'
1
2
  export * from './utils/client-bedrock'
2
3
  export * from './utils/client-bedrock-runtime'
4
+ export * from './utils/model-access'
3
5
 
4
6
  // export * from './chatbots'
5
7
  // export * from './image-generation'
6
8
  // export * from './search'
7
- // export * from './text-generation'
8
- // export * from './text-summary'
package/src/text.ts ADDED
@@ -0,0 +1,69 @@
1
+ import { client, InvokeModelCommand } from './utils/client-bedrock-runtime'
2
+
3
+ export interface AiOptions {
4
+ maxTokenCount?: number
5
+ temperature?: number
6
+ topP?: number
7
+ }
8
+
9
+ export interface SummarizeOptions extends AiOptions {}
10
+ export interface AskOptions extends AiOptions {}
11
+
12
+ export async function summarize(text: string, options: SummarizeOptions = {}): Promise<string> {
13
+ const { maxTokenCount = 512, temperature = 0, topP = 0.9 } = options
14
+
15
+ const command = new InvokeModelCommand({
16
+ modelId: 'amazon.titan-text-express-v1',
17
+ contentType: 'application/json',
18
+ accept: '*/*',
19
+ body: JSON.stringify({
20
+ inputText: `Summarize the following text: ${text}`,
21
+ textGenerationConfig: {
22
+ maxTokenCount,
23
+ stopSequences: [],
24
+ temperature,
25
+ topP,
26
+ },
27
+ }),
28
+ })
29
+
30
+ try {
31
+ const response = await client.send(command)
32
+ const responseBody = JSON.parse(new TextDecoder().decode(response.body))
33
+ return responseBody.results[0].outputText
34
+ }
35
+ catch (error) {
36
+ console.error('Error summarizing text:', error)
37
+ throw error
38
+ }
39
+ }
40
+
41
+ export async function ask(question: string, options: AskOptions = {}): Promise<string> {
42
+ const { maxTokenCount = 512, temperature = 0, topP = 0.9 } = options
43
+
44
+ const command = new InvokeModelCommand({
45
+ modelId: 'amazon.titan-text-express-v1',
46
+ contentType: 'application/json',
47
+ accept: '*/*',
48
+ body: JSON.stringify({
49
+ inputText: question,
50
+ textGenerationConfig: {
51
+ maxTokenCount,
52
+ stopSequences: [],
53
+ temperature,
54
+ topP,
55
+ },
56
+ }),
57
+ })
58
+
59
+ try {
60
+ const response = await client.send(command)
61
+ const responseBody = JSON.parse(new TextDecoder().decode(response.body))
62
+
63
+ return responseBody.results[0].outputText
64
+ }
65
+ catch (error) {
66
+ console.error('Error asking question:', error)
67
+ throw error
68
+ }
69
+ }
@@ -4,15 +4,17 @@ import type {
4
4
  InvokeModelWithResponseStreamCommandInput,
5
5
  InvokeModelWithResponseStreamCommandOutput,
6
6
  } from '@aws-sdk/client-bedrock-runtime'
7
+ import process from 'node:process'
7
8
  import {
8
9
  BedrockRuntimeClient,
9
10
  InvokeModelCommand,
10
11
  InvokeModelWithResponseStreamCommand,
11
12
  } from '@aws-sdk/client-bedrock-runtime'
12
13
 
13
- const client = new BedrockRuntimeClient({
14
+ export const client: BedrockRuntimeClient = new BedrockRuntimeClient({
14
15
  region: process.env.REGION || 'us-east-1',
15
16
  })
17
+
16
18
  const logger = console // import your own logger
17
19
 
18
20
  /*
@@ -47,4 +49,6 @@ export async function invokeModelWithResponseStream(
47
49
  return res
48
50
  }
49
51
 
52
+ export { InvokeModelCommand }
53
+
50
54
  export type { InvokeModelCommandInput, InvokeModelWithResponseStreamCommandInput }
@@ -6,6 +6,7 @@ import type {
6
6
  ListFoundationModelsCommandInput,
7
7
  ListFoundationModelsCommandOutput,
8
8
  } from '@aws-sdk/client-bedrock'
9
+ import process from 'node:process'
9
10
  import {
10
11
  BedrockClient,
11
12
  CreateModelCustomizationJobCommand,
@@ -0,0 +1,48 @@
1
+ import process from 'node:process'
2
+ import { defaultProvider } from '@aws-sdk/credential-provider-node'
3
+ import { log } from '@stacksjs/cli'
4
+ import { ai } from '@stacksjs/config'
5
+ import AWS4 from 'aws4'
6
+
7
+ export async function requestModelAccess(): Promise<void> {
8
+ process.env.AWS_REGION = 'us-east-1'
9
+ const credentials = await defaultProvider()()
10
+
11
+ const models = ai.models
12
+ if (!models)
13
+ throw new Error('No AI models found. Please set ./config/ai.ts values.')
14
+
15
+ for (const model of models) {
16
+ try {
17
+ log.info(`Requesting access to model ${model}`)
18
+ const request = {
19
+ host: 'bedrock.us-east-1.amazonaws.com',
20
+ method: 'POST',
21
+ path: '/foundation-model-entitlement',
22
+ headers: {
23
+ 'Content-Type': 'application/json',
24
+ },
25
+ body: JSON.stringify({ modelId: model }),
26
+ service: 'bedrock',
27
+ region: 'us-east-1',
28
+ }
29
+
30
+ const signedRequest = AWS4.sign(request, credentials)
31
+ const headers = Object.fromEntries(
32
+ Object.entries(signedRequest.headers || {}).map(([key, value]) => [key, String(value)]),
33
+ )
34
+
35
+ const response = await fetch(`https://${signedRequest.host}${signedRequest.path}`, {
36
+ method: signedRequest.method,
37
+ headers,
38
+ body: signedRequest.body,
39
+ })
40
+
41
+ const data = await response.json()
42
+ log.info(`Response for model ${model}:`, data)
43
+ }
44
+ catch (error) {
45
+ log.error(`Error requesting access to model ${model}:`, error)
46
+ }
47
+ }
48
+ }
File without changes
File without changes
File without changes