Skip to content

Commit 10b2700

Browse files
committed
cleanaup bedrock provider
1 parent 6150f06 commit 10b2700

File tree

8 files changed

+36
-24
lines changed

8 files changed

+36
-24
lines changed

src/providers/bedrock/chatComplete.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import {
22
BEDROCK,
33
documentMimeTypes,
4-
fileExtensionMimeTypeMap,
54
imagesMimeTypes,
5+
fileExtensionMimeTypeMap,
66
} from '../../globals';
77
import {
88
Message,
@@ -62,7 +62,7 @@ export interface BedrockChatCompletionsParams extends Params {
6262
}
6363

6464
export interface BedrockConverseAnthropicChatCompletionsParams
65-
extends Omit<BedrockChatCompletionsParams, 'anthropic_beta'> {
65+
extends BedrockChatCompletionsParams {
6666
anthropic_version?: string;
6767
user?: string;
6868
thinking?: {
@@ -477,9 +477,6 @@ export const BedrockChatCompleteResponseTransform: (
477477
}
478478

479479
if ('output' in response) {
480-
const cacheReadInputTokens = response.usage?.cacheReadInputTokens || 0;
481-
const cacheWriteInputTokens = response.usage?.cacheWriteInputTokens || 0;
482-
483480
let content: string = '';
484481
content = response.output.message.content
485482
.filter((item) => item.text)
@@ -489,6 +486,9 @@ export const BedrockChatCompleteResponseTransform: (
489486
? transformContentBlocks(response.output.message.content)
490487
: undefined;
491488

489+
const cacheReadInputTokens = response.usage?.cacheReadInputTokens || 0;
490+
const cacheWriteInputTokens = response.usage?.cacheWriteInputTokens || 0;
491+
492492
const responseObj: ChatCompletionResponse = {
493493
id: Date.now().toString(),
494494
object: 'chat.completion',
@@ -571,7 +571,6 @@ export const BedrockChatCompleteStreamChunkTransform: (
571571
streamState.currentToolCallIndex = -1;
572572
}
573573

574-
// final chunk
575574
if (parsedChunk.usage) {
576575
const cacheReadInputTokens = parsedChunk.usage?.cacheReadInputTokens || 0;
577576
const cacheWriteInputTokens = parsedChunk.usage?.cacheWriteInputTokens || 0;
@@ -605,9 +604,8 @@ export const BedrockChatCompleteStreamChunkTransform: (
605604
},
606605
// we only want to be sending this for anthropic models and this is not openai compliant
607606
...((cacheReadInputTokens > 0 || cacheWriteInputTokens > 0) && {
608-
cache_read_input_tokens: parsedChunk.usage.cacheReadInputTokens,
609-
cache_creation_input_tokens:
610-
parsedChunk.usage.cacheWriteInputTokens,
607+
cache_read_input_tokens: cacheReadInputTokens,
608+
cache_creation_input_tokens: cacheWriteInputTokens,
611609
}),
612610
},
613611
})}\n\n`,

src/providers/bedrock/constants.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
export const BEDROCK_STABILITY_V1_MODELS = [
2+
'stable-diffusion-xl-v0',
3+
'stable-diffusion-xl-v1',
4+
];
5+
6+
export const bedrockInvokeModels = [
7+
'cohere.command-light-text-v14',
8+
'cohere.command-text-v14',
9+
'ai21.j2-mid-v1',
10+
'ai21.j2-ultra-v1',
11+
];
12+
113
export const LLAMA_2_SPECIAL_TOKENS = {
214
BEGINNING_OF_SENTENCE: '<s>',
315
END_OF_SENTENCE: '</s>',
@@ -34,15 +46,3 @@ export const MISTRAL_CONTROL_TOKENS = {
3446
MIDDLE: '[MIDDLE]',
3547
SUFFIX: '[SUFFIX]',
3648
};
37-
38-
export const BEDROCK_STABILITY_V1_MODELS = [
39-
'stable-diffusion-xl-v0',
40-
'stable-diffusion-xl-v1',
41-
];
42-
43-
export const bedrockInvokeModels = [
44-
'cohere.command-light-text-v14',
45-
'cohere.command-text-v14',
46-
'ai21.j2-mid-v1',
47-
'ai21.j2-ultra-v1',
48-
];

src/providers/bedrock/createFinetune.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export const BedrockCreateFinetuneConfig: ProviderConfig = {
4646
return undefined;
4747
}
4848
return {
49-
s3Uri: decodeURIComponent(value.validation_file),
49+
s3Uri: decodeURIComponent(value.validation_file ?? ''),
5050
};
5151
},
5252
},

src/providers/bedrock/embed.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ export const BedrockCohereEmbedConfig: ProviderConfig = {
6060
},
6161
};
6262

63+
const g1EmbedModels = [
64+
'amazon.titan-embed-g1-text-02',
65+
'amazon.titan-embed-text-v1',
66+
'amazon.titan-embed-image-v1',
67+
];
68+
6369
export const BedrockTitanEmbedConfig: ProviderConfig = {
6470
input: [
6571
{
@@ -117,6 +123,8 @@ export const BedrockTitanEmbedConfig: ProviderConfig = {
117123
param: 'embeddingTypes',
118124
required: false,
119125
transform: (params: any): string[] | undefined => {
126+
const model = params.foundationModel || params.model || '';
127+
if (g1EmbedModels.includes(model)) return undefined;
120128
if (Array.isArray(params.encoding_format)) return params.encoding_format;
121129
else if (typeof params.encoding_format === 'string')
122130
return [params.encoding_format];

src/providers/bedrock/getBatchOutput.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { BedrockGetBatchResponse } from './types';
55
import { getOctetStreamToOctetStreamTransformer } from '../../handlers/streamHandlerUtils';
66
import { BedrockUploadFileResponseTransforms } from './uploadFileUtils';
77
import { BEDROCK } from '../../globals';
8+
import { getAwsEndpointDomain } from './utils';
89

910
const getModelProvider = (modelId: string) => {
1011
let provider = '';
@@ -89,7 +90,7 @@ export const BedrockGetBatchOutputRequestHandler = async ({
8990
const awsS3ObjectKey = `${primaryKey}${jobId}/${inputS3URIParts[inputS3URIParts.length - 1]}.out`;
9091
const awsModelProvider = batchDetails.modelId;
9192

92-
const s3FileURL = `https://${awsS3Bucket}.s3.${awsRegion}.amazonaws.com/${awsS3ObjectKey}`;
93+
const s3FileURL = `https://${awsS3Bucket}.s3.${awsRegion}.${getAwsEndpointDomain(c)}/${awsS3ObjectKey}`;
9394
const s3FileHeaders = await BedrockAPIConfig.headers({
9495
c,
9596
providerOptions,

src/providers/bedrock/utils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ import { BedrockFinetuneRecord, BedrockInferenceProfile } from './types';
1414
import { FinetuneRequest } from '../types';
1515
import { BEDROCK } from '../../globals';
1616

17+
export const getAwsEndpointDomain = (c: Context) =>
18+
env(c).AWS_ENDPOINT_DOMAIN || 'amazonaws.com';
19+
1720
export const generateAWSHeaders = async (
1821
body: Record<string, any> | string | undefined,
1922
headers: Record<string, string>,

src/types/requestBody.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,6 @@ export interface Params {
422422
// Google Vertex AI specific
423423
safety_settings?: any;
424424
// Anthropic specific
425-
anthropic_beta?: string;
426425
anthropic_version?: string;
427426
thinking?: {
428427
type?: string;

src/utils/env.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export interface Env {
2+
AWS_ENDPOINT_DOMAIN: string;
3+
}

0 commit comments

Comments
 (0)