Skip to content

integrated CrofAI (in theory) #1164

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open

Conversation

scuzzles
Copy link

Description

Should add CrofAI as a provider in portkey gateway

Motivation

Is the cheapest inference provider for almost every large LLM

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
  • Integration Tests
  • Manual Testing

Screenshots (if applicable)

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

Copy link

matter-code-review bot commented Jun 19, 2025

Code Quality new feature

Description

Summary By MatterAI MatterAI logo

🔄 What Changed

This pull request introduces a new AI provider, CrofAI, into the Portkey gateway. It includes the necessary API configuration, chat completion parameter definitions, and response transformation logic for both standard and streaming API calls. Three new files have been added to support this integration: src/providers/crofai/api.ts, src/providers/crofai/chatComplete.ts, and src/providers/crofai/index.ts.

🔍 Impact of the Change

This change expands the capabilities of the Portkey gateway by adding support for CrofAI, allowing users to route AI requests to this new provider. It ensures proper request formatting, API key handling, and standardized response parsing for CrofAI's chat completion endpoint. The developer role in messages is transformed to system for CrofAI compatibility.

📁 Total Files Changed

3 files changed.

🧪 Test Added

No explicit unit or integration tests were added as part of this pull request. The implementation includes TODO comments indicating areas where further input validation (e.g., max token limits) might be needed, which would typically be covered by tests.

🔒Security Vulnerabilities

No direct security vulnerabilities were detected in the provided code. API key handling uses a standard Bearer token approach. However, the lack of explicit input validation for parameters like max_tokens could potentially lead to unexpected behavior or resource consumption if not handled by the CrofAI API itself.

Motivation

To integrate CrofAI as a new provider in the Portkey gateway, expanding the range of supported AI models and services.

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
  • Integration Tests
  • Manual Testing (Inferred from the PR description "integrated CrofAI (in theory)" and the nature of a new integration, manual testing would be the primary method for initial verification.)

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 (Some TODO comments are present, but more detailed comments on complex transformations or error handling logic could be beneficial.)
  • 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

.

Tip

Quality Recommendations

  1. Address the TODO comments in src/providers/crofai/chatComplete.ts regarding input validation for max_tokens and n. Implementing proper validation for token limits, possibly using a tokenizer, would enhance robustness.

  2. Add comprehensive unit tests for crofaiChatCompleteResponseTransform and crofaiChatCompleteStreamChunkTransform to ensure correct parsing of various success and error scenarios, as well as edge cases for stream chunks (e.g., malformed JSON, empty chunks).

  3. Improve error handling in crofaiChatCompleteStreamChunkTransform. Currently, console.error is used for parsing errors, and an empty string is returned. Consider throwing a custom error or propagating the error more explicitly to allow the calling context to handle it gracefully, rather than silently failing.

  4. Consider adding more specific validation for the messages array content if CrofAI has stricter requirements beyond the developer role transformation. For example, ensuring message structure or content types.

Sequence Diagram

sequenceDiagram
    participant User
    participant Gateway as Portkey Gateway
    participant CrofAIProvider as CrofAI Provider Module
    participant CrofAIApi as CrofAI External API

    User->>Gateway: API Request (e.g., /v1/chat/completions)
    activate Gateway

    Gateway->>CrofAIProvider: Call chatComplete (params: {model, messages, ...})
    activate CrofAIProvider

    CrofAIProvider->>CrofAIProvider: Apply crofaiChatCompleteConfig (validate & transform params)
    Note right of CrofAIProvider: Params transformed (e.g., 'developer' role to 'system')

    CrofAIProvider->>CrofAIProvider: Get API Config (crofaiApiConfig)
    Note right of CrofAIProvider: Checks for providerOptions.apiKey

    CrofAIProvider->>CrofAIApi: HTTP POST /chat/completions (headers: Authorization: Bearer <apiKey>, body: transformed_params)
    activate CrofAIApi

    alt Successful Response
        CrofAIApi-->>CrofAIProvider: HTTP 200 OK (CrofAI specific response/stream)
        deactivate CrofAIApi
        CrofAIProvider->>CrofAIProvider: Apply crofaiChatCompleteResponseTransform (for non-stream)
        Note right of CrofAIProvider: Transforms CrofAI response to generic ChatCompletionResponse
        CrofAIProvider->>CrofAIProvider: Apply crofaiChatCompleteStreamChunkTransform (for stream)
        Note right of CrofAIProvider: Parses and transforms stream chunks, handles '[DONE]' and 'ping'
        CrofAIProvider-->>Gateway: Transformed Response/Stream
    else Error Response
        CrofAIApi-->>CrofAIProvider: HTTP Error (e.g., 4xx, 5xx) (CrofAI specific error response)
        deactivate CrofAIApi
        CrofAIProvider->>CrofAIProvider: Apply crofaiChatCompleteResponseTransform (for error handling)
        Note right of CrofAIProvider: Parses 'detail' field for error message, type, location
        CrofAIProvider-->>Gateway: ErrorResponse
    end

    CrofAIProvider-->>Gateway: Processed Response
    deactivate CrofAIProvider

    Gateway-->>User: Final API Response
    deactivate Gateway
Loading

Copy link

@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.

The CrofAI integration looks good overall, but I've identified a few issues that should be addressed to ensure proper functionality and maintainability.

Skipped files
  • cookbook/integrations/crofai.ipynb: File hunk diff too large

object: response.object,
created: response.created,
model: response.model,
provider: DEEPINFRA,

Choose a reason for hiding this comment

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

🐛 Bug Fix

Issue: The provider is incorrectly set to DEEPINFRA in the response transformation.
Fix: Replace DEEPINFRA with a CrofAI-specific constant.
Impact: Ensures responses correctly identify CrofAI as the provider instead of DeepInfra.

Suggested change
provider: DEEPINFRA,
provider: DEEPINFRA,

object: parsedChunk.object,
created: parsedChunk.created,
model: parsedChunk.model,
provider: DEEPINFRA,

Choose a reason for hiding this comment

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

🐛 Bug Fix

Issue: The provider is incorrectly set to DEEPINFRA in the stream chunk transformation.
Fix: Replace DEEPINFRA with a CrofAI-specific constant.
Impact: Ensures stream responses correctly identify CrofAI as the provider instead of DeepInfra.

Suggested change
provider: DEEPINFRA,
provider: DEEPINFRA,

@narengogi
Copy link
Collaborator

Hey @scuzzles could you add some documentation links?

@scuzzles
Copy link
Author

Hey @scuzzles could you add some documentation links?

documentation for the code I've commited or documentation for CrofAI?

Co-authored-by: matter-code-review[bot] <150888575+matter-code-review[bot]@users.noreply.github.com>
Copy link

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

Co-authored-by: matter-code-review[bot] <150888575+matter-code-review[bot]@users.noreply.github.com>
Copy link

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

@roh26it
Copy link
Collaborator

roh26it commented Jun 24, 2025

@scuzzles - Can you look at the PR comments from matter?

@narengogi
Copy link
Collaborator

Hey @scuzzles could you add some documentation links?

documentation for the code I've commited or documentation for CrofAI?

for CrofAI

@scuzzles
Copy link
Author

Hey @scuzzles could you add some documentation links?

documentation for the code I've commited or documentation for CrofAI?

for CrofAI

https://ai.nahcrof.com/docs

Co-authored-by: matter-code-review[bot] <150888575+matter-code-review[bot]@users.noreply.github.com>
Copy link

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

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

@scuzzles
Copy link
Author

@scuzzles - Can you look at the PR comments from matter?

Could be wrong but I believe they should be fixed or committed now

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.

3 participants