From b61d9aef19438e9d27f743bc56d5cbd7ef8b7b34 Mon Sep 17 00:00:00 2001 From: "Chang Chi, Meng" Date: Wed, 13 Sep 2023 02:54:42 +0800 Subject: [PATCH] openai API: add support for chunked transfer encoding in POST requests (#3870) --- extensions/openai/script.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/extensions/openai/script.py b/extensions/openai/script.py index d1faa019..5b319674 100644 --- a/extensions/openai/script.py +++ b/extensions/openai/script.py @@ -139,8 +139,24 @@ class Handler(BaseHTTPRequestHandler): debug_msg(self.requestline) debug_msg(self.headers) - content_length = int(self.headers['Content-Length']) - body = json.loads(self.rfile.read(content_length).decode('utf-8')) + content_length = self.headers.get('Content-Length') + transfer_encoding = self.headers.get('Transfer-Encoding') + + if content_length: + body = json.loads(self.rfile.read(int(content_length)).decode('utf-8')) + elif transfer_encoding == 'chunked': + chunks = [] + while True: + chunk_size = int(self.rfile.readline(), 16) # Read the chunk size + if chunk_size == 0: + break # End of chunks + chunks.append(self.rfile.read(chunk_size)) + self.rfile.readline() # Consume the trailing newline after each chunk + body = json.loads(b''.join(chunks).decode('utf-8')) + else: + self.send_response(400, "Bad Request: Either Content-Length or Transfer-Encoding header expected.") + self.end_headers() + return debug_msg(body)