Support multiple vision images and 64 MiB chat requests
This commit is contained in:
1 parent
42b79f5065
commit
17d567d2ff
5 files changed
+63
-6
No files matched your search
+4
-4
@@ -19,6 +19,8 @@ import logging
|
||||
from stt import read_upload
|
||||
from responses_compat import translate as translate_responses,ResponsesError
|
||||
|
||||
MAX_JSON_REQUEST_BYTES = 64 * 1024 * 1024
|
||||
|
||||
class APIError(ValueError):
|
||||
def __init__(self,message,status=400,code='invalid_request_error'):
|
||||
super().__init__(message);self.status=status;self.code=code
|
||||
@@ -302,7 +304,8 @@ class APIHandler(BaseHTTPRequestHandler):
|
||||
if self.headers.get('Transfer-Encoding'):raise APIError('Chunked Upload wird nicht unterstützt.')
|
||||
try:length=int(self.headers.get('Content-Length','0'))
|
||||
except ValueError:raise APIError('Ungültige Content-Length.') from None
|
||||
if not 0<length<=1024*1024 or self.headers.get('Content-Type','').split(';')[0]!='application/json':raise APIError('JSON-Anfrage bis 1 MiB erforderlich.',413)
|
||||
if self.headers.get('Content-Type','').split(';')[0]!='application/json':raise APIError('Content-Type application/json erforderlich.',415)
|
||||
if not 0<length<=MAX_JSON_REQUEST_BYTES:raise APIError('Die gesamte Anfrage einschließlich Chatverlauf, Werkzeugen und eingebetteten Bildern darf höchstens 64 MiB groß sein. Bilder verkleinern oder Chatverlauf kürzen.',413,'request_too_large')
|
||||
try:data=json.loads(self.rfile.read(length),parse_constant=lambda _:(_ for _ in ()).throw(ValueError()))
|
||||
except (ValueError,UnicodeError):raise APIError('Ungültiges JSON.') from None
|
||||
if not isinstance(data,dict):raise APIError('JSON-Objekt erforderlich.')
|
||||
@@ -439,7 +442,6 @@ class APIHandler(BaseHTTPRequestHandler):
|
||||
messages=data.get('messages')
|
||||
if not isinstance(messages,list) or not messages or any(not isinstance(m,dict) for m in messages):raise APIError('messages muss eine nichtleere Liste sein.')
|
||||
profile=ep.find_profile(data.get('model'),'chat')
|
||||
images=0
|
||||
for message in messages:
|
||||
content=message.get('content')
|
||||
if isinstance(content,list):
|
||||
@@ -454,8 +456,6 @@ class APIHandler(BaseHTTPRequestHandler):
|
||||
try:raw=base64.b64decode(url.split(',',1)[1],validate=True)
|
||||
except (ValueError,binascii.Error):raise APIError('Ungültige Bildkodierung.') from None
|
||||
if not raw:raise APIError('Leeres Bild.')
|
||||
images+=1
|
||||
if images>1:raise APIError('Zunächst ein Bild pro Anfrage unterstützt.')
|
||||
# Re-resolve after waiting so edits/disable cannot silently launch a stale profile.
|
||||
key=('chat',profile['id'],profile['revision'])
|
||||
def allowed():return ep.allowed() and any(p['id']==profile['id'] and p['revision']==profile['revision'] and p['enabled'] for p in ep.rows())
|
||||
|
||||
Reference in new issue
Block a user