CasRel关系抽取部署教程Nginx反向代理FastAPI封装成RESTful API服务1. 前言为什么需要API服务化部署如果你已经成功运行了CasRel关系抽取模型可能会发现一个问题每次使用都需要打开Python环境运行测试脚本。这在生产环境中显然不够用——我们需要一个随时可用、稳定可靠的服务。本文将带你完成从本地模型到完整API服务的升级通过FastAPI提供专业的RESTful接口再用Nginx做反向代理和负载均衡。学完本教程你将拥有一个支持高并发的API服务具备负载均衡能力可通过HTTP请求随时调用生产环境可用的关系抽取服务2. 环境准备与项目结构2.1 系统要求确保你的系统满足以下要求Ubuntu 18.04 或 CentOS 7Python 3.8推荐3.11至少8GB内存关系抽取比较耗内存已安装Docker可选但推荐2.2 项目目录结构我们先创建清晰的项目结构casrel-api-service/ ├── app/ │ ├── main.py # FastAPI主应用 │ ├── models.py # 数据模型定义 │ ├── utils.py # 工具函数 │ └── requirements.txt # Python依赖 ├── nginx/ │ └── nginx.conf # Nginx配置文件 ├── logs/ # 日志目录 └── docker-compose.yml # Docker编排文件可选3. 使用FastAPI构建RESTful接口3.1 安装所需依赖创建app/requirements.txt文件fastapi0.104.1 uvicorn0.24.0 modelscope1.10.0 torch2.1.0 transformers4.35.0 pydantic2.5.0 python-multipart0.0.6安装依赖pip install -r app/requirements.txt3.2 创建FastAPI应用编写app/main.pyfrom fastapi import FastAPI, HTTPException from pydantic import BaseModel from modelscope.pipelines import pipeline from modelscope.utils.constant import Tasks import logging import time # 配置日志 logging.basicConfig(levellogging.INFO) logger logging.getLogger(__name__) app FastAPI( titleCasRel关系抽取API, description基于CasRel框架的关系抽取RESTful API服务, version1.0.0 ) # 定义请求模型 class ExtractionRequest(BaseModel): text: str max_length: int 512 # 定义响应模型 class RelationTriplet(BaseModel): subject: str relation: str object: str class ExtractionResponse(BaseModel): triplets: list[RelationTriplet] processing_time: float text_length: int # 全局模型实例 relation_pipeline None app.on_event(startup) async def load_model(): 启动时加载模型 global relation_pipeline try: logger.info(正在加载CasRel模型...) start_time time.time() relation_pipeline pipeline( taskTasks.relation_extraction, modeldamo/nlp_bert_relation-extraction_chinese-base ) load_time time.time() - start_time logger.info(f模型加载完成耗时: {load_time:.2f}秒) except Exception as e: logger.error(f模型加载失败: {str(e)}) raise e app.get(/) async def root(): 健康检查端点 return {status: healthy, service: CasRel Relation Extraction API} app.post(/extract, response_modelExtractionResponse) async def extract_relations(request: ExtractionRequest): 关系抽取主端点 if not relation_pipeline: raise HTTPException(status_code503, detail模型未就绪) if not request.text.strip(): raise HTTPException(status_code400, detail输入文本不能为空) # 处理长文本 processed_text request.text[:request.max_length] start_time time.time() try: result relation_pipeline(processed_text) processing_time time.time() - start_time # 格式化结果 triplets [] if triplets in result: for triplet in result[triplets]: triplets.append(RelationTriplet( subjecttriplet.get(subject, ), relationtriplet.get(relation, ), objecttriplet.get(object, ) )) logger.info(f成功处理文本提取到 {len(triplets)} 个关系三元组) return ExtractionResponse( tripletstriplets, processing_timeprocessing_time, text_lengthlen(processed_text) ) except Exception as e: logger.error(f关系抽取失败: {str(e)}) raise HTTPException(status_code500, detailf处理失败: {str(e)}) app.get(/health) async def health_check(): 健康检查 return { status: healthy, model_loaded: relation_pipeline is not None, timestamp: time.time() }3.3 测试FastAPI应用启动服务进行测试cd app uvicorn main:app --host 0.0.0.0 --port 8000 --reload现在访问 http://localhost:8000/docs 可以看到自动生成的API文档。4. 使用Nginx配置反向代理4.1 安装NginxUbuntu系统sudo apt update sudo apt install nginxCentOS系统sudo yum install epel-release sudo yum install nginx4.2 配置Nginx反向代理创建nginx/nginx.conf配置文件# 工作进程数通常设置为CPU核心数 worker_processes auto; # 错误日志路径 error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { # 每个工作进程的最大连接数 worker_connections 1024; # 使用epoll高效处理连接Linux use epoll; # 允许同时接受多个连接 multi_accept on; } http { # 基础设置 include /etc/nginx/mime.types; default_type application/octet-stream; # 日志格式 log_format main $remote_addr - $remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for; access_log /var/log/nginx/access.log main; # 性能优化 sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; # Gzip压缩 gzip on; gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xmlrss text/javascript; # 上游服务配置负载均衡 upstream casrel_api { # 可以配置多个后端实例实现负载均衡 server 127.0.0.1:8000 weight1; # server 127.0.0.1:8001 weight1; # 第二个实例 # server 127.0.0.1:8002 weight1; # 第三个实例 # 最少连接数负载均衡 least_conn; # 保持连接 keepalive 32; } # HTTP服务器配置 server { listen 80; server_name localhost; # 静态文件服务可选 location /static/ { alias /path/to/static/files; expires 30d; } # API路由配置 location /api/ { # 反向代理到FastAPI proxy_pass http://casrel_api/; # 代理头设置 proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; # 超时设置 proxy_connect_timeout 30s; proxy_send_timeout 30s; proxy_read_timeout 30s; # 缓冲设置 proxy_buffering on; proxy_buffer_size 4k; proxy_buffers 8 4k; # 启用WebSocket支持如果需要 proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection upgrade; } # 健康检查端点 location /health { proxy_pass http://casrel_api/health; proxy_set_header Host $host; access_log off; } # 根路径重定向到API文档 location / { proxy_pass http://casrel_api/; proxy_set_header Host $host; } # 错误页面 error_page 500 502 503 504 /50x.html; location /50x.html { root /usr/share/nginx/html; } } }4.3 启动Nginx服务应用配置并启动Nginx# 检查配置文件语法 sudo nginx -t # 重新加载配置 sudo nginx -s reload # 或者重启Nginx sudo systemctl restart nginx5. 完整的部署脚本创建一键部署脚本deploy.sh#!/bin/bash set -e echo 开始部署CasRel API服务... # 创建目录结构 mkdir -p casrel-api-service/{app,nginx,logs} cd casrel-api-service echo 安装Python依赖... pip install -r app/requirements.txt echo 启动FastAPI服务... cd app uvicorn main:app --host 0.0.0.0 --port 8000 --workers 4 echo 配置Nginx... sudo cp ../nginx/nginx.conf /etc/nginx/nginx.conf sudo nginx -t sudo systemctl restart nginx echo 部署完成 echo API服务地址: http://localhost/api echo 文档地址: http://localhost/api/docs echo 健康检查: http://localhost/health给脚本添加执行权限chmod x deploy.sh6. 测试API服务6.1 使用curl测试# 健康检查 curl http://localhost/health # 关系抽取测试 curl -X POST http://localhost/api/extract \ -H Content-Type: application/json \ -d { text: 马斯克是特斯拉和SpaceX的CEO他出生于南非比勒陀利亚。, max_length: 500 }6.2 使用Python客户端测试创建测试脚本test_api.pyimport requests import json def test_casrel_api(): url http://localhost/api/extract test_text 乔布斯是苹果公司的创始人之一他于1955年2月24日在美国旧金山出生。 payload { text: test_text, max_length: 512 } headers { Content-Type: application/json } try: response requests.post(url, jsonpayload, headersheaders, timeout30) response.raise_for_status() result response.json() print(API响应:) print(json.dumps(result, indent2, ensure_asciiFalse)) return result except requests.exceptions.RequestException as e: print(f请求失败: {e}) return None if __name__ __main__: test_casrel_api()7. 性能优化与监控7.1 添加性能监控在FastAPI应用中添加监控端点from prometheus_client import Counter, Histogram, generate_latest from fastapi import Response # 添加监控指标 REQUEST_COUNT Counter(request_count, API请求计数, [method, endpoint]) REQUEST_LATENCY Histogram(request_latency_seconds, 请求延迟, [endpoint]) app.middleware(http) async def monitor_requests(request, call_next): 监控中间件 start_time time.time() REQUEST_COUNT.labels(methodrequest.method, endpointrequest.url.path).inc() response await call_next(request) process_time time.time() - start_time REQUEST_LATENCY.labels(endpointrequest.url.path).observe(process_time) response.headers[X-Process-Time] str(process_time) return response app.get(/metrics) async def metrics(): Prometheus监控端点 return Response(generate_latest(), media_typetext/plain)7.2 配置日志轮转创建日志配置文件# 配置logrotate sudo tee /etc/logrotate.d/casrel-api EOF /var/log/nginx/casrel-*.log { daily missingok rotate 30 compress delaycompress notifempty create 644 www-data www-data postrotate systemctl reload nginx endscript } EOF8. 总结与后续优化建议通过本教程我们成功将CasRel关系抽取模型部署为生产级的RESTful API服务。现在你拥有高性能API服务基于FastAPI的异步处理能力负载均衡通过Nginx实现请求分发监控能力基本的性能监控和日志记录易于扩展可以轻松添加更多后端实例后续优化建议添加认证使用JWT或API密钥保护接口限流控制防止API被滥用容器化部署使用Docker和Kubernetes数据库集成将抽取结果保存到数据库批量处理支持批量文本处理接口现在你的关系抽取服务已经可以投入生产环境使用了获取更多AI镜像想探索更多AI镜像和应用场景访问 CSDN星图镜像广场提供丰富的预置镜像覆盖大模型推理、图像生成、视频生成、模型微调等多个领域支持一键部署。