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.
Files changed (300) hide show
  1. clarifai/__init__.py +1 -1
  2. clarifai/cli/__main__.py +1 -1
  3. clarifai/cli/base.py +144 -136
  4. clarifai/cli/compute_cluster.py +45 -31
  5. clarifai/cli/deployment.py +93 -76
  6. clarifai/cli/model.py +578 -180
  7. clarifai/cli/nodepool.py +100 -82
  8. clarifai/client/__init__.py +12 -2
  9. clarifai/client/app.py +973 -911
  10. clarifai/client/auth/helper.py +345 -342
  11. clarifai/client/auth/register.py +7 -7
  12. clarifai/client/auth/stub.py +107 -106
  13. clarifai/client/base.py +185 -178
  14. clarifai/client/compute_cluster.py +214 -180
  15. clarifai/client/dataset.py +793 -698
  16. clarifai/client/deployment.py +55 -50
  17. clarifai/client/input.py +1223 -1088
  18. clarifai/client/lister.py +47 -45
  19. clarifai/client/model.py +1939 -1717
  20. clarifai/client/model_client.py +525 -502
  21. clarifai/client/module.py +82 -73
  22. clarifai/client/nodepool.py +358 -213
  23. clarifai/client/runner.py +58 -0
  24. clarifai/client/search.py +342 -309
  25. clarifai/client/user.py +419 -414
  26. clarifai/client/workflow.py +294 -274
  27. clarifai/constants/dataset.py +11 -17
  28. clarifai/constants/model.py +8 -2
  29. clarifai/datasets/export/inputs_annotations.py +233 -217
  30. clarifai/datasets/upload/base.py +63 -51
  31. clarifai/datasets/upload/features.py +43 -38
  32. clarifai/datasets/upload/image.py +237 -207
  33. clarifai/datasets/upload/loaders/coco_captions.py +34 -32
  34. clarifai/datasets/upload/loaders/coco_detection.py +72 -65
  35. clarifai/datasets/upload/loaders/imagenet_classification.py +57 -53
  36. clarifai/datasets/upload/loaders/xview_detection.py +274 -132
  37. clarifai/datasets/upload/multimodal.py +55 -46
  38. clarifai/datasets/upload/text.py +55 -47
  39. clarifai/datasets/upload/utils.py +250 -234
  40. clarifai/errors.py +51 -50
  41. clarifai/models/api.py +260 -238
  42. clarifai/modules/css.py +50 -50
  43. clarifai/modules/pages.py +33 -33
  44. clarifai/rag/rag.py +312 -288
  45. clarifai/rag/utils.py +91 -84
  46. clarifai/runners/models/model_builder.py +906 -802
  47. clarifai/runners/models/model_class.py +370 -331
  48. clarifai/runners/models/model_run_locally.py +459 -419
  49. clarifai/runners/models/model_runner.py +170 -162
  50. clarifai/runners/models/model_servicer.py +78 -70
  51. clarifai/runners/server.py +111 -101
  52. clarifai/runners/utils/code_script.py +225 -187
  53. clarifai/runners/utils/const.py +4 -1
  54. clarifai/runners/utils/data_types/__init__.py +12 -0
  55. clarifai/runners/utils/data_types/data_types.py +598 -0
  56. clarifai/runners/utils/data_utils.py +387 -440
  57. clarifai/runners/utils/loader.py +247 -227
  58. clarifai/runners/utils/method_signatures.py +411 -386
  59. clarifai/runners/utils/openai_convertor.py +108 -109
  60. clarifai/runners/utils/serializers.py +175 -179
  61. clarifai/runners/utils/url_fetcher.py +35 -35
  62. clarifai/schema/search.py +56 -63
  63. clarifai/urls/helper.py +125 -102
  64. clarifai/utils/cli.py +129 -123
  65. clarifai/utils/config.py +127 -87
  66. clarifai/utils/constants.py +49 -0
  67. clarifai/utils/evaluation/helpers.py +503 -466
  68. clarifai/utils/evaluation/main.py +431 -393
  69. clarifai/utils/evaluation/testset_annotation_parser.py +154 -144
  70. clarifai/utils/logging.py +324 -306
  71. clarifai/utils/misc.py +60 -56
  72. clarifai/utils/model_train.py +165 -146
  73. clarifai/utils/protobuf.py +126 -103
  74. clarifai/versions.py +3 -1
  75. clarifai/workflows/export.py +48 -50
  76. clarifai/workflows/utils.py +39 -36
  77. clarifai/workflows/validate.py +55 -43
  78. {clarifai-11.3.0rc2.dist-info → clarifai-11.4.0.dist-info}/METADATA +16 -6
  79. clarifai-11.4.0.dist-info/RECORD +109 -0
  80. {clarifai-11.3.0rc2.dist-info → clarifai-11.4.0.dist-info}/WHEEL +1 -1
  81. clarifai/__pycache__/__init__.cpython-310.pyc +0 -0
  82. clarifai/__pycache__/__init__.cpython-311.pyc +0 -0
  83. clarifai/__pycache__/__init__.cpython-39.pyc +0 -0
  84. clarifai/__pycache__/errors.cpython-310.pyc +0 -0
  85. clarifai/__pycache__/errors.cpython-311.pyc +0 -0
  86. clarifai/__pycache__/versions.cpython-310.pyc +0 -0
  87. clarifai/__pycache__/versions.cpython-311.pyc +0 -0
  88. clarifai/cli/__pycache__/__init__.cpython-310.pyc +0 -0
  89. clarifai/cli/__pycache__/__init__.cpython-311.pyc +0 -0
  90. clarifai/cli/__pycache__/base.cpython-310.pyc +0 -0
  91. clarifai/cli/__pycache__/base.cpython-311.pyc +0 -0
  92. clarifai/cli/__pycache__/base_cli.cpython-310.pyc +0 -0
  93. clarifai/cli/__pycache__/compute_cluster.cpython-310.pyc +0 -0
  94. clarifai/cli/__pycache__/compute_cluster.cpython-311.pyc +0 -0
  95. clarifai/cli/__pycache__/deployment.cpython-310.pyc +0 -0
  96. clarifai/cli/__pycache__/deployment.cpython-311.pyc +0 -0
  97. clarifai/cli/__pycache__/model.cpython-310.pyc +0 -0
  98. clarifai/cli/__pycache__/model.cpython-311.pyc +0 -0
  99. clarifai/cli/__pycache__/model_cli.cpython-310.pyc +0 -0
  100. clarifai/cli/__pycache__/nodepool.cpython-310.pyc +0 -0
  101. clarifai/cli/__pycache__/nodepool.cpython-311.pyc +0 -0
  102. clarifai/client/__pycache__/__init__.cpython-310.pyc +0 -0
  103. clarifai/client/__pycache__/__init__.cpython-311.pyc +0 -0
  104. clarifai/client/__pycache__/__init__.cpython-39.pyc +0 -0
  105. clarifai/client/__pycache__/app.cpython-310.pyc +0 -0
  106. clarifai/client/__pycache__/app.cpython-311.pyc +0 -0
  107. clarifai/client/__pycache__/app.cpython-39.pyc +0 -0
  108. clarifai/client/__pycache__/base.cpython-310.pyc +0 -0
  109. clarifai/client/__pycache__/base.cpython-311.pyc +0 -0
  110. clarifai/client/__pycache__/compute_cluster.cpython-310.pyc +0 -0
  111. clarifai/client/__pycache__/compute_cluster.cpython-311.pyc +0 -0
  112. clarifai/client/__pycache__/dataset.cpython-310.pyc +0 -0
  113. clarifai/client/__pycache__/dataset.cpython-311.pyc +0 -0
  114. clarifai/client/__pycache__/deployment.cpython-310.pyc +0 -0
  115. clarifai/client/__pycache__/deployment.cpython-311.pyc +0 -0
  116. clarifai/client/__pycache__/input.cpython-310.pyc +0 -0
  117. clarifai/client/__pycache__/input.cpython-311.pyc +0 -0
  118. clarifai/client/__pycache__/lister.cpython-310.pyc +0 -0
  119. clarifai/client/__pycache__/lister.cpython-311.pyc +0 -0
  120. clarifai/client/__pycache__/model.cpython-310.pyc +0 -0
  121. clarifai/client/__pycache__/model.cpython-311.pyc +0 -0
  122. clarifai/client/__pycache__/module.cpython-310.pyc +0 -0
  123. clarifai/client/__pycache__/module.cpython-311.pyc +0 -0
  124. clarifai/client/__pycache__/nodepool.cpython-310.pyc +0 -0
  125. clarifai/client/__pycache__/nodepool.cpython-311.pyc +0 -0
  126. clarifai/client/__pycache__/search.cpython-310.pyc +0 -0
  127. clarifai/client/__pycache__/search.cpython-311.pyc +0 -0
  128. clarifai/client/__pycache__/user.cpython-310.pyc +0 -0
  129. clarifai/client/__pycache__/user.cpython-311.pyc +0 -0
  130. clarifai/client/__pycache__/workflow.cpython-310.pyc +0 -0
  131. clarifai/client/__pycache__/workflow.cpython-311.pyc +0 -0
  132. clarifai/client/auth/__pycache__/__init__.cpython-310.pyc +0 -0
  133. clarifai/client/auth/__pycache__/__init__.cpython-311.pyc +0 -0
  134. clarifai/client/auth/__pycache__/helper.cpython-310.pyc +0 -0
  135. clarifai/client/auth/__pycache__/helper.cpython-311.pyc +0 -0
  136. clarifai/client/auth/__pycache__/register.cpython-310.pyc +0 -0
  137. clarifai/client/auth/__pycache__/register.cpython-311.pyc +0 -0
  138. clarifai/client/auth/__pycache__/stub.cpython-310.pyc +0 -0
  139. clarifai/client/auth/__pycache__/stub.cpython-311.pyc +0 -0
  140. clarifai/client/cli/__init__.py +0 -0
  141. clarifai/client/cli/__pycache__/__init__.cpython-310.pyc +0 -0
  142. clarifai/client/cli/__pycache__/base_cli.cpython-310.pyc +0 -0
  143. clarifai/client/cli/__pycache__/model_cli.cpython-310.pyc +0 -0
  144. clarifai/client/cli/base_cli.py +0 -88
  145. clarifai/client/cli/model_cli.py +0 -29
  146. clarifai/constants/__pycache__/base.cpython-310.pyc +0 -0
  147. clarifai/constants/__pycache__/base.cpython-311.pyc +0 -0
  148. clarifai/constants/__pycache__/dataset.cpython-310.pyc +0 -0
  149. clarifai/constants/__pycache__/dataset.cpython-311.pyc +0 -0
  150. clarifai/constants/__pycache__/input.cpython-310.pyc +0 -0
  151. clarifai/constants/__pycache__/input.cpython-311.pyc +0 -0
  152. clarifai/constants/__pycache__/model.cpython-310.pyc +0 -0
  153. clarifai/constants/__pycache__/model.cpython-311.pyc +0 -0
  154. clarifai/constants/__pycache__/rag.cpython-310.pyc +0 -0
  155. clarifai/constants/__pycache__/rag.cpython-311.pyc +0 -0
  156. clarifai/constants/__pycache__/search.cpython-310.pyc +0 -0
  157. clarifai/constants/__pycache__/search.cpython-311.pyc +0 -0
  158. clarifai/constants/__pycache__/workflow.cpython-310.pyc +0 -0
  159. clarifai/constants/__pycache__/workflow.cpython-311.pyc +0 -0
  160. clarifai/datasets/__pycache__/__init__.cpython-310.pyc +0 -0
  161. clarifai/datasets/__pycache__/__init__.cpython-311.pyc +0 -0
  162. clarifai/datasets/__pycache__/__init__.cpython-39.pyc +0 -0
  163. clarifai/datasets/export/__pycache__/__init__.cpython-310.pyc +0 -0
  164. clarifai/datasets/export/__pycache__/__init__.cpython-311.pyc +0 -0
  165. clarifai/datasets/export/__pycache__/__init__.cpython-39.pyc +0 -0
  166. clarifai/datasets/export/__pycache__/inputs_annotations.cpython-310.pyc +0 -0
  167. clarifai/datasets/export/__pycache__/inputs_annotations.cpython-311.pyc +0 -0
  168. clarifai/datasets/upload/__pycache__/__init__.cpython-310.pyc +0 -0
  169. clarifai/datasets/upload/__pycache__/__init__.cpython-311.pyc +0 -0
  170. clarifai/datasets/upload/__pycache__/__init__.cpython-39.pyc +0 -0
  171. clarifai/datasets/upload/__pycache__/base.cpython-310.pyc +0 -0
  172. clarifai/datasets/upload/__pycache__/base.cpython-311.pyc +0 -0
  173. clarifai/datasets/upload/__pycache__/features.cpython-310.pyc +0 -0
  174. clarifai/datasets/upload/__pycache__/features.cpython-311.pyc +0 -0
  175. clarifai/datasets/upload/__pycache__/image.cpython-310.pyc +0 -0
  176. clarifai/datasets/upload/__pycache__/image.cpython-311.pyc +0 -0
  177. clarifai/datasets/upload/__pycache__/multimodal.cpython-310.pyc +0 -0
  178. clarifai/datasets/upload/__pycache__/multimodal.cpython-311.pyc +0 -0
  179. clarifai/datasets/upload/__pycache__/text.cpython-310.pyc +0 -0
  180. clarifai/datasets/upload/__pycache__/text.cpython-311.pyc +0 -0
  181. clarifai/datasets/upload/__pycache__/utils.cpython-310.pyc +0 -0
  182. clarifai/datasets/upload/__pycache__/utils.cpython-311.pyc +0 -0
  183. clarifai/datasets/upload/loaders/__pycache__/__init__.cpython-311.pyc +0 -0
  184. clarifai/datasets/upload/loaders/__pycache__/__init__.cpython-39.pyc +0 -0
  185. clarifai/datasets/upload/loaders/__pycache__/coco_detection.cpython-311.pyc +0 -0
  186. clarifai/datasets/upload/loaders/__pycache__/imagenet_classification.cpython-311.pyc +0 -0
  187. clarifai/models/__pycache__/__init__.cpython-39.pyc +0 -0
  188. clarifai/modules/__pycache__/__init__.cpython-39.pyc +0 -0
  189. clarifai/rag/__pycache__/__init__.cpython-310.pyc +0 -0
  190. clarifai/rag/__pycache__/__init__.cpython-311.pyc +0 -0
  191. clarifai/rag/__pycache__/__init__.cpython-39.pyc +0 -0
  192. clarifai/rag/__pycache__/rag.cpython-310.pyc +0 -0
  193. clarifai/rag/__pycache__/rag.cpython-311.pyc +0 -0
  194. clarifai/rag/__pycache__/rag.cpython-39.pyc +0 -0
  195. clarifai/rag/__pycache__/utils.cpython-310.pyc +0 -0
  196. clarifai/rag/__pycache__/utils.cpython-311.pyc +0 -0
  197. clarifai/runners/__pycache__/__init__.cpython-310.pyc +0 -0
  198. clarifai/runners/__pycache__/__init__.cpython-311.pyc +0 -0
  199. clarifai/runners/__pycache__/__init__.cpython-39.pyc +0 -0
  200. clarifai/runners/dockerfile_template/Dockerfile.cpu.template +0 -31
  201. clarifai/runners/dockerfile_template/Dockerfile.cuda.template +0 -42
  202. clarifai/runners/dockerfile_template/Dockerfile.nim +0 -71
  203. clarifai/runners/models/__pycache__/__init__.cpython-310.pyc +0 -0
  204. clarifai/runners/models/__pycache__/__init__.cpython-311.pyc +0 -0
  205. clarifai/runners/models/__pycache__/__init__.cpython-39.pyc +0 -0
  206. clarifai/runners/models/__pycache__/base_typed_model.cpython-310.pyc +0 -0
  207. clarifai/runners/models/__pycache__/base_typed_model.cpython-311.pyc +0 -0
  208. clarifai/runners/models/__pycache__/base_typed_model.cpython-39.pyc +0 -0
  209. clarifai/runners/models/__pycache__/model_builder.cpython-311.pyc +0 -0
  210. clarifai/runners/models/__pycache__/model_class.cpython-310.pyc +0 -0
  211. clarifai/runners/models/__pycache__/model_class.cpython-311.pyc +0 -0
  212. clarifai/runners/models/__pycache__/model_run_locally.cpython-310-pytest-7.1.2.pyc +0 -0
  213. clarifai/runners/models/__pycache__/model_run_locally.cpython-310.pyc +0 -0
  214. clarifai/runners/models/__pycache__/model_run_locally.cpython-311.pyc +0 -0
  215. clarifai/runners/models/__pycache__/model_runner.cpython-310.pyc +0 -0
  216. clarifai/runners/models/__pycache__/model_runner.cpython-311.pyc +0 -0
  217. clarifai/runners/models/__pycache__/model_upload.cpython-310.pyc +0 -0
  218. clarifai/runners/models/base_typed_model.py +0 -238
  219. clarifai/runners/models/model_class_refract.py +0 -80
  220. clarifai/runners/models/model_upload.py +0 -607
  221. clarifai/runners/models/temp.py +0 -25
  222. clarifai/runners/utils/__pycache__/__init__.cpython-310.pyc +0 -0
  223. clarifai/runners/utils/__pycache__/__init__.cpython-311.pyc +0 -0
  224. clarifai/runners/utils/__pycache__/__init__.cpython-38.pyc +0 -0
  225. clarifai/runners/utils/__pycache__/__init__.cpython-39.pyc +0 -0
  226. clarifai/runners/utils/__pycache__/buffered_stream.cpython-310.pyc +0 -0
  227. clarifai/runners/utils/__pycache__/buffered_stream.cpython-38.pyc +0 -0
  228. clarifai/runners/utils/__pycache__/buffered_stream.cpython-39.pyc +0 -0
  229. clarifai/runners/utils/__pycache__/const.cpython-310.pyc +0 -0
  230. clarifai/runners/utils/__pycache__/const.cpython-311.pyc +0 -0
  231. clarifai/runners/utils/__pycache__/constants.cpython-310.pyc +0 -0
  232. clarifai/runners/utils/__pycache__/constants.cpython-38.pyc +0 -0
  233. clarifai/runners/utils/__pycache__/constants.cpython-39.pyc +0 -0
  234. clarifai/runners/utils/__pycache__/data_handler.cpython-310.pyc +0 -0
  235. clarifai/runners/utils/__pycache__/data_handler.cpython-311.pyc +0 -0
  236. clarifai/runners/utils/__pycache__/data_handler.cpython-38.pyc +0 -0
  237. clarifai/runners/utils/__pycache__/data_handler.cpython-39.pyc +0 -0
  238. clarifai/runners/utils/__pycache__/data_utils.cpython-310.pyc +0 -0
  239. clarifai/runners/utils/__pycache__/data_utils.cpython-311.pyc +0 -0
  240. clarifai/runners/utils/__pycache__/data_utils.cpython-38.pyc +0 -0
  241. clarifai/runners/utils/__pycache__/data_utils.cpython-39.pyc +0 -0
  242. clarifai/runners/utils/__pycache__/grpc_server.cpython-310.pyc +0 -0
  243. clarifai/runners/utils/__pycache__/grpc_server.cpython-38.pyc +0 -0
  244. clarifai/runners/utils/__pycache__/grpc_server.cpython-39.pyc +0 -0
  245. clarifai/runners/utils/__pycache__/health.cpython-310.pyc +0 -0
  246. clarifai/runners/utils/__pycache__/health.cpython-38.pyc +0 -0
  247. clarifai/runners/utils/__pycache__/health.cpython-39.pyc +0 -0
  248. clarifai/runners/utils/__pycache__/loader.cpython-310.pyc +0 -0
  249. clarifai/runners/utils/__pycache__/loader.cpython-311.pyc +0 -0
  250. clarifai/runners/utils/__pycache__/logging.cpython-310.pyc +0 -0
  251. clarifai/runners/utils/__pycache__/logging.cpython-38.pyc +0 -0
  252. clarifai/runners/utils/__pycache__/logging.cpython-39.pyc +0 -0
  253. clarifai/runners/utils/__pycache__/stream_source.cpython-310.pyc +0 -0
  254. clarifai/runners/utils/__pycache__/stream_source.cpython-39.pyc +0 -0
  255. clarifai/runners/utils/__pycache__/url_fetcher.cpython-310.pyc +0 -0
  256. clarifai/runners/utils/__pycache__/url_fetcher.cpython-311.pyc +0 -0
  257. clarifai/runners/utils/__pycache__/url_fetcher.cpython-38.pyc +0 -0
  258. clarifai/runners/utils/__pycache__/url_fetcher.cpython-39.pyc +0 -0
  259. clarifai/runners/utils/data_handler.py +0 -231
  260. clarifai/runners/utils/data_handler_refract.py +0 -213
  261. clarifai/runners/utils/data_types.py +0 -469
  262. clarifai/runners/utils/logger.py +0 -0
  263. clarifai/runners/utils/openai_format.py +0 -87
  264. clarifai/schema/__pycache__/search.cpython-310.pyc +0 -0
  265. clarifai/schema/__pycache__/search.cpython-311.pyc +0 -0
  266. clarifai/urls/__pycache__/helper.cpython-310.pyc +0 -0
  267. clarifai/urls/__pycache__/helper.cpython-311.pyc +0 -0
  268. clarifai/utils/__pycache__/__init__.cpython-310.pyc +0 -0
  269. clarifai/utils/__pycache__/__init__.cpython-311.pyc +0 -0
  270. clarifai/utils/__pycache__/__init__.cpython-39.pyc +0 -0
  271. clarifai/utils/__pycache__/cli.cpython-310.pyc +0 -0
  272. clarifai/utils/__pycache__/cli.cpython-311.pyc +0 -0
  273. clarifai/utils/__pycache__/config.cpython-311.pyc +0 -0
  274. clarifai/utils/__pycache__/constants.cpython-310.pyc +0 -0
  275. clarifai/utils/__pycache__/constants.cpython-311.pyc +0 -0
  276. clarifai/utils/__pycache__/logging.cpython-310.pyc +0 -0
  277. clarifai/utils/__pycache__/logging.cpython-311.pyc +0 -0
  278. clarifai/utils/__pycache__/misc.cpython-310.pyc +0 -0
  279. clarifai/utils/__pycache__/misc.cpython-311.pyc +0 -0
  280. clarifai/utils/__pycache__/model_train.cpython-310.pyc +0 -0
  281. clarifai/utils/__pycache__/model_train.cpython-311.pyc +0 -0
  282. clarifai/utils/__pycache__/protobuf.cpython-311.pyc +0 -0
  283. clarifai/utils/evaluation/__pycache__/__init__.cpython-311.pyc +0 -0
  284. clarifai/utils/evaluation/__pycache__/__init__.cpython-39.pyc +0 -0
  285. clarifai/utils/evaluation/__pycache__/helpers.cpython-311.pyc +0 -0
  286. clarifai/utils/evaluation/__pycache__/main.cpython-311.pyc +0 -0
  287. clarifai/utils/evaluation/__pycache__/main.cpython-39.pyc +0 -0
  288. clarifai/workflows/__pycache__/__init__.cpython-310.pyc +0 -0
  289. clarifai/workflows/__pycache__/__init__.cpython-311.pyc +0 -0
  290. clarifai/workflows/__pycache__/__init__.cpython-39.pyc +0 -0
  291. clarifai/workflows/__pycache__/export.cpython-310.pyc +0 -0
  292. clarifai/workflows/__pycache__/export.cpython-311.pyc +0 -0
  293. clarifai/workflows/__pycache__/utils.cpython-310.pyc +0 -0
  294. clarifai/workflows/__pycache__/utils.cpython-311.pyc +0 -0
  295. clarifai/workflows/__pycache__/validate.cpython-310.pyc +0 -0
  296. clarifai/workflows/__pycache__/validate.cpython-311.pyc +0 -0
  297. clarifai-11.3.0rc2.dist-info/RECORD +0 -322
  298. {clarifai-11.3.0rc2.dist-info → clarifai-11.4.0.dist-info}/entry_points.txt +0 -0
  299. {clarifai-11.3.0rc2.dist-info → clarifai-11.4.0.dist-info/licenses}/LICENSE +0 -0
  300. {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
- return f"chatcmpl-{uuid.uuid4().hex}"
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
- if id is None:
19
- id = generate_id()
20
- if created is None:
21
- created = int(time.time())
22
-
23
- response = {
24
- "id":
25
- id,
26
- "object":
27
- "chat.completion",
28
- "created":
29
- created,
30
- "model":
31
- model,
32
- "choices": [{
33
- "index": 0,
34
- "message": {
35
- "role": "assistant",
36
- "content": generated_text,
37
- },
38
- "finish_reason": finish_reason,
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
- return response
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
- if id is None:
61
- id = generate_id()
62
- if created is None:
63
- created = int(time.time())
64
-
65
- for chunk in generated_chunks:
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
- id,
69
- "object":
70
- "chat.completion.chunk",
71
- "created":
72
- created,
73
- "model":
74
- model,
75
- "choices": [{
76
- "index": 0,
77
- "delta": {
78
- "content": chunk,
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
- if stream:
111
- return _format_streaming_response(generated_text, model, id, created, finish_reason)
112
- else:
113
- return _format_non_streaming_response(generated_text, model, id, created, prompt_tokens,
114
- completion_tokens, finish_reason)
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
- hf_messages = []
131
- for msg in openai_messages:
132
- role = msg['role']
133
- content = msg['content']
134
-
135
- if isinstance(content, list):
136
- # Handle multimodal content (e.g., text + images)
137
- converted_content = []
138
- for part in content:
139
- if part['type'] == 'text':
140
- converted_content.append({'type': 'text', 'text': part['text']})
141
- elif part['type'] == 'image_url':
142
- # Handle image (extract base64 or URL)
143
- image_url = part["image_url"]["url"]
144
- if image_url.startswith("data:image"):
145
- # Base64-encoded image
146
- b64_img = image_url.split(",")[1]
147
- converted_content.append({'type': 'image', 'base64': b64_img})
148
- else:
149
- # URL (model must handle downloads)
150
- converted_content.append({'type': 'image', 'url': image_url})
151
- elif part['type'] == 'video_url':
152
- video_url = part["video_url"]["url"]
153
- if video_url.startswith("data:video"):
154
- ValueError("Base64 video data is not supported in HF format.")
155
- else:
156
- # URL (model must handle downloads)
157
- converted_content.append({'type': 'video', 'url': video_url})
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
- raise ValueError(f"Unsupported content type: {part['type']} for conversion.")
160
- hf_content = converted_content
161
- else:
162
- # Text-only content (string)
163
- hf_content = content
161
+ # Text-only content (string)
162
+ hf_content = content
164
163
 
165
- hf_messages.append({'role': role, 'content': hf_content})
164
+ hf_messages.append({'role': role, 'content': hf_content})
166
165
 
167
- return hf_messages
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
- def serialize(self, data_proto, value):
13
- pass
14
+ def deserialize(self, data_proto):
15
+ pass
14
16
 
15
- def deserialize(self, data_proto):
16
- pass
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
- descriptor = resources_pb2.Data.DESCRIPTOR.fields_by_name.get(field_name)
24
- return descriptor and descriptor.label == descriptor.LABEL_REPEATED
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
- def __init__(self, field_name):
30
- self.field_name = field_name
31
-
32
- def serialize(self, data_proto, value):
33
- try:
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
- def deserialize(self, data_proto):
39
- return getattr(data_proto, self.field_name)
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
- def __init__(self, field_name, message_class):
45
- self.field_name = field_name
46
- self.message_class = message_class
47
- self.is_repeated_field = is_repeated_field(field_name)
48
-
49
- def handles_list(self):
50
- return self.is_repeated_field
51
-
52
- def serialize(self, data_proto, value):
53
- value = self.message_class.from_value(value).to_proto()
54
- dst = getattr(data_proto, self.field_name)
55
- try:
56
- if self.is_repeated_field:
57
- dst.add().CopyFrom(value)
58
- else:
59
- dst.CopyFrom(value)
60
- except TypeError as e:
61
- raise TypeError(f"Incompatible type for {self.field_name}: {type(value)}") from e
62
-
63
- def serialize_list(self, data_proto, values):
64
- assert self.is_repeated_field
65
- dst = getattr(data_proto, self.field_name)
66
- dst.extend([self.message_class.from_value(value).to_proto() for value in values])
67
-
68
- def deserialize(self, data_proto):
69
- src = getattr(data_proto, self.field_name)
70
- if self.is_repeated_field:
71
- values = [self.message_class.from_proto(x) for x in src]
72
- if len(values) == 1:
73
- return values[0]
74
- return values if values else None
75
- else:
76
- if not data_proto.HasField(self.field_name):
77
- return None
78
- return self.message_class.from_proto(src)
79
-
80
- def deserialize_list(self, data_proto):
81
- assert self.is_repeated_field
82
- src = getattr(data_proto, self.field_name)
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
- def __init__(self, field_name, as_list=False):
89
- self.field_name = field_name
90
- self.as_list = as_list
91
-
92
- def serialize(self, data_proto, value):
93
- if self.as_list and not isinstance(value, Iterable):
94
- raise TypeError(f"Expected list, got {type(value)}")
95
- value = np.asarray(value)
96
- if not np.issubdtype(value.dtype, np.number):
97
- raise TypeError(f"Expected number array, got {value.dtype}")
98
- proto = getattr(data_proto, self.field_name)
99
- proto.buffer = value.tobytes()
100
- proto.shape.extend(value.shape)
101
- proto.dtype = str(value.dtype)
102
-
103
- def deserialize(self, data_proto):
104
- proto = getattr(data_proto, self.field_name)
105
- if not proto.buffer:
106
- return None
107
- array = np.frombuffer(proto.buffer, dtype=np.dtype(proto.dtype)).reshape(proto.shape)
108
- if self.as_list:
109
- return array.tolist()
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
- def __init__(self, field_name, type=None):
116
- self.field_name = field_name
117
- self.type = type
118
-
119
- def serialize(self, data_proto, value):
120
- #if self.type is not None and not isinstance(value, self.type):
121
- # raise TypeError(f"Expected {self.type}, got {type(value)}")
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
- def deserialize(self, data_proto):
128
- value = getattr(data_proto, self.field_name)
129
- if not value:
130
- return None
131
- return json.loads(value)
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
- def __init__(self, inner_serializer):
137
- self.field_name = 'parts'
138
- self.inner_serializer = inner_serializer
139
-
140
- def handles_list(self):
141
- # if handles_list() is called on this serializer, it means that we're
142
- # trying to serialize a list of lists. In this case, we need to use
143
- # parts[] for the outer list, so we return False here (we can't inline it).
144
- return False
145
-
146
- def serialize(self, data_proto, value):
147
- if not isinstance(value, Iterable):
148
- raise TypeError(f"Expected iterable, got {type(value)}")
149
- if self.inner_serializer.handles_list():
150
- self.inner_serializer.serialize_list(data_proto, value)
151
- else:
152
- for item in value:
153
- part = data_proto.parts.add()
154
- self.inner_serializer.serialize(part.data, item)
155
-
156
- def deserialize(self, data_proto):
157
- if self.inner_serializer.handles_list():
158
- return self.inner_serializer.deserialize_list(data_proto)
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
- def __init__(self, inner_serializers):
165
- self.field_name = 'parts'
166
- self.inner_serializers = inner_serializers
167
-
168
- def serialize(self, data_proto, value):
169
- if not isinstance(value, (tuple, list)):
170
- raise TypeError(f"Expected tuple, got {type(value)}")
171
- if len(value) != len(self.inner_serializers):
172
- raise ValueError(f"Expected tuple of length {len(self.inner_serializers)}, got {len(value)}")
173
- for i, (serializer, item) in enumerate(zip(self.inner_serializers, value)):
174
- part = data_proto.parts.add()
175
- part.id = str(i)
176
- serializer.serialize(part.data, item)
177
-
178
- def deserialize(self, data_proto):
179
- if not data_proto.parts and self.inner_serializers:
180
- return None
181
- if len(data_proto.parts) != len(self.inner_serializers):
182
- raise ValueError(
183
- f"Expected tuple of length {len(self.inner_serializers)}, got {len(data_proto.parts)}")
184
- return tuple(
185
- serializer.deserialize(part.data)
186
- for serializer, part in zip(self.inner_serializers, data_proto.parts))
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
- def __init__(self, named_field_serializers: Dict[str, Serializer]):
192
- self.field_name = 'parts'
193
- self.named_field_serializers = named_field_serializers
194
-
195
- def serialize(self, data_proto, value):
196
- for name, serializer in self.named_field_serializers.items():
197
- if name not in value:
198
- raise TypeError(f"Missing field {name}")
199
- part = self._get_part(data_proto, name, add=True)
200
- serializer.serialize(part.data, value[name])
201
-
202
- def deserialize(self, data_proto):
203
- if not data_proto.parts and self.named_field_serializers:
204
- return None
205
- value = data_types.NamedFields()
206
- for name, serializer in self.named_field_serializers.items():
207
- part = self._get_part(data_proto, name)
208
- value[name] = serializer.deserialize(part.data)
209
- return value
210
-
211
- def _get_part(self, data_proto, name, add=False):
212
- for part in data_proto.parts:
213
- if part.id == name:
214
- return part
215
- if add:
216
- part = data_proto.parts.add()
217
- part.id = name
218
- return part
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?