尧图网站设计 尧图网站设计YAOTU DESIGN
ARTICLE DETAIL

资讯详情

深耕网站设计与一线实操的经验洞察。

Qwen3-32B开源模型实战教程:API服务接入FastAPI中间件实现认证授权

Qwen3-32B开源模型实战教程:API服务接入FastAPI中间件实现认证授权 Qwen3-32B开源模型实战教程API服务接入FastAPI中间件实现认证授权1. 环境准备与快速部署本教程基于RTX 4090D 24GB显存显卡和CUDA 12.4优化环境为您展示如何快速部署Qwen3-32B模型并实现API服务的认证授权功能。1.1 硬件与镜像要求推荐配置GPURTX 4090/4090D 24GB显存内存≥120GBCPU10核心以上存储系统盘50GB 数据盘40GB预装环境Python 3.10PyTorch 2.0 (CUDA 12.4编译版)Transformers/Accelerate/vLLM/FlashAttention-2一键启动脚本1.2 快速启动API服务# 进入工作目录 cd /workspace # 启动API服务 bash start_api.sh服务启动后您可以通过以下地址访问API文档http://localhost:8001/docs默认端口80012. FastAPI中间件基础配置2.1 安装必要依赖pip install fastapi uvicorn python-jose[cryptography] passlib[bcrypt] python-multipart2.2 创建基础FastAPI应用from fastapi import FastAPI, Depends, HTTPException from fastapi.security import OAuth2PasswordBearer app FastAPI() oauth2_scheme OAuth2PasswordBearer(tokenUrltoken) app.get(/) async def root(): return {message: Qwen3-32B API Service} app.get(/items/) async def read_items(token: str Depends(oauth2_scheme)): return {token: token}3. 实现认证授权中间件3.1 用户认证模块from datetime import datetime, timedelta from jose import JWTError, jwt from passlib.context import CryptContext # 安全配置 SECRET_KEY your-secret-key ALGORITHM HS256 ACCESS_TOKEN_EXPIRE_MINUTES 30 pwd_context CryptContext(schemes[bcrypt], deprecatedauto) def verify_password(plain_password, hashed_password): return pwd_context.verify(plain_password, hashed_password) def get_password_hash(password): return pwd_context.hash(password) def create_access_token(data: dict, expires_delta: timedelta None): to_encode data.copy() if expires_delta: expire datetime.utcnow() expires_delta else: expire datetime.utcnow() timedelta(minutes15) to_encode.update({exp: expire}) encoded_jwt jwt.encode(to_encode, SECRET_KEY, algorithmALGORITHM) return encoded_jwt3.2 认证中间件实现from fastapi import Request, HTTPException from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials class JWTBearer(HTTPBearer): def __init__(self, auto_error: bool True): super(JWTBearer, self).__init__(auto_errorauto_error) async def __call__(self, request: Request): credentials: HTTPAuthorizationCredentials await super(JWTBearer, self).__call__(request) if credentials: if not credentials.scheme Bearer: raise HTTPException(status_code403, detailInvalid authentication scheme.) if not self.verify_jwt(credentials.credentials): raise HTTPException(status_code403, detailInvalid token or expired token.) return credentials.credentials else: raise HTTPException(status_code403, detailInvalid authorization code.) def verify_jwt(self, jwtoken: str) - bool: try: payload jwt.decode(jwtoken, SECRET_KEY, algorithms[ALGORITHM]) return bool(payload) except: return False4. 集成Qwen3-32B模型API4.1 保护模型推理端点from fastapi import Depends from typing import Optional app.post(/generate/) async def generate_text( prompt: str, max_length: Optional[int] 100, token: str Depends(JWTBearer()) ): # 加载模型和tokenizer input_ids tokenizer(prompt, return_tensorspt).input_ids.to(cuda) # 生成文本 outputs model.generate( input_ids, max_lengthmax_length, do_sampleTrue, top_p0.9, temperature0.7 ) # 解码输出 generated_text tokenizer.decode(outputs[0], skip_special_tokensTrue) return {generated_text: generated_text}4.2 完整API服务示例from fastapi import FastAPI, Depends, HTTPException, status from fastapi.security import OAuth2PasswordRequestForm app FastAPI() # 模拟用户数据库 fake_users_db { admin: { username: admin, hashed_password: get_password_hash(secret), disabled: False, } } app.post(/token) async def login(form_data: OAuth2PasswordRequestForm Depends()): user_dict fake_users_db.get(form_data.username) if not user_dict: raise HTTPException(status_code400, detailIncorrect username or password) if not verify_password(form_data.password, user_dict[hashed_password]): raise HTTPException(status_code400, detailIncorrect username or password) access_token create_access_token( data{sub: form_data.username}, expires_deltatimedelta(minutesACCESS_TOKEN_EXPIRE_MINUTES) ) return {access_token: access_token, token_type: bearer} app.get(/users/me) async def read_users_me(token: str Depends(JWTBearer())): try: payload jwt.decode(token, SECRET_KEY, algorithms[ALGORITHM]) username: str payload.get(sub) if username is None: raise HTTPException(status_code400, detailInvalid token) except JWTError: raise HTTPException(status_code400, detailInvalid token) user fake_users_db.get(username) if user is None: raise HTTPException(status_code400, detailUser not found) return user5. 部署与优化建议5.1 生产环境部署# 使用GunicornUvicorn部署 gunicorn -w 4 -k uvicorn.workers.UvicornWorker main:app --bind 0.0.0.0:80015.2 性能优化技巧模型加载优化model AutoModelForCausalLM.from_pretrained( model_path, torch_dtypeauto, device_mapauto, trust_remote_codeTrue, load_in_4bitTrue # 4位量化减少显存占用 )API响应缓存from fastapi_cache import FastAPICache from fastapi_cache.backends.redis import RedisBackend from fastapi_cache.decorator import cache app.get(/cached_generate/) cache(expire60) # 缓存60秒 async def cached_generate(prompt: str): # 生成逻辑...速率限制from fastapi import Request from fastapi.middleware import Middleware from slowapi import Limiter from slowapi.util import get_remote_address limiter Limiter(key_funcget_remote_address) app.state.limiter limiter app.get(/limited_generate/) limiter.limit(5/minute) async def limited_generate(request: Request, prompt: str): # 生成逻辑...6. 总结通过本教程我们实现了快速部署利用优化镜像快速启动Qwen3-32B API服务安全认证基于JWT实现了完整的认证授权流程生产就绪提供了性能优化和部署建议灵活扩展中间件架构便于添加更多安全功能这套方案特别适合企业级私有部署场景既能保证模型能力的高效利用又能满足基本的安全需求。获取更多AI镜像想探索更多AI镜像和应用场景访问 CSDN星图镜像广场提供丰富的预置镜像覆盖大模型推理、图像生成、视频生成、模型微调等多个领域支持一键部署。
返回列表