Coverage for mindsdb / interfaces / chatbot / model_executor.py: 16%
48 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
2import datetime as dt
3import pandas as pd
5from mindsdb.interfaces.storage import db
7from .types import BotException
8from .types import ChatBotMessage
11class ModelExecutor:
13 def __init__(self, chat_task, model_name):
14 self.chat_task = chat_task
16 model = chat_task.session.model_controller.get_model(
17 model_name,
18 project_name=chat_task.project_name
19 )
20 model_record = db.Predictor.query.get(model['id'])
21 integration_record = db.Integration.query.get(model_record.integration_id)
23 self.model_info = {
24 'model_name': model_name,
25 'mode': model_record.learn_args['using'].get('mode'),
26 'user_column': model_record.learn_args['using'].get('user_column'),
27 'bot_column': model_record.learn_args['using'].get('assistant_column'),
28 'output': model_record.to_predict[0],
29 'engine': integration_record.engine,
30 }
32 # redefined prompt
33 self.prompt = None
35 def call(self, history: List[ChatBotMessage], functions):
36 model_info = self.model_info
38 if model_info['mode'] != 'conversational':
39 raise BotException('Not supported')
41 messages, user_name = self._chat_history_to_conversation(history, model_info)
42 if model_info['engine'] == 'langchain':
44 all_tools = []
45 for function in functions:
46 all_tools.append({
47 'name': function.name,
48 'func': function.callback,
49 'description': function.description
50 })
52 context_list = [
53 f"- Today's date is {dt.datetime.now().strftime('%Y-%m-%d')}."
54 f" It must be used to understand the input date from string like 'tomorrow', 'today', 'yesterday'"
55 ]
56 context = '\n'.join(context_list)
58 # call model
59 params = {'tools': all_tools, 'context': context, 'prompt': self.prompt}
61 predictions = self.chat_task.project_datanode.predict(
62 model_name=model_info['model_name'],
63 df=pd.DataFrame(messages),
64 params=params
65 )
67 else:
68 predictions = self.chat_task.project_datanode.predict(
69 model_name=model_info['model_name'],
70 df=pd.DataFrame(messages),
71 params={'prompt': self.prompt, 'user_info': {'user_name': user_name}}
72 )
74 output_col = model_info['output']
75 model_output = predictions.iloc[-1][output_col]
76 return model_output
78 def _chat_history_to_conversation(self, history, model_info):
80 bot_username = self.chat_task.bot_params['bot_username']
81 user_name = None
83 question_col = model_info['user_column']
84 answer_col = model_info['bot_column']
86 messages = []
88 for message in history:
89 text = message.text
91 if text is None or text.strip() == '':
92 # skip empty rows
93 continue
95 if message.user != bot_username:
96 user_name = message.user
98 # create new message row
99 messages.append({question_col: text, answer_col: None})
100 else:
101 if len(messages) == 0:
102 # add empty row
103 messages.append({question_col: None, answer_col: None})
105 # update answer in previous column
106 messages[-1][answer_col] = text
107 return messages, user_name