Skip to content

Conversation

zhao-lun
Copy link
Contributor

@zhao-lun zhao-lun commented Jul 2, 2025

Description

  • Krutrim provider integration

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Refactoring (no functional changes)

Copy link
Contributor

matter-code-review bot commented Jul 2, 2025

Code Quality new feature

Description

Summary By MatterAI MatterAI logo

🔄 What Changed

This pull request introduces Krutrim as a new AI provider. Key changes include:

  • src/globals.ts: Added KRUTRIM constant and included it in VALID_PROVIDERS.
  • src/providers/index.ts: Imported KrutrimConfig and integrated it into the main Providers map.
  • src/providers/krutrim/api.ts (New File): Defined KrutrimAPIConfig to specify the base URL (https://cloud.olakrutrim.com/v1), authorization headers (Bearer token), and endpoint for chat completions (/chat/completions).
  • src/providers/krutrim/chatComplete.ts (New File): Implemented KrutrimChatCompleteResponseTransform to handle responses from Krutrim's API, including a specific error format (html-message) and adding provider information to successful responses.
  • src/providers/krutrim/index.ts (New File): Created KrutrimConfig which consolidates the API configuration, chat completion parameters (with a default model 'Llama-3.3-70B-Instruct'), and response transformation logic for Krutrim.

🔍 Impact of the Change

This change significantly expands the platform's capabilities by adding support for a new AI model provider, Krutrim. It allows applications to integrate with Krutrim's chat completion services, providing more options for users and potentially improving model diversity. The new files establish a clear, modular structure for integrating future providers.

📁 Total Files Changed

5 files changed.

🧪 Test Added

No specific tests were added or mentioned in the pull request data.

🔒Security Vulnerabilities

No direct security vulnerabilities were detected in the provided patch. The API key is handled via Bearer token, which is standard. Ensure providerOptions.apiKey is securely managed upstream.

Motivation

Krutrim provider integration

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Refactoring (no functional changes)

How Has This Been Tested?

  • Unit Tests: N/A
  • Integration Tests: N/A
  • Manual Testing: N/A

Screenshots (if applicable)

N/A

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Related Issues

N/A

Tip

Quality Recommendations

  1. Consider adding more specific error handling for various HTTP status codes beyond just the html-message check, as Krutrim's API might return other error formats or standard HTTP errors.

  2. The chatCompleteParams in src/providers/krutrim/index.ts hardcodes the model to 'Llama-3.3-70B-Instruct'. It would be more flexible to allow this model to be configurable via provider options or dynamically determined.

  3. Unit tests for the new Krutrim provider, especially for KrutrimAPIConfig and KrutrimChatCompleteResponseTransform, would improve code robustness and maintainability. This includes testing success cases, different error scenarios, and edge cases for response parsing.

  4. Add comments to explain the specific error handling logic for Krutrim's html-message error format in KrutrimChatCompleteResponseTransform.

Sequence Diagram

sequenceDiagram
  participant User
  participant Application
  participant ProvidersMap as src/providers/index.ts
  participant KrutrimConfigModule as src/providers/krutrim/index.ts
  participant KrutrimAPIConfigModule as src/providers/krutrim/api.ts
  participant KrutrimAPI as Krutrim Cloud API
  participant KrutrimResponseTransform as src/providers/krutrim/chatComplete.ts

  User->>Application: Initiates chat completion request
  Application->>ProvidersMap: Get ProviderConfig for 'krutrim'
  ProvidersMap->>KrutrimConfigModule: Import KrutrimConfig
  KrutrimConfigModule-->>ProvidersMap: Provides KrutrimConfig (api, chatComplete, responseTransforms)
  ProvidersMap-->>Application: Returns KrutrimConfig

  Application->>KrutrimAPIConfigModule: Call getBaseURL() & headers({providerOptions: {apiKey}, fn: 'chatComplete'})
  KrutrimAPIConfigModule-->>Application: Returns Base URL (https://cloud.olakrutrim.com/v1) and Headers (Authorization: Bearer <apiKey>)

  Application->>KrutrimAPIConfigModule: Call getEndpoint({fn: 'chatComplete'})
  KrutrimAPIConfigModule-->>Application: Returns Endpoint (/chat/completions)

  Application->>KrutrimAPI: POST /chat/completions (with request body and headers)
  KrutrimAPI-->>Application: Returns API Response (status, body)

  Application->>KrutrimResponseTransform: Call KrutrimChatCompleteResponseTransform(response, responseStatus)
  KrutrimResponseTransform->>KrutrimResponseTransform: Checks responseStatus (e.g., 200) and 'html-message' for errors
  alt If error (status != 200 && 'html-message' present)
    KrutrimResponseTransform->>KrutrimResponseTransform: Calls generateErrorResponse(errorDetails, KRUTRIM)
  end
  KrutrimResponseTransform-->>Application: Returns Transformed ChatCompletionResponse or ErrorResponse

  Application-->>User: Returns Chat Completion Result
Loading

Copy link
Contributor

@matter-code-review matter-code-review bot left a comment

Choose a reason for hiding this comment

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

This PR adds Krutrim as a new provider to the gateway, following the established patterns for provider integration. The implementation is clean and well-structured. I have a few suggestions to improve error handling and type safety.

Comment on lines +15 to +26
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
);
}
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
);
}

