clarifai 11.3.0rc2__py3-none-any.whl → 11.4.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- clarifai/__init__.py +1 -1
- clarifai/cli/__main__.py +1 -1
- clarifai/cli/base.py +144 -136
- clarifai/cli/compute_cluster.py +45 -31
- clarifai/cli/deployment.py +93 -76
- clarifai/cli/model.py +578 -180
- clarifai/cli/nodepool.py +100 -82
- clarifai/client/__init__.py +12 -2
- clarifai/client/app.py +973 -911
- clarifai/client/auth/helper.py +345 -342
- clarifai/client/auth/register.py +7 -7
- clarifai/client/auth/stub.py +107 -106
- clarifai/client/base.py +185 -178
- clarifai/client/compute_cluster.py +214 -180
- clarifai/client/dataset.py +793 -698
- clarifai/client/deployment.py +55 -50
- clarifai/client/input.py +1223 -1088
- clarifai/client/lister.py +47 -45
- clarifai/client/model.py +1939 -1717
- clarifai/client/model_client.py +525 -502
- clarifai/client/module.py +82 -73
- clarifai/client/nodepool.py +358 -213
- clarifai/client/runner.py +58 -0
- clarifai/client/search.py +342 -309
- clarifai/client/user.py +419 -414
- clarifai/client/workflow.py +294 -274
- clarifai/constants/dataset.py +11 -17
- clarifai/constants/model.py +8 -2
- clarifai/datasets/export/inputs_annotations.py +233 -217
- clarifai/datasets/upload/base.py +63 -51
- clarifai/datasets/upload/features.py +43 -38
- clarifai/datasets/upload/image.py +237 -207
- clarifai/datasets/upload/loaders/coco_captions.py +34 -32
- clarifai/datasets/upload/loaders/coco_detection.py +72 -65
- clarifai/datasets/upload/loaders/imagenet_classification.py +57 -53
- clarifai/datasets/upload/loaders/xview_detection.py +274 -132
- clarifai/datasets/upload/multimodal.py +55 -46
- clarifai/datasets/upload/text.py +55 -47
- clarifai/datasets/upload/utils.py +250 -234
- clarifai/errors.py +51 -50
- clarifai/models/api.py +260 -238
- clarifai/modules/css.py +50 -50
- clarifai/modules/pages.py +33 -33
- clarifai/rag/rag.py +312 -288
- clarifai/rag/utils.py +91 -84
- clarifai/runners/models/model_builder.py +906 -802
- clarifai/runners/models/model_class.py +370 -331
- clarifai/runners/models/model_run_locally.py +459 -419
- clarifai/runners/models/model_runner.py +170 -162
- clarifai/runners/models/model_servicer.py +78 -70
- clarifai/runners/server.py +111 -101
- clarifai/runners/utils/code_script.py +225 -187
- clarifai/runners/utils/const.py +4 -1
- clarifai/runners/utils/data_types/__init__.py +12 -0
- clarifai/runners/utils/data_types/data_types.py +598 -0
- clarifai/runners/utils/data_utils.py +387 -440
- clarifai/runners/utils/loader.py +247 -227
- clarifai/runners/utils/method_signatures.py +411 -386
- clarifai/runners/utils/openai_convertor.py +108 -109
- clarifai/runners/utils/serializers.py +175 -179
- clarifai/runners/utils/url_fetcher.py +35 -35
- clarifai/schema/search.py +56 -63
- clarifai/urls/helper.py +125 -102
- clarifai/utils/cli.py +129 -123
- clarifai/utils/config.py +127 -87
- clarifai/utils/constants.py +49 -0
- clarifai/utils/evaluation/helpers.py +503 -466
- clarifai/utils/evaluation/main.py +431 -393
- clarifai/utils/evaluation/testset_annotation_parser.py +154 -144
- clarifai/utils/logging.py +324 -306
- clarifai/utils/misc.py +60 -56
- clarifai/utils/model_train.py +165 -146
- clarifai/utils/protobuf.py +126 -103
- clarifai/versions.py +3 -1
- clarifai/workflows/export.py +48 -50
- clarifai/workflows/utils.py +39 -36
- clarifai/workflows/validate.py +55 -43
- {clarifai-11.3.0rc2.dist-info → clarifai-11.4.0.dist-info}/METADATA +16 -6
- clarifai-11.4.0.dist-info/RECORD +109 -0
- {clarifai-11.3.0rc2.dist-info → clarifai-11.4.0.dist-info}/WHEEL +1 -1
- clarifai/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/__pycache__/__init__.cpython-311.pyc +0 -0
- clarifai/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/__pycache__/errors.cpython-310.pyc +0 -0
- clarifai/__pycache__/errors.cpython-311.pyc +0 -0
- clarifai/__pycache__/versions.cpython-310.pyc +0 -0
- clarifai/__pycache__/versions.cpython-311.pyc +0 -0
- clarifai/cli/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/cli/__pycache__/__init__.cpython-311.pyc +0 -0
- clarifai/cli/__pycache__/base.cpython-310.pyc +0 -0
- clarifai/cli/__pycache__/base.cpython-311.pyc +0 -0
- clarifai/cli/__pycache__/base_cli.cpython-310.pyc +0 -0
- clarifai/cli/__pycache__/compute_cluster.cpython-310.pyc +0 -0
- clarifai/cli/__pycache__/compute_cluster.cpython-311.pyc +0 -0
- clarifai/cli/__pycache__/deployment.cpython-310.pyc +0 -0
- clarifai/cli/__pycache__/deployment.cpython-311.pyc +0 -0
- clarifai/cli/__pycache__/model.cpython-310.pyc +0 -0
- clarifai/cli/__pycache__/model.cpython-311.pyc +0 -0
- clarifai/cli/__pycache__/model_cli.cpython-310.pyc +0 -0
- clarifai/cli/__pycache__/nodepool.cpython-310.pyc +0 -0
- clarifai/cli/__pycache__/nodepool.cpython-311.pyc +0 -0
- clarifai/client/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/__init__.cpython-311.pyc +0 -0
- clarifai/client/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/client/__pycache__/app.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/app.cpython-311.pyc +0 -0
- clarifai/client/__pycache__/app.cpython-39.pyc +0 -0
- clarifai/client/__pycache__/base.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/base.cpython-311.pyc +0 -0
- clarifai/client/__pycache__/compute_cluster.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/compute_cluster.cpython-311.pyc +0 -0
- clarifai/client/__pycache__/dataset.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/dataset.cpython-311.pyc +0 -0
- clarifai/client/__pycache__/deployment.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/deployment.cpython-311.pyc +0 -0
- clarifai/client/__pycache__/input.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/input.cpython-311.pyc +0 -0
- clarifai/client/__pycache__/lister.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/lister.cpython-311.pyc +0 -0
- clarifai/client/__pycache__/model.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/model.cpython-311.pyc +0 -0
- clarifai/client/__pycache__/module.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/module.cpython-311.pyc +0 -0
- clarifai/client/__pycache__/nodepool.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/nodepool.cpython-311.pyc +0 -0
- clarifai/client/__pycache__/search.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/search.cpython-311.pyc +0 -0
- clarifai/client/__pycache__/user.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/user.cpython-311.pyc +0 -0
- clarifai/client/__pycache__/workflow.cpython-310.pyc +0 -0
- clarifai/client/__pycache__/workflow.cpython-311.pyc +0 -0
- clarifai/client/auth/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/client/auth/__pycache__/__init__.cpython-311.pyc +0 -0
- clarifai/client/auth/__pycache__/helper.cpython-310.pyc +0 -0
- clarifai/client/auth/__pycache__/helper.cpython-311.pyc +0 -0
- clarifai/client/auth/__pycache__/register.cpython-310.pyc +0 -0
- clarifai/client/auth/__pycache__/register.cpython-311.pyc +0 -0
- clarifai/client/auth/__pycache__/stub.cpython-310.pyc +0 -0
- clarifai/client/auth/__pycache__/stub.cpython-311.pyc +0 -0
- clarifai/client/cli/__init__.py +0 -0
- clarifai/client/cli/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/client/cli/__pycache__/base_cli.cpython-310.pyc +0 -0
- clarifai/client/cli/__pycache__/model_cli.cpython-310.pyc +0 -0
- clarifai/client/cli/base_cli.py +0 -88
- clarifai/client/cli/model_cli.py +0 -29
- clarifai/constants/__pycache__/base.cpython-310.pyc +0 -0
- clarifai/constants/__pycache__/base.cpython-311.pyc +0 -0
- clarifai/constants/__pycache__/dataset.cpython-310.pyc +0 -0
- clarifai/constants/__pycache__/dataset.cpython-311.pyc +0 -0
- clarifai/constants/__pycache__/input.cpython-310.pyc +0 -0
- clarifai/constants/__pycache__/input.cpython-311.pyc +0 -0
- clarifai/constants/__pycache__/model.cpython-310.pyc +0 -0
- clarifai/constants/__pycache__/model.cpython-311.pyc +0 -0
- clarifai/constants/__pycache__/rag.cpython-310.pyc +0 -0
- clarifai/constants/__pycache__/rag.cpython-311.pyc +0 -0
- clarifai/constants/__pycache__/search.cpython-310.pyc +0 -0
- clarifai/constants/__pycache__/search.cpython-311.pyc +0 -0
- clarifai/constants/__pycache__/workflow.cpython-310.pyc +0 -0
- clarifai/constants/__pycache__/workflow.cpython-311.pyc +0 -0
- clarifai/datasets/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/datasets/__pycache__/__init__.cpython-311.pyc +0 -0
- clarifai/datasets/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/datasets/export/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/datasets/export/__pycache__/__init__.cpython-311.pyc +0 -0
- clarifai/datasets/export/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/datasets/export/__pycache__/inputs_annotations.cpython-310.pyc +0 -0
- clarifai/datasets/export/__pycache__/inputs_annotations.cpython-311.pyc +0 -0
- clarifai/datasets/upload/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/datasets/upload/__pycache__/__init__.cpython-311.pyc +0 -0
- clarifai/datasets/upload/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/datasets/upload/__pycache__/base.cpython-310.pyc +0 -0
- clarifai/datasets/upload/__pycache__/base.cpython-311.pyc +0 -0
- clarifai/datasets/upload/__pycache__/features.cpython-310.pyc +0 -0
- clarifai/datasets/upload/__pycache__/features.cpython-311.pyc +0 -0
- clarifai/datasets/upload/__pycache__/image.cpython-310.pyc +0 -0
- clarifai/datasets/upload/__pycache__/image.cpython-311.pyc +0 -0
- clarifai/datasets/upload/__pycache__/multimodal.cpython-310.pyc +0 -0
- clarifai/datasets/upload/__pycache__/multimodal.cpython-311.pyc +0 -0
- clarifai/datasets/upload/__pycache__/text.cpython-310.pyc +0 -0
- clarifai/datasets/upload/__pycache__/text.cpython-311.pyc +0 -0
- clarifai/datasets/upload/__pycache__/utils.cpython-310.pyc +0 -0
- clarifai/datasets/upload/__pycache__/utils.cpython-311.pyc +0 -0
- clarifai/datasets/upload/loaders/__pycache__/__init__.cpython-311.pyc +0 -0
- clarifai/datasets/upload/loaders/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/datasets/upload/loaders/__pycache__/coco_detection.cpython-311.pyc +0 -0
- clarifai/datasets/upload/loaders/__pycache__/imagenet_classification.cpython-311.pyc +0 -0
- clarifai/models/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/modules/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/rag/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/rag/__pycache__/__init__.cpython-311.pyc +0 -0
- clarifai/rag/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/rag/__pycache__/rag.cpython-310.pyc +0 -0
- clarifai/rag/__pycache__/rag.cpython-311.pyc +0 -0
- clarifai/rag/__pycache__/rag.cpython-39.pyc +0 -0
- clarifai/rag/__pycache__/utils.cpython-310.pyc +0 -0
- clarifai/rag/__pycache__/utils.cpython-311.pyc +0 -0
- clarifai/runners/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/runners/__pycache__/__init__.cpython-311.pyc +0 -0
- clarifai/runners/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/runners/dockerfile_template/Dockerfile.cpu.template +0 -31
- clarifai/runners/dockerfile_template/Dockerfile.cuda.template +0 -42
- clarifai/runners/dockerfile_template/Dockerfile.nim +0 -71
- clarifai/runners/models/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/runners/models/__pycache__/__init__.cpython-311.pyc +0 -0
- clarifai/runners/models/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/runners/models/__pycache__/base_typed_model.cpython-310.pyc +0 -0
- clarifai/runners/models/__pycache__/base_typed_model.cpython-311.pyc +0 -0
- clarifai/runners/models/__pycache__/base_typed_model.cpython-39.pyc +0 -0
- clarifai/runners/models/__pycache__/model_builder.cpython-311.pyc +0 -0
- clarifai/runners/models/__pycache__/model_class.cpython-310.pyc +0 -0
- clarifai/runners/models/__pycache__/model_class.cpython-311.pyc +0 -0
- clarifai/runners/models/__pycache__/model_run_locally.cpython-310-pytest-7.1.2.pyc +0 -0
- clarifai/runners/models/__pycache__/model_run_locally.cpython-310.pyc +0 -0
- clarifai/runners/models/__pycache__/model_run_locally.cpython-311.pyc +0 -0
- clarifai/runners/models/__pycache__/model_runner.cpython-310.pyc +0 -0
- clarifai/runners/models/__pycache__/model_runner.cpython-311.pyc +0 -0
- clarifai/runners/models/__pycache__/model_upload.cpython-310.pyc +0 -0
- clarifai/runners/models/base_typed_model.py +0 -238
- clarifai/runners/models/model_class_refract.py +0 -80
- clarifai/runners/models/model_upload.py +0 -607
- clarifai/runners/models/temp.py +0 -25
- clarifai/runners/utils/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/__init__.cpython-311.pyc +0 -0
- clarifai/runners/utils/__pycache__/__init__.cpython-38.pyc +0 -0
- clarifai/runners/utils/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/runners/utils/__pycache__/buffered_stream.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/buffered_stream.cpython-38.pyc +0 -0
- clarifai/runners/utils/__pycache__/buffered_stream.cpython-39.pyc +0 -0
- clarifai/runners/utils/__pycache__/const.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/const.cpython-311.pyc +0 -0
- clarifai/runners/utils/__pycache__/constants.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/constants.cpython-38.pyc +0 -0
- clarifai/runners/utils/__pycache__/constants.cpython-39.pyc +0 -0
- clarifai/runners/utils/__pycache__/data_handler.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/data_handler.cpython-311.pyc +0 -0
- clarifai/runners/utils/__pycache__/data_handler.cpython-38.pyc +0 -0
- clarifai/runners/utils/__pycache__/data_handler.cpython-39.pyc +0 -0
- clarifai/runners/utils/__pycache__/data_utils.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/data_utils.cpython-311.pyc +0 -0
- clarifai/runners/utils/__pycache__/data_utils.cpython-38.pyc +0 -0
- clarifai/runners/utils/__pycache__/data_utils.cpython-39.pyc +0 -0
- clarifai/runners/utils/__pycache__/grpc_server.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/grpc_server.cpython-38.pyc +0 -0
- clarifai/runners/utils/__pycache__/grpc_server.cpython-39.pyc +0 -0
- clarifai/runners/utils/__pycache__/health.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/health.cpython-38.pyc +0 -0
- clarifai/runners/utils/__pycache__/health.cpython-39.pyc +0 -0
- clarifai/runners/utils/__pycache__/loader.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/loader.cpython-311.pyc +0 -0
- clarifai/runners/utils/__pycache__/logging.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/logging.cpython-38.pyc +0 -0
- clarifai/runners/utils/__pycache__/logging.cpython-39.pyc +0 -0
- clarifai/runners/utils/__pycache__/stream_source.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/stream_source.cpython-39.pyc +0 -0
- clarifai/runners/utils/__pycache__/url_fetcher.cpython-310.pyc +0 -0
- clarifai/runners/utils/__pycache__/url_fetcher.cpython-311.pyc +0 -0
- clarifai/runners/utils/__pycache__/url_fetcher.cpython-38.pyc +0 -0
- clarifai/runners/utils/__pycache__/url_fetcher.cpython-39.pyc +0 -0
- clarifai/runners/utils/data_handler.py +0 -231
- clarifai/runners/utils/data_handler_refract.py +0 -213
- clarifai/runners/utils/data_types.py +0 -469
- clarifai/runners/utils/logger.py +0 -0
- clarifai/runners/utils/openai_format.py +0 -87
- clarifai/schema/__pycache__/search.cpython-310.pyc +0 -0
- clarifai/schema/__pycache__/search.cpython-311.pyc +0 -0
- clarifai/urls/__pycache__/helper.cpython-310.pyc +0 -0
- clarifai/urls/__pycache__/helper.cpython-311.pyc +0 -0
- clarifai/utils/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/utils/__pycache__/__init__.cpython-311.pyc +0 -0
- clarifai/utils/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/utils/__pycache__/cli.cpython-310.pyc +0 -0
- clarifai/utils/__pycache__/cli.cpython-311.pyc +0 -0
- clarifai/utils/__pycache__/config.cpython-311.pyc +0 -0
- clarifai/utils/__pycache__/constants.cpython-310.pyc +0 -0
- clarifai/utils/__pycache__/constants.cpython-311.pyc +0 -0
- clarifai/utils/__pycache__/logging.cpython-310.pyc +0 -0
- clarifai/utils/__pycache__/logging.cpython-311.pyc +0 -0
- clarifai/utils/__pycache__/misc.cpython-310.pyc +0 -0
- clarifai/utils/__pycache__/misc.cpython-311.pyc +0 -0
- clarifai/utils/__pycache__/model_train.cpython-310.pyc +0 -0
- clarifai/utils/__pycache__/model_train.cpython-311.pyc +0 -0
- clarifai/utils/__pycache__/protobuf.cpython-311.pyc +0 -0
- clarifai/utils/evaluation/__pycache__/__init__.cpython-311.pyc +0 -0
- clarifai/utils/evaluation/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/utils/evaluation/__pycache__/helpers.cpython-311.pyc +0 -0
- clarifai/utils/evaluation/__pycache__/main.cpython-311.pyc +0 -0
- clarifai/utils/evaluation/__pycache__/main.cpython-39.pyc +0 -0
- clarifai/workflows/__pycache__/__init__.cpython-310.pyc +0 -0
- clarifai/workflows/__pycache__/__init__.cpython-311.pyc +0 -0
- clarifai/workflows/__pycache__/__init__.cpython-39.pyc +0 -0
- clarifai/workflows/__pycache__/export.cpython-310.pyc +0 -0
- clarifai/workflows/__pycache__/export.cpython-311.pyc +0 -0
- clarifai/workflows/__pycache__/utils.cpython-310.pyc +0 -0
- clarifai/workflows/__pycache__/utils.cpython-311.pyc +0 -0
- clarifai/workflows/__pycache__/validate.cpython-310.pyc +0 -0
- clarifai/workflows/__pycache__/validate.cpython-311.pyc +0 -0
- clarifai-11.3.0rc2.dist-info/RECORD +0 -322
- {clarifai-11.3.0rc2.dist-info → clarifai-11.4.0.dist-info}/entry_points.txt +0 -0
- {clarifai-11.3.0rc2.dist-info → clarifai-11.4.0.dist-info/licenses}/LICENSE +0 -0
- {clarifai-11.3.0rc2.dist-info → clarifai-11.4.0.dist-info}/top_level.txt +0 -0
@@ -3,7 +3,7 @@ import uuid
|
|
3
3
|
|
4
4
|
|
5
5
|
def generate_id():
|
6
|
-
|
6
|
+
return f"chatcmpl-{uuid.uuid4().hex}"
|
7
7
|
|
8
8
|
|
9
9
|
def _format_non_streaming_response(
|
@@ -15,39 +15,37 @@ def _format_non_streaming_response(
|
|
15
15
|
completion_tokens=None,
|
16
16
|
finish_reason="stop",
|
17
17
|
):
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
"logprobs": None,
|
40
|
-
}],
|
41
|
-
}
|
42
|
-
|
43
|
-
if prompt_tokens is not None and completion_tokens is not None:
|
44
|
-
response["usage"] = {
|
45
|
-
"prompt_tokens": prompt_tokens,
|
46
|
-
"completion_tokens": completion_tokens,
|
47
|
-
"total_tokens": prompt_tokens + completion_tokens,
|
18
|
+
if id is None:
|
19
|
+
id = generate_id()
|
20
|
+
if created is None:
|
21
|
+
created = int(time.time())
|
22
|
+
|
23
|
+
response = {
|
24
|
+
"id": id,
|
25
|
+
"object": "chat.completion",
|
26
|
+
"created": created,
|
27
|
+
"model": model,
|
28
|
+
"choices": [
|
29
|
+
{
|
30
|
+
"index": 0,
|
31
|
+
"message": {
|
32
|
+
"role": "assistant",
|
33
|
+
"content": generated_text,
|
34
|
+
},
|
35
|
+
"finish_reason": finish_reason,
|
36
|
+
"logprobs": None,
|
37
|
+
}
|
38
|
+
],
|
48
39
|
}
|
49
40
|
|
50
|
-
|
41
|
+
if prompt_tokens is not None and completion_tokens is not None:
|
42
|
+
response["usage"] = {
|
43
|
+
"prompt_tokens": prompt_tokens,
|
44
|
+
"completion_tokens": completion_tokens,
|
45
|
+
"total_tokens": prompt_tokens + completion_tokens,
|
46
|
+
}
|
47
|
+
|
48
|
+
return response
|
51
49
|
|
52
50
|
|
53
51
|
def _format_streaming_response(
|
@@ -57,45 +55,45 @@ def _format_streaming_response(
|
|
57
55
|
created=None,
|
58
56
|
finish_reason="stop",
|
59
57
|
):
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
58
|
+
if id is None:
|
59
|
+
id = generate_id()
|
60
|
+
if created is None:
|
61
|
+
created = int(time.time())
|
62
|
+
|
63
|
+
for chunk in generated_chunks:
|
64
|
+
yield {
|
65
|
+
"id": id,
|
66
|
+
"object": "chat.completion.chunk",
|
67
|
+
"created": created,
|
68
|
+
"model": model,
|
69
|
+
"choices": [
|
70
|
+
{
|
71
|
+
"index": 0,
|
72
|
+
"delta": {
|
73
|
+
"content": chunk,
|
74
|
+
},
|
75
|
+
"finish_reason": None,
|
76
|
+
"logprobs": None,
|
77
|
+
}
|
78
|
+
],
|
79
|
+
}
|
80
|
+
|
81
|
+
# Final chunk indicating completion
|
66
82
|
yield {
|
67
|
-
"id":
|
68
|
-
|
69
|
-
"
|
70
|
-
|
71
|
-
"
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
},
|
80
|
-
"finish_reason": None,
|
81
|
-
"logprobs": None,
|
82
|
-
}],
|
83
|
+
"id": id,
|
84
|
+
"object": "chat.completion.chunk",
|
85
|
+
"created": created,
|
86
|
+
"model": model,
|
87
|
+
"choices": [
|
88
|
+
{
|
89
|
+
"index": 0,
|
90
|
+
"delta": {},
|
91
|
+
"finish_reason": finish_reason,
|
92
|
+
"logprobs": None,
|
93
|
+
}
|
94
|
+
],
|
83
95
|
}
|
84
96
|
|
85
|
-
# Final chunk indicating completion
|
86
|
-
yield {
|
87
|
-
"id": id,
|
88
|
-
"object": "chat.completion.chunk",
|
89
|
-
"created": created,
|
90
|
-
"model": model,
|
91
|
-
"choices": [{
|
92
|
-
"index": 0,
|
93
|
-
"delta": {},
|
94
|
-
"finish_reason": finish_reason,
|
95
|
-
"logprobs": None,
|
96
|
-
}],
|
97
|
-
}
|
98
|
-
|
99
97
|
|
100
98
|
def openai_response(
|
101
99
|
generated_text,
|
@@ -107,15 +105,16 @@ def openai_response(
|
|
107
105
|
finish_reason="stop",
|
108
106
|
stream=True,
|
109
107
|
):
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
108
|
+
if stream:
|
109
|
+
return _format_streaming_response(generated_text, model, id, created, finish_reason)
|
110
|
+
else:
|
111
|
+
return _format_non_streaming_response(
|
112
|
+
generated_text, model, id, created, prompt_tokens, completion_tokens, finish_reason
|
113
|
+
)
|
115
114
|
|
116
115
|
|
117
116
|
def openai_to_hf_messages(openai_messages):
|
118
|
-
|
117
|
+
"""
|
119
118
|
Converts OpenAI-style chat messages into a format compatible with Hugging Face's
|
120
119
|
`tokenizer.apply_chat_template()` function, supporting all modalities (text, images, etc.).
|
121
120
|
|
@@ -127,41 +126,41 @@ def openai_to_hf_messages(openai_messages):
|
|
127
126
|
list: Hugging Face-compatible messages. Each message is a dict with 'role' and 'content'.
|
128
127
|
Content is a string (text-only) or a list of parts (multimodal).
|
129
128
|
"""
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
129
|
+
hf_messages = []
|
130
|
+
for msg in openai_messages:
|
131
|
+
role = msg['role']
|
132
|
+
content = msg['content']
|
133
|
+
|
134
|
+
if isinstance(content, list):
|
135
|
+
# Handle multimodal content (e.g., text + images)
|
136
|
+
converted_content = []
|
137
|
+
for part in content:
|
138
|
+
if part['type'] == 'text':
|
139
|
+
converted_content.append({'type': 'text', 'text': part['text']})
|
140
|
+
elif part['type'] == 'image_url':
|
141
|
+
# Handle image (extract base64 or URL)
|
142
|
+
image_url = part["image_url"]["url"]
|
143
|
+
if image_url.startswith("data:image"):
|
144
|
+
# Base64-encoded image
|
145
|
+
b64_img = image_url.split(",")[1]
|
146
|
+
converted_content.append({'type': 'image', 'base64': b64_img})
|
147
|
+
else:
|
148
|
+
# URL (model must handle downloads)
|
149
|
+
converted_content.append({'type': 'image', 'url': image_url})
|
150
|
+
elif part['type'] == 'video_url':
|
151
|
+
video_url = part["video_url"]["url"]
|
152
|
+
if video_url.startswith("data:video"):
|
153
|
+
raise ValueError("Base64 video data is not supported in HF format.")
|
154
|
+
else:
|
155
|
+
# URL (model must handle downloads)
|
156
|
+
converted_content.append({'type': 'video', 'url': video_url})
|
157
|
+
else:
|
158
|
+
raise ValueError(f"Unsupported content type: {part['type']} for conversion.")
|
159
|
+
hf_content = converted_content
|
158
160
|
else:
|
159
|
-
|
160
|
-
|
161
|
-
else:
|
162
|
-
# Text-only content (string)
|
163
|
-
hf_content = content
|
161
|
+
# Text-only content (string)
|
162
|
+
hf_content = content
|
164
163
|
|
165
|
-
|
164
|
+
hf_messages.append({'role': role, 'content': hf_content})
|
166
165
|
|
167
|
-
|
166
|
+
return hf_messages
|
@@ -8,215 +8,211 @@ from clarifai.runners.utils import data_types
|
|
8
8
|
|
9
9
|
|
10
10
|
class Serializer:
|
11
|
+
def serialize(self, data_proto, value):
|
12
|
+
pass
|
11
13
|
|
12
|
-
|
13
|
-
|
14
|
+
def deserialize(self, data_proto):
|
15
|
+
pass
|
14
16
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
def handles_list(self):
|
19
|
-
return False
|
17
|
+
def handles_list(self):
|
18
|
+
return False
|
20
19
|
|
21
20
|
|
22
21
|
def is_repeated_field(field_name):
|
23
|
-
|
24
|
-
|
22
|
+
descriptor = resources_pb2.Data.DESCRIPTOR.fields_by_name.get(field_name)
|
23
|
+
return descriptor and descriptor.label == descriptor.LABEL_REPEATED
|
25
24
|
|
26
25
|
|
27
26
|
class AtomicFieldSerializer(Serializer):
|
27
|
+
def __init__(self, field_name):
|
28
|
+
self.field_name = field_name
|
28
29
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
setattr(data_proto, self.field_name, value)
|
35
|
-
except TypeError as e:
|
36
|
-
raise TypeError(f"Incompatible type for {self.field_name}: {type(value)}") from e
|
30
|
+
def serialize(self, data_proto, value):
|
31
|
+
try:
|
32
|
+
setattr(data_proto, self.field_name, value)
|
33
|
+
except TypeError as e:
|
34
|
+
raise TypeError(f"Incompatible type for {self.field_name}: {type(value)}") from e
|
37
35
|
|
38
|
-
|
39
|
-
|
36
|
+
def deserialize(self, data_proto):
|
37
|
+
return getattr(data_proto, self.field_name)
|
40
38
|
|
41
39
|
|
42
40
|
class MessageSerializer(Serializer):
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
return [self.message_class.from_proto(x) for x in src]
|
41
|
+
def __init__(self, field_name, message_class):
|
42
|
+
self.field_name = field_name
|
43
|
+
self.message_class = message_class
|
44
|
+
self.is_repeated_field = is_repeated_field(field_name)
|
45
|
+
|
46
|
+
def handles_list(self):
|
47
|
+
return self.is_repeated_field
|
48
|
+
|
49
|
+
def serialize(self, data_proto, value):
|
50
|
+
value = self.message_class.from_value(value).to_proto()
|
51
|
+
dst = getattr(data_proto, self.field_name)
|
52
|
+
try:
|
53
|
+
if self.is_repeated_field:
|
54
|
+
dst.add().CopyFrom(value)
|
55
|
+
else:
|
56
|
+
dst.CopyFrom(value)
|
57
|
+
except TypeError as e:
|
58
|
+
raise TypeError(f"Incompatible type for {self.field_name}: {type(value)}") from e
|
59
|
+
|
60
|
+
def serialize_list(self, data_proto, values):
|
61
|
+
assert self.is_repeated_field
|
62
|
+
dst = getattr(data_proto, self.field_name)
|
63
|
+
dst.extend([self.message_class.from_value(value).to_proto() for value in values])
|
64
|
+
|
65
|
+
def deserialize(self, data_proto):
|
66
|
+
src = getattr(data_proto, self.field_name)
|
67
|
+
if self.is_repeated_field:
|
68
|
+
values = [self.message_class.from_proto(x) for x in src]
|
69
|
+
if len(values) == 1:
|
70
|
+
return values[0]
|
71
|
+
return values if values else None
|
72
|
+
else:
|
73
|
+
if not data_proto.HasField(self.field_name):
|
74
|
+
return None
|
75
|
+
return self.message_class.from_proto(src)
|
76
|
+
|
77
|
+
def deserialize_list(self, data_proto):
|
78
|
+
assert self.is_repeated_field
|
79
|
+
src = getattr(data_proto, self.field_name)
|
80
|
+
return [self.message_class.from_proto(x) for x in src]
|
84
81
|
|
85
82
|
|
86
83
|
class NDArraySerializer(Serializer):
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
return array
|
84
|
+
def __init__(self, field_name, as_list=False):
|
85
|
+
self.field_name = field_name
|
86
|
+
self.as_list = as_list
|
87
|
+
|
88
|
+
def serialize(self, data_proto, value):
|
89
|
+
if self.as_list and not isinstance(value, Iterable):
|
90
|
+
raise TypeError(f"Expected list, got {type(value)}")
|
91
|
+
value = np.asarray(value)
|
92
|
+
if not np.issubdtype(value.dtype, np.number):
|
93
|
+
raise TypeError(f"Expected number array, got {value.dtype}")
|
94
|
+
proto = getattr(data_proto, self.field_name)
|
95
|
+
proto.buffer = value.tobytes()
|
96
|
+
proto.shape.extend(value.shape)
|
97
|
+
proto.dtype = str(value.dtype)
|
98
|
+
|
99
|
+
def deserialize(self, data_proto):
|
100
|
+
proto = getattr(data_proto, self.field_name)
|
101
|
+
if not proto.buffer:
|
102
|
+
return None
|
103
|
+
array = np.frombuffer(proto.buffer, dtype=np.dtype(proto.dtype)).reshape(proto.shape)
|
104
|
+
if self.as_list:
|
105
|
+
return array.tolist()
|
106
|
+
return array
|
111
107
|
|
112
108
|
|
113
109
|
class JSONSerializer(Serializer):
|
110
|
+
def __init__(self, field_name, type=None):
|
111
|
+
self.field_name = field_name
|
112
|
+
self.type = type
|
114
113
|
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
try:
|
123
|
-
setattr(data_proto, self.field_name, json.dumps(value))
|
124
|
-
except TypeError as e:
|
125
|
-
raise TypeError(f"Incompatible type for {self.field_name}: {type(value)}") from e
|
114
|
+
def serialize(self, data_proto, value):
|
115
|
+
# if self.type is not None and not isinstance(value, self.type):
|
116
|
+
# raise TypeError(f"Expected {self.type}, got {type(value)}")
|
117
|
+
try:
|
118
|
+
setattr(data_proto, self.field_name, json.dumps(value))
|
119
|
+
except TypeError as e:
|
120
|
+
raise TypeError(f"Incompatible type for {self.field_name}: {type(value)}") from e
|
126
121
|
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
122
|
+
def deserialize(self, data_proto):
|
123
|
+
value = getattr(data_proto, self.field_name)
|
124
|
+
if not value:
|
125
|
+
return None
|
126
|
+
return json.loads(value)
|
132
127
|
|
133
128
|
|
134
129
|
class ListSerializer(Serializer):
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
return [self.inner_serializer.deserialize(part.data) for part in data_proto.parts]
|
130
|
+
def __init__(self, inner_serializer):
|
131
|
+
self.field_name = 'parts'
|
132
|
+
self.inner_serializer = inner_serializer
|
133
|
+
|
134
|
+
def handles_list(self):
|
135
|
+
# if handles_list() is called on this serializer, it means that we're
|
136
|
+
# trying to serialize a list of lists. In this case, we need to use
|
137
|
+
# parts[] for the outer list, so we return False here (we can't inline it).
|
138
|
+
return False
|
139
|
+
|
140
|
+
def serialize(self, data_proto, value):
|
141
|
+
if not isinstance(value, Iterable):
|
142
|
+
raise TypeError(f"Expected iterable, got {type(value)}")
|
143
|
+
if self.inner_serializer.handles_list():
|
144
|
+
self.inner_serializer.serialize_list(data_proto, value)
|
145
|
+
else:
|
146
|
+
for item in value:
|
147
|
+
part = data_proto.parts.add()
|
148
|
+
self.inner_serializer.serialize(part.data, item)
|
149
|
+
|
150
|
+
def deserialize(self, data_proto):
|
151
|
+
if self.inner_serializer.handles_list():
|
152
|
+
return self.inner_serializer.deserialize_list(data_proto)
|
153
|
+
return [self.inner_serializer.deserialize(part.data) for part in data_proto.parts]
|
160
154
|
|
161
155
|
|
162
156
|
class TupleSerializer(Serializer):
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
157
|
+
def __init__(self, inner_serializers):
|
158
|
+
self.field_name = 'parts'
|
159
|
+
self.inner_serializers = inner_serializers
|
160
|
+
|
161
|
+
def serialize(self, data_proto, value):
|
162
|
+
if not isinstance(value, (tuple, list)):
|
163
|
+
raise TypeError(f"Expected tuple, got {type(value)}")
|
164
|
+
if len(value) != len(self.inner_serializers):
|
165
|
+
raise ValueError(
|
166
|
+
f"Expected tuple of length {len(self.inner_serializers)}, got {len(value)}"
|
167
|
+
)
|
168
|
+
for i, (serializer, item) in enumerate(zip(self.inner_serializers, value)):
|
169
|
+
part = data_proto.parts.add()
|
170
|
+
part.id = str(i)
|
171
|
+
serializer.serialize(part.data, item)
|
172
|
+
|
173
|
+
def deserialize(self, data_proto):
|
174
|
+
if not data_proto.parts and self.inner_serializers:
|
175
|
+
return None
|
176
|
+
if len(data_proto.parts) != len(self.inner_serializers):
|
177
|
+
raise ValueError(
|
178
|
+
f"Expected tuple of length {len(self.inner_serializers)}, got {len(data_proto.parts)}"
|
179
|
+
)
|
180
|
+
return tuple(
|
181
|
+
serializer.deserialize(part.data)
|
182
|
+
for serializer, part in zip(self.inner_serializers, data_proto.parts)
|
183
|
+
)
|
187
184
|
|
188
185
|
|
189
186
|
class NamedFieldsSerializer(Serializer):
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
raise TypeError(f"Missing part with key {name}")
|
187
|
+
def __init__(self, named_field_serializers: Dict[str, Serializer]):
|
188
|
+
self.field_name = 'parts'
|
189
|
+
self.named_field_serializers = named_field_serializers
|
190
|
+
|
191
|
+
def serialize(self, data_proto, value):
|
192
|
+
for name, serializer in self.named_field_serializers.items():
|
193
|
+
if name not in value:
|
194
|
+
raise TypeError(f"Missing field {name}")
|
195
|
+
part = self._get_part(data_proto, name, add=True)
|
196
|
+
serializer.serialize(part.data, value[name])
|
197
|
+
|
198
|
+
def deserialize(self, data_proto):
|
199
|
+
if not data_proto.parts and self.named_field_serializers:
|
200
|
+
return None
|
201
|
+
value = data_types.NamedFields()
|
202
|
+
for name, serializer in self.named_field_serializers.items():
|
203
|
+
part = self._get_part(data_proto, name)
|
204
|
+
value[name] = serializer.deserialize(part.data)
|
205
|
+
return value
|
206
|
+
|
207
|
+
def _get_part(self, data_proto, name, add=False):
|
208
|
+
for part in data_proto.parts:
|
209
|
+
if part.id == name:
|
210
|
+
return part
|
211
|
+
if add:
|
212
|
+
part = data_proto.parts.add()
|
213
|
+
part.id = name
|
214
|
+
return part
|
215
|
+
raise TypeError(f"Missing part with key {name}")
|
220
216
|
|
221
217
|
|
222
218
|
# TODO dict serializer, maybe json only?
|