From 76ad12b61140cbc0997445f994ccfe01a66c7d1a Mon Sep 17 00:00:00 2001 From: andrew Date: Fri, 26 Jul 2024 10:36:37 +0300 Subject: [PATCH 1/6] don't create model automatically --- mindsdb_sdk/agents.py | 40 ++++++++-------------------------------- 1 file changed, 8 insertions(+), 32 deletions(-) diff --git a/mindsdb_sdk/agents.py b/mindsdb_sdk/agents.py index 2bd2ebe..e85a777 100644 --- a/mindsdb_sdk/agents.py +++ b/mindsdb_sdk/agents.py @@ -390,43 +390,14 @@ def _create_ml_engine_if_not_exists(self, name: str = 'langchain'): # Create the engine if it doesn't exist. _ = self.ml_engines.create('langchain', handler='langchain') - def _create_model_if_not_exists(self, name: str, model: Union[Model, dict, str]) -> str: - # Create langchain engine if it doesn't exist. - self._create_ml_engine_if_not_exists() - # Create a default model if it doesn't exist. - default_model_params = { - 'predict': 'answer', - 'engine': 'langchain', - 'prompt_template': 'Answer the user"s question in a helpful way: {{question}}', - # Use GPT-4 by default. - 'provider': 'openai', - 'model_name': 'gpt-4' - } - - if isinstance(model, dict): - default_model_params.update(model) - # Create model with passed in params. - return self.models.create( - f'{name}_default_model', - **default_model_params - ).name - - if model is None: - # Create model with default params. - return _DEFAULT_LLM_MODEL - - if isinstance(model, Model): - return model.name - - return model - def create( self, name: str, model: Union[Model, dict, str] = None, provider: str = None, skills: List[Union[Skill, str]] = None, - params: dict = None) -> Agent: + params: dict = None, + **kwargs) -> Agent: """ Create new agent and return it @@ -449,7 +420,12 @@ def create( _ = self.skills.create(skill.name, skill.type, skill.params) skill_names.append(skill.name) - model = self._create_model_if_not_exists(name, model) + if model is None: + model = _DEFAULT_LLM_MODEL + + if params is None: + params = {} + params.update(kwargs) data = self.api.create_agent(self.project, name, model, provider, skill_names, params) return Agent.from_json(data, self) From 045d992d8a599fef3e14d8f0e24b95017416a7ea Mon Sep 17 00:00:00 2001 From: andrew Date: Fri, 26 Jul 2024 10:37:43 +0300 Subject: [PATCH 2/6] req --- mindsdb_sdk/agents.py | 1 - 1 file changed, 1 deletion(-) diff --git a/mindsdb_sdk/agents.py b/mindsdb_sdk/agents.py index e85a777..bf7c344 100644 --- a/mindsdb_sdk/agents.py +++ b/mindsdb_sdk/agents.py @@ -4,7 +4,6 @@ from uuid import uuid4 import datetime import json -import pandas as pd from mindsdb_sdk.databases import Databases from mindsdb_sdk.knowledge_bases import KnowledgeBase, KnowledgeBases From fd83443ffd412e8b35788f1584fbbb450d33fdff Mon Sep 17 00:00:00 2001 From: andrew Date: Fri, 26 Jul 2024 10:50:26 +0300 Subject: [PATCH 3/6] default prompt template --- mindsdb_sdk/agents.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/mindsdb_sdk/agents.py b/mindsdb_sdk/agents.py index bf7c344..32605c0 100644 --- a/mindsdb_sdk/agents.py +++ b/mindsdb_sdk/agents.py @@ -382,13 +382,6 @@ def add_database(self, name: str, database: str, tables: List[str], description: agent.skills.append(database_sql_skill) self.update(agent.name, agent) - def _create_ml_engine_if_not_exists(self, name: str = 'langchain'): - try: - _ = self.ml_engines.get('langchain') - except Exception: - # Create the engine if it doesn't exist. - _ = self.ml_engines.create('langchain', handler='langchain') - def create( self, name: str, @@ -425,6 +418,8 @@ def create( if params is None: params = {} params.update(kwargs) + if 'prompt_template' not in params: + params['prompt_template'] = 'Answer the user"s question in a helpful way: {{question}}' data = self.api.create_agent(self.project, name, model, provider, skill_names, params) return Agent.from_json(data, self) From 2e994e6026c778dc0e657b3e33a229928444c77c Mon Sep 17 00:00:00 2001 From: andrew Date: Fri, 26 Jul 2024 10:54:46 +0300 Subject: [PATCH 4/6] revert staging --- mindsdb_sdk/agents.py | 50 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 40 insertions(+), 10 deletions(-) diff --git a/mindsdb_sdk/agents.py b/mindsdb_sdk/agents.py index 32605c0..2bd2ebe 100644 --- a/mindsdb_sdk/agents.py +++ b/mindsdb_sdk/agents.py @@ -4,6 +4,7 @@ from uuid import uuid4 import datetime import json +import pandas as pd from mindsdb_sdk.databases import Databases from mindsdb_sdk.knowledge_bases import KnowledgeBase, KnowledgeBases @@ -382,14 +383,50 @@ def add_database(self, name: str, database: str, tables: List[str], description: agent.skills.append(database_sql_skill) self.update(agent.name, agent) + def _create_ml_engine_if_not_exists(self, name: str = 'langchain'): + try: + _ = self.ml_engines.get('langchain') + except Exception: + # Create the engine if it doesn't exist. + _ = self.ml_engines.create('langchain', handler='langchain') + + def _create_model_if_not_exists(self, name: str, model: Union[Model, dict, str]) -> str: + # Create langchain engine if it doesn't exist. + self._create_ml_engine_if_not_exists() + # Create a default model if it doesn't exist. + default_model_params = { + 'predict': 'answer', + 'engine': 'langchain', + 'prompt_template': 'Answer the user"s question in a helpful way: {{question}}', + # Use GPT-4 by default. + 'provider': 'openai', + 'model_name': 'gpt-4' + } + + if isinstance(model, dict): + default_model_params.update(model) + # Create model with passed in params. + return self.models.create( + f'{name}_default_model', + **default_model_params + ).name + + if model is None: + # Create model with default params. + return _DEFAULT_LLM_MODEL + + if isinstance(model, Model): + return model.name + + return model + def create( self, name: str, model: Union[Model, dict, str] = None, provider: str = None, skills: List[Union[Skill, str]] = None, - params: dict = None, - **kwargs) -> Agent: + params: dict = None) -> Agent: """ Create new agent and return it @@ -412,14 +449,7 @@ def create( _ = self.skills.create(skill.name, skill.type, skill.params) skill_names.append(skill.name) - if model is None: - model = _DEFAULT_LLM_MODEL - - if params is None: - params = {} - params.update(kwargs) - if 'prompt_template' not in params: - params['prompt_template'] = 'Answer the user"s question in a helpful way: {{question}}' + model = self._create_model_if_not_exists(name, model) data = self.api.create_agent(self.project, name, model, provider, skill_names, params) return Agent.from_json(data, self) From 67d782746af0b2e249e5756c4af3f3e603c80b36 Mon Sep 17 00:00:00 2001 From: dusvyat Date: Mon, 29 Jul 2024 09:46:32 +0300 Subject: [PATCH 5/6] Add ability to get retrieved contexts --- examples/using_agents_with_retrieval.py | 7 +++++-- mindsdb_sdk/agents.py | 18 +++++++++++++++--- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/examples/using_agents_with_retrieval.py b/examples/using_agents_with_retrieval.py index 043e10c..38ffea2 100644 --- a/examples/using_agents_with_retrieval.py +++ b/examples/using_agents_with_retrieval.py @@ -9,11 +9,14 @@ # Now create an agent that will use the model we just created. agent = con.agents.create(name=f'mindsdb_retrieval_agent_{model_name}_{uuid4().hex}', - model='gpt-4') + model='gpt-4', + params={'return_context': True}) agent.add_file('./data/tokaido-rulebook.pdf', 'rule book for the board game Tokaido') question = "what are the rules for the game takaido?" answer = agent.completion([{'question': question, 'answer': None}]) -print(answer.content) +print(answer.context) +print(answer) + diff --git a/mindsdb_sdk/agents.py b/mindsdb_sdk/agents.py index 2bd2ebe..0a8dc17 100644 --- a/mindsdb_sdk/agents.py +++ b/mindsdb_sdk/agents.py @@ -16,12 +16,21 @@ _DEFAULT_LLM_MODEL = 'gpt-4o' class AgentCompletion: - """Represents a full MindsDB agent completion""" - def __init__(self, content: str): + """ + Represents a full MindsDB agent completion response. + + Attributes: + content: The completion content. + context: Only relevant for retrieval agents. Contains the context retrieved from the knowledge base. + + + """ + def __init__(self, content: str, context: List[dict] = None): self.content = content + self.context = context def __repr__(self): - return self.content + return f'{self.__class__.__name__}(content: {self.content}, context: {self.context})' class Agent: @@ -208,6 +217,9 @@ def completion(self, name: str, messages: List[dict]) -> AgentCompletion: :return: completion from querying the agent """ data = self.api.agent_completion(self.project, name, messages) + if 'context' in data['message']: + return AgentCompletion(data['message']['content'], data['message'].get('context')) + return AgentCompletion(data['message']['content']) def completion_stream(self, name, messages: List[dict]) -> Iterable[object]: From ea32d30f358eb0de2991b78f7149dca31f9d23fa Mon Sep 17 00:00:00 2001 From: Andrey Date: Tue, 30 Jul 2024 18:00:44 +0300 Subject: [PATCH 6/6] bump version --- mindsdb_sdk/__about__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mindsdb_sdk/__about__.py b/mindsdb_sdk/__about__.py index 34e3fe1..08d33a1 100755 --- a/mindsdb_sdk/__about__.py +++ b/mindsdb_sdk/__about__.py @@ -1,6 +1,6 @@ __title__ = 'mindsdb_sdk' __package_name__ = 'mindsdb_sdk' -__version__ = '3.0.1' +__version__ = '3.0.2' __description__ = "MindsDB Python SDK, provides an SDK to use a remote mindsdb instance" __email__ = "jorge@mindsdb.com" __author__ = 'MindsDB Inc'