Skip to content

Conversation Context

Generate Answer (RAG) endpoint supports maintaining conversation context across multiple queries.

This feature allows for more natural, contextual searching by considering previous messages in the conversation. This in turn enables implementation of intelligent and context-aware chatbots for question-answering and information retrieval scenarios.

What is a Conversation?

A conversation is a series of messages between the user who asks questions and the Gainly AI playing the role of assistant who answers questions.

Gainly represents a conversation in the form of an array of messages, ordered from oldest to newest. Example:

[
    {
        "role": "user",         // user is asking the question
        "content": [
            {
                "text": "are there any alpaca competitions"
            }
        ]
    },
    {
        "role": "assistant",    // Gainly AI "assistant" is answering the question
        "content": [
            {
                "text": "Yes, according to the sources, there are alpaca shows and competitions that are an integral part of the alpaca industry. These events provide breeders with the opportunity to showcase their animals, network with other breeders, and gain recognition for the quality of their herd. The sources mention that success at an alpaca show can significantly enhance a breeder's reputation and the value of their animals, as winning animals often command higher prices and their offspring are sought after for breeding."
            }
        ]
    }
]

API request parameters

Two API request parameters help you implement conversation context:

  • previous_messages
    • An array of previous messages in the conversation, ordered from oldest to newest.
  • max_messages
    • Specifies the maximum number of previous messages to consider for context when performing the search.

Using conversation context

To maintain context across multiple queries:

1. Initial Request:

  • For the first query in a conversation, omit the previous_messages parameter.

2. Subsequent Requests:

  • For follow-up queries, include the previous_messages parameter to maintain context.
  • Set previous_messages to the messages value from the most recent API response.

    Info

    messages array in the API response contains both the user's queries and the AI ("assistant") responses ordered from oldest to newest, maintaining the conversation flow.

3. Controlling Context Length:

  • Use the max_messages parameter to specify the maximum number of previous messages to consider for context.

Example API request

{
    "query": "when are they held and where",
    "previous_messages": [
        {
            "role": "user",
            "content": [
                {
                    "text": "are there any alpaca competitions"
                }
            ]
        },
        {
            "role": "assistant",
            "content": [
                {
                    "text": "Yes, according to the sources, there are alpaca shows and competitions that are an integral part of the alpaca industry. These events provide breeders with the opportunity to showcase their animals, network with other breeders, and gain recognition for the quality of their herd. The sources mention that success at an alpaca show can significantly enhance a breeder's reputation and the value of their animals, as winning animals often command higher prices and their offspring are sought after for breeding."
                }
            ]
        }
    ],
    "max_messages": 6,
    "tenant_id": ["tenant226"]
}