Skip to content

Improve openai class #601

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

Merged
merged 20 commits into from
May 27, 2025
Merged

Improve openai class #601

merged 20 commits into from
May 27, 2025

Conversation

luv-bansal
Copy link
Contributor

@luv-bansal luv-bansal commented May 27, 2025

This pull request introduces significant enhancements to the OpenAI-compatible API server implementation, including code refactoring, new utility methods, and improved error handling. The changes streamline request processing, add modularity, and simplify parameter extraction and validation. Minor updates include type cleanup and debugging code removal.

Enhancements to OpenAI-compatible API server:

  • Refactored request parameter handling:

    • Introduced _extract_request_params to centralize and validate parameter extraction from request data. This reduces redundancy across methods.
    • Added _create_completion_args to construct completion arguments dynamically, improving flexibility for both streaming and non-streaming requests.
  • Improved error handling:

    • Added _format_error_response to generate OpenAI-compatible error responses in a structured JSON format.
  • Simplified request processing:

    • Updated _process_request and _process_streaming_request to use **kwargs and leverage the new helper methods for argument preparation. This reduces boilerplate code and enhances maintainability.
  • Class initialization updates:

    • Replaced load_model with __init__ to initialize the OpenAI client and model more effectively. Added get_model as an abstract method for subclasses to specify the model name.

Code cleanup and minor updates:

  • Type cleanup:

    • Removed unused imports (Optional, List) in clarifai/runners/models/openai_class.py.
  • Debugging code removal:

    • Removed unnecessary debug print statements in async def main() in clarifai/runners/utils/code_script.py.
  • String handling adjustment:

    • Updated _parse_default_value to return raw strings for STR data types instead of using repr, ensuring consistent handling of string defaults.

@luv-bansal luv-bansal requested review from zeiler and sainivedh May 27, 2025 11:22
@zeiler zeiler enabled auto-merge (squash) May 27, 2025 19:18
@luv-bansal luv-bansal disabled auto-merge May 27, 2025 19:19
@luv-bansal luv-bansal enabled auto-merge (squash) May 27, 2025 19:19
Copy link

Code Coverage

Package Line Rate Health
clarifai 43%
clarifai.cli 43%
clarifai.client 71%
clarifai.client.auth 74%
clarifai.constants 100%
clarifai.datasets 100%
clarifai.datasets.export 80%
clarifai.datasets.upload 75%
clarifai.datasets.upload.loaders 37%
clarifai.models 100%
clarifai.modules 0%
clarifai.rag 72%
clarifai.runners 12%
clarifai.runners.models 62%
clarifai.runners.utils 61%
clarifai.runners.utils.data_types 72%
clarifai.schema 100%
clarifai.urls 75%
clarifai.utils 73%
clarifai.utils.evaluation 67%
clarifai.workflows 94%
Summary 66% (6314 / 9589)

Minimum allowed line rate is 50%

@luv-bansal luv-bansal merged commit 76a1975 into master May 27, 2025
9 checks passed
@luv-bansal luv-bansal deleted the improve-openai-class branch May 27, 2025 19:26
@srikanthbachala20 srikanthbachala20 mentioned this pull request May 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants