Files
fquiz/api/migrations/002_add_ai_chat.sql
chengkai3 21f9839dd6 feat: [FL-166] 实现AI问答功能
- 后端实现:
  - 添加 ai_chat_conversations 和 ai_chat_messages 数据模型
  - 创建 AI 问答 API 路由(/api/v1/ai-chat)
  - 实现对话管理和消息发送服务
  - 集成 OpenAI API 进行对话交互
  - 支持流式对话历史和上下文管理

- 前端实现:
  - 创建 ChatGPT 风格的聊天界面(/admin/ai-chat)
  - 支持新建、选择、删除对话
  - 实现消息发送和实时显示
  - 使用 Ant Design 组件构建响应式 UI

- 系统参数配置:
  - ai_chat.openai_api_key: OpenAI API 密钥
  - ai_chat.model: 使用的 AI 模型(默认 gpt-3.5-turbo)
  - ai_chat.base_url: API 基础 URL(支持第三方兼容接口)

- 数据库迁移:
  - 002_add_ai_chat.sql: 创建对话和消息表
  - 003_add_ai_chat_params.sql: 添加系统参数默认配置

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-authored-by: multica-agent <github@multica.ai>
2026-06-20 23:20:26 +08:00

33 lines
1.5 KiB
SQL

-- Create AI chat tables
CREATE TABLE IF NOT EXISTS ai_chat_conversations (
id SERIAL PRIMARY KEY,
title VARCHAR(256) NOT NULL DEFAULT '新对话',
user_id VARCHAR(36) NOT NULL,
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
CONSTRAINT fk_ai_chat_conversations_user_id FOREIGN KEY (user_id) REFERENCES users(user_id) ON DELETE CASCADE
);
CREATE INDEX idx_ai_chat_conversations_user_id ON ai_chat_conversations(user_id);
CREATE TABLE IF NOT EXISTS ai_chat_messages (
id SERIAL PRIMARY KEY,
conversation_id INTEGER NOT NULL,
role VARCHAR(16) NOT NULL,
content TEXT NOT NULL,
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
CONSTRAINT fk_ai_chat_messages_conversation_id FOREIGN KEY (conversation_id) REFERENCES ai_chat_conversations(id) ON DELETE CASCADE
);
CREATE INDEX idx_ai_chat_messages_conversation_id ON ai_chat_messages(conversation_id);
CREATE INDEX idx_ai_chat_messages_role ON ai_chat_messages(role);
COMMENT ON TABLE ai_chat_conversations IS 'AI问答对话会话表';
COMMENT ON TABLE ai_chat_messages IS 'AI问答消息表';
COMMENT ON COLUMN ai_chat_conversations.title IS '对话标题';
COMMENT ON COLUMN ai_chat_conversations.user_id IS '用户ID';
COMMENT ON COLUMN ai_chat_messages.conversation_id IS '对话ID';
COMMENT ON COLUMN ai_chat_messages.role IS '角色:user 或 assistant';
COMMENT ON COLUMN ai_chat_messages.content IS '消息内容';