clarifai 10.0.0__py3-none-any.whl → 10.1.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 (74) hide show
  1. clarifai/client/base.py +8 -1
  2. clarifai/client/dataset.py +77 -21
  3. clarifai/client/input.py +6 -6
  4. clarifai/client/model.py +1 -1
  5. clarifai/client/module.py +1 -1
  6. clarifai/client/workflow.py +1 -1
  7. clarifai/datasets/upload/features.py +3 -0
  8. clarifai/datasets/upload/image.py +57 -26
  9. clarifai/datasets/upload/loaders/xview_detection.py +4 -0
  10. clarifai/datasets/upload/utils.py +23 -7
  11. clarifai/models/model_serving/README.md +113 -121
  12. clarifai/models/model_serving/__init__.py +2 -0
  13. clarifai/models/model_serving/cli/_utils.py +53 -0
  14. clarifai/models/model_serving/cli/base.py +14 -0
  15. clarifai/models/model_serving/cli/build.py +79 -0
  16. clarifai/models/model_serving/cli/clarifai_clis.py +33 -0
  17. clarifai/models/model_serving/cli/create.py +171 -0
  18. clarifai/models/model_serving/cli/example_cli.py +34 -0
  19. clarifai/models/model_serving/cli/login.py +26 -0
  20. clarifai/models/model_serving/cli/upload.py +182 -0
  21. clarifai/models/model_serving/constants.py +20 -0
  22. clarifai/models/model_serving/docs/cli.md +150 -0
  23. clarifai/models/model_serving/docs/concepts.md +229 -0
  24. clarifai/models/model_serving/docs/dependencies.md +1 -1
  25. clarifai/models/model_serving/docs/inference_parameters.md +112 -107
  26. clarifai/models/model_serving/docs/model_types.md +16 -17
  27. clarifai/models/model_serving/model_config/__init__.py +4 -2
  28. clarifai/models/model_serving/model_config/base.py +369 -0
  29. clarifai/models/model_serving/model_config/config.py +219 -224
  30. clarifai/models/model_serving/model_config/inference_parameter.py +5 -0
  31. clarifai/models/model_serving/model_config/model_types_config/multimodal-embedder.yaml +25 -24
  32. clarifai/models/model_serving/model_config/model_types_config/text-classifier.yaml +19 -18
  33. clarifai/models/model_serving/model_config/model_types_config/text-embedder.yaml +20 -18
  34. clarifai/models/model_serving/model_config/model_types_config/text-to-image.yaml +19 -18
  35. clarifai/models/model_serving/model_config/model_types_config/text-to-text.yaml +19 -18
  36. clarifai/models/model_serving/model_config/model_types_config/visual-classifier.yaml +22 -18
  37. clarifai/models/model_serving/model_config/model_types_config/visual-detector.yaml +32 -28
  38. clarifai/models/model_serving/model_config/model_types_config/visual-embedder.yaml +19 -18
  39. clarifai/models/model_serving/model_config/model_types_config/visual-segmenter.yaml +19 -18
  40. clarifai/models/model_serving/{models → model_config}/output.py +8 -0
  41. clarifai/models/model_serving/model_config/triton/__init__.py +14 -0
  42. clarifai/models/model_serving/model_config/{serializer.py → triton/serializer.py} +3 -1
  43. clarifai/models/model_serving/model_config/triton/triton_config.py +182 -0
  44. clarifai/models/model_serving/{models/model_types.py → model_config/triton/wrappers.py} +4 -4
  45. clarifai/models/model_serving/{models → repo_build}/__init__.py +2 -0
  46. clarifai/models/model_serving/repo_build/build.py +198 -0
  47. clarifai/models/model_serving/repo_build/static_files/_requirements.txt +2 -0
  48. clarifai/models/model_serving/repo_build/static_files/base_test.py +169 -0
  49. clarifai/models/model_serving/repo_build/static_files/inference.py +26 -0
  50. clarifai/models/model_serving/repo_build/static_files/sample_clarifai_config.yaml +25 -0
  51. clarifai/models/model_serving/repo_build/static_files/test.py +40 -0
  52. clarifai/models/model_serving/{models/pb_model.py → repo_build/static_files/triton/model.py} +15 -14
  53. clarifai/models/model_serving/utils.py +21 -0
  54. clarifai/rag/rag.py +45 -12
  55. clarifai/rag/utils.py +3 -2
  56. clarifai/utils/logging.py +7 -0
  57. clarifai/versions.py +1 -1
  58. {clarifai-10.0.0.dist-info → clarifai-10.1.0.dist-info}/METADATA +28 -5
  59. clarifai-10.1.0.dist-info/RECORD +114 -0
  60. clarifai-10.1.0.dist-info/entry_points.txt +2 -0
  61. clarifai/models/model_serving/cli/deploy_cli.py +0 -123
  62. clarifai/models/model_serving/cli/model_zip.py +0 -61
  63. clarifai/models/model_serving/cli/repository.py +0 -89
  64. clarifai/models/model_serving/docs/custom_config.md +0 -33
  65. clarifai/models/model_serving/docs/output.md +0 -28
  66. clarifai/models/model_serving/models/default_test.py +0 -281
  67. clarifai/models/model_serving/models/inference.py +0 -50
  68. clarifai/models/model_serving/models/test.py +0 -64
  69. clarifai/models/model_serving/pb_model_repository.py +0 -108
  70. clarifai-10.0.0.dist-info/RECORD +0 -103
  71. clarifai-10.0.0.dist-info/entry_points.txt +0 -4
  72. {clarifai-10.0.0.dist-info → clarifai-10.1.0.dist-info}/LICENSE +0 -0
  73. {clarifai-10.0.0.dist-info → clarifai-10.1.0.dist-info}/WHEEL +0 -0
  74. {clarifai-10.0.0.dist-info → clarifai-10.1.0.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,198 @@
1
+ # Copyright 2023 Clarifai, Inc.
2
+ # Licensed under the Apache License, Version 2.0 (the "License");
3
+ # you may not use this file except in compliance with the License.
4
+ # You may obtain a copy of the License at
5
+ #
6
+ # http://www.apache.org/licenses/LICENSE-2.0
7
+ #
8
+ # Unless required by applicable law or agreed to in writing, software
9
+ # distributed under the License is distributed on an "AS IS" BASIS,
10
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ # See the License for the specific language governing permissions and
12
+ # limitations under the License.
13
+ """
14
+ Triton python backend inference model controller.
15
+ """
16
+
17
+ import inspect
18
+ import os
19
+ import shutil
20
+ import zipfile
21
+ from dataclasses import asdict
22
+ from pathlib import Path
23
+ from typing import Iterable, Literal, Union
24
+
25
+ import yaml
26
+ from tqdm import tqdm
27
+
28
+ from ..constants import BUILT_MODEL_EXT
29
+ from ..model_config import MODEL_TYPES, ModelConfigClass, base, get_model_config, load_user_config
30
+ from ..model_config.base import * # noqa
31
+ from ..model_config.config import parse_config
32
+ from ..model_config.triton.serializer import Serializer
33
+
34
+
35
+ def __parse_type_to_class():
36
+ _t = {}
37
+ _classes = inspect.getmembers(base, inspect.isclass)
38
+ for cls_name, cls_obj in _classes:
39
+ if cls_obj.__base__ is base._BaseClarifaiModel:
40
+ _t.update({cls_obj._config.clarifai_model.type: cls_name})
41
+ return _t
42
+
43
+
44
+ _TYPE_TO_CLASS = __parse_type_to_class()
45
+
46
+
47
+ def _get_static_file_path(relative_path: str):
48
+ curr_dir = os.path.dirname(__file__)
49
+ return os.path.join(curr_dir, "static_files", relative_path)
50
+
51
+
52
+ def _read_static_file(relative_path: str):
53
+ path = _get_static_file_path(relative_path)
54
+ with open(path, "r") as f:
55
+ return f.read()
56
+
57
+
58
+ def copy_folder(src_folder, dest_folder, exclude_items=None):
59
+
60
+ if exclude_items is None:
61
+ exclude_items = set()
62
+
63
+ # Ensure the destination folder exists
64
+ if not os.path.exists(dest_folder):
65
+ os.makedirs(dest_folder)
66
+
67
+ loader = tqdm(os.listdir(src_folder))
68
+ if exclude_items:
69
+ print(f"NOTE: skipping {exclude_items}")
70
+
71
+ for item in loader:
72
+ loader.set_description(f"copying {item}...")
73
+ src_item = os.path.join(src_folder, item)
74
+ dest_item = os.path.join(dest_folder, item)
75
+
76
+ # Skip items in the exclude list
77
+ if item in exclude_items or item.endswith(BUILT_MODEL_EXT):
78
+ continue
79
+
80
+ # Copy files directly
81
+ if os.path.isfile(src_item):
82
+ shutil.copy2(src_item, dest_item)
83
+
84
+ # Copy directories using copytree
85
+ elif os.path.isdir(src_item):
86
+ shutil.copytree(src_item, dest_item, symlinks=False, ignore=None, dirs_exist_ok=True)
87
+
88
+
89
+ def zip_dir(input: Union[Path, str], zip_filename: Union[Path, str]):
90
+ """
91
+ Zip folder without compressing
92
+ """
93
+ # Convert to Path object
94
+ dir = Path(input)
95
+
96
+ with zipfile.ZipFile(zip_filename, "w", zipfile.ZIP_STORED) as zip_file:
97
+ for entry in dir.rglob("*"):
98
+ zip_file.write(entry, entry.relative_to(dir))
99
+
100
+
101
+ class RepositoryBuilder:
102
+
103
+ @staticmethod
104
+ def init_repository(model_type: str, working_dir: str, backend=Literal['triton'], **kwargs):
105
+ assert model_type in MODEL_TYPES
106
+ model_type = model_type
107
+ default_model_type_config: ModelConfigClass = get_model_config(model_type)
108
+
109
+ os.makedirs(working_dir, exist_ok=True)
110
+
111
+ def __write_to(filename, data):
112
+ with open(os.path.join(working_dir, filename), "w") as f:
113
+ f.write(data)
114
+
115
+ # create inference.py
116
+ _filename = "inference.py"
117
+ inference_py = _read_static_file(_filename)
118
+ inference_py = inference_py.replace("InferenceModel()",
119
+ f"InferenceModel({_TYPE_TO_CLASS[model_type]})")
120
+ inference_py = inference_py.replace("predict_docstring",
121
+ eval(_TYPE_TO_CLASS[model_type]).predict.__doc__)
122
+ # create config
123
+ config = asdict(default_model_type_config)
124
+ if backend == "triton":
125
+ max_batch_size = kwargs.get("max_batch_size", None)
126
+ image_shape = kwargs.get("image_shape", None)
127
+ if max_batch_size:
128
+ config['serving_backend']['triton']['max_batch_size'] = max_batch_size
129
+ if image_shape:
130
+ config['serving_backend']['triton']['image_shape'] = image_shape
131
+ config = parse_config(config).dump_to_user_config()
132
+ config_data = yaml.dump(config)
133
+ sample_yaml = _read_static_file("sample_clarifai_config.yaml")
134
+ config_data = sample_yaml + "\n\n" + config_data
135
+ __write_to("clarifai_config.yaml", config_data)
136
+ #
137
+ # create inference.py after checking all configs
138
+ __write_to(_filename, inference_py)
139
+ # create test.py
140
+ __write_to("test.py", _read_static_file("test.py"))
141
+ # create requirements.txt
142
+ __write_to("requirements.txt", _read_static_file("_requirements.txt"))
143
+
144
+ @staticmethod
145
+ def build(working_dir: str, output_dir: str = None, name: str = None, backend=Literal['triton']):
146
+ if not output_dir:
147
+ output_dir = working_dir
148
+ else:
149
+ os.makedirs(output_dir, exist_ok=True)
150
+
151
+ temp_folder = os.path.join(working_dir, ".cache")
152
+ os.makedirs(temp_folder, exist_ok=True)
153
+
154
+ user_config_file = os.path.join(working_dir, "clarifai_config.yaml")
155
+ assert os.path.exists(
156
+ user_config_file
157
+ ), f"FileNotFound: please make sure `clarifai_config.yaml` exists in {working_dir}"
158
+ user_config = load_user_config(user_config_file)
159
+
160
+ if backend == "triton":
161
+ triton_1_ver = os.path.join(temp_folder, "1")
162
+ os.makedirs(triton_1_ver, exist_ok=True)
163
+ # check if labels exists
164
+ for output_config in user_config.serving_backend.triton.output:
165
+ if output_config.label_filename:
166
+ user_labels = user_config.clarifai_model.labels
167
+ assert user_labels, f"Model type `{user_config.clarifai_model.type}` requires labels, "\
168
+ f"but can not found value of `clarifai_model.labels` in {user_config_file}. Please update this attribute to build the model"
169
+ with open(os.path.join(temp_folder, "labels.txt"), "w") as f:
170
+ if not isinstance(user_labels, Iterable):
171
+ user_labels = [user_labels]
172
+ f.write("\n".join([str(lb) for lb in user_labels]) + "\n")
173
+
174
+ # copy model.py
175
+ shutil.copy(_get_static_file_path("triton/model.py"), triton_1_ver)
176
+ # copy requirements.txt
177
+ shutil.copy(os.path.join(working_dir, "requirements.txt"), temp_folder)
178
+ # copy all other files
179
+ copy_folder(
180
+ working_dir, triton_1_ver, exclude_items=["requirements.txt", ".cache", "__pycache__"])
181
+ # generate config.pbtxt
182
+ _config_pbtxt_serializer = Serializer(user_config.serving_backend.triton)
183
+ _config_pbtxt_serializer.to_file(temp_folder)
184
+
185
+ else:
186
+ raise ValueError(f"backend must be ['triton'], got {backend}")
187
+
188
+ clarifai_model_name = name or user_config.clarifai_model.clarifai_model_id or "model"
189
+ clarifai_model_name += BUILT_MODEL_EXT
190
+ clarifai_model_name = os.path.join(output_dir, clarifai_model_name)
191
+
192
+ print(
193
+ "Model building in progress; the duration may vary depending on the size of checkpoints/assets..."
194
+ )
195
+ zip_dir(temp_folder, clarifai_model_name)
196
+ print(f"Finished. Your model is located at {clarifai_model_name}")
197
+
198
+ return clarifai_model_name
@@ -0,0 +1,2 @@
1
+ clarifai>9.10.4
2
+ tritonclient[all]
@@ -0,0 +1,169 @@
1
+ import os
2
+ from copy import deepcopy
3
+ from typing import Dict, Iterable, List, Union
4
+
5
+ import numpy as np
6
+ import yaml
7
+
8
+ from ...constants import IMAGE_TENSOR_NAME, TEXT_TENSOR_NAME
9
+ from ...model_config import (ClassifierOutput, EmbeddingOutput, ImageOutput, InferParam,
10
+ InferParamManager, MasksOutput, ModelTypes, TextOutput,
11
+ VisualDetector, load_user_config)
12
+
13
+ _default_texts = ["Photo of a cat", "A cat is playing around", "Hello, this is test"]
14
+
15
+ _default_images = [
16
+ np.zeros((100, 100, 3), dtype='uint8'), #black
17
+ np.ones((100, 100, 3), dtype='uint8') * 255, #white
18
+ np.random.uniform(0, 255, (100, 100, 3)).astype('uint8') #noise
19
+ ]
20
+
21
+
22
+ def _is_valid_logit(x: np.array):
23
+ return np.all(0 <= x) and np.all(x <= 1)
24
+
25
+
26
+ def _is_non_negative(x: np.array):
27
+ return np.all(x >= 0)
28
+
29
+
30
+ def _is_integer(x):
31
+ return np.all(np.equal(np.mod(x, 1), 0))
32
+
33
+
34
+ class BaseTest:
35
+ init_inference_parameters = {}
36
+
37
+ def __init__(self, init_inference_parameters={}) -> None:
38
+ import sys
39
+ if 'inference' in sys.modules:
40
+ del sys.modules['inference']
41
+ import inference
42
+ from inference import InferenceModel
43
+ self.model = InferenceModel()
44
+ self._base_dir = os.path.dirname(inference.__file__)
45
+ self.cfg_path = os.path.join(self._base_dir, "clarifai_config.yaml")
46
+ self.user_config = load_user_config(self.cfg_path)
47
+ self._user_labels = None
48
+ # check if labels exists
49
+ for output_config in self.user_config.serving_backend.triton.output:
50
+ if output_config.label_filename:
51
+ self._user_labels = self.user_config.clarifai_model.labels
52
+ assert self._user_labels, f"Model type `{self.user_config.clarifai_model.type}` requires labels, "\
53
+ f"but can not found value of `clarifai_model.labels` in {self.cfg_path}. Please update this attribute to build the model"
54
+
55
+ # update init vs user_defined params
56
+ user_defined_infer_params = [
57
+ InferParam(**each) for each in self.user_config.clarifai_model.inference_parameters
58
+ ]
59
+ total_infer_params = []
60
+ if init_inference_parameters:
61
+ self.init_inference_parameters = init_inference_parameters
62
+ for k, v in self.init_inference_parameters.items():
63
+ _exist = False
64
+ for user_param in user_defined_infer_params:
65
+ if user_param.path == k:
66
+ if user_param.default_value != v:
67
+ print(f"Warning: Overwrite parameter `{k}` with default value `{v}`")
68
+ user_param.default_value = v
69
+ _exist = True
70
+ total_infer_params.append(user_param)
71
+ user_defined_infer_params.remove(user_param)
72
+ break
73
+ if not _exist:
74
+ total_infer_params.append(InferParamManager.from_kwargs(**{k: v}).params[0])
75
+
76
+ self.infer_param_manager = InferParamManager(
77
+ params=total_infer_params + user_defined_infer_params)
78
+ self.user_config.clarifai_model.inference_parameters = self.infer_param_manager.get_list_params(
79
+ )
80
+ self._overwrite_cfg()
81
+
82
+ @property
83
+ def user_labels(self):
84
+ return self._user_labels
85
+
86
+ def _overwrite_cfg(self):
87
+ config = yaml.dump(self.user_config.dump_to_user_config(),)
88
+ with open(self.cfg_path, "w") as f:
89
+ f.write(config)
90
+
91
+ def predict(self, input_data: Union[List[np.ndarray], List[str], Dict[str, Union[List[
92
+ np.ndarray], List[str]]]], **inference_parameters) -> Iterable:
93
+ """
94
+ Test Prediction method is exact `InferenceModel.predict` method with
95
+ checking inference paramters.
96
+
97
+ Args:
98
+ -----
99
+ - input_data: A list of input data item to predict on. The type depends on model input type:
100
+ * `image`: List[np.ndarray]
101
+ * `text`: List[str]
102
+ * `multimodal`:
103
+ input_data is list of dict where key is input type name e.i. `image`, `text` and value is list.
104
+ {"image": List[np.ndarray], "text": List[str]}
105
+
106
+ - **inference_parameters: keyword args of your inference parameters.
107
+
108
+ Returns:
109
+ --------
110
+ List of your inference model output type
111
+ """
112
+ infer_params = self.infer_param_manager.validate(**inference_parameters)
113
+ outputs = self.model.predict(input_data=input_data, inference_parameters=infer_params)
114
+ outputs = self._verify_outputs(outputs)
115
+ return outputs
116
+
117
+ def _verify_outputs(self, outputs: List[Union[ClassifierOutput, VisualDetector, EmbeddingOutput,
118
+ TextOutput, ImageOutput, MasksOutput]]):
119
+ """Test output value/dims
120
+
121
+ Args:
122
+ outputs (List[Union[ClassifierOutput, VisualDetector, EmbeddingOutput, TextOutput, ImageOutput, MasksOutput]]): Outputs of `predict` method
123
+ """
124
+ _outputs = deepcopy(outputs)
125
+ _output = _outputs[0]
126
+
127
+ if isinstance(_output, EmbeddingOutput):
128
+ # not test
129
+ pass
130
+ elif isinstance(_output, ClassifierOutput):
131
+ for each in _outputs:
132
+ assert _is_valid_logit(each.predicted_scores), "`predicted_scores` must be in range [0, 1]"
133
+ assert len(each.predicted_scores) == len(
134
+ self.user_labels
135
+ ), f"`predicted_scores` dim must be equal to labels, got {len(each.predicted_scores)} != labels {len(self.user_labels)}"
136
+ elif isinstance(_output, VisualDetector):
137
+ for each in _outputs:
138
+ assert _is_valid_logit(each.predicted_scores), "`predicted_scores` must be in range [0, 1]"
139
+ assert _is_integer(each.predicted_labels), "`predicted_labels` must be integer"
140
+ assert np.all(0 <= each.predicted_labels) and np.all(each.predicted_labels < len(
141
+ self.user_labels)), f"`predicted_labels` must be in [0, {len(self.user_labels) - 1}]"
142
+ assert _is_non_negative(each.predicted_bboxes), "`predicted_bboxes` must be >= 0"
143
+ elif isinstance(_output, MasksOutput):
144
+ for each in _outputs:
145
+ assert np.all(0 <= each.predicted_mask) and np.all(each.predicted_mask < len(
146
+ self.user_labels)), f"`predicted_mask` must be in [0, {len(self.user_labels) - 1}]"
147
+ elif isinstance(_output, TextOutput):
148
+ pass
149
+ elif isinstance(_output, ImageOutput):
150
+ for each in _outputs:
151
+ assert _is_non_negative(each.image), "`image` must be >= 0"
152
+ else:
153
+ pass
154
+
155
+ return outputs
156
+
157
+ def test_with_default_inputs(self):
158
+ model_type = self.user_config.clarifai_model.type
159
+ if model_type == ModelTypes.multimodal_embedder:
160
+ self.predict(input_data=[{IMAGE_TENSOR_NAME: each} for each in _default_images])
161
+ self.predict(input_data=[{TEXT_TENSOR_NAME: each} for each in _default_texts])
162
+ self.predict(input_data=[{
163
+ TEXT_TENSOR_NAME: text,
164
+ IMAGE_TENSOR_NAME: img
165
+ } for text, img in zip(_default_texts, _default_images)])
166
+ elif model_type.startswith("visual"):
167
+ self.predict(input_data=_default_images)
168
+ else:
169
+ self.predict(input_data=_default_texts)
@@ -0,0 +1,26 @@
1
+ # User model inference script.
2
+
3
+ import os
4
+ from pathlib import Path
5
+ from typing import Dict, Union
6
+ from clarifai.models.model_serving.model_config import * # noqa
7
+
8
+
9
+ class InferenceModel():
10
+ """User model inference class."""
11
+
12
+ def __init__(self) -> None:
13
+ """
14
+ Load inference time artifacts that are called frequently .e.g. models, tokenizers, etc.
15
+ in this method so they are loaded only once for faster inference.
16
+ """
17
+ # current directory
18
+ self.base_path: Path = os.path.dirname(__file__)
19
+
20
+ def predict(self,
21
+ input_data: list,
22
+ inference_parameters: Dict[str, Union[bool, str, float, int]] = {}) -> list:
23
+ """predict_docstring
24
+ """
25
+
26
+ raise NotImplementedError()
@@ -0,0 +1,25 @@
1
+ # Sample config of inference_parameters and labels
2
+ # For detail, please refer to docs
3
+ # --------------------
4
+ # inference_parameters:
5
+ # - path: boolean_var
6
+ # default_value: true
7
+ # field_type: 1
8
+ # description: a boolean variable
9
+ # - path: string_var
10
+ # default_value: "a string"
11
+ # field_type: 2
12
+ # description: a string variable
13
+ # - path: number_var
14
+ # default_value: 1
15
+ # field_type: 3
16
+ # description: a number variable
17
+ # - path: secret_string_var
18
+ # default_value: "YOUR_SECRET"
19
+ # field_type: 21
20
+ # description: a string variable contains secret like API key
21
+ # labels:
22
+ # - concept1
23
+ # - concept2
24
+ # - concept3
25
+ # - concept4
@@ -0,0 +1,40 @@
1
+ import unittest
2
+
3
+ from clarifai.models.model_serving.repo_build import BaseTest
4
+
5
+
6
+ class CustomTest(unittest.TestCase):
7
+ """
8
+ BaseTest loads the InferenceModel from the inference.py file in the current working directory.
9
+ To execute the predict method of the InferenceModel, use the predict method in BaseTest.
10
+ It takes the exact same inputs and inference parameters, returning the same outputs as InferenceModel.predict.
11
+ The difference is that BaseTest.predict verifies your_infer_parameters against config.clarifai_models.inference_parameters and checks the output values.
12
+
13
+ For example, test input value of visual-classifier
14
+
15
+ def test_input(self):
16
+ import cv2
17
+ path = "path/to/image"
18
+ img = cv2.imread(path)
19
+ outputs = self.model.predict([img], infer_param1=..., infer_param2=...)
20
+ print(outputs)
21
+ assert outputs
22
+
23
+ """
24
+
25
+ def setUp(self) -> None:
26
+ your_infer_parameter = dict(
27
+ ) # for example dict(float_var=0.12, string_var="test", _secret_string_var="secret")
28
+ self.model = BaseTest(your_infer_parameter)
29
+
30
+ def test_default_cases(self):
31
+ """Test your model with dummy inputs.
32
+ In general, you only need to run this test to check your InferneceModel implementation.
33
+ In case the default inputs makes your model failed for some reason (not because of assert in `test_with_default_inputs`),
34
+ you can comment out this test.
35
+ """
36
+ self.model.test_with_default_inputs()
37
+
38
+ def test_specific_case1(self):
39
+ """ Implement your test case"""
40
+ pass
@@ -19,8 +19,6 @@ try:
19
19
  import triton_python_backend_utils as pb_utils
20
20
  except ModuleNotFoundError:
21
21
  pass
22
- from google.protobuf import text_format
23
- from tritonclient.grpc.model_config_pb2 import ModelConfig
24
22
  from clarifai.models.model_serving.model_config.inference_parameter import parse_req_parameters
25
23
 
26
24
 
@@ -33,18 +31,13 @@ class TritonPythonModel:
33
31
  """
34
32
  Triton server init.
35
33
  """
36
- args["model_repository"] = args["model_repository"].replace("/1/model.py", "")
37
34
  sys.path.append(os.path.dirname(__file__))
38
35
  from inference import InferenceModel
39
36
 
40
37
  self.inference_obj = InferenceModel()
41
38
 
42
39
  # Read input_name from config file
43
- self.config_msg = ModelConfig()
44
- with open(os.path.join(args["model_repository"], "config.pbtxt"), "r") as f:
45
- cfg = f.read()
46
- text_format.Merge(cfg, self.config_msg)
47
- self.input_names = [inp.name for inp in self.config_msg.input]
40
+ self.input_names = [inp.name for inp in self.inference_obj.config.serving_backend.triton.input]
48
41
 
49
42
  def execute(self, requests):
50
43
  """
@@ -53,22 +46,30 @@ class TritonPythonModel:
53
46
  responses = []
54
47
 
55
48
  for request in requests:
56
- parameters = request.parameters()
49
+ try:
50
+ parameters = request.parameters()
51
+ except Exception:
52
+ print(
53
+ "It seems this triton version does not support `parameters()` in request. "
54
+ "Please upgrade tritonserver version otherwise can not use `inference_parameters`. Error message: {e}"
55
+ )
56
+ parameters = None
57
+
57
58
  parameters = parse_req_parameters(parameters) if parameters else {}
58
59
 
59
60
  if len(self.input_names) == 1:
60
61
  in_batch = pb_utils.get_input_tensor_by_name(request, self.input_names[0])
61
62
  in_batch = in_batch.as_numpy()
62
- inference_response = self.inference_obj.get_predictions(in_batch, **parameters)
63
+ data = in_batch
63
64
  else:
64
- multi_in_batch_dict = {}
65
+ data = {}
65
66
  for input_name in self.input_names:
66
67
  in_batch = pb_utils.get_input_tensor_by_name(request, input_name)
67
68
  in_batch = in_batch.as_numpy() if in_batch is not None else []
68
- multi_in_batch_dict.update({input_name: in_batch})
69
-
70
- inference_response = self.inference_obj.get_predictions(multi_in_batch_dict, **parameters)
69
+ data.update({input_name: in_batch})
71
70
 
71
+ inference_response = self.inference_obj._tritonserver_predict(
72
+ input_data=data, inference_parameters=parameters)
72
73
  responses.append(inference_response)
73
74
 
74
75
  return responses
@@ -0,0 +1,21 @@
1
+ import os
2
+
3
+ from .constants import CLARIFAI_PAT_PATH
4
+
5
+
6
+ def _persist_pat(pat: str):
7
+ """ Write down pat to CLARIFAI_PAT_PATH """
8
+ with open(CLARIFAI_PAT_PATH, "w") as f:
9
+ f.write(pat)
10
+
11
+
12
+ def _read_pat():
13
+ if not os.path.exists(CLARIFAI_PAT_PATH):
14
+ return None
15
+ with open(CLARIFAI_PAT_PATH, "r") as f:
16
+ return f.read().replace("\n", "").replace("\r", "").strip()
17
+
18
+
19
+ def login(pat=None):
20
+ """ if pat provided, set pat to CLARIFAI_PAT otherwise read pat from file"""
21
+ os.environ["CLARIFAI_PAT"] = pat or _read_pat()
clarifai/rag/rag.py CHANGED
@@ -17,6 +17,8 @@ from clarifai.rag.utils import (convert_messages_to_str, format_assistant_messag
17
17
  split_document)
18
18
  from clarifai.utils.logging import get_logger
19
19
 
20
+ DEFAULT_RAG_PROMPT_TEMPLATE = "Context information is below:\n{data.hits}\nGiven the context information and not prior knowledge, answer the query.\nQuery: {data.text.raw}\nAnswer: "
21
+
20
22
 
21
23
  class RAG:
22
24
  """
@@ -24,7 +26,8 @@ class RAG:
24
26
 
25
27
  Example:
26
28
  >>> from clarifai.rag import RAG
27
- >>> rag_agent = RAG()
29
+ >>> rag_agent = RAG(workflow_url=YOUR_WORKFLOW_URL)
30
+ >>> rag_agent.chat(messages=[{"role":"human", "content":"What is Clarifai"}])
28
31
  """
29
32
  chat_state_id = None
30
33
 
@@ -49,40 +52,69 @@ class RAG:
49
52
  @classmethod
50
53
  def setup(cls,
51
54
  user_id: str = None,
55
+ app_url: str = None,
52
56
  llm_url: str = "https://clarifai.com/mistralai/completion/models/mistral-7B-Instruct",
53
57
  base_workflow: str = "Text",
54
58
  workflow_yaml_filename: str = 'prompter_wf.yaml',
59
+ workflow_id: str = None,
55
60
  base_url: str = "https://api.clarifai.com",
56
61
  pat: str = None,
57
62
  **kwargs):
58
63
  """Creates an app with `Text` as base workflow, create prompt model, create prompt workflow.
59
64
 
65
+ **kwargs: Additional keyword arguments to be passed to rag-promter model.
66
+ - min_score (float): The minimum score for search hits.
67
+ - max_results (float): The maximum number of search hits.
68
+ - prompt_template (str): The prompt template used. Must contain {data.hits} for the search hits and {data.text.raw} for the query string.
69
+
60
70
  Example:
61
71
  >>> from clarifai.rag import RAG
62
- >>> rag_agent = RAG.setup()
72
+ >>> rag_agent = RAG.setup(user_id=YOUR_USER_ID)
73
+ >>> rag_agent.chat(messages=[{"role":"human", "content":"What is Clarifai"}])
74
+
75
+ Or if you already have an existing app with ingested data:
76
+ >>> rag_agent = RAG.setup(app_url=YOUR_APP_URL)
77
+ >>> rag_agent.chat(messages=[{"role":"human", "content":"What is Clarifai"}])
63
78
  """
64
- user = User(user_id=user_id, base_url=base_url, pat=pat)
79
+
80
+ if user_id and not app_url:
81
+ user = User(user_id=user_id, base_url=base_url, pat=pat)
82
+ ## Create an App
83
+ now_ts = str(int(datetime.now().timestamp()))
84
+ app_id = f"rag_app_{now_ts}"
85
+ app = user.create_app(app_id=app_id, base_workflow=base_workflow)
86
+
87
+ if not user_id and app_url:
88
+ app = App(url=app_url, pat=pat)
89
+
90
+ if user_id and app_url:
91
+ raise UserError("Must provide one of user_id or app_url, not both.")
92
+
93
+ if not user_id and not app_url:
94
+ raise UserError(
95
+ "user_id or app_url must be provided. The user_id can be found at https://clarifai.com/settings."
96
+ )
97
+
65
98
  llm = Model(llm_url)
66
99
 
100
+ min_score = kwargs.get("min_score", 0.95)
101
+ max_results = kwargs.get("max_results", 5)
102
+ prompt_template = kwargs.get("prompt_template", DEFAULT_RAG_PROMPT_TEMPLATE)
67
103
  params = Struct()
68
104
  params.update({
69
- "prompt_template":
70
- "Context information is below:\n{data.hits}\nGiven the context information and not prior knowledge, answer the query.\nQuery: {data.text.raw}\nAnswer: "
105
+ "min_score": min_score,
106
+ "max_results": max_results,
107
+ "prompt_template": prompt_template
71
108
  })
72
109
  prompter_model_params = {"params": params}
73
110
 
74
- ## Create an App
75
- now_ts = str(int(datetime.now().timestamp()))
76
- app_id = f"rag_app_{now_ts}"
77
- app = user.create_app(app_id=app_id, base_workflow=base_workflow)
78
-
79
111
  ## Create rag-prompter model and version
80
112
  prompter_model = app.create_model(
81
113
  model_id=f"rag_prompter_{now_ts}", model_type_id="rag-prompter")
82
114
  prompter_model = prompter_model.create_version(output_info=prompter_model_params)
83
115
 
84
116
  ## Generate a tmp yaml file for workflow creation
85
- workflow_id = f"rag-wf-{now_ts}"
117
+ workflow_id = f"rag-wf-{now_ts}" if workflow_id is None else workflow_id
86
118
  workflow_dict = {
87
119
  "workflow": {
88
120
  "id":
@@ -138,9 +170,10 @@ class RAG:
138
170
 
139
171
  Example:
140
172
  >>> from clarifai.rag import RAG
141
- >>> rag_agent = RAG.setup()
173
+ >>> rag_agent = RAG.setup(user_id=YOUR_USER_ID)
142
174
  >>> rag_agent.upload(folder_path = "~/work/docs")
143
175
  >>> rag_agent.upload(file_path = "~/work/docs/manual.pdf")
176
+ >>> rag_agent.chat(messages=[{"role":"human", "content":"What is Clarifai"}])
144
177
  """
145
178
  #set batch size
146
179
  if batch_size > MAX_UPLOAD_BATCH_SIZE:
clarifai/rag/utils.py CHANGED
@@ -3,8 +3,9 @@ from pathlib import Path
3
3
  from typing import List
4
4
 
5
5
  import requests
6
- from llama_index import Document, SimpleDirectoryReader, download_loader
7
- from llama_index.node_parser.text import SentenceSplitter
6
+ from llama_index.core import Document, SimpleDirectoryReader
7
+ from llama_index.core.node_parser.text import SentenceSplitter
8
+ from llama_index.core.readers.download import download_loader
8
9
  from pypdf import PdfReader
9
10
 
10
11