|
| 1 | +from typing import List |
| 2 | +from mindsdb_sdk.utils.objects_collection import CollectionBase |
| 3 | + |
| 4 | + |
| 5 | +class Chatbot: |
| 6 | + """ |
| 7 | + Represents a chatbot that can be managed within a project. |
| 8 | + """ |
| 9 | + |
| 10 | + def __init__(self, api, project, data: dict): |
| 11 | + self.api = api |
| 12 | + self.project = project |
| 13 | + self.name = data.get('name') |
| 14 | + self.database_name = data.get('database') |
| 15 | + self.agent_name = data.get('agent') |
| 16 | + self.model_name = data.get('model_name') |
| 17 | + self.is_running = data.get('is_running') |
| 18 | + |
| 19 | + def __repr__(self): |
| 20 | + return f"{self.__class__.__name__}({self.project.name}.{self.name})" |
| 21 | + |
| 22 | + def update(self, name: str = None, agent_name: str = None, model_name: str = None, database_name: str = None, inplace: bool = False): |
| 23 | + """ |
| 24 | + Updates the chatbot's properties. |
| 25 | +
|
| 26 | + Example usage: |
| 27 | + >>> chatbot.update(model_name='gpt-4', database_name='slack_db') |
| 28 | +
|
| 29 | + :param name: (Optional) New name for the chatbot. |
| 30 | + :param agent_name: (Optional) New agent name to associate with the chatbot. |
| 31 | + :param model_name: (Optional) New model to use for the chatbot. |
| 32 | + :param database_name: (Optional) New database connection name. |
| 33 | + :param inplace: If True, updates the current object in-place; otherwise, returns a new Chatbot object. |
| 34 | + :return: Updated Chatbot object, or None if inplace is True. |
| 35 | + """ |
| 36 | + payload = {} |
| 37 | + |
| 38 | + if name: |
| 39 | + payload['name'] = name |
| 40 | + |
| 41 | + if database_name: |
| 42 | + payload['database_name'] = database_name |
| 43 | + |
| 44 | + if agent_name: |
| 45 | + payload['agent_name'] = agent_name |
| 46 | + |
| 47 | + if model_name: |
| 48 | + payload['model_name'] = model_name |
| 49 | + |
| 50 | + updated_chatbot = self.api.update_chatbot( |
| 51 | + project=self.project.name, |
| 52 | + chatbot_name=self.name, |
| 53 | + data=payload |
| 54 | + ) |
| 55 | + |
| 56 | + if inplace: |
| 57 | + self.name = updated_chatbot.get('name', self.name) |
| 58 | + self.database_name = updated_chatbot.get('database', self.database_name) |
| 59 | + self.agent_name = updated_chatbot.get('agent', self.agent_name) |
| 60 | + self.model_name = updated_chatbot.get('model_name', self.model_name) |
| 61 | + return None |
| 62 | + |
| 63 | + return Chatbot(self.api, self.project, updated_chatbot) |
| 64 | + |
| 65 | + def delete(self): |
| 66 | + """ |
| 67 | + Deletes the chatbot from the project. |
| 68 | +
|
| 69 | + Example usage: |
| 70 | + >>> chatbot.delete() |
| 71 | + """ |
| 72 | + self.api.delete_chatbot(self.project.name, self.name) |
| 73 | + |
| 74 | + |
| 75 | +class Chatbots(CollectionBase): |
| 76 | + """ |
| 77 | + Manages chatbots within a project. |
| 78 | +
|
| 79 | + Provides methods to list, retrieve, create, and delete chatbots. |
| 80 | +
|
| 81 | + Example usage: |
| 82 | +
|
| 83 | + List chatbots in a project: |
| 84 | + >>> chatbots = project.chatbots.list() |
| 85 | +
|
| 86 | + Retrieve a chatbot by name: |
| 87 | + >>> chatbot = project.chatbots.get('my_chatbot') |
| 88 | +
|
| 89 | + Create a new chatbot: |
| 90 | + >>> chatbot = project.chatbots.create( |
| 91 | + 'my_chatbot', |
| 92 | + model_name='gpt-4', |
| 93 | + database_name='slack_db', |
| 94 | + is_running=True |
| 95 | + ) |
| 96 | +
|
| 97 | + Delete a chatbot by name: |
| 98 | + >>> project.chatbots.drop('my_chatbot') |
| 99 | + """ |
| 100 | + |
| 101 | + def __init__(self, project, api): |
| 102 | + self.project = project |
| 103 | + self.api = api |
| 104 | + |
| 105 | + def list(self) -> List[Chatbot]: |
| 106 | + """ |
| 107 | + Retrieves a list of all chatbots within the project. |
| 108 | +
|
| 109 | + Example usage: |
| 110 | + >>> chatbots = project.chatbots.list() |
| 111 | +
|
| 112 | + :return: List of Chatbot objects. |
| 113 | + """ |
| 114 | + return [ |
| 115 | + Chatbot(self.api, self.project, item) |
| 116 | + for item in self.api.list_chatbots(self.project.name) |
| 117 | + ] |
| 118 | + |
| 119 | + def get(self, name: str) -> Chatbot: |
| 120 | + """ |
| 121 | + Retrieves a chatbot by its name. |
| 122 | +
|
| 123 | + Example usage: |
| 124 | + >>> chatbot = project.chatbots.get('my_chatbot') |
| 125 | +
|
| 126 | + :param name: The name of the chatbot to retrieve. |
| 127 | + :return: Chatbot object. |
| 128 | + """ |
| 129 | + data = self.api.get_chatbot(self.project.name, name) |
| 130 | + return Chatbot(self.api, self.project, data) |
| 131 | + |
| 132 | + def create(self, name: str, agent_name: str = None, model_name: str = None, database_name: str = None, is_running: bool = False) -> Chatbot: |
| 133 | + """ |
| 134 | + Creates a new chatbot within the project. |
| 135 | +
|
| 136 | + Example usage: |
| 137 | + >>> chatbot = project.chatbots.create( |
| 138 | + 'my_chatbot', |
| 139 | + model_name='gpt-4', |
| 140 | + database_name='slack_db', |
| 141 | + is_running=True |
| 142 | + ) |
| 143 | +
|
| 144 | + :param name: The name of the new chatbot. |
| 145 | + :param agent_name: The agent name to associate with the chatbot. |
| 146 | + :param model_name: The model to use for the chatbot. |
| 147 | + :param database_name: The database connection name for chat applications. |
| 148 | + :param is_running: (Optional) Indicates whether the chatbot should start in a running state. Default is False. |
| 149 | + :return: The created Chatbot object. |
| 150 | + """ |
| 151 | + payload = { |
| 152 | + 'name': name, |
| 153 | + 'database_name': database_name, |
| 154 | + 'is_running': is_running |
| 155 | + } |
| 156 | + |
| 157 | + if agent_name: |
| 158 | + payload['agent_name'] = agent_name |
| 159 | + |
| 160 | + if model_name: |
| 161 | + payload['model_name'] = model_name |
| 162 | + |
| 163 | + self.api.create_chatbot(self.project.name, data=payload) |
| 164 | + |
| 165 | + return self.get(name) |
| 166 | + |
| 167 | + def drop(self, name: str): |
| 168 | + """ |
| 169 | + Deletes a chatbot by its name. |
| 170 | +
|
| 171 | + Example usage: |
| 172 | + >>> project.chatbots.drop('my_chatbot') |
| 173 | +
|
| 174 | + :param name: The name of the chatbot to delete. |
| 175 | + """ |
| 176 | + self.api.delete_chatbot(self.project.name, name) |
0 commit comments