MindsDB 25.4.4.0__py3-none-any.whl → 25.4.5.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.

Potentially problematic release.


This version of MindsDB might be problematic. Click here for more details.

Files changed (37) hide show
  1. mindsdb/__about__.py +1 -1
  2. mindsdb/api/executor/command_executor.py +12 -2
  3. mindsdb/api/executor/datahub/datanodes/mindsdb_tables.py +2 -1
  4. mindsdb/api/executor/planner/query_plan.py +1 -0
  5. mindsdb/api/executor/planner/query_planner.py +5 -0
  6. mindsdb/api/executor/sql_query/sql_query.py +24 -8
  7. mindsdb/api/executor/sql_query/steps/apply_predictor_step.py +20 -3
  8. mindsdb/api/executor/sql_query/steps/fetch_dataframe_partition.py +3 -1
  9. mindsdb/api/http/namespaces/config.py +19 -11
  10. mindsdb/integrations/handlers/openai_handler/helpers.py +3 -5
  11. mindsdb/integrations/handlers/openai_handler/openai_handler.py +20 -8
  12. mindsdb/integrations/handlers/togetherai_handler/__about__.py +9 -0
  13. mindsdb/integrations/handlers/togetherai_handler/__init__.py +20 -0
  14. mindsdb/integrations/handlers/togetherai_handler/creation_args.py +14 -0
  15. mindsdb/integrations/handlers/togetherai_handler/icon.svg +15 -0
  16. mindsdb/integrations/handlers/togetherai_handler/model_using_args.py +5 -0
  17. mindsdb/integrations/handlers/togetherai_handler/requirements.txt +2 -0
  18. mindsdb/integrations/handlers/togetherai_handler/settings.py +33 -0
  19. mindsdb/integrations/handlers/togetherai_handler/togetherai_handler.py +234 -0
  20. mindsdb/integrations/utilities/handler_utils.py +4 -0
  21. mindsdb/integrations/utilities/rag/rerankers/base_reranker.py +360 -0
  22. mindsdb/integrations/utilities/rag/rerankers/reranker_compressor.py +6 -346
  23. mindsdb/interfaces/functions/controller.py +3 -2
  24. mindsdb/interfaces/knowledge_base/controller.py +89 -75
  25. mindsdb/interfaces/query_context/context_controller.py +55 -15
  26. mindsdb/interfaces/query_context/query_task.py +19 -0
  27. mindsdb/interfaces/storage/db.py +2 -2
  28. mindsdb/interfaces/tasks/task_monitor.py +5 -1
  29. mindsdb/interfaces/tasks/task_thread.py +6 -0
  30. mindsdb/migrations/versions/2025-04-22_53502b6d63bf_query_database.py +27 -0
  31. mindsdb/utilities/config.py +12 -1
  32. mindsdb/utilities/context.py +1 -0
  33. {mindsdb-25.4.4.0.dist-info → mindsdb-25.4.5.0.dist-info}/METADATA +229 -226
  34. {mindsdb-25.4.4.0.dist-info → mindsdb-25.4.5.0.dist-info}/RECORD +37 -26
  35. {mindsdb-25.4.4.0.dist-info → mindsdb-25.4.5.0.dist-info}/WHEEL +1 -1
  36. {mindsdb-25.4.4.0.dist-info → mindsdb-25.4.5.0.dist-info}/licenses/LICENSE +0 -0
  37. {mindsdb-25.4.4.0.dist-info → mindsdb-25.4.5.0.dist-info}/top_level.txt +0 -0
@@ -2,343 +2,22 @@ from __future__ import annotations
2
2
 
3
3
  import asyncio
4
4
  import logging
5
- import math
6
- import os
7
- import random
8
- from typing import Any, Dict, List, Optional, Sequence, Tuple
5
+ from typing import Any, Dict, Optional, Sequence
9
6
 
10
7
  from langchain.retrievers.document_compressors.base import BaseDocumentCompressor
11
8
  from langchain_core.callbacks import Callbacks, dispatch_custom_event
12
9
  from langchain_core.documents import Document
13
- from openai import AsyncOpenAI, AsyncAzureOpenAI
14
- from pydantic import field_validator
15
10
 
16
- from mindsdb.integrations.utilities.rag.settings import DEFAULT_RERANKING_MODEL, DEFAULT_LLM_ENDPOINT
11
+ from mindsdb.integrations.utilities.rag.rerankers.base_reranker import BaseLLMReranker
17
12
 
