Sign inTry Now

Memory functionality

This page details how to use ChatSonic's memory functionality in API

ChatSonic can maintain the context of your conversations just like you would with a person. It remembers past questions or comments in your conversation and can easily answer follow-up questions.


Field format

Here is how to use ChatSonic's memory functionality in API

  • set enable_memory field to true
  • add previous conversations to thehistory_data in the following format
[{
  	"is_sent": bool,
    "message": string
}]
  • purpose of is_sent field is to flag whether message is a user prompt or ChatSonic response
  • set is_sent to true for user prompt
  • set is_sent to false for ChatSonic response

Example

Let's try this with an example


STEP 1

  • Let's first ask ChatSonic Who is the President of the United States?
  • since this is the first message, history_data is an empty array of objects
// REQUEST BODY
{
    "enable_memory": true,
    "enable_google_results": true,
    "input_text": "Who is the President of the United States?",
    "history_data": []
}

// RESPONSE BODY
{
    "message": "The 46th and current President of the United States is Joseph R. Biden, Jr., who was sworn in on January 20, 2021.",
    "image_urls": null
}

STEP 2

  • Now, let's ask When was he born?
  • Ensure to pass the previous prompt and response in thehistory_data field
  • is_sent is set as true for user prompt
  • is_sent is set as false for ChatSonic response
// REQUEST BODY
{
    "enable_memory": true,
    "enable_google_results": true,
    "input_text": "When was he born?",
    "history_data": [
        {
            "is_sent": true,
            "message": "Who is the President of the United States?"
        },
        {
            "is_sent": false,
            "message": "The 46th and current President of the United States is Joseph R. Biden, Jr., who was sworn in on January 20, 2021."
        }
    ]
}

// RESPONSE BODY
{
    "message": "Joseph Robinette Biden Jr. was born on November 20, 1942, in Scranton, Pennsylvania.",
    "image_urls": null
}

STEP 3

  • Now, let's ask What is his favorite sports team?
  • Ensure to pass the previous prompt and response in thehistory_data field
  • is_sent is set as true for user prompt
  • is_sent is set as false for ChatSonic response
// REQUEST BODY
{
    "enable_memory": true,
    "enable_google_results": true,
    "input_text": "What is his favorite sports team?",
    "history_data": [
        {
            "is_sent": true,
            "message": "Who is the President of the United States?"
        },
        {
            "is_sent": false,
            "message": "The 46th and current President of the United States is Joseph R. Biden, Jr., who was sworn in on January 20, 2021."
        },
        {
            "is_sent": true,
            "message": "When was he born?"
        },
        {
            "is_sent": false,
            "message": "Joseph Robinette Biden Jr. was born on November 20, 1942, in Scranton, Pennsylvania."
        }
    ]
}

// RESPONSE BODY
{
    "message": "Joe Biden is a Pennsylvania and Delaware native, and he roots for the Philadelphia Eagles. Biden was at Super Bowl LII cheering for his team with his family.",
    "image_urls": null
}