Discuz! BBS

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 174|回复: 0

本地搭建大模型,能用OpenAPI HTTPS访问吗?

[复制链接]

394

主题

559

帖子

3303

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
3303
发表于 2025-9-16 20:38:47 | 显示全部楼层 |阅读模式
ChatGLM3的OpenAPI方案:
使用项目中的openai_api.py文件启动OpenAPI兼容服务
默认监听http://localhost:8000,可通过Nginx配置HTTPS
阿里云MCP方案:
通过阿里云的OpenAPI MCP Server作为中间层
提供标准化的HTTPS接口
以下是ChatGLM3的OpenAPI服务完整实现方案:
Nginx:
Nginx_https.conf
  1. server {
  2.     listen 443 ssl;
  3.     server_name your-domain.com;

  4.     ssl_certificate /path/to/cert.pem;
  5.     ssl_certificate_key /path/to/key.pem;

  6.     location / {
  7.         proxy_pass http://localhost:8000;
  8.         proxy_set_header Host $host;
  9.         proxy_set_header X-Real-IP $remote_addr;
  10.     }
  11. }
复制代码

Python:
openai_api.py:
  1. from fastapi import FastAPI
  2. from fastapi.middleware.cors import CORSMiddleware
  3. import uvicorn
  4. from pydantic import BaseModel
  5. from typing import List, Optional
  6. import os

  7. app = FastAPI()

  8. # 允许跨域
  9. app.add_middleware(
  10.     CORSMiddleware,
  11.     allow_origins=["*"],
  12.     allow_credentials=True,
  13.     allow_methods=["*"],
  14.     allow_headers=["*"],
  15. )

  16. class Message(BaseModel):
  17.     role: str
  18.     content: str

  19. class ChatRequest(BaseModel):
  20.     model: str = "chatglm3"
  21.     messages: List[Message]
  22.     temperature: float = 0.8
  23.     top_p: float = 0.8
  24.     max_tokens: Optional[int] = None
  25.     stream: bool = False

  26. @app.post("/v1/chat/completions")
  27. async def chat_completion(request: ChatRequest):
  28.     # 这里替换为实际调用本地模型的代码
  29.     response = {
  30.         "model": request.model,
  31.         "choices": [{
  32.             "message": {
  33.                 "role": "assistant",
  34.                 "content": "这是本地大模型的响应"
  35.             }
  36.         }]
  37.     }
  38.     return response

  39. @app.get("/v1/models")
  40. async def list_models():
  41.     return {
  42.         "data": [{
  43.             "id": "chatglm3",
  44.             "object": "model",
  45.             "created": os.path.getmtime(__file__),
  46.             "owned_by": "local"
  47.         }]
  48.     }

  49. if __name__ == "__main__":
  50.     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组合提高并发性能
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|DiscuzX

GMT+8, 2025-10-25 19:43 , Processed in 0.011242 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表