> For the complete documentation index, see [llms.txt](https://docs.zus.network/zus-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.zus.network/zus-docs/webapps/vult/vult-ai/architecture-overview.md).

# 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**.
