|
5 | 5 |
|
6 | 6 | 'use strict'
|
7 | 7 |
|
| 8 | +const { stringifyClaudeChunkedMessage } = require('./utils') |
| 9 | + |
8 | 10 | /**
|
9 | 11 | * Parses an AWS invoke command instance into a re-usable entity.
|
10 | 12 | */
|
@@ -68,37 +70,34 @@ class BedrockCommand {
|
68 | 70 | /**
|
69 | 71 | * The question posed to the LLM.
|
70 | 72 | *
|
71 |
| - * @returns {string|string[]|undefined} |
| 73 | + * @returns {object[]} The array of context messages passed to the LLM (or a single user prompt for legacy "non-chat" models) |
72 | 74 | */
|
73 | 75 | get prompt() {
|
74 |
| - let result |
75 | 76 | if (this.isTitan() === true || this.isTitanEmbed() === true) {
|
76 |
| - result = this.#body.inputText |
| 77 | + return [ |
| 78 | + { |
| 79 | + role: 'user', |
| 80 | + content: this.#body.inputText |
| 81 | + } |
| 82 | + ] |
77 | 83 | } else if (this.isCohereEmbed() === true) {
|
78 |
| - result = this.#body.texts.join(' ') |
| 84 | + return [ |
| 85 | + { |
| 86 | + role: 'user', |
| 87 | + content: this.#body.texts.join(' ') |
| 88 | + } |
| 89 | + ] |
79 | 90 | } else if (
|
80 |
| - this.isClaude() === true || |
| 91 | + this.isClaudeTextCompletionApi() === true || |
81 | 92 | this.isAi21() === true ||
|
82 | 93 | this.isCohere() === true ||
|
83 | 94 | this.isLlama() === true
|
84 | 95 | ) {
|
85 |
| - result = this.#body.prompt |
86 |
| - } else if (this.isClaude3() === true) { |
87 |
| - const collected = [] |
88 |
| - for (const message of this.#body?.messages) { |
89 |
| - if (message?.role === 'assistant') { |
90 |
| - continue |
91 |
| - } |
92 |
| - if (typeof message?.content === 'string') { |
93 |
| - collected.push(message?.content) |
94 |
| - continue |
95 |
| - } |
96 |
| - const mappedMsgObj = message?.content.map((msgContent) => msgContent.text) |
97 |
| - collected.push(mappedMsgObj) |
98 |
| - } |
99 |
| - result = collected.join(' ') |
| 96 | + return [{ role: 'user', content: this.#body.prompt }] |
| 97 | + } else if (this.isClaudeMessagesApi() === true) { |
| 98 | + return normalizeClaude3Messages(this.#body?.messages) |
100 | 99 | }
|
101 |
| - return result |
| 100 | + return [] |
102 | 101 | }
|
103 | 102 |
|
104 | 103 | /**
|
@@ -151,6 +150,41 @@ class BedrockCommand {
|
151 | 150 | isTitanEmbed() {
|
152 | 151 | return this.#modelId.startsWith('amazon.titan-embed')
|
153 | 152 | }
|
| 153 | + |
| 154 | + isClaudeMessagesApi() { |
| 155 | + return (this.isClaude3() === true || this.isClaude() === true) && 'messages' in this.#body |
| 156 | + } |
| 157 | + |
| 158 | + isClaudeTextCompletionApi() { |
| 159 | + return this.isClaude() === true && 'prompt' in this.#body |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +/** |
| 164 | + * Claude v3 requests in Bedrock can have two different "chat" flavors. This function normalizes them into a consistent |
| 165 | + * format per the AIM agent spec |
| 166 | + * |
| 167 | + * @param messages - The raw array of messages passed to the invoke API |
| 168 | + * @returns {number|undefined} - The normalized messages |
| 169 | + */ |
| 170 | +function normalizeClaude3Messages(messages) { |
| 171 | + const result = [] |
| 172 | + for (const message of messages ?? []) { |
| 173 | + if (message == null) { |
| 174 | + continue |
| 175 | + } |
| 176 | + if (typeof message.content === 'string') { |
| 177 | + // Messages can be specified with plain string content |
| 178 | + result.push({ role: message.role, content: message.content }) |
| 179 | + } else if (Array.isArray(message.content)) { |
| 180 | + // Or in a "chunked" format for multi-modal support |
| 181 | + result.push({ |
| 182 | + role: message.role, |
| 183 | + content: stringifyClaudeChunkedMessage(message.content) |
| 184 | + }) |
| 185 | + } |
| 186 | + } |
| 187 | + return result |
154 | 188 | }
|
155 | 189 |
|
156 | 190 | module.exports = BedrockCommand
|
0 commit comments