Skip to content

Commit d7fd38e

Browse files
authored
Merge branch 'main' into feat/krutrim-integration
2 parents b64d033 + 9775e10 commit d7fd38e

File tree

12 files changed

+216
-54
lines changed

12 files changed

+216
-54
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@portkey-ai/gateway",
3-
"version": "1.10.0",
3+
"version": "1.10.1",
44
"description": "A fast AI gateway by Portkey",
55
"repository": {
66
"type": "git",

src/providers/anthropic/chatComplete.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -362,11 +362,15 @@ export const AnthropicChatCompleteConfig: ProviderConfig = {
362362
cache_control: { type: 'ephemeral' },
363363
}),
364364
});
365-
} else if (tool.computer) {
365+
} else if (tool.type) {
366+
const toolOptions = tool[tool.type];
366367
tools.push({
367-
...tool.computer,
368-
name: 'computer',
369-
type: tool.computer.name,
368+
...(toolOptions && { ...toolOptions }),
369+
name: tool.type,
370+
type: toolOptions?.name,
371+
...(tool.cache_control && {
372+
cache_control: { type: 'ephemeral' },
373+
}),
370374
});
371375
}
372376
});

src/providers/azure-openai/chatComplete.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ export const AzureOpenAIChatCompleteConfig: ProviderConfig = {
111111
stream_options: {
112112
param: 'stream_options',
113113
},
114+
web_search_options: {
115+
param: 'web_search_options',
116+
},
114117
};
115118

116119
interface AzureOpenAIChatCompleteResponse extends ChatCompletionResponse {}

