Get and Update Page Metadata with CustomGPT RAG API – Step-by-Step Instructions

Author Image

Written by: Priyansh Khodiyar

Get and Update Page Metadata with CustomGPT RAG API – Step-by-Step Instructions cover image

Watch the video at the top of this post for a live demo and explanation!

1. Introduction

Welcome! In this guide, we’ll show you how to get and update page metadata for your chatbot using the CustomGPT RAG API in a Google Colab notebook. We now refer to these as “agents” (even though the code still says “project”). You’ll learn how to create an agent, retrieve one of its pages, fetch its metadata, and then update that metadata—all in easy, step-by-step instructions.

Make sure you have read our Getting Started with CustomGPT.ai for New Developers blog to get an overview of the entire platform.

Get the cookbook link here – https://github.com/Poll-The-People/customgpt-cookbook/blob/main/examples/GetAndUpdatePageMetadata.ipynb

2. Setting Up the Environment

Before we start working with the RAG API, we need to set up our Colab environment. This involves defining the RAG API URL, setting your RAG API token, and importing the necessary libraries.

Code Sample:

# setup API URL and API Token

api_endpoint = 'https://app.customgpt.ai/api/v1/'

# api_token = 'ADD_YOUR_API_TOKEN_HERE'

api_token = 'YOUR_API_KEY'

headers = {

    'Content-Type': 'application/json',

    'Authorization': 'Bearer ' + api_token

}

# imports

import requests

import json

What This Does

  • RAG API Endpoint: Sets the base URL for all CustomGPT’s RAG API calls.
  • RAG API Token: Uses your API key (replace ‘YOUR_API_KEY‘ with your actual key) to authenticate your requests.
  • Headers: Prepares a dictionary with the content type (JSON) and your authorization token so that the RAG API knows who is making the request.
  • Imports: Imports the requests library to handle HTTP requests and the json module to work with JSON data.

3. Get Your RAG API Key

To get your RAG API key, there are two ways:

Method 1 – Via Agent

  1. Agent > All Agents.
  2. Select your agent and go to deploy, click on the API key section and create an API. 

Method 2 – Via Profile section.

  1. Go to profile (top right corner of your screen)
  2. Click on My Profile
  3. 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.

4. Prerequisites to Use CustomGPT RAG APIs

Before diving in, make sure you have the following:

  • CustomGPT.ai Account: Sign up and log in at CustomGPT.ai.
  • RAG API Key: Generate your RAG API token from your account dashboard.
  • Basic Python Knowledge: Familiarity with Python and REST APIs will help you follow along.
  • Google Colab: We’ll use Google Colab to run the notebook—no local setup required!

With prerequisites done, lets  start with creating an agent.

5. Creating a Chatbot (Agent)

Let’s create a chatbot using a sitemap as its source. Although the code refers to a “project,” we’ll call it an agent in our discussion.

Code Sample:

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)

Explanation 

  • Agent Details: Sets the agent’s name and specifies a sitemap URL as the content source.
  • Payload Creation: Converts these details into a JSON-formatted string that the RAG API can understand.
  • Build URL & POST Request: Combines the base RAG API URL with the endpoint for creating projects, then sends a POST request with the payload and headers.
    The RAG API returns details of the created agent, including its unique ID.
  • Output: Prints the response (sample output shows an agent with an ID like 1242).

Now that you have generated an agent,  let’s create a project page.

6. Get a Project Page

After creating your agent, you need to retrieve one of its pages. This step checks if your agent is active and gets the page ID.

Code Sample:

# Check status of the project if chat bot is active

data = json.loads(create_project.text)["data"]

# Get project id from response for created project

project_id = data["id"]

url = api_endpoint + 'projects' + f"/{project_id}" + '/pages'

project_pages = requests.request('GET', url, headers=headers)

data = json.loads(project_pages.text)['data']

page_id = data['pages']['data'][0]['id']

print(page_id)

Explanation 

  • Parse Agent Details: Converts the JSON response from the agent creation into a Python dictionary.
  • Extract Agent ID: Retrieves the unique ID of your agent from the parsed data.
  • Build Pages URL & GET Request: Constructs the URL to access the agent’s pages using the agent ID, then sends a GET request to fetch the pages.
  • Extract Page ID: Parses the response to extract the first page’s ID and prints it (sample output might be something like 29814).

7. Get Page Metadata

Now that you have a page ID, you can retrieve its metadata, such as the title.

Code Sample:

url = api_endpoint + 'projects' + f"/{project_id}" + '/pages' + f"/{page_id}" +'/metadata'

page_metadata = requests.request('GET', url, headers=headers)

print(page_metadata)

data = json.loads(page_metadata.text)['data']

title = data['title']

print(title)

Explanation 

  • Build Metadata URL: Combines the agent ID and page ID into a URL that targets the metadata endpoint for that page.
  • GET Request for Metadata: Sends a GET request to fetch the metadata.
  • Print and Parse Metadata: Prints the full metadata response, then parses it to extract and print the page title.
    (Sample output might display a title like: “Brady’s unprecedented career filled with highlight moments”)

8. Update Project Metadata

Finally, let’s update the metadata of the page. For this example, we’re updating the title.

Code Sample:

payload = json.dumps({

    "title": "Test2"

})

response = requests.request('PUT', url, headers=headers, data=payload)

print(response.content)

Explanation 

  • Prepare Update Payload: Creates a JSON payload with the new title (“Test2”).
  • PUT Request to Update Metadata: Sends a PUT request to the same metadata URL with the update payload and headers.
  • Confirm Update: Prints the response content to confirm that the page metadata has been successfully updated.

9. Troubleshooting Common Issues

If you run into any issues, here are some common pitfalls and their solutions:

  • Invalid RAG API Token: Double-check that you’ve replaced ‘YOUR_API_KEY‘ with your actual RAG API key and that it’s valid.
  • JSON Parsing Errors: If you experience errors parsing the response, print the raw response to see its structure, then verify you’re accessing the right keys.
  • Connection Problems: Ensure your internet connection is stable and the RAG API endpoint URL is correct.
  • Unexpected Outputs: Verify that your URLs and JSON payloads are correctly formatted according to the RAG API documentation.

For more help, refer to the official CustomGPT documentation or join the community support forum.

Conclusion

Great job! In this guide, you learned how to:

  • Set up your Google Colab environment to interact with the CustomGPT RAG API.
  • Create a chatbot (agent) using a sitemap as the content source.
  • Retrieve a page from your agent and fetch its metadata.
  • Update the page metadata using a PUT request.

By following these step-by-step instructions, you can easily manage and customize your agent’s page details. If you have any questions or need further assistance, feel free to check out the CustomGPT documentation or ask in our community. Happy coding, and enjoy building your custom chatbot solutions with CustomGPT!

Build a Custom GPT for your business, in minutes.

Deliver exceptional customer experiences and maximize employee efficiency with custom AI agents.

Trusted by thousands of organizations worldwide

Related posts

Leave a reply

Your email address will not be published. Required fields are marked *

*

3x productivity.
Cut costs in half.

Launch a custom AI agent in minutes.

Instantly access all your data.
Automate customer service.
Streamline employee training.
Accelerate research.
Gain customer insights.

Try 100% free. Cancel anytime.