yym68686 commited on
Commit
aa42078
·
1 Parent(s): 02ce0cb

Add get models endpoint.

Browse files
Files changed (2) hide show
  1. main.py +10 -2
  2. utils.py +22 -15
main.py CHANGED
@@ -8,7 +8,7 @@ from contextlib import asynccontextmanager
8
  from fastapi import FastAPI, HTTPException, Depends
9
 
10
  from models import RequestModel
11
- from utils import config, api_keys_db, api_list, error_handling_wrapper, get_all_models, verify_api_key
12
  from request import get_payload
13
  from response import fetch_response, fetch_response_stream
14
 
@@ -160,7 +160,15 @@ async def options_handler():
160
 
161
  @app.post("/v1/models")
162
  async def list_models(token: str = Depends(verify_api_key)):
163
- models = get_all_models(token)
 
 
 
 
 
 
 
 
164
  return {
165
  "object": "list",
166
  "data": models
 
8
  from fastapi import FastAPI, HTTPException, Depends
9
 
10
  from models import RequestModel
11
+ from utils import config, api_keys_db, api_list, error_handling_wrapper, get_all_models, verify_api_key, post_all_models
12
  from request import get_payload
13
  from response import fetch_response, fetch_response_stream
14
 
 
160
 
161
  @app.post("/v1/models")
162
  async def list_models(token: str = Depends(verify_api_key)):
163
+ models = post_all_models(token)
164
+ return {
165
+ "object": "list",
166
+ "data": models
167
+ }
168
+
169
+ @app.get("/v1/models")
170
+ async def list_models():
171
+ models = get_all_models()
172
  return {
173
  "object": "list",
174
  "data": models
utils.py CHANGED
@@ -61,7 +61,7 @@ async def error_handling_wrapper(generator, status_code=200):
61
  # 处理生成器为空的情况
62
  return []
63
 
64
- def get_all_models(token):
65
  all_models = []
66
  unique_models = set()
67
 
@@ -84,7 +84,8 @@ def get_all_models(token):
84
  "id": model_item,
85
  "object": "model",
86
  "created": 1720524448858,
87
- "owned_by": provider_item['provider']
 
88
  }
89
  all_models.append(model_info)
90
  else:
@@ -98,7 +99,7 @@ def get_all_models(token):
98
  "id": model_item,
99
  "object": "model",
100
  "created": 1720524448858,
101
- "owned_by": provider_item['provider']
102
  }
103
  all_models.append(model_info)
104
  continue
@@ -112,18 +113,24 @@ def get_all_models(token):
112
  "owned_by": model
113
  }
114
  all_models.append(model_info)
115
- else:
116
- for provider in config["providers"]:
117
- for model in provider['model'].keys():
118
- if model not in unique_models:
119
- unique_models.add(model)
120
- model_info = {
121
- "id": model,
122
- "object": "model",
123
- "created": 1720524448858,
124
- "owned_by": provider['provider']
125
- }
126
- all_models.append(model_info)
 
 
 
 
 
 
127
 
128
  return all_models
129
 
 
61
  # 处理生成器为空的情况
62
  return []
63
 
64
+ def post_all_models(token):
65
  all_models = []
66
  unique_models = set()
67
 
 
84
  "id": model_item,
85
  "object": "model",
86
  "created": 1720524448858,
87
+ "owned_by": "uni-api"
88
+ # "owned_by": provider_item['provider']
89
  }
90
  all_models.append(model_info)
91
  else:
 
99
  "id": model_item,
100
  "object": "model",
101
  "created": 1720524448858,
102
+ "owned_by": "uni-api"
103
  }
104
  all_models.append(model_info)
105
  continue
 
113
  "owned_by": model
114
  }
115
  all_models.append(model_info)
116
+
117
+ return all_models
118
+
119
+ def get_all_models():
120
+ all_models = []
121
+ unique_models = set()
122
+
123
+ for provider in config["providers"]:
124
+ for model in provider['model'].keys():
125
+ if model not in unique_models:
126
+ unique_models.add(model)
127
+ model_info = {
128
+ "id": model,
129
+ "object": "model",
130
+ "created": 1720524448858,
131
+ "owned_by": "uni-api"
132
+ }
133
+ all_models.append(model_info)
134
 
135
  return all_models
136