Skip to content

Commit 7d4316e

Browse files
author
zhaolun
committed
feat: add krutrim as provider
1 parent 2b229d4 commit 7d4316e

File tree

5 files changed

+83
-0
lines changed

5 files changed

+83
-0
lines changed

src/globals.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ export const KLUSTER_AI: string = 'kluster-ai';
9696
export const NSCALE: string = 'nscale';
9797
export const HYPERBOLIC: string = 'hyperbolic';
9898
export const FEATHERLESS_AI: string = 'featherless-ai';
99+
export const KRUTRIM: string = 'krutrim';
99100

100101
export const VALID_PROVIDERS = [
101102
ANTHROPIC,
@@ -157,6 +158,7 @@ export const VALID_PROVIDERS = [
157158
NSCALE,
158159
HYPERBOLIC,
159160
FEATHERLESS_AI,
161+
KRUTRIM,
160162
];
161163

162164
export const CONTENT_TYPES = {

src/providers/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ import KlusterAIConfig from './kluster-ai';
6060
import NscaleConfig from './nscale';
6161
import HyperbolicConfig from './hyperbolic';
6262
import { FeatherlessAIConfig } from './featherless-ai';
63+
import KrutrimConfig from './krutrim';
6364

6465
const Providers: { [key: string]: ProviderConfigs } = {
6566
openai: OpenAIConfig,
@@ -120,6 +121,7 @@ const Providers: { [key: string]: ProviderConfigs } = {
120121
nscale: NscaleConfig,
121122
hyperbolic: HyperbolicConfig,
122123
'featherless-ai': FeatherlessAIConfig,
124+
krutrim: KrutrimConfig,
123125
};
124126

125127
export default Providers;

src/providers/krutrim/api.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { ProviderAPIConfig } from '../types';
2+
3+
const KrutrimAPIConfig: ProviderAPIConfig = {
4+
getBaseURL: () => 'https://cloud.olakrutrim.com/v1',
5+
headers: ({ providerOptions, fn }) => {
6+
const headersObj: Record<string, string> = {
7+
Authorization: `Bearer ${providerOptions.apiKey}`,
8+
};
9+
return headersObj;
10+
},
11+
getEndpoint: ({ fn }) => {
12+
switch (fn) {
13+
case 'chatComplete':
14+
return '/chat/completions';
15+
default:
16+
return '';
17+
}
18+
},
19+
};
20+
21+
export default KrutrimAPIConfig;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { ChatCompletionResponse, ErrorResponse } from '../types';
2+
3+
import { generateErrorResponse } from '../utils';
4+
5+
import { KRUTRIM } from '../../globals';
6+
7+
interface KrutrimChatCompleteResponse extends ChatCompletionResponse {}
8+
interface KrutrimChatCompleteErrorResponse extends ErrorResponse {
9+
'html-message'?: string;
10+
}
11+
export const KrutrimChatCompleteResponseTransform: (
12+
response: KrutrimChatCompleteResponse | KrutrimChatCompleteErrorResponse,
13+
responseStatus: number
14+
) => ChatCompletionResponse | ErrorResponse = (response, responseStatus) => {
15+
if (responseStatus !== 200 && 'html-message' in response) {
16+
// Handle Krutrim's error format
17+
return generateErrorResponse(
18+
{
19+
message: response['html-message'] ?? '',
20+
type: 'error',
21+
param: null,
22+
code: String(responseStatus),
23+
},
24+
KRUTRIM
25+
);
26+
}
27+
28+
// Success case - add provider info
29+
Object.defineProperty(response, 'provider', {
30+
value: KRUTRIM,
31+
enumerable: true,
32+
});
33+
34+
return response as ChatCompletionResponse;
35+
};

src/providers/krutrim/index.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { ProviderConfigs } from '../types';
2+
import KrutrimAPIConfig from './api';
3+
import { chatCompleteParams } from '../open-ai-base';
4+
import { KrutrimChatCompleteResponseTransform } from './chatComplete';
5+
const KrutrimConfig: ProviderConfigs = {
6+
api: KrutrimAPIConfig,
7+
chatComplete: chatCompleteParams([
8+
'max_tokens',
9+
'temperature',
10+
'top_p',
11+
'frequency_penalty',
12+
'logit_bias',
13+
'logprobs',
14+
'presence_penalty',
15+
'seed',
16+
'top_k',
17+
]),
18+
responseTransforms: {
19+
chatComplete: KrutrimChatCompleteResponseTransform,
20+
},
21+
};
22+
23+
export default KrutrimConfig;

0 commit comments

Comments
 (0)