在 AI 应用开发中,处理大量并发请求时,同步调用方式往往会导致响应超时、资源耗尽等问题。本文将深入探讨如何基于 Celery + Redis 构建生产级别的异步任务队列,结合 HolySheep AI API 实现高吞吐量、低延迟的 AI 请求处理架构。

一、为什么需要异步队列处理 AI 请求

当我们构建 AI 应用(如智能客服、内容生成、批量翻译等)时,面临以下挑战:

异步队列设计的核心价值在于:解耦请求与处理、实现削峰填谷、支持水平扩展。结合 HolySheep AI 国内直连<50ms 的低延迟特性,异步处理如虎添翼。

二、整体架构设计

2.1 系统架构图

┌─────────────┐    ┌─────────────┐    ┌─────────────┐
│   Client    │───▶│   FastAPI   │───▶│   Redis     │
│  (HTTP/WS)  │    │   (Web)     │    │  (Broker)   │
└─────────────┘    └─────────────┘    └──────┬──────┘
                                              │
                   ┌─────────────┐    ┌──────▼──────┐
                   │   Celery    │◀───│   Worker    │
                   │   Backend   │    │  (Python)   │
                   └──────┬──────┘    └──────┬──────┘
                          │                  │
                   ┌──────▼──────────────────▼──────┐
                   │      HolySheep AI API          │
                   │   (国内直连 · 汇率优势)         │
                   └─────────────────────────────────┘

2.2 核心组件职责

三、项目初始化与依赖配置

3.1 安装依赖

pip install celery[redis] redis fastapi uvicorn httpx openai

3.2 Celery 配置 (celery_config.py)

import os
from celery import Celery

Redis 连接配置

REDIS_URL = os.getenv("REDIS_URL", "redis://localhost:6379/0")

HolySheep AI API 配置

HOLYSHEEP_API_KEY = os.getenv("HOLYSHEEP_API_KEY", "YOUR_HOLYSHEEP_API_KEY") HOLYSHEEP_BASE_URL = "https://api.holysheep.ai/v1"

Celery 应用初始化

