Back to Astron Rpa

AI Service - Intelligent Service Platform

backend/ai-service/README.md

1.1.614.0 KB
Original Source

AI Service - Intelligent Service Platform

๐Ÿ“– Project Introduction

AI Service is a comprehensive intelligent service platform built on FastAPI, integrating multiple AI capabilities and a complete points management system. The platform provides core functions including chat conversations, OCR text recognition, captcha recognition, and implements fine-grained service usage management through a points mechanism.

โœจ Key Features

  • ๐Ÿค– AI Chat Service - Integration with DeepSeek and other large language models, supporting both streaming and non-streaming conversations
  • ๐Ÿ“ OCR Text Recognition - Integration with Xunfei general text recognition API, supporting Chinese-English mixed recognition
  • ๐Ÿ” Captcha Recognition - Integration with Yunma captcha recognition service, supporting multiple captcha types
  • ๐Ÿ’ฐ Points Management System - Complete user points allocation, consumption, expiration and priority management
  • ๐Ÿ“Š Request Tracing - Complete logging and request ID tracking mechanism
  • ๐Ÿ”ง Asynchronous High Performance - Based on FastAPI async framework, supporting high concurrency requests
  • ๐Ÿณ Containerized Deployment - Complete Docker and Docker Compose deployment solution

๐Ÿš€ Core Functions

1. AI Chat Service (/v1/chat/completions)

  • Support for both streaming and non-streaming conversation modes
  • Compatible with OpenAI ChatGPT API format
  • Automatic points deduction and balance checking
  • Complete error handling and retry mechanism

2. AI Model Management (/v1/models)

  • List all supported AI models
  • Return model names, IDs and other metadata
  • Used for dynamic discovery and model selection

3. OCR Text Recognition (/ocr/general)

  • Based on Xunfei general text recognition API
  • Support for Chinese-English mixed text recognition
  • Support for handwritten and printed text
  • Support for tilted text and rare character optimization
  • Automatic points deduction mechanism

4. Captcha Recognition (/jfbym/customApi)

  • Integration with Yunma captcha recognition service
  • Support for multiple captcha types
  • High accuracy recognition
  • Pay-per-success mechanism (points deducted only on success)
  • Chinese error message return

5. Points Management System

  • Allocation Strategy: Monthly automatic distribution, manual addition, priority management
  • Consumption Strategy: Consumption by priority and expiration time
  • Expiration Management: Support for multiple expiration strategies (30 days, end of month, never expire, etc.)
  • Cache Optimization: Redis cache for user points, improving query performance

6. Admin Interface (/admin)

  • User points query and management
  • Manual points addition/deduction
  • Points usage statistics and monitoring

๐Ÿ›  Technology Stack

ComponentTechnologyVersion Requirement
Backend FrameworkFastAPI>=0.115.12
PythonPython>=3.13
DatabaseMySQL + SQLAlchemy>=2.0.41
CacheRedis>=6.1.0
Async Supportasyncio + aiomysql>=0.2.0
Config ManagementPydantic Settings>=2.9.1
ContainerizationDocker + Docker Compose-
Testing Frameworkpytest + pytest-asyncio>=8.3.5
Code QualityRuff>=0.11.11
Encryption Supportbcrypt + cryptography>=4.3.0

๐Ÿ“ Project Structure

