Skip to content
Go back

AI Chat Feature Configuration Guide

Comprehensively configure astro-minimax AI chat: Provider setup, RAG search, Mock mode, author profiling, and quality evaluation.

astro-minimax includes a built-in AI chat assistant with support for automatic failover across multiple providers, RAG-enhanced retrieval, streaming responses, and Mock fallback. This guide walks through the complete AI configuration process.

Overview

The AI chat system consists of the following modules:

ModuleDescription
@astro-minimax/aiAI core package: RAG pipeline, Provider management, chat UI
@astro-minimax/cliCLI tools: AI content processing, author profile building, quality evaluation
@astro-minimax/notifyNotification system: real-time AI conversation notifications to Telegram/Email/Webhook

Quick Start

1. Enable AI Features

In src/config.ts:

ai: {
  enabled: true,
  mockMode: false,
  apiEndpoint: "/api/chat",
},
typescript

2. Configure a Provider

Configure your AI Provider in .env:

# OpenAI-compatible API (supports DeepSeek, Moonshot, Qwen, etc.)
AI_BASE_URL=https://api.openai.com/v1
AI_API_KEY=your-api-key
AI_MODEL=gpt-4o-mini

# Site information
SITE_AUTHOR=YourName
SITE_URL=https://your-blog.com
bash

3. Build AI Data

astro-minimax ai process         # Generate article summaries and SEO data
astro-minimax ai profile build   # Build the author profile
bash

4. Start the Development Server

pnpm run dev
bash

The AI chat button will appear in the bottom-right corner of the page.

Provider Configuration Details

Cloudflare Workers AI

When deploying to Cloudflare Pages, you can use free Workers AI:

# wrangler.toml
[ai]
binding = "minimaxAI"
toml

Workers AI has the highest provider priority and does not require an API key.

OpenAI-Compatible API

Any OpenAI-compatible API service is supported:

AI_BASE_URL=https://api.openai.com/v1
AI_API_KEY=sk-xxx
AI_MODEL=gpt-4o-mini
bash

You can also configure different models for different tasks:

AI_KEYWORD_MODEL=gpt-4o-mini    # Keyword extraction model
AI_EVIDENCE_MODEL=gpt-4o-mini   # Evidence analysis model
bash

Failover Mechanism

flowchart LR
    Request[User Request] --> W{Workers AI}
    W -->|Success| Response[Streaming Response]
    W -->|Fails 3 Times| O{OpenAI API}
    O -->|Success| Response
    O -->|Fails| M[Mock Fallback]
    M --> Response

    style W fill:#f97316,color:#fff
    style O fill:#3b82f6,color:#fff
    style M fill:#6b7280,color:#fff
    style Response fill:#22c55e,color:#fff

AI Tool Calling and Actions

When a user expresses actions in natural language such as “switch to dark mode”, “open an article”, or “jump to a section”, the model can do more than explain the steps. Through tool calling, it can directly drive site behavior, reducing the gap between “telling” and “doing”.

Available Tools at a Glance

The current chat pipeline registers 7 tools (names consistent with the codebase):

Tool NameSummary
toggleThemeSwitch the theme between light / dark / system
navigateToArticleNavigate to an article page by slug (and optional language, section)
scrollToSectionScroll to a specified section on the current page and optionally highlight it
toggleImmersiveModeEnable or disable immersive mode, with adjustable font size and more
highlightTextHighlight content in the article by text or selector
setPreferenceWrite user preferences (aligned with the site’s preference system key-value model)
searchArticlesSearch articles and projects by keyword, returning titles, links, summaries, etc.

How It Works

Action System and Cross-Page Chaining

Site behaviors are centrally handled in packages/core/src/actions/: ActionExecutor performs concrete actions, while modules such as URLHandler support continuing a sequence of actions after navigation via query parameters (such as theme, section, ai_actions, and queue tokens), enabling cross-page action chaining. When the chat UI receives a client-side tool call, it converts the parameters into the corresponding Action and executes it.

Mock Mode

No real API is needed during development:

ai: {
  enabled: true,
  mockMode: true,  // Development environment
},
typescript

Mock mode returns predefined article recommendations and external resource links to simulate real AI responses.

AI Security Features

Source Layering Protocol

AI responses follow L1–L5 source priority:

Privacy Protection

Automatically refuses to answer sensitive personal information:

Intent Classification

7 intent categories improve search relevance:

Quality Evaluation

Configure the Test Set

Edit datas/eval/gold-set.json to define test cases:

{
  "cases": [
    {
      "id": "about-001",
      "category": "about",
      "question": "Introduce yourself",
      "answerMode": "fact",
      "expectedTopics": ["blog", "AI"],
      "forbiddenClaims": [],
      "lang": "zh"
    }
  ]
}
json

