Coverage for mindsdb / integrations / handlers / unify_handler / unify_handler.py: 0%
40 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 Optional, Dict
3import unify
4import pandas as pd
6from mindsdb.integrations.libs.base import BaseMLEngine
8from mindsdb.utilities import log
10from mindsdb.integrations.utilities.handler_utils import get_api_key
13logger = log.getLogger(__name__)
16class UnifyHandler(BaseMLEngine):
17 """
18 Integration with the Unifyai Python Library
19 """
20 name = 'unify'
22 def create(self, target: str, df: Optional[pd.DataFrame] = None, args: Optional[Dict] = None) -> None:
23 if 'using' not in args:
24 raise Exception("Unify engine requires a USING clause! Refer to its documentation for more details.")
26 self.generative = True
27 self.model_storage.json_set('args', args)
29 def predict(self, df: Optional[pd.DataFrame] = None, args: Optional[Dict] = None) -> None:
31 args = self.model_storage.json_get('args')
33 input_keys = list(args.keys())
35 logger.info(f"Input keys: {input_keys}!")
37 if 'model' not in args['using']:
38 raise Exception("Unify requires an model parameter in the USING clause! Refer to its documentation for more details.")
39 model = args['using']['model']
41 if 'provider' not in args['using']:
42 raise Exception("Unify requires a provider parameter in the USING clause! Refer to its documentation for more details.")
43 provider = args['using']['provider']
45 self.endpoint = model + '@' + provider
46 self.api_key = get_api_key('unify', args["using"], self.engine_storage, strict=False)
47 available_endpoints = unify.utils.list_endpoints(api_key=self.api_key)
48 if self.endpoint not in available_endpoints:
49 raise Exception("The model, provider or their combination is not supported by Unify! The supported endpoints are: " + str(available_endpoints))
51 question_column = args['using']['column']
52 if question_column not in df.columns:
53 raise RuntimeError(f'Column "{question_column}" not found in input data')
55 result_df = pd.DataFrame()
56 result_df['predictions'] = df[question_column].apply(self.predict_answer)
57 result_df = result_df.rename(columns={'predictions': args['target']})
59 return result_df
61 def predict_answer(self, text):
63 client = unify.Unify(self.endpoint, api_key=self.api_key)
65 response = client.generate(text)
67 return response