18
13
  log = logging.getLogger(__name__)
19
14
 
20
15
 
21
- class LLMReranker(BaseDocumentCompressor):
22
- filtering_threshold: float = 0.0 # Default threshold for filtering
23
- provider: str = 'openai'
24
- model: str = DEFAULT_RERANKING_MODEL # Model to use for reranking
25
- temperature: float = 0.0 # Temperature for the model
26
- api_key: Optional[str] = None
16
+ class LLMReranker(BaseDocumentCompressor, BaseLLMReranker):
27
17
  remove_irrelevant: bool = True # New flag to control removal of irrelevant documents
28
- base_url: Optional[str] = None
29
- api_version: Optional[str] = None
30
- num_docs_to_keep: Optional[int] = None # How many of the top documents to keep after reranking & compressing.
31
- method: str = "multi-class" # Scoring method: 'multi-class' or 'binary'
32
- _api_key_var: str = "OPENAI_API_KEY"
33
- client: Optional[AsyncOpenAI] = None
34
- _semaphore: Optional[asyncio.Semaphore] = None
35
- max_concurrent_requests: int = 20
36
- max_retries: int = 3
37
- retry_delay: float = 1.0
38
- request_timeout: float = 20.0 # Timeout for API requests
39
- early_stop: bool = True # Whether to enable early stopping
40
- early_stop_threshold: float = 0.8 # Confidence threshold for early stopping
41
18
 
