Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

test-sagemaker-provider.js 4.4 KB

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

Press p or to see the previous file or, n or to see the next file

Comments

Loading...