Skip to content

Commit

Permalink
Access All Tables of Given Database Only for SQL Completions (fix) (#…
Browse files Browse the repository at this point in the history
…9223)

* Account for prediction args when looking for API keys

* Add agent params to predict params when getting completions

* Use all tables in given database for usable tables
  • Loading branch information
tmichaeldb committed May 17, 2024
1 parent e62634a commit b8db3b3
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def _get_chat_model_params(self, args: Dict, pred_args: Dict) -> Dict:
model_config.update(pred_args)
# Include API keys.
model_config['api_keys'] = {
p: get_api_key(p, args, self.engine_storage, strict=False) for p in SUPPORTED_PROVIDERS
p: get_api_key(p, model_config, self.engine_storage, strict=False) for p in SUPPORTED_PROVIDERS
}
llm_config = get_llm_config(args.get('provider', self._get_llm_provider(args)), model_config)
config_dict = llm_config.model_dump()
Expand Down
1 change: 1 addition & 0 deletions mindsdb/interfaces/agents/agents_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ def get_completion(
# Underlying handler (e.g. Langchain) will handle default tools like mdb_read, mdb_write, etc.
'tools': tools,
'skills': [s for s in agent.skills],
**(agent.params or {})
}
project_datanode = self.datahub.get(project_name)
return project_datanode.predict(
Expand Down
1 change: 1 addition & 0 deletions mindsdb/interfaces/skills/skill_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def _make_text_to_sql_tools(self, skill: db.Skills, llm) -> dict:
tables_to_include = [f'{database}.{table}' for table in tables]
db = MindsDBSQL(
engine=self.get_command_executor(),
database=database,
metadata=self.get_command_executor().session.integration_controller,
include_tables=tables_to_include
)
Expand Down
7 changes: 4 additions & 3 deletions mindsdb/interfaces/skills/sql_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class SQLAgent:
def __init__(
self,
command_executor,
database: Optional[str] = 'mindsdb',
database: str,
include_tables: Optional[List[str]] = None,
ignore_tables: Optional[List[str]] = None,
sample_rows_in_table_info: int = 3,
Expand All @@ -32,7 +32,7 @@ def __init__(
if not self._tables_to_include:
# ignore_tables and include_tables should not be used together.
# include_tables takes priority if it's set.
self._tables_to_ignore = ignore_tables
self._tables_to_ignore = ignore_tables or []

def _call_engine(self, query: str, database=None):
# switch database
Expand Down Expand Up @@ -67,11 +67,12 @@ def get_usable_table_names(self) -> Iterable[str]:
dbs = [lst[0] for lst in ret.data if lst[0] != 'information_schema']
usable_tables = []
for db in dbs:
if db != 'mindsdb':
if db != 'mindsdb' and db == self._database:
try:
ret = self._call_engine('show tables', database=db)
tables = [lst[0] for lst in ret.data if lst[0] != 'information_schema']
for table in tables:
# By default, include all tables in a database unless expilcitly ignored.
table_name = f'{db}.{table}'
if table_name not in self._tables_to_ignore:
usable_tables.append(table_name)
Expand Down

0 comments on commit b8db3b3

Please sign in to comment.