ai-service/
โ”œโ”€โ”€ app/                          # Main application directory
โ”‚   โ”œโ”€โ”€ main.py                   # FastAPI application entry point
โ”‚   โ”œโ”€โ”€ config.py                 # Configuration management
โ”‚   โ”œโ”€โ”€ database.py               # Database connection and session management
โ”‚   โ”œโ”€โ”€ redis_op.py               # Redis connection pool management
โ”‚   โ”œโ”€โ”€ logger.py                 # Logging configuration
โ”‚   โ”œโ”€โ”€ dependencies/             # Dependency injection modules
โ”‚   โ”‚   โ”œโ”€โ”€ __init__.py          # Common dependencies
โ”‚   โ”‚   โ””โ”€โ”€ points.py            # Points-related dependencies
โ”‚   โ”œโ”€โ”€ models/                   # Data models
โ”‚   โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”‚   โ””โ”€โ”€ point.py             # Points-related models
โ”‚   โ”œโ”€โ”€ schemas/                  # Pydantic data schemas
โ”‚   โ”‚   โ”œโ”€โ”€ chat.py              # Chat interface schemas
โ”‚   โ”‚   โ”œโ”€โ”€ ocr.py               # OCR interface schemas
โ”‚   โ”‚   โ””โ”€โ”€ jfbym.py             # Captcha interface schemas
โ”‚   โ”œโ”€โ”€ routers/                  # API routes
โ”‚   โ”‚   โ”œโ”€โ”€ ocr.py               # OCR routes
โ”‚   โ”‚   โ”œโ”€โ”€ jfbym.py             # Captcha routes
โ”‚   โ”‚   โ””โ”€โ”€ v1/
โ”‚   โ”‚       โ”œโ”€โ”€ chat.py          # Chat routes
โ”‚   โ”‚       โ””โ”€โ”€ models.py        # Model management routes
โ”‚   โ”œโ”€โ”€ services/                 # Business logic services
โ”‚   โ”‚   โ””โ”€โ”€ point.py             # Points management service
โ”‚   โ”œโ”€โ”€ utils/                    # Utility functions
โ”‚   โ”‚   โ”œโ”€โ”€ ocr.py               # OCR utilities
โ”‚   โ”‚   โ””โ”€โ”€ jfbym.py             # Captcha utilities
โ”‚   โ”œโ”€โ”€ middlewares/              # Middlewares
โ”‚   โ”‚   โ””โ”€โ”€ tracing.py           # Request tracing middleware
โ”‚   โ””โ”€โ”€ internal/                 # Internal management interfaces
โ”‚       โ””โ”€โ”€ admin.py             # Admin interface
โ”œโ”€โ”€ tests/                        # Test code
โ”‚   โ”œโ”€โ”€ conftest.py              # Test configuration
โ”‚   โ”œโ”€โ”€ test_main.py             # Main application tests
โ”‚   โ””โ”€โ”€ routers/                 # Route tests
โ”œโ”€โ”€ logs/                         # Log directory (e.g., app.log)
โ”œโ”€โ”€ Dockerfile                    # Docker image build
โ”œโ”€โ”€ pyproject.toml                # Project dependency configuration
โ”œโ”€โ”€ uv.lock                       # uv dependency lock file
โ””โ”€โ”€ README.md                     # Project documentation

๐Ÿš€ Quick Start

Environment Requirements

  • Python 3.13+
  • MySQL 8.0+
  • Redis 7.0+
  • Docker & Docker Compose (optional)

1. Clone Project

bash
git clone <repository-url>
cd ai-service

2. Install Dependencies

bash
# Install using pip
pip install -e .

# Or use uv (recommended)
uv sync

It is recommended to use uv for dependency management. The uv.lock file has locked dependency versions to ensure environment consistency.

3. Configure Environment Variables

There are three configuration files, sorted by priority from low to high: .env.default < .env < .env.local, where .env.local is only used for local debugging and should never be used in production.

Edit the .env file and configure necessary environment variables:

bash
# Database configuration
DATABASE_URL=mysql+aiomysql://username:password@localhost:3306/ai_service
DATABASE_USERNAME=your_db_username
DATABASE_PASSWORD=your_db_password

# Redis configuration
REDIS_URL=redis://localhost:6379/0

# AI chat service configuration
AICHAT_BASE_URL=https://api.deepseek.com/v1/
AICHAT_API_KEY=your_deepseek_api_key

# Xunfei OCR configuration
XFYUN_APP_ID=your_xfyun_app_id
XFYUN_API_SECRET=your_xfyun_api_secret
XFYUN_API_KEY=your_xfyun_api_key

# Yunma captcha configuration
JFBYM_API_TOKEN=your_jfbym_token

# Points strategy configuration (can also use default .env.default)
MONTHLY_GRANT_AMOUNT=100000
AICHAT_POINTS_COST=100
OCR_GENERAL_POINTS_COST=50
JFBYM_POINTS_COST=10

4. Start Service

bash
uv run python run.py dev

5. Verify Service

Visit http://localhost:8010/docs to view API documentation.

๐Ÿณ Docker Deployment

Production Environment Deployment

  1. Configure Environment Variables

    Create .env file and configure relevant environment variables.

  2. Start Service

    bash
    docker-compose up -d
    
  3. Check Service Status

    bash
    docker-compose ps
    docker-compose logs -f app
    

Unit Test Environment Deployment

  1. Start Test Dependency Services

    bash
    docker-compose -f docker-compose.test.yaml up -d
    
  2. Run Tests

    bash
    pytest
    

๐Ÿ“š API Documentation

Authentication

All API requests need to include user ID in request headers:

bash
X-User-Id: 123
# or
user_id: 123

Core Interfaces

1. AI Chat Interface

POST /v1/chat/completions

json
{
  "model": "deepseek-chat",
  "messages": [
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "Hello!"}
  ],
  "stream": false,
  "temperature": 0.7,
  "max_tokens": 4096
}

2. OCR Text Recognition

POST /ocr/general

json
{
  "image": "base64_encoded_image_string",
  "encoding": "jpg",
  "status": 3
}

3. Captcha Recognition

POST /jfbym/customApi

json
{
  "type": "captcha_type",
  "image": "base64_encoded_image_string"
}