42
- class Config:
43
- arbitrary_types_allowed = True
44
-
45
- @field_validator('provider')
46
- @classmethod
47
- def validate_provider(cls, v: str) -> str:
48
- allowed = {'openai', 'azure_openai'}
49
- v_lower = v.lower()
50
- if v_lower not in allowed:
51
- raise ValueError(f"Unsupported provider: {v}.")
52
- return v_lower
53
-
54
- def __init__(self, **kwargs):
55
- super().__init__(**kwargs)
56
- self._semaphore = asyncio.Semaphore(self.max_concurrent_requests)
57
-
58
- async def _init_client(self):
59
- if self.client is None:
60
-
61
- if self.provider == "azure_openai":
62
-
63
- azure_api_key = self.api_key or os.getenv("AZURE_OPENAI_API_KEY")
64
- azure_api_endpoint = self.base_url or os.environ.get("AZURE_OPENAI_ENDPOINT")
65
- azure_api_version = self.api_version or os.environ.get("AZURE_OPENAI_API_VERSION")
66
- self.client = AsyncAzureOpenAI(api_key=azure_api_key,
67
- azure_endpoint=azure_api_endpoint,
68
- api_version=azure_api_version,
69
- timeout=self.request_timeout,
70
- max_retries=2)
71
- elif self.provider == "openai":
72
- api_key_var: str = "OPENAI_API_KEY"
73
- openai_api_key = self.api_key or os.getenv(api_key_var)
74
- if not openai_api_key:
75
- raise ValueError(f"OpenAI API key not found in environment variable {api_key_var}")
76
-
77
- base_url = self.base_url or DEFAULT_LLM_ENDPOINT
78
- self.client = AsyncOpenAI(api_key=openai_api_key, base_url=base_url, timeout=self.request_timeout, max_retries=2)
79
-
80
- async def search_relevancy(self, query: str, document: str, custom_event: bool = True) -> Any:
81
- await self._init_client()
82
-
83
- async with self._semaphore:
84
- for attempt in range(self.max_retries):
85
- try:
86
- response = await self.client.chat.completions.create(
87
- model=self.model,
88
- messages=[
89
- {"role": "system", "content": "Rate the relevance of the document to the query. Respond with 'yes' or 'no'."},
90
- {"role": "user", "content": f"Query: {query}\nDocument: {document}\nIs this document relevant?"}
91
- ],
92
- temperature=self.temperature,
93
- n=1,
94
- logprobs=True,
95
- max_tokens=1
96
- )
97
-
98
- # Extract response and logprobs
99
- answer = response.choices[0].message.content
100
- logprob = response.choices[0].logprobs.content[0].logprob
101
- rerank_data = {
102
- "document": document,
103
- "answer": answer,
104
- "logprob": logprob
105
- }
106
-
107
- # Stream reranking update.
108
- if custom_event:
109
- dispatch_custom_event("rerank", rerank_data)
110
- return rerank_data
111
-
112
- except Exception as e:
113
- if attempt == self.max_retries - 1:
114
- log.error(f"Failed after {self.max_retries} attempts: {str(e)}")
115
- raise
116
- # Exponential backoff with jitter
117
- retry_delay = self.retry_delay * (2 ** attempt) + random.uniform(0, 0.1)
118
- await asyncio.sleep(retry_delay)
119
-
120
- async def _rank(self, query_document_pairs: List[Tuple[str, str]], custom_event: bool = True) -> List[Tuple[str, float]]:
121
- ranked_results = []
122
-
123
- # Process in larger batches for better throughput
124
- batch_size = min(self.max_concurrent_requests * 2, len(query_document_pairs))
125
- for i in range(0, len(query_document_pairs), batch_size):
126
- batch = query_document_pairs[i:i + batch_size]
127
- try:
128
- results = await asyncio.gather(
129
- *[self.search_relevancy(query=query, document=document, custom_event=custom_event) for (query, document) in batch],
130
- return_exceptions=True
131
- )
132
-
133
- for idx, result in enumerate(results):
134
- if isinstance(result, Exception):
135
- log.error(f"Error processing document {i+idx}: {str(result)}")
136
- ranked_results.append((batch[idx][1], 0.0))
137
- continue
138
-
139
- answer = result["answer"]
140
- logprob = result["logprob"]
141
- prob = math.exp(logprob)
142
-
143
- # Convert answer to score using the model's confidence
144
- if answer.lower().strip() == "yes":
145
- score = prob # If yes, use the model's confidence
146
- elif answer.lower().strip() == "no":
147
- score = 1 - prob # If no, invert the confidence
148
- else:
149
- score = 0.5 * prob # For unclear answers, reduce confidence
150
-
151
- ranked_results.append((batch[idx][1], score))
152
-
153
- # Check if we should stop early
154
- try:
155
- high_scoring_docs = [r for r in ranked_results if r[1] >= self.filtering_threshold]
156
- can_stop_early = (
157
- self.early_stop # Early stopping is enabled
158
- and self.num_docs_to_keep # We have a target number of docs
159
- and len(high_scoring_docs) >= self.num_docs_to_keep # Found enough good docs
160
- and score >= self.early_stop_threshold # Current doc is good enough
161
- )
162
-
163
- if can_stop_early:
164
- log.info(f"Early stopping after finding {self.num_docs_to_keep} documents with high confidence")
165
- return ranked_results
166
- except Exception as e:
167
- # Don't let early stopping errors stop the whole process
168
- log.warning(f"Error in early stopping check: {str(e)}")
169
-
170
- except Exception as e:
171
- log.error(f"Batch processing error: {str(e)}")
172
- continue
173
- return ranked_results
174
-
175
- async def search_relevancy_score(self, query: str, document: str) -> Any:
176
- await self._init_client()
177
-
178
- async with self._semaphore:
179
- for attempt in range(self.max_retries):
180
- try:
181
- response = await self.client.chat.completions.create(
182
- model=self.model,
183
- messages=[
184
- {"role": "system", "content": """
185
- You are an intelligent assistant that evaluates how relevant a given document chunk is to a user's search query.
186
- Your task is to analyze the similarity between the search query and the document chunk, and return **only the class label** that best represents the relevance:
187
-
188
- - "class_1": Not relevant (score between 0.0 and 0.25)
189
- - "class_2": Slightly relevant (score between 0.25 and 0.5)
190
- - "class_3": Moderately relevant (score between 0.5 and 0.75)
191
- - "class_4": Highly relevant (score between 0.75 and 1.0)
192
-
193
- Respond with only one of: "class_1", "class_2", "class_3", or "class_4".
194
-
195
- Examples:
196
-
197
- Search query: "How to reset a router to factory settings?"
198
- Document chunk: "Computers often come with customizable parental control settings."
199
- Score: class_1
200
-
201
- Search query: "Symptoms of vitamin D deficiency"
202
- Document chunk: "Vitamin D deficiency has been linked to fatigue, bone pain, and muscle weakness."
203
- Score: class_4
204
-
205
- Search query: "Best practices for onboarding remote employees"
206
- Document chunk: "An employee handbook can be useful for new hires, outlining company policies and benefits."
207
- Score: class_2
208
-
209
- Search query: "Benefits of mindfulness meditation"
210
- Document chunk: "Practicing mindfulness has shown to reduce stress and improve focus in multiple studies."
211
- Score: class_3
212
-
213
- Search query: "What is Kubernetes used for?"
214
- Document chunk: "Kubernetes is an open-source system for automating deployment, scaling, and management of containerized applications."
215
- Score: class_4
216
-
217
- Search query: "How to bake sourdough bread at home"
218
- Document chunk: "The French Revolution began in 1789 and radically transformed society."
219
- Score: class_1
220
-
221
- Search query: "Machine learning algorithms for image classification"
222
- Document chunk: "Convolutional Neural Networks (CNNs) are particularly effective in image classification tasks."
223
- Score: class_4
224
-
225
- Search query: "How to improve focus while working remotely"
226
- Document chunk: "Creating a dedicated workspace and setting a consistent schedule can significantly improve focus during remote work."
227
- Score: class_4
228
-
229
- Search query: "Carbon emissions from electric vehicles vs gas cars"
230
- Document chunk: "Electric vehicles produce zero emissions while driving, but battery production has environmental impacts."
231
- Score: class_3
232
-
233
- Search query: "Time zones in the United States"
234
- Document chunk: "The U.S. is divided into six primary time zones: Eastern, Central, Mountain, Pacific, Alaska, and Hawaii-Aleutian."
235
- Score: class_4
236
- """},
237
-
238
- {"role": "user", "content": f"""
239
- Now evaluate the following pair:
240
-
241
- Search query: {query}
242
- Document chunk: {document}
243
-
244
- Which class best represents the relevance?
245
- """}
246
- ],
247
- temperature=self.temperature,
248
- n=1,
249
- logprobs=True,
250
- top_logprobs=4,
251
- max_tokens=3
252
- )
253
-
254
- # Extract response and logprobs
255
- class_label = response.choices[0].message.content.strip()
256
- token_logprobs = response.choices[0].logprobs.content
257
- # Reconstruct the prediction and extract the top logprobs from the final token (e.g., "1")
258
- final_token_logprob = token_logprobs[-1]
259
- top_logprobs = final_token_logprob.top_logprobs
260
- # Create a map of 'class_1' -> probability, using token combinations
261
- class_probs = {}
262
- for top_token in top_logprobs:
263
- full_label = f"class_{top_token.token}"
264
- prob = math.exp(top_token.logprob)
265
- class_probs[full_label] = prob
266
- # Optional: normalize in case some are missing
267
- total_prob = sum(class_probs.values())
268
- class_probs = {k: v / total_prob for k, v in class_probs.items()}
269
- # Assign weights to classes
270
- class_weights = {
271
- "class_1": 0.25,
272
- "class_2": 0.5,
273
- "class_3": 0.75,
274
- "class_4": 1.0
275
- }
276
- # Compute the final smooth score
277
- relevance_score = sum(class_weights.get(class_label, 0) * prob for class_label, prob in class_probs.items())
278
- rerank_data = {
279
- "document": document,
280
- "answer": class_label,
281
- "relevance_score": relevance_score
282
- }
283
- return rerank_data
284
-
285
- except Exception as e:
286
- if attempt == self.max_retries - 1:
287
- log.error(f"Failed after {self.max_retries} attempts: {str(e)}")
288
- raise
289
- # Exponential backoff with jitter
290
- retry_delay = self.retry_delay * (2 ** attempt) + random.uniform(0, 0.1)
291
- await asyncio.sleep(retry_delay)
292
-
293
- async def _rank_score(self, query_document_pairs: List[Tuple[str, str]]) -> List[Tuple[str, float]]:
294
- ranked_results = []
295
-
296
- # Process in larger batches for better throughput
297
- batch_size = min(self.max_concurrent_requests * 2, len(query_document_pairs))
298
- for i in range(0, len(query_document_pairs), batch_size):
299
- batch = query_document_pairs[i:i + batch_size]
300
- try:
301
- results = await asyncio.gather(
302
- *[self.search_relevancy_score(query=query, document=document) for (query, document) in batch],
303
- return_exceptions=True
304
- )
305
-
306
- for idx, result in enumerate(results):
307
- if isinstance(result, Exception):
308
- log.error(f"Error processing document {i+idx}: {str(result)}")
309
- ranked_results.append((batch[idx][1], 0.0))
310
- continue
311
-
312
- score = result["relevance_score"]
313
- if score is not None:
314
- if score > 1.0:
315
- score = 1.0
316
- elif score < 0.0:
317
- score = 0.0
318
-
319
- ranked_results.append((batch[idx][1], score))
320
- # Check if we should stop early
321
- try:
322
- high_scoring_docs = [r for r in ranked_results if r[1] >= self.filtering_threshold]
323
- can_stop_early = (
324
- self.early_stop # Early stopping is enabled
325
- and self.num_docs_to_keep # We have a target number of docs
326
- and len(high_scoring_docs) >= self.num_docs_to_keep # Found enough good docs
327
- and score >= self.early_stop_threshold # Current doc is good enough
328
- )
329
-
330
- if can_stop_early:
331
- log.info(f"Early stopping after finding {self.num_docs_to_keep} documents with high confidence")
332
- return ranked_results
333
- except Exception as e:
334
- # Don't let early stopping errors stop the whole process
335
- log.warning(f"Error in early stopping check: {str(e)}")
336
-
337
- except Exception as e:
338
- log.error(f"Batch processing error: {str(e)}")
339
- continue
340
-
341
- return ranked_results
19
+ def _dispatch_rerank_event(self, data):
20
+ dispatch_custom_event("rerank", data)
342
21
 
