Skip to content

PR-500 simplified client wrapper functions#562

Merged
mogith-pn merged 13 commits intomasterfrom
PR-500-simplify-client-wrapper
May 23, 2025
Merged

PR-500 simplified client wrapper functions#562
mogith-pn merged 13 commits intomasterfrom
PR-500-simplify-client-wrapper

Conversation

@mogith-pn
Copy link
Copy Markdown
Contributor

Why

  • https://clarifai.atlassian.net/browse/PR-500
  • The client wrapper function in model upload code, still needed some abstraction for converting images/video/audio into openai compatible format before sending to chat function.

How

  • Since it can be abstacted into the runner data utils function, moved those preprocessing steps into SDK.

Tests

@mogith-pn mogith-pn requested review from Copilot and luv-bansal and removed request for Copilot April 23, 2025 15:58
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR simplifies the client wrapper functions by introducing a unified helper to format OpenAI chat messages with various media types (image, audio, video) and by adding base64 conversion methods to the corresponding data types.

  • Updated PIL image conversions by importing PILImage and aligning type hints.
  • Added build_openai_chat_format and internal helper functions to process images, audio, and video.
  • Extended data types with new to_base64_str methods for Image, Audio, and Video objects.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
clarifai/runners/utils/data_utils.py Updated image conversion functions and added media processing functions for chat messages.
clarifai/runners/utils/data_types.py Added to_base64_str utility methods for converting media bytes to base64 strings.
Comments suppressed due to low confidence (2)

clarifai/runners/utils/data_utils.py:132

  • [nitpick] Consider using a distinct local variable for the base64 string conversion in _process_audio instead of reassigning the parameter 'audio', to improve code clarity.
if audio.bytes:

clarifai/runners/utils/data_utils.py:158

  • [nitpick] Consider using a separate variable to hold the base64 string in _process_video rather than overwriting the parameter 'video', for improved readability.
if video.bytes:

Copy link
Copy Markdown
Contributor

@luv-bansal luv-bansal left a comment

Choose a reason for hiding this comment

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

Looks good, but not sure , should we add build_openai_chat_format function to utils/openai_convertor.py file?

Comment thread clarifai/runners/utils/data_utils.py Outdated
return True


def build_openai_chat_format(prompt: str, image: Image, images: List[Image], audio: Audio,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Not sure, do you think we could move this to here

Comment thread clarifai/runners/utils/data_utils.py Outdated
return True


def build_openai_chat_format(prompt: str, image: Image, images: List[Image], audio: Audio,
Copy link
Copy Markdown
Contributor

@luv-bansal luv-bansal May 22, 2025

Choose a reason for hiding this comment

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

also should be rename this function it to build_openai_messages or just openai_messages like we have openai_response function here https://github.com/Clarifai/clarifai-python/blob/master/clarifai/runners/utils/openai_convertor.py#L98?

@mogith-pn mogith-pn requested review from Copilot and luv-bansal May 22, 2025 14:13
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR centralizes preprocessing of image, audio, and video inputs into SDK utilities and provides helpers to assemble OpenAI-compatible chat messages.

  • Introduce openai_convertor.py with build_openai_messages and is_openai_chat_format
  • Add process_image, process_audio, and process_video in data_utils.py
  • Extend data types with to_base64_str methods and update import in model_client.py

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
clarifai/runners/utils/openai_convertor.py Added message builder and chat-format validator for multimodal inputs
clarifai/runners/utils/data_utils.py Implemented processing helpers for Image, Audio, and Video
clarifai/runners/utils/data_types/data_types.py Added to_base64_str to Image, Audio, and Video types
clarifai/client/model_client.py Updated is_openai_chat_format import to the new converter
Comments suppressed due to low confidence (2)

clarifai/runners/utils/openai_convertor.py:1

  • [nitpick] The filename uses 'convertor' spelling; consider renaming to 'converter' for consistency with common terminology.
import time

clarifai/runners/utils/openai_convertor.py:173

  • Add unit tests for multimodal message construction and validation (process_image, process_audio, process_video, build_openai_messages, is_openai_chat_format) to ensure correct behavior across modalities.
def build_openai_messages(

Comment on lines +174 to +181
prompt: str,
image: Image,
images: List[Image],
audio: Audio,
audios: List[Audio],
video: Video,
videos: List[Video],
messages: List[Dict],
Copy link

Copilot AI May 22, 2025

Choose a reason for hiding this comment

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

Consider making parameters optional with default values (e.g., prompt: str = '', images: List[Image] = None) to simplify calls when some modalities are unused.

Suggested change
prompt: str,
image: Image,
images: List[Image],
audio: Audio,
audios: List[Audio],
video: Video,
videos: List[Video],
messages: List[Dict],
prompt: str = '',
image: Image = None,
images: List[Image] = [],
audio: Audio = None,
audios: List[Audio] = [],
video: Video = None,
videos: List[Video] = [],
messages: List[Dict] = [],

Copilot uses AI. Check for mistakes.
Comment on lines 2 to 4
import json
import math
import operator
Copy link

Copilot AI May 22, 2025

Choose a reason for hiding this comment

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

[nitpick] Remove unused imports (json, math, operator) to reduce clutter, unless they are used elsewhere in this module.

Suggested change
import json
import math
import operator

Copilot uses AI. Check for mistakes.
raise ValueError("Image has no bytes")
return PILImage.open(io.BytesIO(self.proto.base64))

def to_base64_str(self) -> str:
Copy link

Copilot AI May 22, 2025

Choose a reason for hiding this comment

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

[nitpick] The to_base64_str implementation is duplicated across Image, Audio, and Video; consider extracting a common mixin or utility to avoid code duplication.

Copilot uses AI. Check for mistakes.
Comment thread clarifai/runners/utils/data_types/data_types.py
Comment on lines +477 to +484
def to_base64_str(self) -> str:
if isinstance(self.proto.base64, bytes):
return self.proto.base64.decode('utf-8')
elif isinstance(self.proto.base64, str):
return self.proto.base64
elif not self.proto.base64:
raise ValueError("Audio has no bytes")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

similarly here for all to_base64_str methods

Copy link
Copy Markdown
Contributor

@luv-bansal luv-bansal left a comment

Choose a reason for hiding this comment

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

I feel like to_base64_str implementation is currently wrong, because I'm sure self.proto.base64 is bytes and not a base64-encoded bytes. I know this confusion in naming is for long time in Clarifai

Comment thread clarifai/runners/utils/openai_convertor.py Outdated
@mogith-pn mogith-pn requested a review from luv-bansal May 23, 2025 09:02
Comment thread clarifai/runners/utils/data_types/data_types.py
@mogith-pn mogith-pn requested a review from luv-bansal May 23, 2025 11:35
@github-actions
Copy link
Copy Markdown

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 10%
clarifai.runners.models 57%
clarifai.runners.utils 56%
clarifai.runners.utils.data_types 72%
clarifai.schema 100%
clarifai.urls 75%
clarifai.utils 73%
clarifai.utils.evaluation 67%
clarifai.workflows 94%
Summary 65% (6086 / 9427)

Minimum allowed line rate is 50%

@mogith-pn mogith-pn merged commit 681d6a7 into master May 23, 2025
9 checks passed
@mogith-pn mogith-pn deleted the PR-500-simplify-client-wrapper branch May 23, 2025 12:09
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