Response Format:

json
{
  "code": 10000,
  "message": "success",
  "data": {
    "code": 0,
    "data": "recognition_result"
  }
}

Error Response:

json
{
  "code": 400,
  "message": "Yunma captcha processing failed: specific error message",
  "data": null
}

4. User Points Query

GET /admin/user/points?user_id=123

5. Manual Points Addition

POST /admin/user/points?user_id=123&amount=1000

โš™๏ธ Configuration

Points Strategy Configuration

ConfigurationDefault ValueDescription
MONTHLY_GRANT_AMOUNT100000Monthly automatic points distribution amount
AICHAT_POINTS_COST100Points consumed per AI chat request
OCR_GENERAL_POINTS_COST50Points consumed per OCR recognition
JFBYM_POINTS_COST10Points consumed per captcha recognition

Points Expiration Strategy

  • Monthly distributed points: Expire at end of month
  • Manually added points: Never expire
  • Other type points: Expire after 30 days

Logging Configuration

ConfigurationDefault ValueDescription
LOG_LEVELINFOLog level (DEBUG, INFO, WARNING, ERROR)
LOG_DIR/var/log/ai_serviceLog file storage directory

๐Ÿงช Development Guide

Running Tests

bash
# Start test database
docker-compose -f docker-compose.test.yaml up -d

# Run all tests
pytest

# Run specific test file
pytest tests/routers/test_chat.py

# Run tests with coverage
pytest --cov=app

Code Quality Check

bash
# Format code
ruff format

# Check code quality
ruff check

# Fix auto-fixable issues
ruff check --fix

View Logs

bash
# Real-time view of application logs
tail -f logs/app.log

Adding New Service Modules

  1. Create new route file in app/routers/
  2. Define data schemas in app/schemas/
  3. Implement specific logic in app/utils/
  4. Register routes in app/main.py
  5. Write corresponding test cases

Adding New Points Consumption Types

  1. Add new type to PointTransactionType enum in app/models/point.py
  2. Add corresponding points consumption configuration in config file
  3. Use PointChecker dependency injection for points checking and deduction

Note: After adding new points types, database needs to be manually upgraded. This is not recommended in production.

๐Ÿ“ Logging

Log Configuration

  • Log Level: Configured via LOG_LEVEL environment variable (default: INFO)
  • Log Directory: Configured via LOG_DIR environment variable (default: /var/log/ai_service)
  • Log Format: Contains timestamp, module name, request ID, log level and message content
  • Log Rotation: Single log file max 10MB, keep 10 historical files

Request Tracing

Each request is assigned a unique Request ID for easy troubleshooting:

2025-06-03 10:30:15 - app.main - [abc-123-def] - INFO - Root endpoint accessed!

Log Output

  • Console Output: Formatted logs with request ID
  • File Output: Formatted logs with request ID, supporting log rotation
  • Error Handling: All exceptions are logged in detail for troubleshooting

โ“ FAQ

Q: How to reset user points?

A: You can manually deduct all current user points through admin interface, then re-add:

bash
# 1. Query current user points
curl -X GET "http://localhost:8010/admin/user/points?user_id=123"

# 2. Deduct all points
curl -X POST "http://localhost:8010/admin/user/points/deduct?user_id=123&amount=current_points"

# 3. Add new points
curl -X POST "http://localhost:8010/admin/user/points?user_id=123&amount=new_points"

Q: How to handle insufficient points?

A: The system automatically checks user points. If insufficient, returns 403 status code. Users need to contact admin to add points or wait for monthly automatic distribution.

Q: How to monitor service health?

A: You can monitor through:

  1. Access root path / to check if service responds normally
  2. View log files to understand detailed running status
  3. Monitor Redis and MySQL connection status
  4. Check container running status (if using Docker)

Q: How to backup and restore data?

A:

Backup:

bash
# Backup MySQL data
docker exec mysql_container mysqldump -u root -p ai_service > backup.sql

# Backup Redis data
docker exec redis_container redis-cli BGSAVE

Restore:

bash
# Restore MySQL data
docker exec -i mysql_container mysql -u root -p ai_service < backup.sql

Q: What to do when captcha recognition fails?

A: When captcha recognition fails, the system returns detailed error information:

  • Business logic errors: Returns 400 status code with specific error message
  • Network errors: Returns 503 status code, indicating service temporarily unavailable
  • Unknown errors: Returns 500 status code, indicating unknown error occurred

All error messages are in Chinese for easy user understanding.

Q: What to do if request_id is not visible in logs?

A: Ensure:

  1. Application correctly configured RequestTracingMiddleware middleware
  2. Log configuration includes RequestIdFilter
  3. Check if log format includes %(request_id)s

If problems persist, check if log configuration and middleware are correctly loaded.