Skip to content

Commit b6eedd3

Browse files
authored
Merge pull request #1436 from narengogi/fix/bedrock-count-tokens-ignore-region
remove region identifier prefix when making request to count tokens endpoint
2 parents d4b374b + 9435fe7 commit b6eedd3

File tree

4 files changed

+13
-9
lines changed

4 files changed

+13
-9
lines changed

src/providers/anthropic/api.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,7 @@ import { ProviderAPIConfig } from '../types';
33
const AnthropicAPIConfig: ProviderAPIConfig = {
44
getBaseURL: () => 'https://api.anthropic.com/v1',
55

6-
headers: ({
7-
providerOptions,
8-
fn,
9-
headers: requestHeaders,
10-
gatewayRequestBody,
11-
}) => {
6+
headers: ({ providerOptions, fn, gatewayRequestBody }) => {
127
const apiKey =
138
providerOptions.apiKey || providerOptions.anthropicApiKey || '';
149
const headers: Record<string, string> = {

src/providers/bedrock/api.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
generateAWSHeaders,
88
getFoundationModelFromInferenceProfile,
99
providerAssumedRoleCredentials,
10+
getBedrockModelWithoutRegion,
1011
} from './utils';
1112
import { GatewayError } from '../../errors/GatewayError';
1213

@@ -234,7 +235,10 @@ const BedrockAPIConfig: BedrockAPIConfigInterface = {
234235
return `/model-invocation-job/${batchId}/stop`;
235236
}
236237
const { model, stream } = gatewayRequestBody;
237-
const uriEncodedModel = encodeURIComponent(decodeURIComponent(model ?? ''));
238+
const decodedModel = decodeURIComponent(model ?? '');
239+
const uriEncodedModel = encodeURIComponent(decodedModel);
240+
const modelWithoutRegion = getBedrockModelWithoutRegion(decodedModel);
241+
const uriEncodedModelWithoutRegion = encodeURIComponent(modelWithoutRegion);
238242
if (!model && !BEDROCK_NO_MODEL_ENDPOINTS.includes(fn as endpointStrings)) {
239243
throw new GatewayError('Model is required');
240244
}
@@ -305,7 +309,7 @@ const BedrockAPIConfig: BedrockAPIConfigInterface = {
305309
return `/model-customization-jobs/${jobId}/stop`;
306310
}
307311
case 'messagesCountTokens': {
308-
return `/model/${uriEncodedModel}/count-tokens`;
312+
return `/model/${uriEncodedModelWithoutRegion}/count-tokens`;
309313
}
310314
default:
311315
return '';

src/providers/bedrock/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ import {
8585
BedrockConverseMessageCountTokensConfig,
8686
BedrockConverseMessageCountTokensResponseTransform,
8787
} from './countTokens';
88+
import { getBedrockModelWithoutRegion } from './utils';
8889

8990
const BedrockConfig: ProviderConfigs = {
9091
api: BedrockAPIConfig,
@@ -101,7 +102,7 @@ const BedrockConfig: ProviderConfigs = {
101102

102103
if (params.model) {
103104
let providerModel = providerOptions.foundationModel || params.model;
104-
providerModel = providerModel.replace(/^(us\.|eu\.|apac\.)/, '');
105+
providerModel = getBedrockModelWithoutRegion(providerModel);
105106
const providerModelArray = providerModel?.split('.');
106107
const provider = providerModelArray?.[0];
107108
const model = providerModelArray?.slice(1).join('.');

src/providers/bedrock/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,3 +537,7 @@ export const getBedrockErrorChunk = (id: string, model: string) => {
537537
`data: [DONE]\n\n`,
538538
];
539539
};
540+
541+
export const getBedrockModelWithoutRegion = (model: string) => {
542+
return model.replace(/^(us\.|eu\.|apac\.|au\.|ca\.|jp\.|global\.)/, '');
543+
};

0 commit comments

Comments
 (0)