|
import os |
|
replace_chat = os.getenv("REPLACE_CHAT","/replace/chat") |
|
prefix_chat = os.getenv("PREFIX_CHAT","/pre/api,/pre/hf") |
|
append_chat = os.getenv("APPEND_CHAT","/append/v1/go,/append/v2") |
|
|
|
|
|
def get_dynamic_routes(): |
|
""" |
|
根据环境变量动态生成路由路径。 |
|
|
|
优先级: REPLACE_CHAT > PREFIX_CHAT > APPEND_CHAT |
|
- REPLACE_CHAT: 替换默认访问地址,直接替换默认路径,支持多个,逗号分割 |
|
- PREFIX_CHAT: 在默认访问地址前加上前缀,支持多个,逗号分割 |
|
- APPEND_CHAT: 添加额外的访问地址,支持多个,逗号分割 |
|
|
|
:return: 返回一个包含动态生成的路由路径的列表 |
|
""" |
|
|
|
default_path = "/v1/chat/completions" |
|
|
|
|
|
replace_chat = os.getenv("REPLACE_CHAT", "") |
|
prefix_chat = os.getenv("PREFIX_CHAT", "") |
|
append_chat = os.getenv("APPEND_CHAT", "") |
|
|
|
|
|
if replace_chat: |
|
|
|
return [path.strip() for path in replace_chat.split(",") if path.strip()] |
|
|
|
|
|
routes = [] |
|
|
|
|
|
if prefix_chat: |
|
|
|
prefixes = prefix_chat.split(",") |
|
for prefix in prefixes: |
|
|
|
routes.append(prefix + default_path) |
|
|
|
return routes |
|
|
|
|
|
if append_chat: |
|
|
|
append_paths = [path.strip() for path in append_chat.split(",") if path.strip()] |
|
|
|
routes = [default_path] + append_paths |
|
|
|
|
|
if not routes: |
|
routes.append(default_path) |
|
|
|
|
|
return routes |
|
|
|
|
|
if __name__ == '__main__': |
|
print(get_dynamic_routes()) |