src/providers/bedrock/chatComplete.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -315,13 +315,15 @@ export const BedrockConverseChatCompleteConfig: ProviderConfig = {
315315
| { cachePoint: { type: string } }
316316
> = [];
317317
params.tools?.forEach((tool) => {
318-
tools.push({
319-
toolSpec: {
320-
name: tool.function.name,
321-
description: tool.function.description,
322-
inputSchema: { json: tool.function.parameters },
323-
},
324-
});
318+
if (tool.function) {
319+
tools.push({
320+
toolSpec: {
321+
name: tool.function.name,
322+
description: tool.function.description,
323+
inputSchema: { json: tool.function.parameters },
324+
},
325+
});
326+
}
325327
if (tool.cache_control && !canBeAmazonModel) {
326328
tools.push({
327329
cachePoint: {
@@ -353,7 +355,8 @@ export const BedrockConverseChatCompleteConfig: ProviderConfig = {
353355
}
354356
}
355357
}
356-
return { ...toolConfig, toolChoice };
358+
// TODO: split this into two provider options, one for tools and one for toolChoice
359+
return tools.length ? { ...toolConfig, toolChoice } : null;
357360
},
358361
},
359362
guardrailConfig: {

src/providers/bedrock/embed.ts

Lines changed: 108 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,129 @@
11
import { BEDROCK } from '../../globals';
2-
import { EmbedResponse } from '../../types/embedRequestBody';
2+
import { EmbedParams, EmbedResponse } from '../../types/embedRequestBody';
33
import { Params } from '../../types/requestBody';
44
import { ErrorResponse, ProviderConfig } from '../types';
55
import { generateInvalidProviderResponseError } from '../utils';
66
import { BedrockErrorResponseTransform } from './chatComplete';
77

88
export const BedrockCohereEmbedConfig: ProviderConfig = {
9-
input: {
10-
param: 'texts',
11-
required: true,
12-
transform: (params: any): string[] => {
13-
if (Array.isArray(params.input)) {
14-
return params.input;
15-
} else {
16-
return [params.input];
17-
}
9+
input: [
10+
{
11+
param: 'texts',
12+
required: false,
13+
transform: (params: EmbedParams): string[] | undefined => {
14+
if (typeof params.input === 'string') return [params.input];
15+
else if (Array.isArray(params.input) && params.input.length > 0) {
16+
const texts: string[] = [];
17+
params.input.forEach((item) => {
18+
if (typeof item === 'string') {
19+
texts.push(item);
20+
} else if (item.text) {
21+
texts.push(item.text);
22+
}
23+
});
24+
return texts.length > 0 ? texts : undefined;
25+
}
26+
},
1827
},
19-
},
28+
{
29+
param: 'images',
30+
required: false,
31+
transform: (params: EmbedParams): string[] | undefined => {
32+
if (Array.isArray(params.input) && params.input.length > 0) {
33+
const images: string[] = [];
34+
params.input.forEach((item) => {
35+
if (typeof item === 'object' && item.image?.base64) {
36+
images.push(item.image.base64);
37+
}
38+
});
39+
return images.length > 0 ? images : undefined;
40+
}
41+
},
42+
},
43+
],
2044
input_type: {
2145
param: 'input_type',
2246
required: true,
2347
},
2448
truncate: {
2549
param: 'truncate',
50+
required: false,
51+
},
52+
encoding_format: {
53+
param: 'embedding_types',
54+
required: false,
55+
transform: (params: any): string[] => {
56+
if (Array.isArray(params.encoding_format)) return params.encoding_format;
57+
return [params.encoding_format];
58+
},
2659
},
2760
};
2861

2962
export const BedrockTitanEmbedConfig: ProviderConfig = {
30-
input: {
31-
param: 'inputText',
32-
required: true,
63+
input: [
64+
{
65+
param: 'inputText',
66+
required: false,
67+
transform: (params: EmbedParams): string | undefined => {
68+
if (
69+
Array.isArray(params.input) &&
70+
typeof params.input[0] === 'object' &&
71+
params.input[0].text
72+
) {
73+
return params.input[0].text;
74+
}
75+
if (typeof params.input === 'string') return params.input;
76+
},
77+
},
78+
{
79+
param: 'inputImage',
80+
required: false,
81+
transform: (params: EmbedParams) => {
82+
// Titan models only support one image per request
83+
if (
84+
Array.isArray(params.input) &&
85+
typeof params.input[0] === 'object' &&
86+
params.input[0].image?.base64
87+
) {
88+
return params.input[0].image.base64;
89+
}
90+
},
91+
},
92+
],
93+
dimensions: [
94+
{
95+
param: 'dimensions',
96+
required: false,
97+
transform: (params: EmbedParams): number | undefined => {
98+
if (typeof params.input === 'string') return params.dimensions;
99+
},
100+
},
101+
{
102+
param: 'embeddingConfig',
103+
required: false,
104+
transform: (
105+
params: EmbedParams
106+
): { outputEmbeddingLength: number } | undefined => {
107+
if (Array.isArray(params.input) && params.dimensions) {
108+
return {
109+
outputEmbeddingLength: params.dimensions,
110+
};
111+
}
112+
},
113+
},
114+
],
115+
encoding_format: {
116+
param: 'embeddingTypes',
117+
required: false,
118+
transform: (params: any): string[] => {
119+
if (Array.isArray(params.encoding_format)) return params.encoding_format;
120+
return [params.encoding_format];
121+
},
122+
},
123+
// Titan specific parameters
124+
normalize: {
125+
param: 'normalize',
126+
required: false,
33127
},
34128
};
35129

src/providers/bedrock/utils.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
BedrockConverseAnthropicChatCompletionsParams,
99
BedrockConverseCohereChatCompletionsParams,
1010
} from './chatComplete';
11-
import { Options } from '../../types/requestBody';
11+
import { Options, Tool } from '../../types/requestBody';
1212
import { GatewayError } from '../../errors/GatewayError';
1313
import { BedrockFinetuneRecord, BedrockInferenceProfile } from './types';
1414
import { FinetuneRequest } from '../types';
@@ -137,6 +137,25 @@ export const transformAnthropicAdditionalModelRequestFields = (
137137
additionalModelRequestFields['anthropic_beta'] = params['anthropic_beta'];
138138
}
139139
}
140+
if (params.tools && params.tools.length) {
141+
const anthropicTools: any[] = [];
142+
params.tools.forEach((tool: Tool) => {
143+
if (tool.type !== 'function') {
144+
const toolOptions = tool[tool.type];
145+
anthropicTools.push({
146+
...(toolOptions && { ...toolOptions }),
147+
name: tool.type,
148+
type: toolOptions?.name,
149+
...(tool.cache_control && {
150+
cache_control: { type: 'ephemeral' },
151+
}),
152+
});
153+
}
154+
});
155+
if (anthropicTools.length) {
156+
additionalModelRequestFields['tools'] = anthropicTools;
157+
}
158+
}
140159
return additionalModelRequestFields;
141160
};
142161

src/providers/cohere/embed.ts

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,60 @@ import { generateErrorResponse } from '../utils';
44
import { COHERE } from '../../globals';
55

66
export const CohereEmbedConfig: ProviderConfig = {
7-
input: {
8-
param: 'texts',
9-
required: true,
10-
transform: (params: EmbedParams): string[] => {
11-
if (Array.isArray(params.input)) {
12-
return params.input as string[];
13-
} else {
14-
return [params.input];
15-
}
7+
input: [
8+
{
9+
param: 'texts',
10+
required: false,
11+
transform: (params: EmbedParams): string[] | undefined => {
12+
if (typeof params.input === 'string') return [params.input];
13+
else if (Array.isArray(params.input) && params.input.length > 0) {
14+
const texts: string[] = [];
15+
params.input.forEach((item) => {
16+
if (typeof item === 'string') {
17+
texts.push(item);
18+
} else if (item.text) {
19+
texts.push(item.text);
20+
}
21+
});
22+
return texts.length > 0 ? texts : undefined;
23+
}
24+
},
1625
},
17-
},
18-
model: {
19-
param: 'model',
20-
default: 'embed-english-light-v2.0',
21-
},
26+
{
27+
param: 'images',
28+
required: false,
29+
transform: (params: EmbedParams): string[] | undefined => {
30+
if (Array.isArray(params.input) && params.input.length > 0) {
31+
const images: string[] = [];
32+
params.input.forEach((item) => {
33+
if (typeof item === 'object' && item.image?.base64) {
34+
images.push(item.image.base64);
35+
}
36+
});
37+
return images.length > 0 ? images : undefined;
38+
}
39+
},
40+
},
41+
],
2242
input_type: {
2343
param: 'input_type',
44+
required: true,
45+
},
46+
truncate: {
47+
param: 'truncate',
2448
required: false,
2549
},
26-
embedding_types: {
50+
encoding_format: {
2751
param: 'embedding_types',
2852
required: false,
53+
transform: (params: any): string[] => {
54+
if (Array.isArray(params.encoding_format)) return params.encoding_format;
55+
return [params.encoding_format];
56+
},
2957
},
30-
truncate: {
31-
param: 'truncate',
58+
//backwards compatibility
59+
embedding_types: {
60+
param: 'embedding_types',
3261
required: false,
3362
},
3463
};

src/providers/google-vertex-ai/chatComplete.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,10 @@ export const VertexAnthropicChatCompleteStreamChunkTransform: (
832832

833833
if (parsedChunk.type === 'message_start' && parsedChunk.message?.usage) {
834834
streamState.model = parsedChunk?.message?.model ?? '';
835+
836+
streamState.usage = {
837+
prompt_tokens: parsedChunk.message.usage?.input_tokens,
838+
};
835839
return (
836840
`data: ${JSON.stringify({
837841
id: fallbackId,
@@ -850,7 +854,7 @@ export const VertexAnthropicChatCompleteStreamChunkTransform: (
850854
},
851855
],
852856
usage: {
853-
prompt_tokens: parsedChunk.message?.usage?.input_tokens,
857+
prompt_tokens: streamState.usage.prompt_tokens,
854858
},
855859
})}` + '\n\n'
856860
);
@@ -873,6 +877,10 @@ export const VertexAnthropicChatCompleteStreamChunkTransform: (
873877
],
874878
usage: {
875879
completion_tokens: parsedChunk.usage?.output_tokens,
880+
prompt_tokens: streamState.usage?.prompt_tokens,
881+
total_tokens:
882+
(streamState.usage?.prompt_tokens || 0) +
883+
(parsedChunk.usage?.output_tokens || 0),
876884
},
877885
})}` + '\n\n'
878886
);

src/providers/open-ai-base/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ export const chatCompleteParams = (
132132
stream_options: {
133133
param: 'stream_options',
134134
},
135+
web_search_options: {
136+
param: 'web_search_options',
137+
},
135138
};
136139

137140
// Exclude params that are not needed.

0 commit comments

Comments
 (0)