1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
- #!/usr/bin/env node
- /**
- * Test script for the SageMaker provider.
- * Shows how to use the provider with both Llama and Mistral models.
- */
- const { ArgumentParser } = require('argparse');
- const {
- SageMakerCompletionProvider,
- SageMakerEmbeddingProvider,
- } = require('../../dist/providers/sagemaker');
- // Define usage information
- function printUsage() {
- console.log('Usage: node test-sagemaker-provider.js [options]');
- console.log('');
- console.log('Options:');
- console.log(' --model=<name> Specify the model (llama or mistral, default: llama)');
- console.log(' --prompt=<text> Specify the prompt text');
- console.log(' --max-tokens=<num> Maximum tokens to generate (default: 256)');
- console.log(' --temperature=<num> Temperature setting (default: 0.7)');
- console.log('');
- }
- // Process command line arguments
- function parseArgs() {
- const parser = new ArgumentParser({
- description: 'Test SageMaker provider',
- });
- parser.add_argument('--endpoint', {
- help: 'SageMaker endpoint name',
- default: 'your-endpoint-name',
- });
- parser.add_argument('--region', { help: 'AWS region', default: 'us-west-2' });
- parser.add_argument('--model-type', {
- help: 'Model type',
- choices: ['openai', 'anthropic', 'llama', 'huggingface', 'jumpstart', 'custom'],
- default: 'custom',
- dest: 'modelType',
- });
- parser.add_argument('--embedding', {
- help: 'Test embedding endpoint',
- action: 'store_true',
- });
- parser.add_argument('--transform', {
- help: 'Test transform functionality',
- action: 'store_true',
- });
- parser.add_argument('--transform-file', {
- help: 'Path to transform file',
- default: 'transform.js',
- dest: 'transformFile',
- });
- parser.add_argument('--response-path', {
- help: 'Response path expression',
- default: 'json.generated_text',
- dest: 'responsePath',
- });
- const args = parser.parse_args();
- return args;
- }
- async function testSageMaker() {
- const args = parseArgs();
- console.log(`Testing SageMaker provider with endpoint: ${args.endpoint}`);
- console.log(`Region: ${args.region}`);
- console.log(`Model type: ${args.modelType}`);
- // Test prompt
- const prompt = 'Generate a creative name for a coffee shop that specializes in caramel coffee.';
- try {
- if (args.embedding) {
- // Test embedding functionality
- console.log('Testing embedding endpoint...');
- const provider = new SageMakerEmbeddingProvider(args.endpoint, {
- config: {
- region: args.region,
- modelType: args.modelType,
- responseFormat: {
- path: args.responsePath,
- },
- },
- transform: args.transform
- ? args.transformFile.startsWith('file://')
- ? args.transformFile
- : `file://${args.transformFile}`
- : undefined,
- });
- const result = await provider.callEmbeddingApi(prompt);
- console.log('Embedding result:');
- console.log(`Success: ${!result.error}`);
- if (result.error) {
- console.error(`Error: ${result.error}`);
- } else {
- console.log(`Embedding length: ${result.embedding.length}`);
- console.log(`First few values: ${result.embedding.slice(0, 5).join(', ')}`);
- }
- } else {
- // Test completion functionality
- console.log('Testing completion endpoint...');
- const provider = new SageMakerCompletionProvider(args.endpoint, {
- config: {
- region: args.region,
- modelType: args.modelType,
- responseFormat: {
- path: args.responsePath,
- },
- },
- transform: args.transform
- ? args.transformFile.startsWith('file://')
- ? args.transformFile
- : `file://${args.transformFile}`
- : undefined,
- });
- const result = await provider.callApi(prompt);
- console.log('Completion result:');
- console.log(`Success: ${!result.error}`);
- if (result.error) {
- console.error(`Error: ${result.error}`);
- } else {
- console.log('Output:');
- console.log(result.output);
- // Show additional metadata
- console.log('\nMetadata:');
- console.log(JSON.stringify(result.metadata, null, 2));
- // Show token usage
- console.log('\nToken usage:');
- console.log(JSON.stringify(result.tokenUsage, null, 2));
- }
- }
- } catch (error) {
- console.error('Error testing SageMaker provider:');
- console.error(error);
- }
- }
- // Run the test
- testSageMaker().catch(console.error);
|