Add conversational memory for AI agents

更新时间:
复制 MD 格式

AI agents can use previous conversation history to deliver more coherent, context-aware responses across multiple interactions.

What is conversational memory

Conversational memory allows a system to record and recall previous interactions across one or more sessions. The agent can reference past messages in ongoing and future conversations to better understand and respond to user needs.

How to add conversational memory

Call the StartAIAgentInstance or GenerateAIAgentCall operation to start an AI agent instance. Configure AIAgentConfig.LlmConfig.LlmHistory to pass context information and previous messages to the agent. For more information about the AIAgentConfig parameter, see AIAgentConfig.

The following table describes the data structure of the LlmHistory parameter.

Item

Type

Description

Sample value

LlmHistory

Array

The context and messages from past interactions with large language models (LLMs) or multimodal large language models (MLLMs).

  • Role

String

The role of the conversation participant. Valid values:

  • user

  • assistant

  • system

  • function

  • plugin

  • tool

user

  • Content

String

The conversation text that captures what the role said or responded.

Hello

Example

[{
  "Role": "user",
  "Content": "I am in Beijing. Please introduce the history of Beijing."
},{
  "Role": "assistant",
  "Content": "Beijing has a history of over 3,000 years. It became the national political center starting from the Yuan Dynasty and further developed into a world-class metropolis during the Ming and Qing Dynasties. As the capital of multiple dynasties, Beijing witnessed the long history and cultural prosperity of China.",
},{
  "Role": "user",
  "Content": "Introduce the attractions here."
},{
  "Role": "assistant",
  "Content": "Beijing has many famous attractions. The Forbidden City is the world's largest ancient palace complex, showcasing the splendor of ancient Chinese royalty. The Great Wall, particularly the Badaling section, is one of China's symbols, attracting countless tourists with its magnificent scenery and long history."
}]
Note

For information about how to retrieve the conversation records, see Retrieve call records. You must reconstruct the retrieved records into the specified format.

After you pass the LlmHistory parameter to the agent, it can better understand your queries and generate accurate, context-relevant responses.

For example, you can query:

How is the climate here?

The agent retrieves your location from the chat history and responds:

Beijing has a temperate semi-humid and semi-arid climate with distinct seasons. Winters are cold and dry, summers are hot and rainy, and springs and autumns are relatively short but pleasant. The annual average temperature is about 12°C, with summer highs reaching over 35°C and winter lows dropping to around -10°C.