Skip to content

Commit 90f8537

Browse files
committed
base response transforms for non streaming bedrock calls for /v1/messages
1 parent 7af3637 commit 90f8537

File tree

4 files changed

+134
-68
lines changed

4 files changed

+134
-68
lines changed

src/providers/bedrock/chatComplete.ts

Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
BedrockCohereStreamChunk,
2828
} from './complete';
2929
import { BedrockErrorResponse } from './embed';
30+
import { BedrockChatCompletionResponse, BedrockContentItem } from './types';
3031
import {
3132
transformAdditionalModelRequestFields,
3233
transformAI21AdditionalModelRequestFields,
@@ -406,63 +407,6 @@ export const BedrockConverseChatCompleteConfig: ProviderConfig = {
406407
},
407408
};
408409

409-
type BedrockContentItem = {
410-
text?: string;
411-
toolUse?: {
412-
toolUseId: string;
413-
name: string;
414-
input: object;
415-
};
416-
reasoningContent?: {
417-
reasoningText?: {
418-
signature: string;
419-
text: string;
420-
};
421-
redactedContent?: string;
422-
};
423-
image?: {
424-
source: {
425-
bytes: string;
426-
};
427-
format: string;
428-
};
429-
document?: {
430-
format: string;
431-
name: string;
432-
source: {
433-
bytes?: string;
434-
s3Location?: {
435-
uri: string;
436-
};
437-
};
438-
};
439-
cachePoint?: {
440-
type: string;
441-
};
442-
};
443-
444-
interface BedrockChatCompletionResponse {
445-
metrics: {
446-
latencyMs: number;
447-
};
448-
output: {
449-
message: {
450-
role: string;
451-
content: BedrockContentItem[];
452-
};
453-
};
454-
stopReason: string;
455-
usage: {
456-
inputTokens: number;
457-
outputTokens: number;
458-
totalTokens: number;
459-
cacheReadInputTokenCount?: number;
460-
cacheReadInputTokens?: number;
461-
cacheWriteInputTokenCount?: number;
462-
cacheWriteInputTokens?: number;
463-
};
464-
}
465-
466410
export const BedrockErrorResponseTransform: (
467411
response: BedrockErrorResponse
468412
) => ErrorResponse | undefined = (response) => {

src/providers/bedrock/messages.ts

Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,20 @@ import {
88
ToolResultBlockParam,
99
ToolUseBlockParam,
1010
} from '../../types/MessagesRequest';
11-
import { MessagesResponse } from '../../types/messagesResponse';
11+
import {
12+
ContentBlock,
13+
MessagesResponse,
14+
STOP_REASON,
15+
} from '../../types/messagesResponse';
1216
import { ErrorResponse, ProviderConfig } from '../types';
1317
import { generateInvalidProviderResponseError } from '../utils';
1418
import { BedrockErrorResponseTransform } from './chatComplete';
1519
import { BedrockErrorResponse } from './embed';
16-
import { BedrockMessagesParams } from './types';
20+
import {
21+
BedrockChatCompletionResponse,
22+
BedrockContentItem,
23+
BedrockMessagesParams,
24+
} from './types';
1725
import {
1826
transformInferenceConfig,
1927
transformToolsConfig as transformToolConfig,
@@ -316,9 +324,46 @@ export const BedrockConverseMessagesConfig: ProviderConfig = {
316324
},
317325
};
318326

327+
const transformContentBlocks = (
328+
contentBlocks: BedrockContentItem[]
329+
): ContentBlock[] => {
330+
const transformedContent: ContentBlock[] = [];
331+
for (const contentBlock of contentBlocks) {
332+
if (contentBlock.text) {
333+
transformedContent.push({
334+
type: 'text',
335+
text: contentBlock.text,
336+
});
337+
} else if (contentBlock.reasoningContent?.reasoningText) {
338+
transformedContent.push({
339+
type: 'thinking',
340+
thinking: contentBlock.reasoningContent.reasoningText.text,
341+
signature: contentBlock.reasoningContent.reasoningText.signature,
342+
});
343+
} else if (contentBlock.reasoningContent?.redactedContent) {
344+
transformedContent.push({
345+
type: 'redacted_thinking',
346+
data: contentBlock.reasoningContent.redactedContent,
347+
});
348+
} else if (contentBlock.toolUse) {
349+
transformedContent.push({
350+
type: 'tool_use',
351+
id: contentBlock.toolUse.toolUseId,
352+
name: contentBlock.toolUse.name,
353+
input: contentBlock.toolUse.input,
354+
});
355+
}
356+
}
357+
return transformedContent;
358+
};
359+
319360
export const BedrockMessagesResponseTransform = (
320-
response: MessagesResponse | BedrockErrorResponse,
321-
responseStatus: number
361+
response: BedrockChatCompletionResponse | BedrockErrorResponse,
362+
responseStatus: number,
363+
_responseHeaders: Headers,
364+
_strictOpenAiCompliance: boolean,
365+
_gatewayRequestUrl: string,
366+
gatewayRequest: Params
322367
): MessagesResponse | ErrorResponse => {
323368
if (responseStatus !== 200 && 'error' in response) {
324369
return (
@@ -327,7 +372,28 @@ export const BedrockMessagesResponseTransform = (
327372
);
328373
}
329374

330-
if ('model' in response) return response;
375+
if ('output' in response) {
376+
const transformedContent = transformContentBlocks(
377+
response.output.message.content
378+
);
379+
const responseObj: MessagesResponse = {
380+
// TODO: shorten this
381+
id: 'portkey-' + crypto.randomUUID(),
382+
model: (gatewayRequest.model as string) || '',
383+
type: 'message',
384+
role: 'assistant',
385+
content: transformedContent,
386+
// TODO: pull changes from stop reason transformation PR
387+
stop_reason: response.stopReason as STOP_REASON,
388+
usage: {
389+
cache_read_input_tokens: response.usage.cacheReadInputTokens,
390+
cache_creation_input_tokens: response.usage.cacheWriteInputTokens,
391+
input_tokens: response.usage.inputTokens,
392+
output_tokens: response.usage.outputTokens,
393+
},
394+
};
395+
return responseObj;
396+
}
331397

332398
return generateInvalidProviderResponseError(response, BEDROCK);
333399
};

src/providers/bedrock/types.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,62 @@ export interface BedrockMessagesParams extends MessageCreateParamsBase {
9898
anthropic_version?: string;
9999
countPenalty?: number;
100100
}
101+
export interface BedrockChatCompletionResponse {
102+
metrics: {
103+
latencyMs: number;
104+
};
105+
output: {
106+
message: {
107+
role: string;
108+
content: BedrockContentItem[];
109+
};
110+
};
111+
stopReason: string;
112+
usage: {
113+
inputTokens: number;
114+
outputTokens: number;
115+
totalTokens: number;
116+
cacheReadInputTokenCount?: number;
117+
cacheReadInputTokens?: number;
118+
cacheWriteInputTokenCount?: number;
119+
cacheWriteInputTokens?: number;
120+
};
121+
}
122+
123+
export type BedrockContentItem = {
124+
text?: string;
125+
toolUse?: {
126+
toolUseId: string;
127+
name: string;
128+
input: object;
129+
};
130+
reasoningContent?: {
131+
reasoningText?: {
132+
signature: string;
133+
text: string;
134+
};
135+
redactedContent?: string;
136+
};
137+
image?: {
138+
source: {
139+
bytes: string;
140+
};
141+
format: string;
142+
};
143+
document?: {
144+
format: string;
145+
name: string;
146+
source: {
147+
bytes?: string;
148+
s3Location?: {
149+
uri: string;
150+
};
151+
};
152+
};
153+
cachePoint?: {
154+
type: string;
155+
};
156+
};
101157

102158
// export interface BedrockConverseRequestBody {
103159
// additionalModelRequestFields?: Record<string, any>;

src/types/messagesResponse.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export interface TextBlock {
6666
* Citing a PDF results in `page_location`, plain text results in `char_location`,
6767
* and content document results in `content_block_location`.
6868
*/
69-
citations: Array<TextCitation> | null;
69+
citations?: Array<TextCitation> | null;
7070

7171
text: string;
7272

@@ -170,12 +170,12 @@ export interface Usage {
170170
/**
171171
* The number of input tokens used to create the cache entry.
172172
*/
173-
cache_creation_input_tokens: number | null;
173+
cache_creation_input_tokens?: number | null;
174174

175175
/**
176176
* The number of input tokens read from the cache.
177177
*/
178-
cache_read_input_tokens: number | null;
178+
cache_read_input_tokens?: number | null;
179179

180180
/**
181181
* The number of input tokens which were used.
@@ -190,12 +190,12 @@ export interface Usage {
190190
/**
191191
* The number of server tool requests.
192192
*/
193-
server_tool_use: ServerToolUsage | null;
193+
server_tool_use?: ServerToolUsage | null;
194194

195195
/**
196196
* If the request used the priority, standard, or batch tier.
197197
*/
198-
service_tier: 'standard' | 'priority' | 'batch' | null;
198+
service_tier?: 'standard' | 'priority' | 'batch' | null;
199199
}
200200

201201
export interface MessagesResponse {
@@ -242,7 +242,7 @@ export interface MessagesResponse {
242242
* This value will be a non-null string if one of your custom stop sequences was
243243
* generated.
244244
*/
245-
stop_sequence: string | null;
245+
stop_sequence?: string | null;
246246

247247
/**
248248
* Object type.

0 commit comments

Comments
 (0)