@stacksjs/ai 0.65.0 → 0.67.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/index.js +80 -83
- package/package.json +3 -5
- package/dist/index.js.map +0 -657
- package/src/image.ts +0 -0
- package/src/index.ts +0 -8
- package/src/personalization.ts +0 -0
- package/src/search.ts +0 -0
- package/src/text.ts +0 -69
- package/src/utils/client-bedrock-runtime.ts +0 -54
- package/src/utils/client-bedrock.ts +0 -75
- package/src/utils/model-access.ts +0 -48
package/src/image.ts
DELETED
|
File without changes
|
package/src/index.ts
DELETED
package/src/personalization.ts
DELETED
|
File without changes
|
package/src/search.ts
DELETED
|
File without changes
|
package/src/text.ts
DELETED
|
@@ -1,69 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
InvokeModelCommandInput,
|
|
3
|
-
InvokeModelCommandOutput,
|
|
4
|
-
InvokeModelWithResponseStreamCommandInput,
|
|
5
|
-
InvokeModelWithResponseStreamCommandOutput,
|
|
6
|
-
} from '@aws-sdk/client-bedrock-runtime'
|
|
7
|
-
import process from 'node:process'
|
|
8
|
-
import {
|
|
9
|
-
BedrockRuntimeClient,
|
|
10
|
-
InvokeModelCommand,
|
|
11
|
-
InvokeModelWithResponseStreamCommand,
|
|
12
|
-
} from '@aws-sdk/client-bedrock-runtime'
|
|
13
|
-
|
|
14
|
-
export const client: BedrockRuntimeClient = new BedrockRuntimeClient({
|
|
15
|
-
region: process.env.REGION || 'us-east-1',
|
|
16
|
-
})
|
|
17
|
-
|
|
18
|
-
const logger = console // import your own logger
|
|
19
|
-
|
|
20
|
-
/*
|
|
21
|
-
* Invoke Model
|
|
22
|
-
* @param {InvokeModelCommandInput} params
|
|
23
|
-
* @returns {Promise<InvokeModelCommandOutput>}
|
|
24
|
-
* @see https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/BedrockRuntime.html#invokeModel-property
|
|
25
|
-
*/
|
|
26
|
-
export async function invokeModel(params: InvokeModelCommandInput): Promise<InvokeModelCommandOutput> {
|
|
27
|
-
logger.debug(params)
|
|
28
|
-
const command = new InvokeModelCommand(params)
|
|
29
|
-
const res = await client.send(command)
|
|
30
|
-
logger.debug('Successfully invoke model')
|
|
31
|
-
logger.debug(res)
|
|
32
|
-
return res
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/*
|
|
36
|
-
* Invoke Model With Response Stream
|
|
37
|
-
* @param {InvokeModelWithResponseStreamCommandInput} params
|
|
38
|
-
* @returns {Promise<InvokeModelWithResponseStreamCommandOutput>}
|
|
39
|
-
* @see https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/BedrockRuntime.html#invokeModelWithResponseStream-property
|
|
40
|
-
*/
|
|
41
|
-
export async function invokeModelWithResponseStream(
|
|
42
|
-
params: InvokeModelWithResponseStreamCommandInput,
|
|
43
|
-
): Promise<InvokeModelWithResponseStreamCommandOutput> {
|
|
44
|
-
logger.debug(params)
|
|
45
|
-
const command = new InvokeModelWithResponseStreamCommand(params)
|
|
46
|
-
const res = await client.send(command)
|
|
47
|
-
logger.debug('Successfully invoke model with response stream')
|
|
48
|
-
logger.debug(res)
|
|
49
|
-
return res
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export { InvokeModelCommand }
|
|
53
|
-
|
|
54
|
-
export type { InvokeModelCommandInput, InvokeModelWithResponseStreamCommandInput }
|
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
CreateModelCustomizationJobCommandInput,
|
|
3
|
-
CreateModelCustomizationJobCommandOutput,
|
|
4
|
-
GetModelCustomizationJobCommandInput,
|
|
5
|
-
GetModelCustomizationJobCommandOutput,
|
|
6
|
-
ListFoundationModelsCommandInput,
|
|
7
|
-
ListFoundationModelsCommandOutput,
|
|
8
|
-
} from '@aws-sdk/client-bedrock'
|
|
9
|
-
import process from 'node:process'
|
|
10
|
-
import {
|
|
11
|
-
BedrockClient,
|
|
12
|
-
CreateModelCustomizationJobCommand,
|
|
13
|
-
GetModelCustomizationJobCommand,
|
|
14
|
-
ListFoundationModelsCommand,
|
|
15
|
-
} from '@aws-sdk/client-bedrock'
|
|
16
|
-
|
|
17
|
-
const client = new BedrockClient({ region: process.env.REGION || 'us-east-1' })
|
|
18
|
-
const logger = console // import your own logger
|
|
19
|
-
|
|
20
|
-
/*
|
|
21
|
-
* Create Model Customization Job
|
|
22
|
-
* @param {CreateModelCustomizationJobCommandInput} params
|
|
23
|
-
* @returns {Promise<CreateModelCustomizationJobCommandOutput>}
|
|
24
|
-
* @see https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Bedrock.html#CreateModelCustomizationJob-property
|
|
25
|
-
*/
|
|
26
|
-
export async function createModelCustomizationJob(
|
|
27
|
-
param: CreateModelCustomizationJobCommandInput,
|
|
28
|
-
): Promise<CreateModelCustomizationJobCommandOutput> {
|
|
29
|
-
logger.debug(param)
|
|
30
|
-
const command = new CreateModelCustomizationJobCommand(param)
|
|
31
|
-
const res = await client.send(command)
|
|
32
|
-
logger.debug('Successfully create model customization job')
|
|
33
|
-
logger.debug(res)
|
|
34
|
-
return res
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/*
|
|
38
|
-
* Get Model Customization Job
|
|
39
|
-
* @param {GetModelCustomizationJobCommandInput} params
|
|
40
|
-
* @returns {Promise<GetModelCustomizationJobCommandOutput>}
|
|
41
|
-
* @see https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Bedrock.html#getModelCustomizationJob-property
|
|
42
|
-
*/
|
|
43
|
-
export async function getModelCustomizationJob(
|
|
44
|
-
params: GetModelCustomizationJobCommandInput,
|
|
45
|
-
): Promise<GetModelCustomizationJobCommandOutput> {
|
|
46
|
-
logger.debug(params)
|
|
47
|
-
const command = new GetModelCustomizationJobCommand(params)
|
|
48
|
-
const res = await client.send(command)
|
|
49
|
-
logger.debug('Successfully get model customization job')
|
|
50
|
-
logger.debug(res)
|
|
51
|
-
return res
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/*
|
|
55
|
-
* List Foundation Models
|
|
56
|
-
* @param {ListFoundationModelsCommandInput} params
|
|
57
|
-
* @returns {Promise<ListFoundationModelsCommandOutput>}
|
|
58
|
-
* @see https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Bedrock.html#listFoundationModels-property
|
|
59
|
-
*/
|
|
60
|
-
export async function listFoundationModels(
|
|
61
|
-
params: ListFoundationModelsCommandInput,
|
|
62
|
-
): Promise<ListFoundationModelsCommandOutput> {
|
|
63
|
-
logger.debug(params)
|
|
64
|
-
const command = new ListFoundationModelsCommand(params)
|
|
65
|
-
const res = await client.send(command)
|
|
66
|
-
logger.debug('Successfully list foundation models')
|
|
67
|
-
logger.debug(res)
|
|
68
|
-
return res
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
export type {
|
|
72
|
-
CreateModelCustomizationJobCommandInput,
|
|
73
|
-
GetModelCustomizationJobCommandInput,
|
|
74
|
-
ListFoundationModelsCommandInput,
|
|
75
|
-
}
|
|
@@ -1,48 +0,0 @@
|
|
|
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
|
-
}
|