yym68686 commited on
Commit
89ae4e8
·
1 Parent(s): e81d06a

Fixed the bug in openrouter sse special format handling.

Browse files

Add functionality to not use Authorization when there is no API.

Files changed (3) hide show
  1. request.py +4 -2
  2. response.py +2 -2
  3. utils.py +0 -1
request.py CHANGED
@@ -1,5 +1,6 @@
1
  import json
2
  from models import RequestModel
 
3
 
4
  async def get_image_message(base64_image, engine = None):
5
  if "gpt" == engine:
@@ -159,9 +160,11 @@ async def get_gpt_payload(request, engine, provider):
159
 
160
  async def get_openrouter_payload(request, engine, provider):
161
  headers = {
162
- 'Authorization': f"Bearer {provider['api']}",
163
  'Content-Type': 'application/json'
164
  }
 
 
 
165
  url = provider['base_url']
166
 
167
  messages = []
@@ -246,7 +249,6 @@ async def get_claude_payload(request, engine, provider):
246
  "x-api-key": f"{provider['api']}",
247
  "anthropic-version": "2023-06-01",
248
  "anthropic-beta": "max-tokens-3-5-sonnet-2024-07-15" if "claude-3-5-sonnet" in model else "tools-2024-05-16",
249
-
250
  }
251
  url = provider['base_url']
252
 
 
1
  import json
2
  from models import RequestModel
3
+ from log_config import logger
4
 
5
  async def get_image_message(base64_image, engine = None):
6
  if "gpt" == engine:
 
160
 
161
  async def get_openrouter_payload(request, engine, provider):
162
  headers = {
 
163
  'Content-Type': 'application/json'
164
  }
165
+ if provider.get("api"):
166
+ headers['Authorization'] = f"Bearer {provider['api']}"
167
+
168
  url = provider['base_url']
169
 
170
  messages = []
 
249
  "x-api-key": f"{provider['api']}",
250
  "anthropic-version": "2023-06-01",
251
  "anthropic-beta": "max-tokens-3-5-sonnet-2024-07-15" if "claude-3-5-sonnet" in model else "tools-2024-05-16",
 
252
  }
253
  url = provider['base_url']
254
 
response.py CHANGED
@@ -80,12 +80,12 @@ async def fetch_gpt_response_stream(client, url, headers, payload):
80
  yield {"error": f"fetch_gpt_response_stream HTTP Error {response.status_code}", "details": error_json}
81
  buffer = ""
82
  async for chunk in response.aiter_text():
83
- # print("chunk", repr(chunk))
84
  buffer += chunk
85
  while "\n" in buffer:
86
  line, buffer = buffer.split("\n", 1)
87
  # print("line", repr(line))
88
- if line and line != "data: " and line != "data:":
89
  yield line + "\n"
90
 
91
  async def fetch_claude_response_stream(client, url, headers, payload, model):
 
80
  yield {"error": f"fetch_gpt_response_stream HTTP Error {response.status_code}", "details": error_json}
81
  buffer = ""
82
  async for chunk in response.aiter_text():
83
+ # logger.info(f"chunk: {repr(chunk)}")
84
  buffer += chunk
85
  while "\n" in buffer:
86
  line, buffer = buffer.split("\n", 1)
87
  # print("line", repr(line))
88
+ if line and line != "data: " and line != "data:" and not line.startswith(": "):
89
  yield line + "\n"
90
 
91
  async def fetch_claude_response_stream(client, url, headers, payload, model):
utils.py CHANGED
@@ -93,7 +93,6 @@ async def error_handling_wrapper(generator, status_code=200):
93
  first_item_str = json.loads(first_item_str)
94
  except json.JSONDecodeError:
95
  logger.error("error_handling_wrapper JSONDecodeError!" + repr(first_item_str))
96
- pass # 如果不是有效的JSON,保持原样
97
  if isinstance(first_item_str, dict) and 'error' in first_item_str:
98
  # 如果第一个 yield 的项是错误信息,抛出 HTTPException
99
  raise HTTPException(status_code=status_code, detail=f"{first_item_str}"[:300])
 
93
  first_item_str = json.loads(first_item_str)
94
  except json.JSONDecodeError:
95
  logger.error("error_handling_wrapper JSONDecodeError!" + repr(first_item_str))
 
96
  if isinstance(first_item_str, dict) and 'error' in first_item_str:
97
  # 如果第一个 yield 的项是错误信息,抛出 HTTPException
98
  raise HTTPException(status_code=status_code, detail=f"{first_item_str}"[:300])