-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathchatbots.py
165 lines (125 loc) · 5.05 KB
/
chatbots.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
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.
:return: Updated Chatbot object.
"""
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
)
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 self
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)