-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added Chatbot functions in SDK #178
Open
parthiv11
wants to merge
9
commits into
mindsdb:main
Choose a base branch
from
parthiv11:chatbot
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ec289e8
added chatbot
parthiv11 2c78c02
update rest
f8fe81f
chatbots updated
f55ddea
update
acfe1be
done
b00d247
updates doc string
6c8d43a
flake8
1644d4e
Delete mindsdb_sdk/chatbot.py
parthiv11 5a6326f
fixed update method
parthiv11 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
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 | ||
|
||
def delete(self): | ||
""" | ||
Deletes the chatbot from the project. | ||
|
||
Example usage: | ||
>>> chatbot.delete() | ||
""" | ||
self.api.delete_chatbot(self.project.name, self.name) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is chatbots.drop(name) method for this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this can be used to delete chatbot through it's instance instead of name. shoud i remove this? |
||
|
||
|
||
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just an idea what methods could be useful:
Also a user would be interested in last error message for chatbot. is it accessible via rest API?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for start stop methos we cane change is_running.
but "last error message", i am unabe to get it clearly