celery_app = Celery( "ai_tasks", broker=REDIS_URL, backend=REDIS_URL, include=["tasks"] # 任务模块 )

性能调优配置

celery_app.conf.update( # Worker 配置 worker_prefetch_multiplier=4, # 预取任务数 worker_max_tasks_per_child=1000, # 防止内存泄漏 task_acks_late=True, # 任务完成后确认 task_reject_on_worker_lost=True, # Worker 崩溃时重新入队 # 并发控制 worker_concurrency=8, # 每 Worker 并发数 task_time_limit=300, # 任务硬性超时 (秒) task_soft_time_limit=240, # 任务软性超时 (秒) # 结果存储 result_expires=3600, # 结果过期时间 (秒) result_extended=True, # 存储更多元数据 # 队列配置 task_default_queue="ai_processing", task_routes={ "tasks.chat_completion": {"queue": "ai_chat"}, "tasks.batch_embedding": {"queue": "ai_embedding"}, }, # 重试策略 task_default_retry_delay=60, task_max_retries=3, )

四、AI 任务定义与实现

4.1 HolySheep AI 客户端封装

import httpx
from typing import Optional, List, Dict, Any
import asyncio

class HolySheepAIClient:
    """HolySheep AI API 异步客户端封装"""
    
    def __init__(self, api_key: str, base_url: str = "https://api.holysheep.ai/v1"):
        self.api_key = api_key
        self.base_url = base_url
        self._client: Optional[httpx.AsyncClient] = None
    
    async def __aenter__(self):
        # 连接池配置,优化并发性能
        self._client = httpx.AsyncClient(
            base_url=self.base_url,
            headers={"Authorization": f"Bearer {self.api_key}"},
            timeout=httpx.Timeout(60.0, connect=10.0),
            limits=httpx.Limits(max_keepalive_connections=20, max_connections=100)
        )
        return self
    
    async def __aexit__(self, exc_type, exc_val, exc_tb):
        if self._client:
            await self._client.aclose()
    
    async def chat_completion(
        self,
        messages: List[Dict[str, str]],
        model: str = "gpt-4.1",
        temperature: float = 0.7,
        max_tokens: int = 2048,
        **kwargs
    ) -> Dict[str, Any]:
        """发送聊天补全请求"""
        payload = {
            "model": model,
            "messages": messages,
            "temperature": temperature,
            "max_tokens": max_tokens,
            **kwargs
        }
        
        response = await self._client.post("/chat/completions", json=payload)
        response.raise_for_status()
        return response.json()
    
    async def batch_chat(self, requests: List[Dict]) -> List[Dict]:
        """批量处理聊天请求 - 提升吞吐量"""
        tasks = [
            self.chat_completion(**req) for req in requests
        ]
        return await asyncio.gather(*tasks, return_exceptions=True)


全局客户端实例(连接池复用)

_ai_client: Optional[HolySheepAIClient] = None def get_ai_client() -> HolySheepAIClient: global _ai_client if _ai_client is None: _ai_client = HolySheepAIClient( api_key="YOUR_HOLYSHEEP_API_KEY" ) return _ai_client

4.2 Celery 任务定义 (tasks.py)

from celery_config import celery_app
from holy_sheep_client import HolySheepAIClient
import asyncio
import logging
from typing import List, Dict
from functools import wraps
import hashlib

logger = logging.getLogger(__name__)

def async_task(func):
    """装饰器:将异步函数适配为 Celery 同步任务"""
    @wraps(func)
    def wrapper(*args, **kwargs):
        loop = asyncio.get_event_loop()
        return loop.run_until_complete(func(*args, **kwargs))
    return wrapper

@celery_app.task(bind=True, max_retries=3, default_retry_delay=30)
def chat_completion_task(
    self,
    messages: List[Dict[str, str]],
    model: str = "gpt-4.1",
    temperature: float = 0.7,
    user_id: str = None,
    **kwargs
):
    """
    AI 聊天补全任务
    
    Args:
        messages: 对话消息列表
        model: 模型名称 (gpt-4.1, claude-sonnet-4.5, gemini-2.5-flash, deepseek-v3.2)
        temperature: 温度参数
        user_id: 用户标识(用于缓存)
    """
    # 请求缓存 key(避免重复请求)
    cache_key = f"chat_cache:{user_id}:{hashlib.md5(str(messages).encode()).hexdigest()}"
    
    try:
        async def _execute():
            async with HolySheepAIClient(
                api_key="YOUR_HOLYSHEEP_API_KEY"
            ) as client:
                result = await client.chat_completion(
                    messages=messages,
                    model=model,
                    temperature=temperature,
                    **kwargs
                )
                return result
        
        result = asyncio.run(_execute())
        
        logger.info(
            f"Task {self.request.id} completed: model={model}, "
            f"tokens={result.get('usage', {}).get('total_tokens', 0)}"
        )
        
        return {
            "status": "success",
            "task_id": self.request.id,
            "result": result
        }
        
    except Exception as exc:
        logger.error(f"Task {self.request.id} failed: {str(exc)}")
        raise self.retry(exc=exc)

@celery_app.task(bind=True)
def batch_chat_completion_task(
    self,
    requests: List[Dict],
    batch_size: int = 10
):
    """
    批量聊天补全任务 - 优化成本
    
    适用场景:批量内容生成、批量翻译、批量摘要
    结合 HolySheep 汇率优势(¥1=$1),大幅降低批量处理成本
    """
    results = []
    total_tokens = 0
    
    try:
        async def _execute_batch():
            nonlocal total_tokens
            
            async with HolySheepAIClient(
                api_key="YOUR_HOLYSHEEP_API_KEY"
            ) as client:
                # 分批处理,控制并发
                for i in range(0, len(requests), batch_size):
                    batch = requests[i:i + batch_size]
                    
                    batch_results = await client.batch_chat(batch)
                    
                    for idx, result in enumerate(batch_results):
                        if isinstance(result, Exception):
                            results.append({
                                "index": i + idx,
                                "status": "error",
                                "error": str(result)
                            })
                        else:
                            results.append({
                                "index": i + idx,
                                "status": "success",
                                "result": result
                            })
                            total_tokens += result.get("usage", {}).get("total_tokens", 0)
                    
                    # 批次间短暂延迟,避免限流
                    if i + batch_size < len(requests):
                        await asyncio.sleep(0.1)
                
                return results
        
        results = asyncio.run(_execute_batch())
        
        logger.info(
            f"Batch task {self.request.id} completed: "
            f"total={len(requests)}, success={len([r for r in results if r['status']=='success'])}"
        )
        
        return {
            "status": "completed",
            "task_id": self.request.id,
            "results": results,
            "total_tokens": total_tokens
        }
        
    except Exception as exc:
        logger.error(f"Batch task {self.request.id} failed: {str(exc)}")
        return {
            "status": "failed",
            "task_id": self.request.id,
            "error": str(exc)
        }

五、API 接口层实现

from fastapi import FastAPI, HTTPException, BackgroundTasks
from pydantic import BaseModel
from typing import List, Optional
from tasks import chat_completion_task, batch_chat_completion_task
from celery.result import AsyncResult
import redis
import json

app = FastAPI(title="AI 异步处理服务")

Redis 连接(用于结果缓存)

redis_client = redis.Redis(host="localhost", port=6379, db=1, decode_responses=True) class ChatRequest(BaseModel): messages: List[dict] model: str = "gpt-4.1" temperature: float = 0.7 max_tokens: int = 2048 user_id: Optional[str] = None class BatchChatRequest(BaseModel): requests: List[dict] batch_size: int = 10 model: str = "gpt-4.1" @app.post("/api/v1/chat/async") async def create_chat_task(request: ChatRequest): """ 创建异步聊天任务 立即返回 task_id,支持后续查询结果 """ # 参数验证 valid_models = ["gpt-4.1", "claude-sonnet-4.5", "gemini-2.5-flash", "deepseek-v3.2"] if request.model not in valid_models: raise HTTPException(status_code=400, detail=f"不支持的模型: {request.model}") # 提交 Celery 任务 task = chat_completion_task.delay( messages=request.messages, model=request.model, temperature=request.temperature, max_tokens=request.max_tokens, user_id=request.user_id ) return { "code": 0, "message": "任务已提交", "data": { "task_id": str(task.id), "status": "PENDING", "status_url": f"/api/v1/task/{task.id}" } } @app.get("/api/v1/task/{task_id}") async def get_task_result(task_id: str): """查询任务执行结果""" # 从 Redis 获取 Celery 结果 cache_key = f"celery_result:{task_id}" cached = redis_client.get(cache_key) if cached: return { "code": 0, "message": "success", "data": json.loads(cached) } # 查询 Celery 任务状态 task_result = AsyncResult(task_id) if task_result.ready(): if task_result.successful(): result = task_result.result # 缓存结果(1小时) redis_client.setex(cache_key, 3600, json.dumps(result)) return { "code": 0, "message": "success", "data": result } else: return { "code": 500, "message": "任务执行失败", "data": {"error": str(task_result.result)} } else: return { "code": 0, "message": "任务进行中", "data": {"status": task_result.state} } @app.post("/api/v1/chat/batch") async def create_batch_chat_task(request: BatchChatRequest): """创建批量聊天任务 - 享受 HolySheep 汇率优势(¥1=$1)""" if len(request.requests) > 1000: raise HTTPException(status_code=400, detail="单次批量请求不超过1000条") task = batch_chat_completion_task.delay( requests=request.requests, batch_size=request.batch_size, model=request.model ) return { "code": 0, "message": "批量任务已提交", "data": { "task_id": str(task.id), "total_requests": len(request.requests), "status_url": f"/api/v1/task/{task.id}" } } @app.get("/api/v1/models") async def list_available_models(): """获取可用模型列表及价格(基于 HolySheep AI)""" return { "code": 0, "data": { "models": [ {"id": "deepseek-v3.2", "name": "DeepSeek V3.2", "price_per_mtok": 0.42},