Watch the demo video at the top of the post for a live walkthrough of this tutorial! (coming soon)
1. Introduction
Hi there! In this tutorial, we’ll show you how to delete an agent (or “project” as many of us refer to it) using the CustomGPT RAG APIs Python SDK. This guide is designed for developers who want to learn how to programmatically remove a project from their account. We’ll walk through the process step by step, explaining every line of code in detail so you fully understand how it works.
Make sure you have read our Getting Started with CustomGPT.ai for New Developers blog to get an overview of the entire platform.
By the end, you’ll be able to programmatically inspect where your chatbot’s content comes from. For full API reference, see the CustomGPT API docs.
Code – https://github.com/Poll-The-People/customgpt-cookbook/blob/main/examples/SDK_Delete_a_project.ipynb
2. Setting Up the Environment
Before you begin, you need to install the CustomGPT Python SDK. This SDK simplifies your work by providing Pythonic interfaces to the CustomGPT API.
!pip install customgpt-client
What This Does:
- Installation:
This command installs the customgpt-client package from PyPI, allowing you to access the API using Python objects and methods.
With the SDK installed, let’s move on to creating a project (agent) using a sitemap.
3. Creating Your Custom Chat Bot Using a Sitemap
In this step, we’ll create a project (agent) using a sitemap as the content source. Even though our code calls it a project, we often refer to these as agents.
# Import the CustomGPT SDK from the customgpt_client package.
from customgpt_client import CustomGPT
# Set your API token. Replace "YOUR_API_TOKEN" with your actual API token.
CustomGPT.api_key = "YOUR_API_TOKEN"
# Define the name of your project (agent). This is the title of your chatbot.
project_name = 'Example ChatBot using Sitemap'
# Define the sitemap URL which provides the content source for your chatbot.
sitemap_path = 'https://adorosario.github.io/small-sitemap.xml'
# Create the project (agent) using the SDK.
# The create method takes the project name and sitemap_path as parameters.
create_project = CustomGPT.Project.create(project_name=project_name, sitemap_path=sitemap_path)
# Print the response from the API which includes details about the created project.
print(create_project)
Get API keys
To get your API key, there are two ways:
Method 1 – Via Agent
- Agent > All Agents.
- Select your agent and go to deploy, click on the API key section and create an API.
Method 2 – Via Profile section.
- Go to profile (top right corner of your screen)
- Click on My Profile
- You will see the screen something like this (below screenshot). Here you can click on “Create API key”, give it a name and copy the key.
Please save this secret key somewhere safe and accessible. For security reasons, You won’t be able to view it again through your CustomGPT.ai account. If you lose this secret key, you’ll need to generate a new one.
Detailed Explanation:
- Import Statement:
- from customgpt_client import CustomGPT: Imports the main class from the SDK so you can interact with the API.
- from customgpt_client import CustomGPT: Imports the main class from the SDK so you can interact with the API.
- Setting the API Key:
- CustomGPT.api_key = “YOUR_API_TOKEN”: Assigns your API token to the SDK. Replace the placeholder with your real token for authentication.
- CustomGPT.api_key = “YOUR_API_TOKEN”: Assigns your API token to the SDK. Replace the placeholder with your real token for authentication.
- Defining Variables:
- project_name and sitemap_path are defined to set up your project. These values determine your chatbot’s title and the URL from which it pulls its content.
- project_name and sitemap_path are defined to set up your project. These values determine your chatbot’s title and the URL from which it pulls its content.
- Creating the Project:
- CustomGPT.Project.create(…) calls the SDK method to create a new project. This method sends your project details (name and sitemap path) to the CustomGPT API.
- CustomGPT.Project.create(…) calls the SDK method to create a new project. This method sends your project details (name and sitemap path) to the CustomGPT API.
- Printing the Response:
- print(create_project) displays the API’s response, which contains information about your newly created project including its unique ID.
- print(create_project) displays the API’s response, which contains information about your newly created project including its unique ID.
Now that we’ve created your project, it’s time to delete it using the SDK.
4. Deleting a Project
Once you have created a project, you might need to delete an agent, for example, if it was just a test project or if you need to clean up your account. The following code demonstrates how to do this using the SDK
# Extract the project ID from the response of the project creation.
# The 'parsed' property contains the parsed JSON response, and 'data.id' holds the unique project identifier.
project_id = create_project.parsed.data.id
# To make sure that the project is sharable before deletion,
# sometimes additional steps are needed, such as updating sharing status.
# However, in this example, we proceed directly to deletion.
# Use the CustomGPT SDK to delete the project.
# The delete method takes the project_id as a parameter.
project_deleted = CustomGPT.Project.delete(project_id=project_id)
# Print the response from the API which confirms the project has been deleted.
print(project_deleted)
Detailed Explanation:
- Extracting the Project ID:
- project_id = create_project.parsed.data.id:
- This line parses the response from the project creation call to retrieve the unique identifier (id) of your project.
- Accessing parsed.data.id gives you the project ID, which is necessary to perform further actions, such as deletion.
- This line parses the response from the project creation call to retrieve the unique identifier (id) of your project.
- project_id = create_project.parsed.data.id:
- Deleting the Project:
- project_deleted = CustomGPT.Project.delete(project_id=project_id):
- Calls the SDK’s delete method, passing the extracted project ID.
- This method sends a deletion request to the API, effectively removing the project from your account.
- Calls the SDK’s delete method, passing the extracted project ID.
- project_deleted = CustomGPT.Project.delete(project_id=project_id):
- Printing the Deletion Response:
- print(project_deleted):
- Outputs the response, which should indicate that the project has been successfully deleted (typically with a confirmation flag like “deleted”: true).
- Outputs the response, which should indicate that the project has been successfully deleted (typically with a confirmation flag like “deleted”: true).
- print(project_deleted):
With these steps, you’ve successfully deleted a project using the CustomGPT Python SDK.
5. Conclusion
Congratulations! In this guide, you learned how to:
- Install and set up the CustomGPT Python SDK.
- Create a custom chatbot (project/agent) using a sitemap as the content source.
- Extract the project ID from the API response.
- Delete the project using the SDK and confirm the deletion.
These steps provide a clear and straightforward process to manage your projects programmatically. The CustomGPT SDK streamlines API interactions and makes it easier to integrate chatbot functionalities into your applications.
If you have any questions or need further assistance, feel free to consult the official CustomGPT documentation or join our developer community for support.
Happy coding, and enjoy building your chatbot solutions with CustomGPT!.
Priyansh is Developer Relations Advocate who loves technology, writer about them, creates deeply researched content about them.