yym68686 commited on
Commit
b3ed199
·
1 Parent(s): 3f0a3dd

update README

Browse files
Files changed (2) hide show
  1. README.md +7 -0
  2. main.py +10 -12
README.md CHANGED
@@ -1 +1,8 @@
1
  # uni-api
 
 
 
 
 
 
 
 
1
  # uni-api
2
+
3
+ ```bash
4
+ curl -X POST http://127.0.0.1:8000/v1/chat/completions \
5
+ -H "Content-Type: application/json" \
6
+ -H "Authorization: Bearer sk-KjjI60Yf0JFcsvgRmXqFwgGmWUd9GZnmi3KlvowmRWpWpQRo" \
7
+ -d '{"model": "gpt-4o","messages": [{"role": "user", "content": "Hello"}],"stream": true}'
8
+ ```
main.py CHANGED
@@ -142,21 +142,18 @@ class ModelRequestHandler:
142
  # 检查是否启用轮询
143
  use_round_robin = os.environ.get('USE_ROUND_ROBIN', 'false').lower() == 'true'
144
 
145
- if use_round_robin:
146
- return await self.round_robin_request(request, matching_providers)
147
- else:
148
- # 使用第一个匹配的提供者
149
- provider = matching_providers[0]
150
- try:
151
- return await process_request(request, provider)
152
- except Exception as e:
153
- raise HTTPException(status_code=500, detail=f"Error calling API: {str(e)}")
154
 
155
- async def round_robin_request(self, request: RequestModel, providers: List[Dict]):
156
  num_providers = len(providers)
 
 
157
  for i in range(num_providers):
158
- self.last_provider_index = (self.last_provider_index + 1) % num_providers
159
- # print(f"Trying provider {self.last_provider_index}")
 
 
 
160
  provider = providers[self.last_provider_index]
161
  try:
162
  response = await process_request(request, provider)
@@ -164,6 +161,7 @@ class ModelRequestHandler:
164
  except Exception as e:
165
  print(f"Error with provider {provider['provider']}: {str(e)}")
166
  continue
 
167
  raise HTTPException(status_code=500, detail="All providers failed")
168
 
169
  model_handler = ModelRequestHandler()
 
142
  # 检查是否启用轮询
143
  use_round_robin = os.environ.get('USE_ROUND_ROBIN', 'false').lower() == 'true'
144
 
145
+ return await self.try_all_providers(request, matching_providers, use_round_robin)
 
 
 
 
 
 
 
 
146
 
147
+ async def try_all_providers(self, request: RequestModel, providers: List[Dict], use_round_robin: bool):
148
  num_providers = len(providers)
149
+ start_index = self.last_provider_index if use_round_robin else 0
150
+
151
  for i in range(num_providers):
152
+ if use_round_robin:
153
+ self.last_provider_index = (start_index + i) % num_providers
154
+ else:
155
+ self.last_provider_index = i
156
+
157
  provider = providers[self.last_provider_index]
158
  try:
159
  response = await process_request(request, provider)
 
161
  except Exception as e:
162
  print(f"Error with provider {provider['provider']}: {str(e)}")
163
  continue
164
+
165
  raise HTTPException(status_code=500, detail="All providers failed")
166
 
167
  model_handler = ModelRequestHandler()