Coverage for mindsdb / integrations / handlers / twelve_labs_handler / settings.py: 0%
80 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-21 00:36 +0000
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-21 00:36 +0000
1from typing import List, Optional, Any
3from pydantic import BaseModel, model_validator
4from pydantic_settings import BaseSettings
6from mindsdb.integrations.utilities.handlers.validation_utilities import ParameterValidationUtilities
9class TwelveLabsHandlerModel(BaseModel):
10 """
11 Model for the Twelve Labs handler.
13 Attributes
14 ----------
16 index_name : str
17 Name of the index to be created or used.
19 engine_id : str, Optional
20 ID of the engine. If not provided, the default engine is used.
22 api_key : str, Optional
23 API key for the Twelve Labs API. If not provided, attempts will be made to get the API key from the following sources:
24 1. From the engine storage.
25 2. From the environment variable TWELVE_LABS_API_KEY.
26 3. From the config.json file.
28 base_url : str, Optional
29 Base URL for the Twelve Labs API. If not provided, the default URL https://api.twelvelabs.io/v1.2 is used.
31 index_options : List[str]
32 List of that specifies how the platform will process the videos uploaded to this index. This will have no effect if the index already exists.
34 addons : List[str], Optional
35 List of addons that should be enabled for the index. This will have no effect if the index already exists.
37 video_urls : List[str], Optional
38 List of video URLs to be indexed. Either video_urls, video_files, video_urls_column or video_files_column should be provided.
40 video_urls_column : str, Optional
41 Name of the column containing video URLs to be indexed. Either video_urls, video_files, video_urls_column or video_files_column should be provided.
43 video_files : List[str], Optional
44 List of video files to be indexed. Either video_urls, video_files, video_urls_column or video_files_column should be provided.
46 video_files_column : str, Optional
47 Name of the column containing video files to be indexed. Either video_urls, video_files, video_urls_column or video_files_column should be provided.
49 task : str, Optional
50 Task to be performed.
52 search_options : List[str], Optional
53 List of search options to be used for searching. This will only be required if the task is search.
55 search_query_column : str, Optional
56 Name of the column containing the query to be used for searching. This will only be required if the task is search. Each query will be run against the entire index, not individual videos.
58 summarization_type : str, Optional
59 Type of the summary to be generated. This will only be required if the task is summarize. Supported types are 'summary', 'chapter' and 'highlight'.
61 prompt : str, Optional
62 Prompt to be used for the summarize. This will only be required if the type is summary, chapter or highlight.
63 For more information, refer the API reference: https://docs.twelvelabs.io/reference/api-reference
64 """
66 index_name: str
67 engine_id: Optional[str] = None
68 api_key: Optional[str] = None
69 base_url: Optional[str] = None
70 index_options: List[str]
71 addons: List[str] = []
72 video_urls: Optional[List[str]] = None
73 video_urls_column: Optional[str] = None
74 video_files: Optional[List[str]] = None
75 video_files_column: Optional[str] = None
76 task: str = None
77 search_options: Optional[List[str]] = None
78 search_query_column: Optional[str] = None
79 summarization_type: Optional[str] = None
80 prompt: Optional[str] = None
82 class Config:
83 extra = "forbid"
85 @model_validator(mode="before")
86 @classmethod
87 def check_param_typos(cls, values: Any) -> Any:
88 """
89 Root validator to check if there are any typos in the parameters.
91 Parameters
92 ----------
93 values : Dict
94 Dictionary containing the attributes of the model.
96 Raises
97 ------
98 ValueError
99 If there are any typos in the parameters.
100 """
102 ParameterValidationUtilities.validate_parameter_spelling(cls, values)
104 return values
106 @model_validator(mode="before")
107 @classmethod
108 def check_for_valid_task(cls, values: Any) -> Any:
109 """
110 Root validator to check if the task provided is valid.
112 Parameters
113 ----------
114 values : Dict
115 Dictionary containing the attributes of the model.
117 Raises
118 ------
119 ValueError
120 If the task provided is not valid.
121 """
123 task = values.get("task")
125 if task and task not in ["search", "summarization"]:
126 raise ValueError(
127 f"task {task} is not supported. Please provide a valid task."
128 )
130 return values
132 @model_validator(mode="before")
133 @classmethod
134 def check_for_valid_engine_options(cls, values: Any) -> Any:
135 """
136 Root validator to check if the options specified for particular engines are valid.
138 Parameters
139 ----------
140 values : Dict
141 Dictionary containing the attributes of the model.
143 Raises
144 ------
145 ValueError
146 If there are any typos in the parameters.
147 """
149 engine_id = values.get("engine_id")
150 index_options = values.get("index_options")
152 if engine_id and 'pegasus' in engine_id:
153 if not set(index_options).issubset(set(['visual', 'conversation'])):
154 raise ValueError(
155 "index_optios for the Pegasus family of video understanding engines should be one or both of the following engine options: visual and conversation.."
156 )
158 return values
160 @model_validator(mode="before")
161 @classmethod
162 def check_for_video_urls_or_video_files(cls, values: Any) -> Any:
163 """
164 Root validator to check if video_urls or video_files have been provided.
166 Parameters
167 ----------
168 values : Dict
169 Dictionary containing the attributes of the model.
171 Raises
172 ------
173 ValueError
174 If neither video_urls, video_files, video_urls_column nor video_files_column have been provided.
176 """
178 video_urls = values.get("video_urls")
179 video_urls_column = values.get("video_urls_column")
180 video_files = values.get("video_files")
181 video_files_column = values.get("video_files_column")
183 if not video_urls and not video_files and not video_urls_column and not video_files_column:
184 raise ValueError(
185 "Neither video_urls, video_files, video_urls_column nor video_files_column have been provided. Please provide one of them."
186 )
188 return values
190 @model_validator(mode="before")
191 @classmethod
192 def check_for_task_specific_parameters(cls, values: Any) -> Any:
193 """
194 Root validator to check if task has been provided along with the other relevant parameters for each task.
196 Parameters
197 ----------
198 values : Dict
199 Dictionary containing the attributes of the model.
201 Raises
202 ------
203 ValueError
204 If the relevant parameters for the task have not been provided.
205 """
207 task = values.get("task")
209 if task == "search":
210 search_options = values.get("search_options")
211 if not search_options:
212 raise ValueError(
213 "search_options have not been provided. Please provide search_options."
214 )
216 # search options should be a subset of index options
217 index_options = values.get("index_options")
218 if not set(search_options).issubset(set(index_options)):
219 raise ValueError(
220 "search_options should be a subset of index_options."
221 )
223 search_query_column = values.get("search_query_column")
224 if not search_query_column:
225 raise ValueError(
226 "search_query_column has not been provided. Please provide query_column."
227 )
229 elif task == "summarization":
230 summarization_type = values.get("summarization_type")
231 if summarization_type:
232 if summarization_type not in ["summary", "chapter", "highlight"]:
233 raise ValueError(
234 "summarization_type should be one of the following: 'summary', 'chapter' and 'highlight'."
235 )
237 else:
238 raise ValueError(
239 "type has not been provided. Please provide summarization_type."
240 )
242 else:
243 raise ValueError(
244 f"task {task} is not supported. Please provide a valid task."
245 )
247 return values
250class TwelveLabsHandlerConfig(BaseSettings):
251 """
252 Configuration for Twelve Labs handler.
254 Attributes
255 ----------
257 BASE_URL : str
258 Base URL for the Twelve Labs API.
260 DEFAULT_ENGINE : str
261 Default engine for the Twelve Labs API.
263 DEFAULT_WAIT_DURATION : int
264 Default wait duration when polling video indexing tasks created via the Twelve Labs API.
265 """
266 BASE_URL: str = "https://api.twelvelabs.io/v1.2"
267 DEFAULT_ENGINE: str = "marengo2.6"
268 DEFAULT_WAIT_DURATION: int = 5
271twelve_labs_handler_config = TwelveLabsHandlerConfig()