Administrator affdc5e3a6 问题概述
1.保存消息和计费逻辑存在耦合
2.修改计费逻辑:
按次计费被阈值限制:旧逻辑把 TIMES 分支放在 totalTokens ≥ 100 的大分支里,导致没到100 token时不扣费,违背“每次调用就扣费”的语义。
token累计不当:TIMES 分支只扣费不处理累计,同时在 totalTokens < 100 时不会进入任何TIMES逻辑,累计会无意义增长。
粒度不稳定:TOKEN 计费一旦达阈值就把 total 全扣完并清零,不利于对账与用户体验。
打印方式:使用 System.out.println,不利于生产追踪。

改动要点
1.新增独立方法
saveMessage(ChatRequest): 只落库。
publishBillingEvent(ChatRequest): 只发布异步计费事件。
保留组合方法 saveMessageAndPublishEvent(ChatRequest) 以便需要一行调用时使用。
调用处已改为“先保存,再发布事件”
SseServiceImpl: 先 saveMessage,再 publishBillingEvent。
SSEEventSourceListener: 同上。
DifyServiceImpl: 同上。

2.计费模式分流:
TIMES:每次调用直接扣费,不累计。
TOKEN:按阈值(100)批量扣费,保留余数,账单颗粒稳定。
保留余数:total = prev + delta;billable = floor(total/threshold)threshold;remainder = total % threshold。
日志替换:统一使用 log.debug。
结构更清晰、可维护。
所有金额计算统一用 BigDecimal,保留两位小数,RoundingMode.HALF_UP
按次计费:每次直接扣费(BigDecimal),边界转 Double
按 token 计费:按阈值批量结算,保留余数;费用=单价(BigDecimal)×可结算token数
1. 消息分类存储
用户消息:role="user", deductCost=null, totalTokens=本次token数, remark="用户消息"
系统账单:role="system", deductCost=实际扣费, totalTokens=计费token数, remark="TIMES_BILLING/TOKEN_BILLING"
2. 数据流程
用户发送消息 → 预检查余额 → 保存用户消息 → 发布计费事件 → 异步扣费 → 保存账单记录
2025-08-14 14:00:48 +08:00
2025-07-08 14:31:01 +08:00
2025-07-07 12:36:53 +08:00
2025-08-14 14:00:48 +08:00
2025-08-02 21:25:16 +08:00
2025-07-07 12:33:06 +08:00
2025-05-11 19:45:57 +08:00
2024-01-16 12:38:04 +08:00
2025-05-24 16:18:18 +08:00
2025-07-10 23:00:04 +08:00
2025-07-10 23:00:04 +08:00

RuoYi AI

Contributors Forks Stargazers Issues MIT License

RuoYi AI Logo

Enterprise-Grade AI Assistant Platform

Production-ready AI platform with deep integration of FastGPT, Coze, DIFY and advanced RAG technology

📖 中文文档 | 📚 Documentation | 🚀 Live Demo | 🐛 Report Bug | 💡 Request Feature

Key Features

🤖 Advanced AI Engine

  • Multi-Model Support: OpenAI GPT-4, Azure, ChatGLM, Qwen, ZhipuAI
  • AI Platform Integration: Deep integration with FastGPT, Coze, DIFY and other leading AI platforms
  • Spring AI MCP Integration: Extensible tool ecosystem with Model Context Protocol
  • Streaming Chat: Real-time SSE/WebSocket communication
  • AI Copilot: Intelligent code analysis and project scaffolding

🌟 AI Platform Ecosystem

  • FastGPT Deep Integration: Native FastGPT API support with knowledge base retrieval, workflow orchestration and context management
  • Coze Official SDK: Integration with ByteDance Coze platform official SDK, supporting Bot conversations and streaming responses
  • DIFY Full Compatibility: Using DIFY Java Client for app orchestration, workflows and knowledge base management
  • Unified Chat Interface: Standardized chat service interface supporting seamless platform switching and load balancing

🧠 Enterprise RAG Solution

  • Local Knowledge Base: Langchain4j + BGE-large-zh-v1.5 embeddings
  • Vector Database Support: Milvus, Weaviate, Qdrant
  • Privacy-First: On-premise deployment with local LLM support
  • Ollama & vLLM Compatible: Flexible model deployment options

🎨 Creative AI Tools

  • AI Art Generation: DALL·E-3, MidJourney, Stable Diffusion integration
  • PPT Creation: Automated slide generation from text input
  • Multi-Modal Processing: Text, image, and document understanding

🚀 Quick Start

Live Demo

Source Code

Component GitHub Gitee GitCode
Backend API ruoyi-ai ruoyi-ai ruoyi-ai
User Frontend ruoyi-web ruoyi-web ruoyi-web
Admin Frontend ruoyi-admin ruoyi-admin ruoyi-admin

Collaborative Projects

Project Description GitHub Repository Gitee Repository
Simplified Frontend ruoyi-element-ai ruoyi-element-ai

🛠️ Tech Stack

Core Framework

  • Backend: Spring Boot 3.4, Spring AI, Langchain4j
  • Database: MySQL 8.0, Redis, Vector Databases (Milvus/Weaviate/Qdrant)
  • Frontend: Vue 3, Vben Admin, Naive UI
  • Authentication: Sa-Token, JWT

System Components

  • File Processing: PDF, Word, Excel parsing, intelligent image analysis
  • Real-time Communication: WebSocket real-time communication, SSE streaming
  • System Monitoring: Comprehensive logging, performance monitoring, health checks

📚 Documentation

For detailed setup, configuration, and development guides, visit our comprehensive documentation:

📖 Official Documentation

🤝 Contributing

We welcome contributions from developers of all skill levels! Whether you're fixing bugs, adding features, or improving documentation, your help is appreciated.

How to Contribute

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Please submit PRs to GitHub - they will be synchronized to other platforms automatically.

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

🙏 Acknowledgments

Special thanks to these amazing open source projects:

🌐 Ecosystem Partners

  • PPIO Cloud - Cost-effective GPU containers and model APIs

💬 Community

WeChat
Add Author WeChat
Scan to join learning group
QQ Group
QQ Group
Technical discussion

Star this repo🍴 Fork it📖 中文文档📚 Documentation

Built with ❤️ by the RuoYi AI community

Description
No description provided
Readme MIT 66 MiB
Languages
Java 94.9%
Shell 3.7%
JavaScript 0.8%
CSS 0.3%
HTML 0.2%