Skip to content

Fix openai messages Utils function and code-snippet function #595

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 4 commits into from
May 26, 2025
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: 1 addition & 1 deletion clarifai/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "11.4.3"
__version__ = "11.4.4"
2 changes: 1 addition & 1 deletion clarifai/client/model_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def f(*args, **kwargs):
f.__name__ = method_name
f.__qualname__ = f'{self.__class__.__name__}.{method_name}'
input_annotations = code_script._get_annotations_source(method_signature)
return_annotation = input_annotations.pop('return', (None, None))[0]
return_annotation = input_annotations.pop('return', (None, None, None))[0]
sig = inspect.signature(f).replace(
parameters=[
inspect.Parameter(k, inspect.Parameter.POSITIONAL_OR_KEYWORD, annotation=v[0])
Expand Down
15 changes: 5 additions & 10 deletions clarifai/runners/utils/code_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,13 @@ async def main():
method_name = method_signature.name
client_script_str = f'response = model.{method_name}('
annotations = _get_annotations_source(method_signature)
for param_name, (param_type, default_value) in annotations.items():
for param_name, (param_type, default_value, required) in annotations.items():
print(
f"param_name: {param_name}, param_type: {param_type}, default_value: {default_value}"
)
if param_name == "return":
continue
if default_value is None:
if default_value is None and required:
default_value = _set_default_value(param_type)
client_script_str += f"{param_name}={default_value}, "
client_script_str = client_script_str.rstrip(", ") + ")"
Expand Down Expand Up @@ -132,15 +132,15 @@ def _get_annotations_source(method_signature: resources_pb2.MethodSignature) ->
default_value = None
if data_utils.Param.get_default(input_field):
default_value = _parse_default_value(input_field)
annotations[param_name] = (param_type, default_value)
annotations[param_name] = (param_type, default_value, input_field.required)
if not method_signature.output_fields:
raise ValueError("MethodSignature must have at least one output field")
for output_field in method_signature.output_fields:
param_name = output_field.name
param_type = _get_base_type(output_field)
if output_field.iterator:
param_type = f"Iterator[{param_type}]"
annotations[param_name] = (param_type, None)
annotations[param_name] = (param_type, None, output_field.required)
return annotations


Expand Down Expand Up @@ -189,7 +189,7 @@ def _map_default_value(field_type):
default_value = None

if field_type == "str":
default_value = 'What is the future of AI?'
default_value = repr('What is the future of AI?')
elif field_type == "bytes":
default_value = b""
elif field_type == "int":
Expand Down Expand Up @@ -224,11 +224,9 @@ def _set_default_value(field_type):
Set the default value of a field if it is not set.
"""
is_iterator = False
print(f"before field_type: {field_type}")
if field_type.startswith("Iterator["):
is_iterator = True
field_type = field_type[9:-1]
print(f"after field_type: {field_type}")
default_value = None
default_value = _map_default_value(field_type)
if field_type.startswith("List["):
Expand All @@ -245,11 +243,8 @@ def _set_default_value(field_type):
element_type_defaults = [_map_default_value(et) for et in element_types]
default_value = f"{{{', '.join([str(et) for et in element_type_defaults])}}}"

if field_type == 'str':
default_value = repr(default_value)
if is_iterator:
default_value = f'iter([{default_value}])'
print(f"after default_value: {default_value}")
return default_value


Expand Down
4 changes: 1 addition & 3 deletions clarifai/runners/utils/data_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,7 @@ def to_proto(self, proto=None) -> ParamProto:
proto.model_type_range_info.CopyFrom(range_info)
proto.is_param = self.is_param

if self.default is not None:
proto = self.set_default(proto, self.default)

proto = self.set_default(proto, self.default)
return proto

@classmethod
Expand Down
20 changes: 0 additions & 20 deletions clarifai/runners/utils/method_signatures.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,32 +74,12 @@ def build_function_signature(func):
assert method_type in ('UNARY_UNARY', 'UNARY_STREAMING', 'STREAMING_STREAMING')
# method_signature.method_type = method_type
method_signature.description = inspect.cleandoc(func.__doc__ or '')
# method_signature.annotations_json = json.dumps(_get_annotations_source(func))

method_signature.input_fields.extend(input_sigs)
method_signature.output_fields.append(output_sig)
return method_signature


# def _get_annotations_source(func):
# """Extracts raw annotation strings from the function source."""
# source = inspect.getsource(func) # Get function source code
# source = textwrap.dedent(source) # Dedent source code
# tree = ast.parse(source) # Parse into AST
# func_node = next(node for node in tree.body
# if isinstance(node, ast.FunctionDef)) # Get function node

# annotations = {}
# for arg in func_node.args.args: # Process arguments
# if arg.annotation:
# annotations[arg.arg] = ast.unparse(arg.annotation) # Get raw annotation string

# if func_node.returns: # Process return type
# annotations["return"] = ast.unparse(func_node.returns)

# return annotations


def _process_input_field(field: resources_pb2.ModelTypeField) -> str:
base_type = _get_base_type(field)
if field.iterator:
Expand Down
16 changes: 8 additions & 8 deletions clarifai/runners/utils/openai_convertor.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,14 @@ def openai_to_hf_messages(openai_messages):


def build_openai_messages(
prompt: str,
image: Image,
images: List[Image],
audio: Audio,
audios: List[Audio],
video: Video,
videos: List[Video],
messages: List[Dict],
prompt: str = None,
image: Image = None,
images: List[Image] = None,
audio: Audio = None,
audios: List[Audio] = None,
video: Video = None,
videos: List[Video] = None,
messages: List[Dict] = None,
) -> List[Dict]:
"""
Construct OpenAI-compatible messages from input components.
Expand Down