|
1 | | -import { Injectable } from '@nestjs/common'; |
2 | 1 | import { BedrockRuntimeClient, ConverseCommand } from '@aws-sdk/client-bedrock-runtime'; |
| 2 | +import { Injectable } from '@nestjs/common'; |
3 | 3 | import { IAIProvider } from './ai-provider.interface.js'; |
4 | 4 |
|
5 | 5 | @Injectable() |
6 | 6 | export class AmazonBedrockAiProvider implements IAIProvider { |
7 | | - private readonly bedrockRuntimeClient: BedrockRuntimeClient; |
8 | | - private readonly modelId: string = 'global.anthropic.claude-sonnet-4-5-20250929-v1:0'; |
9 | | - private readonly temperature: number = 0.7; |
10 | | - private readonly maxTokens: number = 1024; |
11 | | - private readonly region: string = 'us-west-2'; |
12 | | - private readonly topP: number = 0.9; |
| 7 | + private readonly bedrockRuntimeClient: BedrockRuntimeClient; |
| 8 | + private readonly modelId: string = 'global.anthropic.claude-sonnet-4-5-20250929-v1:0'; |
13 | 9 |
|
14 | | - constructor() { |
15 | | - this.bedrockRuntimeClient = new BedrockRuntimeClient({ |
16 | | - region: this.region, |
17 | | - credentials: { |
18 | | - accessKeyId: process.env.AWS_ACCESS_KEY_ID, |
19 | | - secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY, |
20 | | - }, |
21 | | - }); |
22 | | - } |
23 | | - public async generateResponse(prompt: string): Promise<string> { |
24 | | - const conversation = [ |
25 | | - { |
26 | | - role: 'user' as const, |
27 | | - content: [{ text: prompt }], |
28 | | - }, |
29 | | - ]; |
| 10 | + constructor() { |
| 11 | + this.bedrockRuntimeClient = new BedrockRuntimeClient(); |
| 12 | + } |
| 13 | + public async generateResponse(prompt: string): Promise<string> { |
| 14 | + const conversation = [ |
| 15 | + { |
| 16 | + role: 'user' as const, |
| 17 | + content: [{ text: prompt }], |
| 18 | + }, |
| 19 | + ]; |
30 | 20 |
|
31 | | - const command = new ConverseCommand({ |
32 | | - modelId: this.modelId, |
33 | | - messages: conversation, |
34 | | - inferenceConfig: { maxTokens: this.maxTokens, temperature: this.temperature, topP: this.topP }, |
35 | | - }); |
36 | | - try { |
37 | | - const response = await this.bedrockRuntimeClient.send(command); |
38 | | - const responseText = response.output.message?.content[0].text; |
39 | | - return responseText || 'No response generated.'; |
40 | | - } catch (error) { |
41 | | - console.error('Error generating AI response:', error); |
42 | | - throw new Error('Failed to generate AI response.'); |
43 | | - } |
44 | | - } |
| 21 | + const command = new ConverseCommand({ |
| 22 | + modelId: this.modelId, |
| 23 | + messages: conversation, |
| 24 | + }); |
| 25 | + try { |
| 26 | + const response = await this.bedrockRuntimeClient.send(command); |
| 27 | + console.info('AI response received from Amazon Bedrock.'); |
| 28 | + const responseText = response.output.message?.content[0].text; |
| 29 | + console.info('AI response text. ', responseText); |
| 30 | + return responseText || 'No response generated.'; |
| 31 | + } catch (error) { |
| 32 | + console.error('Error generating AI response:', error); |
| 33 | + throw new Error('Failed to generate AI response.'); |
| 34 | + } |
| 35 | + } |
45 | 36 | } |
0 commit comments