From ec289e869ec7ffa257d7fdb2d90fa10c6dc112de Mon Sep 17 00:00:00 2001 From: Parthiv Makwana <75653580+parthiv11@users.noreply.github.com> Date: Fri, 13 Dec 2024 09:22:49 +0000 Subject: [PATCH 1/7] added chatbot --- mindsdb_sdk/chatbot.py | 167 +++++++++++++++++++++++++++++ mindsdb_sdk/connectors/rest_api.py | 116 ++++++++++++++++++++ 2 files changed, 283 insertions(+) create mode 100644 mindsdb_sdk/chatbot.py diff --git a/mindsdb_sdk/chatbot.py b/mindsdb_sdk/chatbot.py new file mode 100644 index 0000000..aa7f8ab --- /dev/null +++ b/mindsdb_sdk/chatbot.py @@ -0,0 +1,167 @@ +import json +from typing import Union, List + +from mindsdb_sdk.utils.context import is_saving +from mindsdb_sdk.utils.objects_collection import CollectionBase +from .query import Query +from .databases import Database + +class Chatbot: + """ + Chatbot object, used to manage or query chatbots. + + Create and interact with chatbots: + + >>> chatbot = project.chatbots.create('my_chatbot', model_name='gpt-4') + >>> response = chatbot.ask('Hello! How are you?') + + """ + + def __init__(self, api, project, data: dict): + self.api = api + self.project = project + self.name = data['name'] + self.database_name = data.get('database_name') + self.agent_name = data.get('agent_name') + + + + def __repr__(self): + return f"{self.__class__.__name__}({self.project.name}.{self.name})" + + def ask(self, query: str, **options): + """ + Ask the chatbot a question or send a message. + + >>> response = chatbot.ask('What is the weather today?') + + :param query: The input query or message for the chatbot. + :param options: Additional options to customize the query. + :return: Chatbot response. + """ + payload = { + 'query': query, + **options + } + return self.api.chatbot_interaction(self.project.name, self.name, payload) + + def update(self, model_name: str = None, database_name: str = None): + """ + Update chatbot properties. + + >>> chatbot.update(model_name='gpt-4', database_name='slack_db') + + :param model_name: New model to use for the chatbot. + :param database_name: New database connection name. + """ + payload = {} + if model_name: + payload['agent_name'] = model_name + if database_name: + payload['database_name'] = database_name + + self.api.update_chatbot( + self.project.name, + self.name, + data=payload + ) + + def delete(self): + """ + Delete the chatbot. + + >>> chatbot.delete() + + """ + self.api.delete_chatbot(self.project.name, self.name) + + +class Chatbots(CollectionBase): + """ + Chatbots + + Manage chatbots within a project. + + List chatbots: + + >>> chatbots = project.chatbots.list() + + Get chatbot by name: + + >>> chatbot = project.chatbots.get('my_chatbot') + + Create a chatbot: + + >>> chatbot = project.chatbots.create('my_chatbot', model_name='gpt-4') + + Delete a chatbot: + + >>> project.chatbots.drop('my_chatbot') + + """ + + def __init__(self, project, api): + self.project = project + self.api = api + + def list(self) -> List[Chatbot]: + """ + Get the list of chatbots in the project. + + >>> chatbots = project.chatbots.list() + + :return: List of chatbot objects. + """ + return [ + Chatbot(self.api, self.project, item) + for item in self.api.list_chatbots(self.project.name) + ] + + def get(self, name: str) -> Chatbot: + """ + Get a chatbot by name. + + >>> chatbot = project.chatbots.get('my_chatbot') + + :param name: Name of the chatbot. + :return: Chatbot object. + """ + data = self.api.get_chatbot(self.project.name, name) + return Chatbot(self.api, self.project, data) + + def create(self, name: str, model_name: str, database_name: str = None) -> Chatbot: + """ + Create a new chatbot. + + >>> chatbot = project.chatbots.create( + ... 'my_chatbot', + ... model_name='gpt-4', + ... database_name='slack_db' + ... ) + + :param name: Name of the chatbot. + :param model_name: Name of the model or agent. + :param database_name: Connection name for chat applications (e.g., Slack, Teams). + :return: Created Chatbot object. + """ + payload = { + 'name': name, + 'agent_name': model_name + } + + if database_name: + payload['database_name'] = database_name + + self.api.create_chatbot(self.project.name, data=payload) + + return self.get(name) + + def drop(self, name: str): + """ + Delete a chatbot by name. + + >>> project.chatbots.drop('my_chatbot') + + :param name: Name of the chatbot to delete. + """ + self.api.delete_chatbot(self.project.name, name) diff --git a/mindsdb_sdk/connectors/rest_api.py b/mindsdb_sdk/connectors/rest_api.py index 3f82cd5..d9fb664 100644 --- a/mindsdb_sdk/connectors/rest_api.py +++ b/mindsdb_sdk/connectors/rest_api.py @@ -453,3 +453,119 @@ def knowledge_base_completion(self, project: str, knowledge_base_name, payload): _raise_for_status(r) return r.json() + @_try_relogin + def list_chatbots(self, project: str): + """ + Retrieve the list of chatbots in a project. + :param project: Name of the project. + :return: List of chatbots as a JSON response. + """ + url = f'{self.url}/api/projects/{project}/chatbots' + r = self.session.get(url) + _raise_for_status(r) + return r.json() + + @_try_relogin + def get_chatbot(self, project: str, chatbot_name: str): + """ + Retrieve details of a specific chatbot. + :param project: Name of the project. + :param chatbot_name: Name of the chatbot. + :return: Chatbot details as a JSON response. + """ + url = f'{self.url}/api/projects/{project}/chatbots/{chatbot_name}' + r = self.session.get(url) + _raise_for_status(r) + return r.json() + + @_try_relogin + def create_chatbot(self, project: str, chatbot_name: str, params: dict): + """ + Create a new chatbot. + :param project: Name of the project. + :param chatbot_name: Name of the chatbot to create. + :param params: Configuration parameters for the chatbot. + :return: Details of the created chatbot as a JSON response. + """ + url = f'{self.url}/api/projects/{project}/chatbots' + r = self.session.post(url, json={ + 'chatbot': { + 'name': chatbot_name, + 'params': params + } + }) + _raise_for_status(r) + return r.json() + + @_try_relogin + def update_chatbot(self, project: str, chatbot_name: str, params: dict): + """ + Update an existing chatbot's configuration. + :param project: Name of the project. + :param chatbot_name: Name of the chatbot to update. + :param params: Updated configuration parameters. + :return: Details of the updated chatbot as a JSON response. + """ + url = f'{self.url}/api/projects/{project}/chatbots/{chatbot_name}' + r = self.session.put(url, json={ + 'chatbot': { + 'params': params + } + }) + _raise_for_status(r) + return r.json() + + @_try_relogin + def delete_chatbot(self, project: str, chatbot_name: str): + """ + Delete a chatbot. + :param project: Name of the project. + :param chatbot_name: Name of the chatbot to delete. + :return: None + """ + url = f'{self.url}/api/projects/{project}/chatbots/{chatbot_name}' + r = self.session.delete(url) + _raise_for_status(r) + + @_try_relogin + def send_message(self, project: str, chatbot_name: str, message: str, context: dict = None): + """ + Send a message to a chatbot and retrieve the response. + :param project: Name of the project. + :param chatbot_name: Name of the chatbot. + :param message: User message to send. + :param context: Optional context for the chatbot. + :return: Chatbot's response as a JSON response. + """ + url = f'{self.url}/api/projects/{project}/chatbots/{chatbot_name}/messages' + payload = { + 'message': message + } + if context: + payload['context'] = context + + r = self.session.post(url, json=payload) + _raise_for_status(r) + return r.json() + + @_try_relogin + def stream_chatbot_response(self, project: str, chatbot_name: str, message: str, context: dict = None): + """ + Send a message to a chatbot and stream its response. + :param project: Name of the project. + :param chatbot_name: Name of the chatbot. + :param message: User message to send. + :param context: Optional context for the chatbot. + :return: A generator yielding streamed chatbot responses. + """ + url = f'{self.url}/api/projects/{project}/chatbots/{chatbot_name}/messages/stream' + payload = { + 'message': message + } + if context: + payload['context'] = context + + stream = self.session.post(url, json=payload, stream=True) + client = SSEClient(stream) + for chunk in client.events(): + yield json.loads(chunk.data) \ No newline at end of file From 2c78c02d40746391f096d09917c563e4d5326934 Mon Sep 17 00:00:00 2001 From: parthiv11 Date: Sat, 14 Dec 2024 12:33:55 +0000 Subject: [PATCH 2/7] update rest --- mindsdb_sdk/connectors/rest_api.py | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/mindsdb_sdk/connectors/rest_api.py b/mindsdb_sdk/connectors/rest_api.py index d9fb664..8d4e743 100644 --- a/mindsdb_sdk/connectors/rest_api.py +++ b/mindsdb_sdk/connectors/rest_api.py @@ -479,26 +479,23 @@ def get_chatbot(self, project: str, chatbot_name: str): return r.json() @_try_relogin - def create_chatbot(self, project: str, chatbot_name: str, params: dict): + def create_chatbot(self, project: str, data: dict): """ Create a new chatbot. :param project: Name of the project. - :param chatbot_name: Name of the chatbot to create. - :param params: Configuration parameters for the chatbot. + :param data: Configuration parameters for the chatbot. :return: Details of the created chatbot as a JSON response. """ url = f'{self.url}/api/projects/{project}/chatbots' r = self.session.post(url, json={ - 'chatbot': { - 'name': chatbot_name, - 'params': params - } + 'chatbot': data + }) _raise_for_status(r) return r.json() @_try_relogin - def update_chatbot(self, project: str, chatbot_name: str, params: dict): + def update_chatbot(self, project: str, chatbot_name: str, data: dict): """ Update an existing chatbot's configuration. :param project: Name of the project. @@ -508,9 +505,7 @@ def update_chatbot(self, project: str, chatbot_name: str, params: dict): """ url = f'{self.url}/api/projects/{project}/chatbots/{chatbot_name}' r = self.session.put(url, json={ - 'chatbot': { - 'params': params - } + 'chatbot': data }) _raise_for_status(r) return r.json() From f8fe81f6197d39fcf36a6c71ca0b40843c725504 Mon Sep 17 00:00:00 2001 From: parthiv11 Date: Sat, 14 Dec 2024 12:34:49 +0000 Subject: [PATCH 3/7] chatbots updated --- mindsdb_sdk/chatbot.py | 60 +++++++++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 21 deletions(-) diff --git a/mindsdb_sdk/chatbot.py b/mindsdb_sdk/chatbot.py index aa7f8ab..7757033 100644 --- a/mindsdb_sdk/chatbot.py +++ b/mindsdb_sdk/chatbot.py @@ -1,10 +1,6 @@ -import json -from typing import Union, List - -from mindsdb_sdk.utils.context import is_saving +from typing import List from mindsdb_sdk.utils.objects_collection import CollectionBase -from .query import Query -from .databases import Database + class Chatbot: """ @@ -20,11 +16,10 @@ class Chatbot: def __init__(self, api, project, data: dict): self.api = api self.project = project - self.name = data['name'] - self.database_name = data.get('database_name') - self.agent_name = data.get('agent_name') - - + self.name = data.get('name') + self.database_name = data.get('database') + self.agent_name = data.get('agent') + self.model_name = data.get('model_name') def __repr__(self): return f"{self.__class__.__name__}({self.project.name}.{self.name})" @@ -45,27 +40,47 @@ def ask(self, query: str, **options): } return self.api.chatbot_interaction(self.project.name, self.name, payload) - def update(self, model_name: str = None, database_name: str = None): + def update(self, name: str = None, agent_name: str = None, model_name: str = None, database_name: str = None, inplace: bool = False): """ Update chatbot properties. >>> chatbot.update(model_name='gpt-4', database_name='slack_db') + :param name: New name for the chatbot. :param model_name: New model to use for the chatbot. :param database_name: New database connection name. + :param inplace: If True, updates the current object in-place. + :return: Updated Chatbot object or None if inplace is True. """ payload = {} - if model_name: - payload['agent_name'] = model_name + + if name: + payload['name'] = name + if database_name: payload['database_name'] = database_name - self.api.update_chatbot( - self.project.name, - self.name, + if agent_name: + payload['agent_name'] = agent_name + + if model_name: + payload['model_name'] = model_name + + updated_chatbot = self.api.update_chatbot( + project=self.project.name, + chatbot_name=self.name, data=payload ) + if inplace: + self.name = updated_chatbot.get('name', self.name) + self.database_name = updated_chatbot.get('database', self.database_name) + self.agent_name = updated_chatbot.get('agent', self.agent_name) + self.model_name = updated_chatbot.get('model_name', self.model_name) + return None + + return Chatbot(self.api, self.project, updated_chatbot) + def delete(self): """ Delete the chatbot. @@ -129,7 +144,7 @@ def get(self, name: str) -> Chatbot: data = self.api.get_chatbot(self.project.name, name) return Chatbot(self.api, self.project, data) - def create(self, name: str, model_name: str, database_name: str = None) -> Chatbot: + def create(self, name: str, agent_name: str = None, model_name: str = None, database_name: str = None) -> Chatbot: """ Create a new chatbot. @@ -146,11 +161,14 @@ def create(self, name: str, model_name: str, database_name: str = None) -> Chatb """ payload = { 'name': name, - 'agent_name': model_name + 'database_name': database_name } - if database_name: - payload['database_name'] = database_name + if agent_name: + payload['agent_name'] = agent_name + + if model_name: + payload['model_name'] = model_name self.api.create_chatbot(self.project.name, data=payload) From f55ddea6869b9b322a3a09436701aed8d4bfad1c Mon Sep 17 00:00:00 2001 From: parthiv11 Date: Sat, 14 Dec 2024 12:35:02 +0000 Subject: [PATCH 4/7] update --- mindsdb_sdk/projects.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mindsdb_sdk/projects.py b/mindsdb_sdk/projects.py index 98aa47d..4b69b2c 100644 --- a/mindsdb_sdk/projects.py +++ b/mindsdb_sdk/projects.py @@ -13,6 +13,7 @@ from .views import Views from .jobs import Jobs from .knowledge_bases import KnowledgeBases +from .chatbot import Chatbots class Project: @@ -79,6 +80,7 @@ def __init__(self, server, api, name): self.skills = Skills(self, api) self.agents = Agents(self, api) + self.chatbots = Chatbots(self, api) def __repr__(self): return f'{self.__class__.__name__}({self.name})' From acfe1bef893e41cf84f20c4d6c9c75d8aeff6bbd Mon Sep 17 00:00:00 2001 From: parthiv11 Date: Sat, 14 Dec 2024 12:58:29 +0000 Subject: [PATCH 5/7] done --- mindsdb_sdk/chatbot.py | 23 ++++----------- mindsdb_sdk/connectors/rest_api.py | 45 +----------------------------- 2 files changed, 6 insertions(+), 62 deletions(-) diff --git a/mindsdb_sdk/chatbot.py b/mindsdb_sdk/chatbot.py index 7757033..5baecc9 100644 --- a/mindsdb_sdk/chatbot.py +++ b/mindsdb_sdk/chatbot.py @@ -20,26 +20,11 @@ def __init__(self, api, project, data: dict): self.database_name = data.get('database') self.agent_name = data.get('agent') self.model_name = data.get('model_name') + self.is_running = data.get('is_running') def __repr__(self): return f"{self.__class__.__name__}({self.project.name}.{self.name})" - def ask(self, query: str, **options): - """ - Ask the chatbot a question or send a message. - - >>> response = chatbot.ask('What is the weather today?') - - :param query: The input query or message for the chatbot. - :param options: Additional options to customize the query. - :return: Chatbot response. - """ - payload = { - 'query': query, - **options - } - return self.api.chatbot_interaction(self.project.name, self.name, payload) - def update(self, name: str = None, agent_name: str = None, model_name: str = None, database_name: str = None, inplace: bool = False): """ Update chatbot properties. @@ -144,7 +129,7 @@ def get(self, name: str) -> Chatbot: data = self.api.get_chatbot(self.project.name, name) return Chatbot(self.api, self.project, data) - def create(self, name: str, agent_name: str = None, model_name: str = None, database_name: str = None) -> Chatbot: + def create(self, name: str, agent_name: str = None, model_name: str = None, database_name: str = None, is_running: bool = False) -> Chatbot: """ Create a new chatbot. @@ -152,6 +137,7 @@ def create(self, name: str, agent_name: str = None, model_name: str = None, data ... 'my_chatbot', ... model_name='gpt-4', ... database_name='slack_db' + ... ) :param name: Name of the chatbot. @@ -161,7 +147,8 @@ def create(self, name: str, agent_name: str = None, model_name: str = None, data """ payload = { 'name': name, - 'database_name': database_name + 'database_name': database_name, + 'is_running':is_running } if agent_name: diff --git a/mindsdb_sdk/connectors/rest_api.py b/mindsdb_sdk/connectors/rest_api.py index 8d4e743..74bcd91 100644 --- a/mindsdb_sdk/connectors/rest_api.py +++ b/mindsdb_sdk/connectors/rest_api.py @@ -520,47 +520,4 @@ def delete_chatbot(self, project: str, chatbot_name: str): """ url = f'{self.url}/api/projects/{project}/chatbots/{chatbot_name}' r = self.session.delete(url) - _raise_for_status(r) - - @_try_relogin - def send_message(self, project: str, chatbot_name: str, message: str, context: dict = None): - """ - Send a message to a chatbot and retrieve the response. - :param project: Name of the project. - :param chatbot_name: Name of the chatbot. - :param message: User message to send. - :param context: Optional context for the chatbot. - :return: Chatbot's response as a JSON response. - """ - url = f'{self.url}/api/projects/{project}/chatbots/{chatbot_name}/messages' - payload = { - 'message': message - } - if context: - payload['context'] = context - - r = self.session.post(url, json=payload) - _raise_for_status(r) - return r.json() - - @_try_relogin - def stream_chatbot_response(self, project: str, chatbot_name: str, message: str, context: dict = None): - """ - Send a message to a chatbot and stream its response. - :param project: Name of the project. - :param chatbot_name: Name of the chatbot. - :param message: User message to send. - :param context: Optional context for the chatbot. - :return: A generator yielding streamed chatbot responses. - """ - url = f'{self.url}/api/projects/{project}/chatbots/{chatbot_name}/messages/stream' - payload = { - 'message': message - } - if context: - payload['context'] = context - - stream = self.session.post(url, json=payload, stream=True) - client = SSEClient(stream) - for chunk in client.events(): - yield json.loads(chunk.data) \ No newline at end of file + _raise_for_status(r) \ No newline at end of file From b00d2476eaa11a45b3d198b779a44394dc0c8c2d Mon Sep 17 00:00:00 2001 From: parthiv11 Date: Sat, 14 Dec 2024 13:11:53 +0000 Subject: [PATCH 6/7] updates doc string --- mindsdb_sdk/chatbot.py | 110 +++++++++++++++++++++-------------------- 1 file changed, 56 insertions(+), 54 deletions(-) diff --git a/mindsdb_sdk/chatbot.py b/mindsdb_sdk/chatbot.py index 5baecc9..d68f54b 100644 --- a/mindsdb_sdk/chatbot.py +++ b/mindsdb_sdk/chatbot.py @@ -4,13 +4,7 @@ class Chatbot: """ - Chatbot object, used to manage or query chatbots. - - Create and interact with chatbots: - - >>> chatbot = project.chatbots.create('my_chatbot', model_name='gpt-4') - >>> response = chatbot.ask('Hello! How are you?') - + Represents a chatbot that can be managed within a project. """ def __init__(self, api, project, data: dict): @@ -27,15 +21,17 @@ def __repr__(self): def update(self, name: str = None, agent_name: str = None, model_name: str = None, database_name: str = None, inplace: bool = False): """ - Update chatbot properties. + Updates the chatbot's properties. - >>> chatbot.update(model_name='gpt-4', database_name='slack_db') + Example usage: + >>> chatbot.update(model_name='gpt-4', database_name='slack_db') - :param name: New name for the chatbot. - :param model_name: New model to use for the chatbot. - :param database_name: New database connection name. - :param inplace: If True, updates the current object in-place. - :return: Updated Chatbot object or None if inplace is True. + :param name: (Optional) New name for the chatbot. + :param agent_name: (Optional) New agent name to associate with the chatbot. + :param model_name: (Optional) New model to use for the chatbot. + :param database_name: (Optional) New database connection name. + :param inplace: If True, updates the current object in-place; otherwise, returns a new Chatbot object. + :return: Updated Chatbot object, or None if inplace is True. """ payload = {} @@ -68,36 +64,37 @@ def update(self, name: str = None, agent_name: str = None, model_name: str = Non def delete(self): """ - Delete the chatbot. - - >>> chatbot.delete() + Deletes the chatbot from the project. + Example usage: + >>> chatbot.delete() """ self.api.delete_chatbot(self.project.name, self.name) class Chatbots(CollectionBase): """ - Chatbots - - Manage chatbots within a project. - - List chatbots: - - >>> chatbots = project.chatbots.list() - - Get chatbot by name: + Manages chatbots within a project. - >>> chatbot = project.chatbots.get('my_chatbot') + Provides methods to list, retrieve, create, and delete chatbots. - Create a chatbot: + Example usage: - >>> chatbot = project.chatbots.create('my_chatbot', model_name='gpt-4') + List chatbots in a project: + >>> chatbots = project.chatbots.list() - Delete a chatbot: + Retrieve a chatbot by name: + >>> chatbot = project.chatbots.get('my_chatbot') - >>> project.chatbots.drop('my_chatbot') + Create a new chatbot: + >>> chatbot = project.chatbots.create( + ... 'my_chatbot', + ... model_name='gpt-4', + ... database_name='slack_db' + ... ) + Delete a chatbot by name: + >>> project.chatbots.drop('my_chatbot') """ def __init__(self, project, api): @@ -106,11 +103,12 @@ def __init__(self, project, api): def list(self) -> List[Chatbot]: """ - Get the list of chatbots in the project. + Retrieves a list of all chatbots within the project. - >>> chatbots = project.chatbots.list() + Example usage: + >>> chatbots = project.chatbots.list() - :return: List of chatbot objects. + :return: List of Chatbot objects. """ return [ Chatbot(self.api, self.project, item) @@ -119,11 +117,12 @@ def list(self) -> List[Chatbot]: def get(self, name: str) -> Chatbot: """ - Get a chatbot by name. + Retrieves a chatbot by its name. - >>> chatbot = project.chatbots.get('my_chatbot') + Example usage: + >>> chatbot = project.chatbots.get('my_chatbot') - :param name: Name of the chatbot. + :param name: The name of the chatbot to retrieve. :return: Chatbot object. """ data = self.api.get_chatbot(self.project.name, name) @@ -131,24 +130,26 @@ def get(self, name: str) -> Chatbot: def create(self, name: str, agent_name: str = None, model_name: str = None, database_name: str = None, is_running: bool = False) -> Chatbot: """ - Create a new chatbot. - - >>> chatbot = project.chatbots.create( - ... 'my_chatbot', - ... model_name='gpt-4', - ... database_name='slack_db' - - ... ) - - :param name: Name of the chatbot. - :param model_name: Name of the model or agent. - :param database_name: Connection name for chat applications (e.g., Slack, Teams). - :return: Created Chatbot object. + Creates a new chatbot within the project. + + Example usage: + >>> chatbot = project.chatbots.create( + ... 'my_chatbot', + ... model_name='gpt-4', + ... database_name='slack_db' + ... ) + + :param name: The name of the new chatbot. + :param agent_name: The agent name to associate with the chatbot. + :param model_name: The model to use for the chatbot. + :param database_name: The database connection name for chat applications. + :param is_running: (Optional) Indicates whether the chatbot should start in a running state. Default is False. + :return: The created Chatbot object. """ payload = { 'name': name, 'database_name': database_name, - 'is_running':is_running + 'is_running': is_running } if agent_name: @@ -163,10 +164,11 @@ def create(self, name: str, agent_name: str = None, model_name: str = None, data def drop(self, name: str): """ - Delete a chatbot by name. + Deletes a chatbot by its name. - >>> project.chatbots.drop('my_chatbot') + Example usage: + >>> project.chatbots.drop('my_chatbot') - :param name: Name of the chatbot to delete. + :param name: The name of the chatbot to delete. """ self.api.delete_chatbot(self.project.name, name) From 6c8d43a22775d9e7c975f0898b39b666e83f8d19 Mon Sep 17 00:00:00 2001 From: parthiv11 Date: Sat, 14 Dec 2024 17:24:34 +0000 Subject: [PATCH 7/7] flake8 --- mindsdb_sdk/chatbots.py | 176 ++++++++++++++++++++++++++++++++++++++++ mindsdb_sdk/projects.py | 6 +- 2 files changed, 179 insertions(+), 3 deletions(-) create mode 100644 mindsdb_sdk/chatbots.py diff --git a/mindsdb_sdk/chatbots.py b/mindsdb_sdk/chatbots.py new file mode 100644 index 0000000..02e6d71 --- /dev/null +++ b/mindsdb_sdk/chatbots.py @@ -0,0 +1,176 @@ +from typing import List +from mindsdb_sdk.utils.objects_collection import CollectionBase + + +class Chatbot: + """ + Represents a chatbot that can be managed within a project. + """ + + def __init__(self, api, project, data: dict): + self.api = api + self.project = project + self.name = data.get('name') + self.database_name = data.get('database') + self.agent_name = data.get('agent') + self.model_name = data.get('model_name') + self.is_running = data.get('is_running') + + def __repr__(self): + return f"{self.__class__.__name__}({self.project.name}.{self.name})" + + def update(self, name: str = None, agent_name: str = None, model_name: str = None, database_name: str = None, inplace: bool = False): + """ + Updates the chatbot's properties. + + Example usage: + >>> chatbot.update(model_name='gpt-4', database_name='slack_db') + + :param name: (Optional) New name for the chatbot. + :param agent_name: (Optional) New agent name to associate with the chatbot. + :param model_name: (Optional) New model to use for the chatbot. + :param database_name: (Optional) New database connection name. + :param inplace: If True, updates the current object in-place; otherwise, returns a new Chatbot object. + :return: Updated Chatbot object, or None if inplace is True. + """ + payload = {} + + if name: + payload['name'] = name + + if database_name: + payload['database_name'] = database_name + + if agent_name: + payload['agent_name'] = agent_name + + if model_name: + payload['model_name'] = model_name + + updated_chatbot = self.api.update_chatbot( + project=self.project.name, + chatbot_name=self.name, + data=payload + ) + + if inplace: + self.name = updated_chatbot.get('name', self.name) + self.database_name = updated_chatbot.get('database', self.database_name) + self.agent_name = updated_chatbot.get('agent', self.agent_name) + self.model_name = updated_chatbot.get('model_name', self.model_name) + return None + + return Chatbot(self.api, self.project, updated_chatbot) + + def delete(self): + """ + Deletes the chatbot from the project. + + Example usage: + >>> chatbot.delete() + """ + self.api.delete_chatbot(self.project.name, self.name) + + +class Chatbots(CollectionBase): + """ + Manages chatbots within a project. + + Provides methods to list, retrieve, create, and delete chatbots. + + Example usage: + + List chatbots in a project: + >>> chatbots = project.chatbots.list() + + Retrieve a chatbot by name: + >>> chatbot = project.chatbots.get('my_chatbot') + + Create a new chatbot: + >>> chatbot = project.chatbots.create( + 'my_chatbot', + model_name='gpt-4', + database_name='slack_db', + is_running=True + ) + + Delete a chatbot by name: + >>> project.chatbots.drop('my_chatbot') + """ + + def __init__(self, project, api): + self.project = project + self.api = api + + def list(self) -> List[Chatbot]: + """ + Retrieves a list of all chatbots within the project. + + Example usage: + >>> chatbots = project.chatbots.list() + + :return: List of Chatbot objects. + """ + return [ + Chatbot(self.api, self.project, item) + for item in self.api.list_chatbots(self.project.name) + ] + + def get(self, name: str) -> Chatbot: + """ + Retrieves a chatbot by its name. + + Example usage: + >>> chatbot = project.chatbots.get('my_chatbot') + + :param name: The name of the chatbot to retrieve. + :return: Chatbot object. + """ + data = self.api.get_chatbot(self.project.name, name) + return Chatbot(self.api, self.project, data) + + def create(self, name: str, agent_name: str = None, model_name: str = None, database_name: str = None, is_running: bool = False) -> Chatbot: + """ + Creates a new chatbot within the project. + + Example usage: + >>> chatbot = project.chatbots.create( + 'my_chatbot', + model_name='gpt-4', + database_name='slack_db', + is_running=True + ) + + :param name: The name of the new chatbot. + :param agent_name: The agent name to associate with the chatbot. + :param model_name: The model to use for the chatbot. + :param database_name: The database connection name for chat applications. + :param is_running: (Optional) Indicates whether the chatbot should start in a running state. Default is False. + :return: The created Chatbot object. + """ + payload = { + 'name': name, + 'database_name': database_name, + 'is_running': is_running + } + + if agent_name: + payload['agent_name'] = agent_name + + if model_name: + payload['model_name'] = model_name + + self.api.create_chatbot(self.project.name, data=payload) + + return self.get(name) + + def drop(self, name: str): + """ + Deletes a chatbot by its name. + + Example usage: + >>> project.chatbots.drop('my_chatbot') + + :param name: The name of the chatbot to delete. + """ + self.api.delete_chatbot(self.project.name, name) diff --git a/mindsdb_sdk/projects.py b/mindsdb_sdk/projects.py index 4b69b2c..8f25807 100644 --- a/mindsdb_sdk/projects.py +++ b/mindsdb_sdk/projects.py @@ -1,4 +1,4 @@ -from typing import List +from typing import List from mindsdb_sql_parser.ast.mindsdb import CreateDatabase, DropPredictor from mindsdb_sql_parser.ast import DropDatabase @@ -13,7 +13,7 @@ from .views import Views from .jobs import Jobs from .knowledge_bases import KnowledgeBases -from .chatbot import Chatbots +from .chatbots import Chatbots class Project: @@ -184,4 +184,4 @@ def drop(self, name: str): :param name: name of the project """ ast_query = DropDatabase(name=Identifier(name)) - self.api.sql_query(ast_query.to_string()) \ No newline at end of file + self.api.sql_query(ast_query.to_string())