-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
77 lines (63 loc) · 1.67 KB
/
main.py
File metadata and controls
77 lines (63 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""
LLM Proxy 主入口文件
"""
import sys
import socket
import uvicorn
import litellm
from fastapi import FastAPI, Request
from app import __version__
from app.config import config
from app.api import api_router
from app.utils.logging_utils import setup_logging
# 配置 LiteLLM
litellm.drop_params = True
litellm.set_verbose = False
litellm.request_timeout = config.request_timeout
litellm.num_retries = config.max_retries
# 设置日志
logger = setup_logging()
# 创建 FastAPI 应用
app = FastAPI(
title="LLM Proxy - Local Model to Claude API",
version=__version__,
description="将本地 LLM 模型适配为 Claude API 格式的代理服务"
)
# 注册路由
app.include_router(api_router)
# 请求中间件
@app.middleware("http")
async def log_requests(request: Request, call_next):
method = request.method
path = request.url.path
logger.debug(f"Request: {method} {path}")
response = await call_next(request)
return response
def validate_startup() -> bool:
"""验证启动配置"""
if not config.api_key:
return False
return True
def main():
"""主函数"""
if not validate_startup():
sys.exit(1)
# 启动服务器
if config.workers > 1:
# 多 worker 模式需要使用字符串形式的 app
uvicorn.run(
"main:app",
host=config.host,
port=config.port,
workers=config.workers,
log_level=config.log_level.lower()
)
else:
uvicorn.run(
app,
host=config.host,
port=config.port,
log_level=config.log_level.lower()
)
if __name__ == "__main__":
main()