> For the complete documentation index, see [llms.txt](https://developer.rpggo.ai/dev-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developer.rpggo.ai/dev-docs/player-api/api-overview/api-v1-deprecated/memory.md).

# Memory

The RPGGO game engine automatically performs a series of engineering optimizations and algorithmic recalls on the dialogue history between users and AI to enhance the quality of conversations.&#x20;

Developers have the flexibility to retrieve the raw NPC memory (dialogue history) at any time, allowing them to insert custom dialogues at appropriate intervals to enrich the memory content.

## Get Memory by Session

```bash
curl -X 'POST' \
  'https://api.rpggo.ai/open/game/memory' \
  -H 'accept: application/json' \
  -H 'Application-ID: $YOUR_APPLICATION_ID' \
  -H 'Authorization: $YOUR_BEARER_TOKEN' \
  -H 'Content-Type: application/json' \
  -d '{
  "session_id": "$SESSION_ID"
}'
```

## Retrieve the entire conversation history of the current session using the session ID.

<mark style="color:green;">`POST`</mark> `https://api.rpggo.ai/open/game/memory`

#### Request Body

| Name                                          | Type   | Description                                    |
| --------------------------------------------- | ------ | ---------------------------------------------- |
| session\_id<mark style="color:red;">\*</mark> | String | session ID, must be an ongoing game session id |

{% tabs %}
{% tab title="200: OK " %}

```json
{
  "code": 0,
  "msg": "ok",
  "data": {
    "memory_item": [
      {
        "SessionID": "$SESSION_ID",
        "Source": "discord",
        "BotIndex": "1160442433985859738",
        "Message": "How long and often how does this happen with the fridge",
        "Reply": "How long and how often does this happen with the fridge? *(rolls eyes)* Well, it seems like Karen's lunch goes missing quite frequently. Maybe she's just forgetful or someone's got a real appetite for her food. But hey, who knows, maybe it's Lamar with his holy prayers, sneaking a snack when nobody's looking. Or maybe it's Alan, the quiet immigrant driver, trying to make ends meet by taking a bite out of Karen's lunch. *(smirking)* Who knows, right?",
        "Type": 0,
        "CreateTime": "1701139538",
        "UpdateTime": "1701139538"
      },
      {
        "SessionID": "$SESSION_ID",
        "Source": "discord",
        "BotIndex": "1160442433985859738",
        "Message": "Is Lamar Muslim?",
        "Reply": "How would I know if Lamar is Muslim? Do I look like his personal religious advisor? *(rolls eyes)* It's not like he prays in the break room every day or anything. *(sarcastic tone)* But hey, who knows, maybe he's just really committed to his faith and has nothing to do with Karen's missing lunch. Or maybe it's Alan, the starving immigrant driver, who's desperate enough to steal food. *(smirking)* Who knows, right?",
        "Type": 0,
        "CreateTime": "1701139538",
        "UpdateTime": "1701139538"
      }
    ],
    "counters": {
      "1160442433985859738": 2,
      "all": 2
    }
  }
}
```

{% endtab %}

{% tab title="401: Unauthorized " %}

{% endtab %}
{% endtabs %}

Developers can retrieve the complete conversation records of the current session from the `memory_item` array in response.&#x20;

* `botIndex` corresponds to the NPC ID, allowing you to filter memories for a specific NPC using this field.&#x20;
* `message` and `reply` fields represent a user-AI dialogue
* &#x20;`type` field distinguishes memory types. 0 for normal interactions via the llm and 1 for dialogues  stored through the `open/game/recordmemory` API.

## Record Memory

```bash
curl -X 'POST' \
  'https://api.rpggo.ai/open/game/recordmemory' \
  -H 'accept: application/json' \
  -H 'Application-ID: $YOUR_APPLICATION_ID' \
  -H 'Authorization: $YOUR_BEARER_TOKEN' \
  -H 'Content-Type: application/json' \
  -d '{
  "session_id": "rs_222531704307449856_1178834756272660602_20_3d529def-d6de-4ac7-989b-ff46dc5ac3ff",
  "chats": [
    {
      "npc_id": "1160442433985859738",
      "message": "hi, this is the user message",
      "reply": "hi, this is the assistant reply"
    },
    {
      "npc_id": "1160442433985859738",
      "message": "hi, this is another user message",
      "reply": "hi, this is the assistant reply"
    }
  ]
}'
```

<mark style="color:green;">`POST`</mark> `https://api.rpggo.ai/open/game/recordmemory`&#x20;

#### Request Body

| Name                                          | Type   | Description                                                      |
| --------------------------------------------- | ------ | ---------------------------------------------------------------- |
| session\_id<mark style="color:red;">\*</mark> | String | session ID, must be an ongoing game session id                   |
| chats<mark style="color:red;">\*</mark>       | List   | A dictionary with three fields: `npc_id`, `message`, and `reply` |

{% tabs %}
{% tab title="200: OK " %}

```json
{
  "code": 0,
  "msg": "successfully saved 2 memories"
}
```

{% endtab %}

{% tab title="401: Unauthorized " %}

{% endtab %}
{% endtabs %}

Before invoking the `recordmemory` API, it is essential to initiate a game to obtain a session id or use an already ongoing game. Memories are recorded in chronological order based on the invocation of the interface, and developers can retrieve them through the  `/open/game/memory` interface.

In the `chats` field, developers can submit one or multiple conversation histories. Each dialogue history consists of three elements:&#x20;

* `npc_id` designates the associated NPC
* `message` and `reply` simulate user and AI interactions


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://developer.rpggo.ai/dev-docs/player-api/api-overview/api-v1-deprecated/memory.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
