| 1 | import { |
| 2 | BedrockRuntimeClient, |
| 3 | InvokeModelWithResponseStreamCommand, |
| 4 | } from "@aws-sdk/client-bedrock-runtime"; |
| 5 | import { fromNodeProviderChain } from "@aws-sdk/credential-providers"; |
| 6 | |
| 7 | import { CompletionOptions, LLMOptions } from "../../index.js"; |
| 8 | import { BaseLLM } from "../index.js"; |
| 9 | |
| 10 | class BedrockImport extends BaseLLM { |
| 11 | static providerName = "bedrockimport"; |
| 12 | static defaultOptions: Partial<LLMOptions> = { |
| 13 | region: "us-east-1", |
| 14 | }; |
| 15 | // the BedRock imported custom model ARN |
| 16 | modelArn?: string | undefined; |
| 17 | |
| 18 | constructor(options: LLMOptions) { |
| 19 | super(options); |
| 20 | if (!options.apiBase) { |
| 21 | this.apiBase = `https://bedrock-runtime.${options.region}.amazonaws.com`; |
| 22 | } |
| 23 | if (options.modelArn) { |
| 24 | this.modelArn = options.modelArn; |
| 25 | } |
| 26 | if (options.profile) { |
| 27 | this.profile = options.profile; |
| 28 | } else { |
| 29 | this.profile = "bedrock"; |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | protected async *_streamComplete( |
| 34 | prompt: string, |
| 35 | signal: AbortSignal, |
| 36 | options: CompletionOptions, |
| 37 | ): AsyncGenerator<string> { |
| 38 | const credentials = await this._getCredentials(); |
| 39 | const client = new BedrockRuntimeClient({ |
| 40 | region: this.region, |
| 41 | credentials: { |
| 42 | accessKeyId: credentials.accessKeyId, |
| 43 | secretAccessKey: credentials.secretAccessKey, |
| 44 | sessionToken: credentials.sessionToken || "", |
| 45 | }, |
| 46 | }); |
| 47 | |
| 48 | const input = this._generateInvokeModelCommandInput(prompt, options); |
| 49 | const command = new InvokeModelWithResponseStreamCommand(input); |
| 50 | const response = await client.send(command, { abortSignal: signal }); |
| 51 | |
| 52 | if (response.body) { |
| 53 | for await (const item of response.body) { |
| 54 | const decoder = new TextDecoder(); |
| 55 | const decoded = decoder.decode(item.chunk?.bytes); |
| 56 | try { |
| 57 | const chunk = JSON.parse(decoded); |
| 58 | if (chunk.outputs[0].text) { |
| 59 | yield chunk.outputs[0].text; |
| 60 | } |
| 61 | } catch (e) { |
| 62 | throw new Error(`Malformed JSON received from Bedrock: ${decoded}`); |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | private _generateInvokeModelCommandInput( |
| 69 | prompt: string, |
| 70 | options: CompletionOptions, |
| 71 | ): any { |
| 72 | const payload = { |
| 73 | prompt: prompt, |
| 74 | }; |
| 75 | |
| 76 | return { |
| 77 | body: JSON.stringify(payload), |
| 78 | modelId: this.modelArn, |
| 79 | accept: "application/json", |
| 80 | contentType: "application/json", |
| 81 | }; |
| 82 | } |
| 83 | |
| 84 | private async _getCredentials() { |
| 85 | try { |
| 86 | return await fromNodeProviderChain({ |
| 87 | profile: this.profile, |
| 88 | ignoreCache: true, |
| 89 | })(); |
| 90 | } catch (e) { |
| 91 | console.warn( |
| 92 | `AWS profile with name ${this.profile} not found in ~/.aws/credentials, using default profile`, |
| 93 | ); |
| 94 | return await fromNodeProviderChain()(); |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | export default BedrockImport; |