Watch the demo video at the top for a live demonstration as you follow along! (coming soon)
1. Introduction
Hi there! In this tutorial, we’ll guide you through the process of updating the settings for a particular agent (or “project” as we sometimes call it) using the CustomGPT RAG API. Whether you’re looking to change your chatbot’s default prompt, update its avatar, background image, or example questions, this step-by-step tutorial will help you understand each part of the process. We’ll cover everything from setting up your environment and creating a project to retrieving and updating project settings in detail.
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.
2. Setting Up the Environment
Before diving into the main code, let’s configure our environment in Google Colab. This includes specifying the API endpoint, inserting your API token, and importing 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:
- Sets the base URL (api_endpoint) for all your API calls.
- Uses your API token (api_token) to authenticate your requests. Replace the placeholder with your actual token.
- Sets the base URL (api_endpoint) for all your API calls.
- Headers:
- Defines HTTP headers that specify you’re sending JSON data and include the authentication bearer token.
- Defines HTTP headers that specify you’re sending JSON data and include the authentication bearer token.
- Imports:
- Imports the requests library for making HTTP requests.
- Imports the json library to encode and decode JSON data.
- Imports the requests library for making HTTP requests.
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.
With the environment ready, let’s verify you have everything set up by checking the prerequisites.
3. Prerequisites
Before you proceed, make sure 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: We’re using Google Colab, so you don’t need any local setup!
Now that you’ve confirmed your prerequisites, we can move on to creating an agent.
4. Creating an Agent (Project)
Even though the code uses the term “project,” many of us refer to these as “agents.” In this step, you create an agent using a sitemap as the 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)
Key Details:
- Agent Details:
- project_name: The name you want to assign to your agent.
- sitemap_path: The URL of the sitemap that provides the content for your agent.
- project_name: The name you want to assign to your agent.
- Payload Creation:
- Converts the agent details into a JSON string required by the API.
- Converts the agent details into a JSON string required by the API.
- POST Request:
- Sends the payload to the CustomGPT API’s projects endpoint to create your agent.
- Sends the payload to the CustomGPT API’s projects endpoint to create your agent.
- Output:
- Prints the JSON response containing details about the newly created agent (for example, its unique “id”).
- Prints the JSON response containing details about the newly created agent (for example, its unique “id”).
After creating your agent, the next step is to retrieve its current settings.
5. Retrieving Current Project Settings
Once your agent is created, you can retrieve its settings to see the default configuration. These settings include the chatbot’s avatar, background, default prompt, example questions, and language.
**Get Project settings**
data = json.loads(create_project.text)["data"]
project_id = data["id"]
# Get the project settings
url = api_endpoint + 'projects' + f"/{project_id}" + '/settings'
project_settings = requests.request('GET', url, headers=headers)
print(project_settings.text)
Key Details:
- Extracting Agent ID:
- The code parses the JSON response from the previous step and extracts the id of the agent.
- The code parses the JSON response from the previous step and extracts the id of the agent.
- Building the Settings URL:
- Combines the base URL with the agent’s ID and the /settings endpoint.
- Combines the base URL with the agent’s ID and the /settings endpoint.
- GET Request:
- Sends a GET request to fetch the current settings of your agent.
- Sends a GET request to fetch the current settings of your agent.
- Output:
- Prints the settings, which by default might show values like:
{
"data": {
"chatbot_avatar": "",
"chatbot_background": "",
"default_prompt": "Ask Me Anything ...",
"example_questions": null,
"response_source": "default",
"chatbot_msg_lang": "en",
"remove_branding": false
},
"status": "success"
}
With the current settings in view, we can now proceed to update them.
6. Updating Project Settings
In this step, you will update your agent’s settings by uploading new images for the avatar and background, changing the default prompt, setting example questions, and modifying the chatbot’s message language.
**Update Project Settings**
from google.colab import files
if 'Content-type' in headers.keys():
del headers['Content-type']
# Give a new default prompt
default_prompt = 'Example ChatBot using Sitemap'
# Upload new avatar image file
uploaded_file = files.upload()
file_content = next(iter(uploaded_file.values()))
chatbot_avatar_binary = file_content
# Upload new background image file
uploaded_file_2 = files.upload()
file_content_2 = next(iter(uploaded_file_2.values()))
chatbot_background_binary = file_content_2
payload = {
"chat_bot_avatar": chatbot_avatar_binary,
"chat_bot_bg": chatbot_background_binary,
"default_prompt": (None, default_prompt),
"example_questions[0]": (None, 'Test1'),
"example_questions[1]": (None, 'Test2'),
"response_source": (None, "default"),
"chatbot_msg_lang": (None, "ur")
}
url = api_endpoint + 'projects' + f'/{project_id}' + '/settings'
update_project_settings = requests.request('POST', url, headers=headers, files=payload)
print(update_project_settings.text)
# Retrieve updated settings to confirm changes
url = api_endpoint + 'projects' + f"/{project_id}" + '/settings'
project_settings = requests.request('GET', url, headers=headers)
print(project_settings.text)
Key Details:
- Adjust Headers:
- Removes the “Content-type” header if it exists. This is necessary since the payload for updating settings uses multipart/form-data (which is handled automatically by the files parameter).
- Removes the “Content-type” header if it exists. This is necessary since the payload for updating settings uses multipart/form-data (which is handled automatically by the files parameter).
- Default Prompt:
- Sets the new default_prompt value to update your agent’s prompt.
- Sets the new default_prompt value to update your agent’s prompt.
- Uploading Files:
- The code uses files.upload() from Google Colab to allow you to upload a new avatar image and a new background image.
- The uploaded files are stored in variables (chatbot_avatar_binary and chatbot_background_binary).
- The code uses files.upload() from Google Colab to allow you to upload a new avatar image and a new background image.
- Payload Construction:
- Constructs a payload as a dictionary. Unlike previous JSON payloads, this payload includes file data and key-value pairs for other settings.
- For textual fields that don’t involve files (like default_prompt, example_questions, etc.), tuples with (None, value) are used.
- Constructs a payload as a dictionary. Unlike previous JSON payloads, this payload includes file data and key-value pairs for other settings.
- POST Request to Update Settings:
- Sends the update request to the /settings endpoint for your specific agent.
- Sends the update request to the /settings endpoint for your specific agent.
- Output Confirmation:
- Prints a success message indicating the settings have been updated.
- Retrieves the updated settings by sending a GET request again, which should display the new avatar URL, background URL, updated prompt, example questions, and language settings.
- Prints a success message indicating the settings have been updated.
After updating the settings, you should see confirmation in the output that your changes have been saved.
7. Troubleshooting Common Issues
If you run into any issues during these steps, here are some common troubleshooting tips:
- Invalid API Token:
- Make sure you have replaced ‘ADD_YOUR_API_TOKEN_HERE’ with your actual API token.
- Make sure you have replaced ‘ADD_YOUR_API_TOKEN_HERE’ with your actual API token.
- JSON or Payload Formatting Errors:
- Ensure your JSON and multipart/form-data payloads are correctly formatted.
- Ensure your JSON and multipart/form-data payloads are correctly formatted.
- File Upload Problems:
- If files are not uploading, check your file sizes and formats to ensure they meet the API requirements.
- If files are not uploading, check your file sizes and formats to ensure they meet the API requirements.
- Connection Issues:
- Verify your internet connection and confirm that the API endpoint is accessible.
- Verify your internet connection and confirm that the API endpoint is accessible.
If the problem persists, refer to the official CustomGPT documentation or ask for help in the community forum.
8. Conclusion
Congratulations! In this guide, you learned how to:
- Set up your Google Colab environment with the CustomGPT API.
- Create an agent using a sitemap as the content source.
- Retrieve your agent’s current settings.
- Update the project settings by uploading new images, changing the default prompt, setting example questions, and modifying the chatbot’s message language.
This step-by-step process empowers you to customize and update your chatbot’s settings as needed, ensuring it remains aligned with your requirements. If you have any questions or need further assistance, feel free to check the CustomGPT documentation or join our community for support.
Happy coding, and enjoy enhancing your chatbot experience with CustomGPT!
Priyansh is Developer Relations Advocate who loves technology, writer about them, creates deeply researched content about them.