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

1from typing import List, Optional, Any 

2 

3from pydantic import BaseModel, model_validator 

4from pydantic_settings import BaseSettings 

5 

6from mindsdb.integrations.utilities.handlers.validation_utilities import ParameterValidationUtilities 

7 

8 

9class TwelveLabsHandlerModel(BaseModel): 

10 """ 

11 Model for the Twelve Labs handler. 

12 

13 Attributes 

14 ---------- 

15 

16 index_name : str 

17 Name of the index to be created or used. 

18 

19 engine_id : str, Optional 

20 ID of the engine. If not provided, the default engine is used. 

21 

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. 

27 

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. 

30 

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. 

33 

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. 

36 

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. 

39 

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. 

42 

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. 

45 

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. 

48 

49 task : str, Optional 

50 Task to be performed. 

51 

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. 

54 

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. 

57 

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'. 

60 

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 """ 

65 

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 

81 

82 class Config: 

83 extra = "forbid" 

84 

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. 

90 

91 Parameters 

92 ---------- 

93 values : Dict 

94 Dictionary containing the attributes of the model. 

95 

96 Raises 

97 ------ 

98 ValueError 

99 If there are any typos in the parameters. 

100 """ 

101 

102 ParameterValidationUtilities.validate_parameter_spelling(cls, values) 

103 

104 return values 

105 

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. 

111 

112 Parameters 

113 ---------- 

114 values : Dict 

115 Dictionary containing the attributes of the model. 

116 

117 Raises 

118 ------ 

119 ValueError 

120 If the task provided is not valid. 

121 """ 

122 

123 task = values.get("task") 

124 

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 ) 

129 

130 return values 

131 

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. 

137 

138 Parameters 

139 ---------- 

140 values : Dict 

141 Dictionary containing the attributes of the model. 

142 

143 Raises 

144 ------ 

145 ValueError 

146 If there are any typos in the parameters. 

147 """ 

148 

149 engine_id = values.get("engine_id") 

150 index_options = values.get("index_options") 

151 

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 ) 

157 

158 return values 

159 

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. 

165 

166 Parameters 

167 ---------- 

168 values : Dict 

169 Dictionary containing the attributes of the model. 

170 

171 Raises 

172 ------ 

173 ValueError 

174 If neither video_urls, video_files, video_urls_column nor video_files_column have been provided. 

175 

176 """ 

177 

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") 

182 

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 ) 

187 

188 return values 

189 

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. 

195 

196 Parameters 

197 ---------- 

198 values : Dict 

199 Dictionary containing the attributes of the model. 

200 

201 Raises 

202 ------ 

203 ValueError 

204 If the relevant parameters for the task have not been provided. 

205 """ 

206 

207 task = values.get("task") 

208 

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 ) 

215 

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 ) 

222 

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 ) 

228 

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 ) 

236 

237 else: 

238 raise ValueError( 

239 "type has not been provided. Please provide summarization_type." 

240 ) 

241 

242 else: 

243 raise ValueError( 

244 f"task {task} is not supported. Please provide a valid task." 

245 ) 

246 

247 return values 

248 

249 

250class TwelveLabsHandlerConfig(BaseSettings): 

251 """ 

252 Configuration for Twelve Labs handler. 

253 

254 Attributes 

255 ---------- 

256 

257 BASE_URL : str 

258 Base URL for the Twelve Labs API. 

259 

260 DEFAULT_ENGINE : str 

261 Default engine for the Twelve Labs API. 

262 

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 

269 

270 

271twelve_labs_handler_config = TwelveLabsHandlerConfig()