Run the Evaluation

pnpm run ai:eval                              # Local test
pnpm run ai:eval -- --url=https://your.com   # Remote test
pnpm run ai:eval -- --category=no_answer     # Evaluate a specific category
pnpm run ai:eval -- --verbose                # Verbose output
bash

The evaluation is based on the golden test set in datas/eval/gold-set.json and automatically checks:

The evaluation report is saved to datas/eval/report.json.

Extension System

The extension system allows you to inject custom data into the AI chat flow to enhance the AI’s response capabilities.

Extension Types

TypeDescriptionUse Case
searchableSearchable documentsAdd extra knowledge base content
factsStructured factsAdd verified factual data
contextContext injectionAdd custom prompt sections
voice-styleWriting styleDefine AI response style modes
semantic-fallbackSemantic fallbackQuery rewriting rules

Extension File Structure

Extension files are placed in the datas/extensions/ directory:

datas/extensions/
├── travel.json        # Travel-related extension
├── social.json        # Social network extension
└── custom-*.json      # Custom extensions
text

Extension File Format

CLI Commands

# View extension status
astro-minimax ai extensions status

# Validate extension files
astro-minimax ai extensions validate

# Build extensions (validate and organize)
astro-minimax ai extensions build --verbose

# Test loading extensions
astro-minimax ai extensions load
bash

Extension Priority

Extensions use the priority field (0–100) to control priority. The higher the value, the higher the priority. When multiple extensions provide the same type of data, the higher-priority extension is used first.

Data Lifecycle

┌─────────────────────────────────────────────────────────────┐
│ BUILD TIME                                                  │
│  datas/extensions/*.json ──→ CLI validate ──→ Registry      │
└─────────────────────────────────────────────────────────────┘

┌─────────────────────────────────────────────────────────────┐
│ REQUEST TIME                                                │
│  loadExtensions() ──→ resolveVoiceStyleMode()               │
│     ├─ getSemanticFallback(query)                           │
│     └─ mergeSearchDocuments() / mergeFacts()                │
└─────────────────────────────────────────────────────────────┘
text

Notification Integration

After an AI conversation completes, a notification is automatically sent (fire-and-forget):

# .env
NOTIFY_TELEGRAM_BOT_TOKEN=your-bot-token
NOTIFY_TELEGRAM_CHAT_ID=your-chat-id
bash

Notification content includes: user question, AI answer summary, cited articles, token usage, and timing for each stage.

See Notification System Configuration Guide for details.

Environment Variable Reference

VariableDescriptionRequired
AI_BASE_URLOpenAI-compatible API endpointRequired when using OpenAI
AI_API_KEYAPI keyRequired when using OpenAI
AI_MODELMain chat modelNo (default: gpt-4o-mini)
AI_KEYWORD_MODELKeyword extraction modelNo (same as main model)
AI_EVIDENCE_MODELEvidence analysis modelNo (same as keyword model)
SITE_AUTHORAuthor nameNo
SITE_URLSite URLNo

AI Tool Calling

The AI assistant has 7 built-in page interaction tools, cont can control the current page through conversation:

ToolDescription
toggleThemeToggle light/dark theme
navigateToArticleNavigate to a specific article
scrollToSectionScroll to a page section
toggleImmersiveModeToggle immersive mode
highlightTextHighlight text on page
setPreferenceSet user preference
searchArticlesSearch articles (server-side)

No additional configuration needed — tools are enabled automatically when AI chat is enabled. Supports registerTool() / unregisterTool() API for registering custom tools.

See AI Tool Calling Guide.

Extensions System

The AI chat extension system (packages/ai/src/extensions/) provides custom context sections, semantic fallback rules, and more:

astro-minimax ai extensions build      # Build extensions
astro-minimax ai extensions validate  # Validate extensions
astro-minimax ai extensions status    # Check extension status
bash

See AI Module Architecture.

Fact Registry

AI extracts verified facts from blog content and injects them into prompts to reduce hallucinations:

astro-minimax ai facts build      # Build fact registry
astro-minimax ai facts validate  # Validate facts
astro-minimax ai facts status    # Check status
bash

See AI Module Architecture.

The AI search system uses a TF-IDF scoring + vector reranking (RRF fusion) for hybrid search:

Structured Output

The packages/ai/src/structured-output/ module supports Schema-validated structured generation for evidence analysis and other scenarios requiring precise JSON formats. See AI Module Architecture.

Next Steps



Previous Post
Adding new posts in astro-minimax theme
Next Post
AI Tool Calling and Action System

评论区

文明评论,共建和谐社区