Watch the video above for a live demo and walkthrough of this guide! (coming soon)
1. Introduction
Hi there! In this tutorial, we’ll walk through the process of updating and deleting a conversation using the CustomGPT RAG API in a Google Colab notebook. You’ll learn how to create an agent (often still called a “project” in the code), initiate a conversation, update the conversation’s name, and delete a conversation if needed. This complete guide explains every step in detail, so let’s dive right in!
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.
Get the cookbook here – https://github.com/Poll-The-People/customgpt-cookbook/blob/main/examples/Update_Delete_a_conversation.ipynb
2. Setting Up the Environment
Before we start, we need to get our workspace ready. In our Google Colab notebook, we set up the API endpoint, add our API token, and import the necessary libraries.
# setup API URL and API Token
api_endpoint = 'https://app.customgpt.ai/api/v1/'
api_token = 'ADD_YOUR_API_TOKEN_HERE'
headers = {
'Content-type': 'application/json',
'Authorization': 'Bearer ' + api_token
}
# imports
import requests
import json
What This Does:
- API Endpoint & Token: Establishes the base URL for all API calls and ensures authenticated requests using your API token.
- Headers: Configures the HTTP headers to indicate that JSON data will be sent along with the authentication token.
- Imports: Brings in the requests library for making HTTP calls and json for encoding/decoding JSON data.
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.
Now that our environment is set up, make sure you have met all prerequisites before continuing.
3. Prerequisites
Before you begin, please ensure you have:
- CustomGPT.ai Account: Sign up and log in at CustomGPT.ai.
- API Key: Generate your API token from your account dashboard.
- Basic Python Knowledge: Familiarity with Python and REST APIs.
- Google Colab: No local installation is required, as we’ll be running this in Google Colab.
With all prerequisites checked off, let’s create an agent (project) for your account.
4. Creating an Agent (Project)
Even though the code still uses the term “project,” we refer to these as agents. In this section, you’ll create an agent using a sitemap as its content source.
**Create a project for the account**
# Give a name to your project
project_name = 'Example ChatBot using Sitemap'
sitemap_path = 'https://adorosario.github.io/small-sitemap.xml'
payload = json.dumps({
"project_name": project_name,
"sitemap_path": sitemap_path
})
url = api_endpoint + 'projects'
create_project = requests.request('POST', url, headers=headers, data=payload)
print(create_project.text)
What This Does:
- Agent Details: Sets up the agent’s name and defines a sitemap URL as the source of content.
- Payload Creation: Encodes these details into JSON format.
- POST Request: Sends a request to create the agent.
- Output: Prints a JSON response including the agent’s unique ID (e.g., “id”:532) and other details.
Great! With your agent created, it’s time to open a conversation within it.
5. Creating a Conversation
Now, we’ll initiate a conversation in your newly created agent. Conversations allow you to interact with and track the communication history of your agent.
**Create Project Conversation**
data = json.loads(create_project.text)["data"]
project_id = data["id"]
# Get the project settings
name = 'Test Converasation'
payload = json.dumps({
"name": project_name
})
url = api_endpoint + 'projects' + f"/{project_id}" + '/conversations'
create_conversation = requests.request('POST', url, headers=headers, data=payload)
print(create_conversation.text)
What This Does:
- Extract Agent ID: Parses the JSON response from the agent creation step to obtain the agent’s unique ID.
- Set Conversation Name: Defines the conversation’s name; note that the payload here uses the original project name.
- POST Request: Sends a request to the specific conversation endpoint within the agent.
- Output: Prints a JSON response containing conversation details, including a session_id (e.g., “session_id”:”8ff8078b-3e88-4ad1-8e3f-9a37324964af”).
Now that you’ve created a conversation, let’s move forward to update its name.
6. Updating the Conversation Name
After starting the conversation, you might want to update its name to better reflect its purpose. Here’s how you can do that using a PUT request.
**Update a conversation name**
# Create a message to the above conversation
conversation_data = json.loads(create_conversation.text)["data"]
# session_id is important to maintain chat history
session_id = conversation_data["session_id"]
# pass in your updated conversation name
name = "Who is Tom"
url = api_endpoint + 'projects/' + str(project_id) + '/conversations/' + str(session_id)
payload = json.dumps({
"name": name
})
update_conversation_response = requests.put(url, headers=headers, data=payload)
print(update_conversation_response.text)
What This Does:
- Retrieve Session ID:
Extracts the session_id from the conversation creation response to identify which conversation to update. - Define New Conversation Name:
Sets the new name (in this case, “Who is Tom”) within the payload. - PUT Request:
Sends a PUT request to update the conversation’s name. The URL is built using the agent’s ID and the conversation’s session ID. - Output:
Prints a JSON response showing the updated conversation details, confirming that the name has been successfully updated.
With the conversation name updated, you’re now ready to see how deletion works. Let’s move on to the final step.
7. Deleting a Conversation
If you need to delete a conversation—maybe for cleanup or testing purposes—you can do this with a DELETE request.
**Delete a conversation**
url = api_endpoint + 'projects/' + str(project_id) + '/conversations/' + str(session_id)
payload = json.dumps({
"name": name
})
delete_conversation_response = requests.delete(url, headers=headers)
print(delete_conversation_response.text)
What This Does:
- Build Deletion URL:
Constructs the URL for the DELETE endpoint using the agent’s ID and the session ID. - DELETE Request:
Sends a DELETE request to remove the conversation. - Output:
Prints a JSON response indicating whether the deletion was successful (e.g., “deleted”:true).
With this final step, you have successfully updated and then deleted a conversation within your agent.
8. Troubleshooting Common Issues
Here are some common issues you might encounter along with possible solutions:
- Invalid API Token: Ensure you replace ‘ADD_YOUR_API_TOKEN_HERE’ with your valid API token.
- JSON Formatting Errors: Verify that your JSON payloads are correctly formatted, especially when updating data.
- Connection Problems: Check your internet connection and make sure the API endpoint is correct.
- Unexpected API Responses: If the API responses are not as expected, print the raw responses for debugging and refer to the official CustomGPT API documentation for clarification.
If you still face issues, consider consulting the community forum or the official documentation for further support.
9. Conclusion
Congratulations! In this guide, you learned how to:
- Set up your Google Colab environment with the necessary API configurations.
- Create an agent (project) and start a conversation.
- Update the name of an ongoing conversation.
- Delete a conversation if necessary.
These steps provide a complete workflow for managing conversations using the CustomGPT API. Whether you’re updating conversation titles for clarity or cleaning up old conversations, these methods give you full control over your interactions with the API.
If you have any questions or need further assistance, feel free to check out the CustomGPT documentation or join our community for support.
Happy coding, and enjoy building and maintaining your chatbot solutions with CustomGPT!
Priyansh is Developer Relations Advocate who loves technology, writer about them, creates deeply researched content about them.