343
22
  async def acompress_documents(
344
23
  self,
@@ -367,7 +46,7 @@ class LLMReranker(BaseDocumentCompressor):
367
46
  await callbacks.on_text("Starting document reranking...")
368
47
 
369
48
  # Get ranked results
370
- ranked_results = await self._rank(query_document_pairs)
49
+ ranked_results = await self._rank(query_document_pairs, rerank_callback=self._dispatch_rerank_event)
371
50
 
372
51
  # Sort by score in descending order
373
52
  ranked_results.sort(key=lambda x: x[1], reverse=True)
@@ -418,22 +97,3 @@ class LLMReranker(BaseDocumentCompressor):
418
97
  "remove_irrelevant": self.remove_irrelevant,
419
98
  "method": self.method,
420
99
  }
421
-
422
- def get_scores(self, query: str, documents: list[str], custom_event: bool = False):
423
- query_document_pairs = [(query, doc) for doc in documents]
424
- # Create event loop and run async code
425
- import asyncio
426
- try:
427
- loop = asyncio.get_running_loop()
428
- except RuntimeError:
429
- # If no running loop exists, create a new one
430
- loop = asyncio.new_event_loop()
431
- asyncio.set_event_loop(loop)
432
-
433
- if self.method == "multi-class": # default 'multi-class' method
434
- documents_and_scores = loop.run_until_complete(self._rank_score(query_document_pairs))
435
- else:
436
- documents_and_scores = loop.run_until_complete(self._rank(query_document_pairs, custom_event=custom_event))
437
-
438
- scores = [score for _, score in documents_and_scores]
439
- return scores
@@ -3,6 +3,7 @@ import os
3
3
  from duckdb.typing import BIGINT, DOUBLE, VARCHAR, BLOB, BOOLEAN
4
4
  from mindsdb.interfaces.functions.to_markdown import ToMarkdown
5
5
  from mindsdb.interfaces.storage.model_fs import HandlerStorage
6
+ from mindsdb.utilities.config import config
6
7
 
7
8
 
8
9
  def python_to_duckdb_type(py_type):
@@ -164,7 +165,7 @@ class FunctionController(BYOMFunctionsController):
164
165
  return self.callbacks[name]
165
166
 
166
167
  def callback(file_path_or_url, use_llm):
167
- chat_model_params = self._parse_chat_model_params()
168
+ chat_model_params = self._parse_chat_model_params('TO_MARKDOWN_FUNCTION_')
168
169
 
169
170
  llm_client = None
170
171
  llm_model = None
@@ -192,7 +193,7 @@ class FunctionController(BYOMFunctionsController):
192
193
  """
193
194
  Parses the environment variables for chat model parameters.
194
195
  """
195
- chat_model_params = {}
196
+ chat_model_params = config.get("default_llm") or {}
196
197
  for k, v in os.environ.items():
197
198
  if k.startswith(param_prefix):
198
199
  param_name = k[len(param_prefix):]