| 
 | 
 
ChatGLM3的OpenAPI方案: 
使用项目中的openai_api.py文件启动OpenAPI兼容服务 
默认监听http://localhost:8000,可通过Nginx配置HTTPS 
阿里云MCP方案: 
通过阿里云的OpenAPI MCP Server作为中间层 
提供标准化的HTTPS接口 
以下是ChatGLM3的OpenAPI服务完整实现方案: 
Nginx: 
Nginx_https.conf 
- server {
 
 -     listen 443 ssl;
 
 -     server_name your-domain.com;
 
  
-     ssl_certificate /path/to/cert.pem;
 
 -     ssl_certificate_key /path/to/key.pem;
 
  
-     location / {
 
 -         proxy_pass http://localhost:8000;
 
 -         proxy_set_header Host $host;
 
 -         proxy_set_header X-Real-IP $remote_addr;
 
 -     }
 
 - }
 
 
  复制代码 
Python: 
openai_api.py: 
- from fastapi import FastAPI
 
 - from fastapi.middleware.cors import CORSMiddleware
 
 - import uvicorn
 
 - from pydantic import BaseModel
 
 - from typing import List, Optional
 
 - import os
 
  
- app = FastAPI()
 
  
- # 允许跨域
 
 - app.add_middleware(
 
 -     CORSMiddleware,
 
 -     allow_origins=["*"],
 
 -     allow_credentials=True,
 
 -     allow_methods=["*"],
 
 -     allow_headers=["*"],
 
 - )
 
  
- class Message(BaseModel):
 
 -     role: str
 
 -     content: str
 
  
- class ChatRequest(BaseModel):
 
 -     model: str = "chatglm3"
 
 -     messages: List[Message]
 
 -     temperature: float = 0.8
 
 -     top_p: float = 0.8
 
 -     max_tokens: Optional[int] = None
 
 -     stream: bool = False
 
  
- @app.post("/v1/chat/completions")
 
 - async def chat_completion(request: ChatRequest):
 
 -     # 这里替换为实际调用本地模型的代码
 
 -     response = {
 
 -         "model": request.model,
 
 -         "choices": [{
 
 -             "message": {
 
 -                 "role": "assistant",
 
 -                 "content": "这是本地大模型的响应"
 
 -             }
 
 -         }]
 
 -     }
 
 -     return response
 
  
- @app.get("/v1/models")
 
 - async def list_models():
 
 -     return {
 
 -         "data": [{
 
 -             "id": "chatglm3",
 
 -             "object": "model",
 
 -             "created": os.path.getmtime(__file__),
 
 -             "owned_by": "local"
 
 -         }]
 
 -     }
 
  
- if __name__ == "__main__":
 
 -     uvicorn.run(app, host="0.0.0.0", port=8000)
 
 
  复制代码 
 
1、 HTTPS安全配置 - Nginx反向代理配置 
2、系统服务化 - 使用systemd管理服务 
注意事项: 
1、需要申请有效的SSL证书(可使用Let's Encrypt免费证书) 
2、生产环境建议添加身份验证(如API Key) 
3、高并发场景需要配置适当的负载均衡 
4、建议使用gunicorn+uvicorn组合提高并发性能 |   
 
 
 
 |