# Architecture Overview

### **High-Level System Components**

The memory retention system consists of the following components:

<figure><img src="/files/muhOJOHp8JXpdJbaJL5w" alt="" width="563"><figcaption></figcaption></figure>

* **Frontend (Vult AI Chat UI)**
  * Users interact with AI via the chat interface.
  * Prompts and responses are stored in the AI Chat Folder.
* **Backend (AI Processing & Memory Storage)**
  * Handles user queries and processes AI-generated responses.
  * Uses Mem0 AI API for memory storage and retrieval.
  * Stores structured chat logs in a database or file system.
* **Memory Storage Layer (Mem0 AI API)**
  * Stores chat histories and user data.
  * Retrieves relevant memories based on user input and prompt context.
  * Ensures quick access to stored interactions for seamless responses.

### **Backend Implementation**

#### **1. Data Structures (Go Structs for Memory Storage)**

```go
type Chat struct {
    ShareInfoUrls       []string                `json:"share_info_urls"`
    ShareInfoIds        string                  `json:"share_info_ids"`
    Prompt             string                  `json:"prompt"`
    Model              string                  `json:"model"`
    IsFileProcessing   bool                    `json:"is_file_processing"`
    MasterPrompt       string                  `json:"master_prompt"`
    OpenAiMessages     []map[string]interface{} `json:"open_ai_messages"`
    DeepSeekMessages   []map[string]interface{} `json:"deep_seek_messages"`
    FileType           string                  `json:"file_type"`
    IsAgentInput       bool                    `json:"is_agent_input"`
    WebSearch          bool                    `json:"web_search"`
}
```

This struct represents a **chat session** in Vult AI. The key fields are:

* `Prompt`: Stores the user’s **latest query**.
* `Model`: The **AI model** used for processing.
* `OpenAiMessages`: Stores **previous interactions** with OpenAI models.
* `DeepSeekMessages`: Stores **previous interactions** with DeepSeek models.
* `IsAgentInput`: Indicates if the request is from **AI Agent** (file-based processing).

#### **2. Memory Retrieval via Mem0 AI**

```go
func retrieveMemory(userID string, prompt string) (string, error) {
    memoryClient := memory.NewMem0Client(config.Configuration.Mem0ApiKey)

    memoryResponse, err := memoryClient.SearchMemory(userID, prompt)
    if err != nil {
        logger.LogError("AI Chat: Failed to search memory", zap.Error(err))
        return "", err
    }

    if memoryResponse != nil {
        return memoryResponse.Memories, nil
    }
    return "", nil
}
```

This function:

1. **Initializes the Mem0 API client**.
2. Calls `SearchMemory()` to fetch **previously stored interactions**.
3. If memory exists, it returns **retrieved context** to be appended to the user prompt.

#### **3. AI Model Processing with Memory Context**

```go
func processChatRequest(ctx context.Context, chatInput Chat) (Response, error) {
    memoryContext, _ := retrieveMemory(chatInput.UserID, chatInput.Prompt)

    chatInput.Prompt = memoryContext + "\nUser Question: " + chatInput.Prompt

    res, err := aiAgentService.CreateChat(ctx, authHeaders, chatInput)
    if err != nil {
        ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
        return nil, err
    }

    return res, nil
}
```

This function:

1. Calls `retrieveMemory()` to fetch **relevant past interactions**.
2. Appends retrieved memory to the **current user prompt**.
3. Passes the enriched prompt to the **AI model** for response generation.
4. Returns the processed response to the **Vult AI UI**.


---

# Agent Instructions: 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:

```
GET https://docs.zus.network/zus-docs/webapps/vult/vult-ai/architecture-overview.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