Comment on lines +5 to +10
headers: ({ providerOptions, fn }) => {
const headersObj: Record<string, string> = {
Authorization: `Bearer ${providerOptions.apiKey}`,
};
return headersObj;
},
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}`,
};
},

Comment on lines 1 to 10
import { ChatCompletionResponse, ErrorResponse } from '../types';

import { generateErrorResponse } from '../utils';

import { KRUTRIM } from '../../globals';

interface KrutrimChatCompleteResponse extends ChatCompletionResponse {}
interface KrutrimChatCompleteErrorResponse extends ErrorResponse {
'html-message'?: string;
}
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 interface KrutrimChatCompleteResponse is defined but doesn't add any properties beyond the base ChatCompletionResponse.
Fix: Either add Krutrim-specific properties to the interface or use the base type directly to simplify the code.
Impact: Improves code clarity and maintainability by avoiding unnecessary type definitions.

Suggested change
import { ChatCompletionResponse, ErrorResponse } from '../types';
import { generateErrorResponse } from '../utils';
import { KRUTRIM } from '../../globals';
interface KrutrimChatCompleteResponse extends ChatCompletionResponse {}
interface KrutrimChatCompleteErrorResponse extends ErrorResponse {
'html-message'?: string;
}
import { ChatCompletionResponse, ErrorResponse } from '../types';
import { generateErrorResponse } from '../utils';
import { KRUTRIM } from '../../globals';
interface KrutrimChatCompleteErrorResponse extends ErrorResponse {
'html-message'?: string;
}

@siddharthsambharia-portkey
Copy link
Contributor

@narengogi can you take a look at this PR?

Copy link
Collaborator

@narengogi narengogi left a comment

Choose a reason for hiding this comment

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

formatting is a little off in /providers/krutrim/chatComplete + 1 minor comment, rest looks good!

api: KrutrimAPIConfig,
chatComplete: chatCompleteParams([
'max_tokens',
'temperature',
Copy link
Collaborator

Choose a reason for hiding this comment

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

I can see that these parameters are supported in the API signature
Uploading image.png…

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @narengogi,
I've pushed a commit to remove the excludes. I had mistakenly marked the params as required.
additionally, added a default model.

Copy link
Contributor

Important

PR Review Skipped

PR review skipped as per the configuration setting. Run a manually review by commenting /matter review

💡Tips to use Matter AI

Command List

  • /matter summary: Generate AI Summary for the PR
  • /matter review: Generate AI Reviews for the latest commit in the PR
  • /matter review-full: Generate AI Reviews for the complete PR
  • /matter release-notes: Generate AI release-notes for the PR
  • /matter : Chat with your PR with Matter AI Agent
  • /matter remember : Generate AI memories for the PR
  • /matter explain: Get an explanation of the PR
  • /matter help: Show the list of available commands and documentation
  • Need help? Join our Discord server: https://discord.gg/fJU5DvanU3

Copy link
Contributor

Important

PR Review Skipped

PR review skipped as per the configuration setting. Run a manually review by commenting /matter review

💡Tips to use Matter AI

Command List

  • /matter summary: Generate AI Summary for the PR
  • /matter review: Generate AI Reviews for the latest commit in the PR
  • /matter review-full: Generate AI Reviews for the complete PR
  • /matter release-notes: Generate AI release-notes for the PR
  • /matter : Chat with your PR with Matter AI Agent
  • /matter remember : Generate AI memories for the PR
  • /matter explain: Get an explanation of the PR
  • /matter help: Show the list of available commands and documentation
  • Need help? Join our Discord server: https://discord.gg/fJU5DvanU3

@VisargD VisargD merged commit fe7f143 into Portkey-AI:main Jul 10, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants