Skip to content

Commit 038082a

Browse files
committed
changes from review
1 parent f412646 commit 038082a

File tree

7 files changed

+55
-15
lines changed

7 files changed

+55
-15
lines changed

src/providers/bedrock/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ const BedrockConfig: ProviderConfigs = {
203203
if (!config.chatComplete) {
204204
config.chatComplete = BedrockConverseChatCompleteConfig;
205205
}
206+
if (!config.messages) {
207+
config.messages = BedrockConverseMessagesConfig;
208+
}
206209
if (!config.responseTransforms?.['stream-chatComplete']) {
207210
config.responseTransforms = {
208211
...(config.responseTransforms ?? {}),

src/providers/bedrock/messages.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
import {
1212
ContentBlock,
1313
MessagesResponse,
14-
STOP_REASON,
14+
ANTHROPIC_STOP_REASON,
1515
} from '../../types/messagesResponse';
1616
import { RawContentBlockDeltaEvent } from '../../types/MessagesStreamResponse';
1717
import {
@@ -22,7 +22,10 @@ import {
2222
ANTHROPIC_MESSAGE_STOP_EVENT,
2323
} from '../anthropic-base/constants';
2424
import { ErrorResponse, ProviderConfig } from '../types';
25-
import { generateInvalidProviderResponseError } from '../utils';
25+
import {
26+
generateInvalidProviderResponseError,
27+
transformToAnthropicStopReason,
28+
} from '../utils';
2629
import { BedrockErrorResponseTransform } from './chatComplete';
2730
import { BedrockErrorResponse } from './embed';
2831
import {
@@ -185,7 +188,6 @@ const appendToolResultBlock = (
185188
text: item.text,
186189
});
187190
} else if (item.type === 'image') {
188-
// TODO: test this
189191
appendImageBlock(transformedToolResultContent, item);
190192
}
191193
}
@@ -419,8 +421,7 @@ export const BedrockMessagesResponseTransform = (
419421
type: 'message',
420422
role: 'assistant',
421423
content: transformedContent,
422-
// TODO: pull changes from stop reason transformation PR
423-
stop_reason: response.stopReason as STOP_REASON,
424+
stop_reason: transformToAnthropicStopReason(response.stopReason),
424425
usage: {
425426
cache_read_input_tokens: response.usage.cacheReadInputTokens,
426427
cache_creation_input_tokens: response.usage.cacheWriteInputTokens,
@@ -535,15 +536,16 @@ export const BedrockConverseMessagesStreamChunkTransform = (
535536
parsedChunk.usage.cacheReadInputTokens;
536537
messageDeltaEvent.usage.cache_creation_input_tokens =
537538
parsedChunk.usage.cacheWriteInputTokens;
538-
messageDeltaEvent.delta.stop_reason = streamState.stopReason || '';
539+
messageDeltaEvent.delta.stop_reason = transformToAnthropicStopReason(
540+
streamState.stopReason
541+
);
539542
const contentBlockStopEvent = { ...ANTHROPIC_CONTENT_BLOCK_STOP_EVENT };
540543
contentBlockStopEvent.index = streamState.currentContentBlockIndex;
541544
let returnChunk = `event: content_block_stop\ndata: ${JSON.stringify(contentBlockStopEvent)}\n\n`;
542545
returnChunk += `event: message_delta\ndata: ${JSON.stringify(messageDeltaEvent)}\n\n`;
543546
returnChunk += `event: message_stop\ndata: ${JSON.stringify(ANTHROPIC_MESSAGE_STOP_EVENT)}\n\n`;
544547
return returnChunk;
545548
}
546-
// console.log(JSON.stringify(parsedChunk, null, 2));
547549
};
548550

549551
function getMessageStartEvent(fallbackId: string, gatewayRequest: Params<any>) {

src/providers/bedrock/types.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ export interface BedrockChatCompletionResponse {
108108
content: BedrockContentItem[];
109109
};
110110
};
111-
stopReason: string;
111+
stopReason: BEDROCK_STOP_REASON;
112112
usage: {
113113
inputTokens: number;
114114
outputTokens: number;
@@ -154,8 +154,9 @@ export type BedrockContentItem = {
154154
type: string;
155155
};
156156
};
157+
157158
export interface BedrockStreamState {
158-
stopReason?: string;
159+
stopReason?: BEDROCK_STOP_REASON;
159160
currentToolCallIndex?: number;
160161
currentContentBlockIndex?: number;
161162
}
@@ -185,7 +186,7 @@ export interface BedrockChatCompleteStreamChunk {
185186
input?: object;
186187
};
187188
};
188-
stopReason?: string;
189+
stopReason?: BEDROCK_STOP_REASON;
189190
metrics?: {
190191
latencyMs: number;
191192
};

src/providers/utils.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
import { ANTHROPIC_STOP_REASON } from './anthropic/types';
12
import { FINISH_REASON, ErrorResponse, PROVIDER_FINISH_REASON } from './types';
2-
import { finishReasonMap } from './utils/finishReasonMap';
3+
import {
4+
AnthropicFinishReasonMap,
5+
finishReasonMap,
6+
} from './utils/finishReasonMap';
37

48
export const generateInvalidProviderResponseError: (
59
response: Record<string, any>,
@@ -78,3 +82,19 @@ export const transformFinishReason = (
7882
}
7983
return transformedFinishReason;
8084
};
85+
86+
/*
87+
Transforms the finish reason from the provider to the finish reason used by the Anthropic API.
88+
If the finish reason is not found in the map, it will return the stop reason.
89+
NOTE: this function always returns a finish reason
90+
*/
91+
export const transformToAnthropicStopReason = (
92+
finishReason?: PROVIDER_FINISH_REASON
93+
): ANTHROPIC_STOP_REASON => {
94+
if (!finishReason) return ANTHROPIC_STOP_REASON.end_turn;
95+
const transformedFinishReason = AnthropicFinishReasonMap.get(finishReason);
96+
if (!transformedFinishReason) {
97+
return ANTHROPIC_STOP_REASON.end_turn;
98+
}
99+
return transformedFinishReason;
100+
};

src/providers/utils/finishReasonMap.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { ANTHROPIC_STOP_REASON } from '../anthropic/types';
22
import { FINISH_REASON, PROVIDER_FINISH_REASON } from '../types';
33
import { BEDROCK_STOP_REASON } from '../bedrock/types';
44

5+
// TODO: rename this to OpenAIFinishReasonMap
56
export const finishReasonMap = new Map<PROVIDER_FINISH_REASON, FINISH_REASON>([
67
// https://docs.anthropic.com/en/api/messages#response-stop-reason
78
[ANTHROPIC_STOP_REASON.stop_sequence, FINISH_REASON.stop],
@@ -17,3 +18,16 @@ export const finishReasonMap = new Map<PROVIDER_FINISH_REASON, FINISH_REASON>([
1718
[BEDROCK_STOP_REASON.guardrail_intervened, FINISH_REASON.content_filter],
1819
[BEDROCK_STOP_REASON.content_filtered, FINISH_REASON.content_filter],
1920
]);
21+
22+
export const AnthropicFinishReasonMap = new Map<
23+
PROVIDER_FINISH_REASON,
24+
ANTHROPIC_STOP_REASON
25+
>([
26+
// https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_Converse.html#API_runtime_Converse_ResponseSyntax
27+
[BEDROCK_STOP_REASON.end_turn, ANTHROPIC_STOP_REASON.end_turn],
28+
[BEDROCK_STOP_REASON.tool_use, ANTHROPIC_STOP_REASON.tool_use],
29+
[BEDROCK_STOP_REASON.max_tokens, ANTHROPIC_STOP_REASON.max_tokens],
30+
[BEDROCK_STOP_REASON.stop_sequence, ANTHROPIC_STOP_REASON.stop_sequence],
31+
[BEDROCK_STOP_REASON.guardrail_intervened, ANTHROPIC_STOP_REASON.end_turn],
32+
[BEDROCK_STOP_REASON.content_filtered, ANTHROPIC_STOP_REASON.end_turn],
33+
]);

src/types/MessagesStreamResponse.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
MessagesResponse,
77
RedactedThinkingBlock,
88
ServerToolUseBlock,
9-
STOP_REASON,
9+
ANTHROPIC_STOP_REASON,
1010
TextBlock,
1111
ThinkingBlock,
1212
ToolUseBlock,
@@ -21,7 +21,7 @@ export interface RawMessageStartEvent {
2121
}
2222

2323
export interface RawMessageDelta {
24-
stop_reason: STOP_REASON | null;
24+
stop_reason: ANTHROPIC_STOP_REASON | null;
2525

2626
stop_sequence: string | null;
2727
}

src/types/messagesResponse.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ export type ContentBlock =
150150
| ThinkingBlock
151151
| RedactedThinkingBlock;
152152

153-
export enum STOP_REASON {
153+
export enum ANTHROPIC_STOP_REASON {
154154
end_turn = 'end_turn',
155155
max_tokens = 'max_tokens',
156156
stop_sequence = 'stop_sequence',
@@ -234,7 +234,7 @@ export interface MessagesResponse {
234234
* In non-streaming mode this value is always non-null. In streaming mode, it is
235235
* null in the `message_start` event and non-null otherwise.
236236
*/
237-
stop_reason: STOP_REASON | null;
237+
stop_reason: ANTHROPIC_STOP_REASON | null;
238238

239239
/**
240240
* Which custom stop sequence was generated, if any.

0 commit comments

Comments
 (0)