Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export const KLUSTER_AI: string = 'kluster-ai';
export const NSCALE: string = 'nscale';
export const HYPERBOLIC: string = 'hyperbolic';
export const FEATHERLESS_AI: string = 'featherless-ai';
export const KRUTRIM: string = 'krutrim';

export const VALID_PROVIDERS = [
ANTHROPIC,
Expand Down Expand Up @@ -157,6 +158,7 @@ export const VALID_PROVIDERS = [
NSCALE,
HYPERBOLIC,
FEATHERLESS_AI,
KRUTRIM,
];

export const CONTENT_TYPES = {
Expand Down
2 changes: 2 additions & 0 deletions src/providers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import KlusterAIConfig from './kluster-ai';
import NscaleConfig from './nscale';
import HyperbolicConfig from './hyperbolic';
import { FeatherlessAIConfig } from './featherless-ai';
import KrutrimConfig from './krutrim';

const Providers: { [key: string]: ProviderConfigs } = {
openai: OpenAIConfig,
Expand Down Expand Up @@ -120,6 +121,7 @@ const Providers: { [key: string]: ProviderConfigs } = {
nscale: NscaleConfig,
hyperbolic: HyperbolicConfig,
'featherless-ai': FeatherlessAIConfig,
krutrim: KrutrimConfig,
};

export default Providers;
21 changes: 21 additions & 0 deletions src/providers/krutrim/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { ProviderAPIConfig } from '../types';

const KrutrimAPIConfig: ProviderAPIConfig = {
getBaseURL: () => 'https://cloud.olakrutrim.com/v1',
headers: ({ providerOptions, fn }) => {
const headersObj: Record<string, string> = {
Authorization: `Bearer ${providerOptions.apiKey}`,
};
return headersObj;
},
Comment on lines +5 to +10
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚡️ Performance Improvement

Issue: The headers function creates a new object on every call, which is unnecessary since the object structure is static.
Fix: Optimize the headers function to directly return the object without the intermediate variable.
Impact: Slightly improves performance by reducing unnecessary object creation and variable assignment.

Suggested change
headers: ({ providerOptions, fn }) => {
const headersObj: Record<string, string> = {
Authorization: `Bearer ${providerOptions.apiKey}`,
};
return headersObj;
},
headers: ({ providerOptions, fn }) => {
return {
Authorization: `Bearer ${providerOptions.apiKey}`,
};
},

getEndpoint: ({ fn }) => {
switch (fn) {
case 'chatComplete':
return '/chat/completions';
default:
return '';
}
},
};

export default KrutrimAPIConfig;
33 changes: 33 additions & 0 deletions src/providers/krutrim/chatComplete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { ChatCompletionResponse, ErrorResponse } from '../types';
import { generateErrorResponse } from '../utils';
import { KRUTRIM } from '../../globals';

interface KrutrimChatCompleteResponse extends ChatCompletionResponse {}
interface KrutrimChatCompleteErrorResponse extends ErrorResponse {
'html-message'?: string;
}
export const KrutrimChatCompleteResponseTransform: (
response: KrutrimChatCompleteResponse | KrutrimChatCompleteErrorResponse,
responseStatus: number
) => ChatCompletionResponse | ErrorResponse = (response, responseStatus) => {
if (responseStatus !== 200 && 'html-message' in response) {
// Handle Krutrim's error format
return generateErrorResponse(
{
message: response['html-message'] ?? '',
type: 'error',
param: null,
code: String(responseStatus),
},
KRUTRIM
);
}
Comment on lines +13 to +24
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Code Refactor

Issue: The error handling only checks for 'html-message' property but doesn't handle standard error responses that might not have this property.
Fix: Add a more comprehensive error handling approach that can handle both Krutrim-specific and standard error formats.
Impact: Improves error handling robustness and prevents potential unhandled error scenarios.

Suggested change
if (responseStatus !== 200 && 'html-message' in response) {
// Handle Krutrim's error format
return generateErrorResponse(
{
message: response['html-message'] ?? '',
type: 'error',
param: null,
code: String(responseStatus),
},
KRUTRIM
);
}
if (responseStatus !== 200) {
// Handle Krutrim's error format
return generateErrorResponse(
{
message: 'html-message' in response ? response['html-message'] ?? '' : 'Unknown error from Krutrim API',
type: 'error',
param: null,
code: String(responseStatus),
},
KRUTRIM
);
}


// Success case - add provider info
Object.defineProperty(response, 'provider', {
value: KRUTRIM,
enumerable: true,
});

return response as ChatCompletionResponse;
};
13 changes: 13 additions & 0 deletions src/providers/krutrim/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { ProviderConfigs } from '../types';
import KrutrimAPIConfig from './api';
import { chatCompleteParams } from '../open-ai-base';
import { KrutrimChatCompleteResponseTransform } from './chatComplete';
const KrutrimConfig: ProviderConfigs = {
api: KrutrimAPIConfig,
chatComplete: chatCompleteParams([], { model: 'Llama-3.3-70B-Instruct' }),
responseTransforms: {
chatComplete: KrutrimChatCompleteResponseTransform,
},
};

export default KrutrimConfig;