Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,11 @@ import json
|
|
| 3 |
from datetime import datetime
|
| 4 |
import urllib.request
|
| 5 |
import ssl
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
class ChatCompletionsHandler(BaseHTTPRequestHandler):
|
| 8 |
def do_POST(self):
|
|
@@ -45,65 +50,78 @@ class ChatCompletionsHandler(BaseHTTPRequestHandler):
|
|
| 45 |
ctx.check_hostname = False
|
| 46 |
ctx.verify_mode = ssl.CERT_NONE
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
"
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
}
|
| 91 |
-
],
|
| 92 |
-
"usage": {
|
| 93 |
-
"prompt_tokens": len(prompt),
|
| 94 |
-
"completion_tokens": len(image_url),
|
| 95 |
-
"total_tokens": len(prompt) + len(image_url)
|
| 96 |
}
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
def run(server_class=HTTPServer, handler_class=ChatCompletionsHandler, port=8000):
|
| 104 |
server_address = ('', port)
|
| 105 |
httpd = server_class(server_address, handler_class)
|
| 106 |
-
|
| 107 |
httpd.serve_forever()
|
| 108 |
|
| 109 |
if __name__ == '__main__':
|
|
|
|
| 3 |
from datetime import datetime
|
| 4 |
import urllib.request
|
| 5 |
import ssl
|
| 6 |
+
import logging
|
| 7 |
+
|
| 8 |
+
# 设置日志
|
| 9 |
+
logging.basicConfig(level=logging.INFO)
|
| 10 |
+
logger = logging.getLogger(__name__)
|
| 11 |
|
| 12 |
class ChatCompletionsHandler(BaseHTTPRequestHandler):
|
| 13 |
def do_POST(self):
|
|
|
|
| 50 |
ctx.check_hostname = False
|
| 51 |
ctx.verify_mode = ssl.CERT_NONE
|
| 52 |
|
| 53 |
+
try:
|
| 54 |
+
with urllib.request.urlopen(req, context=ctx) as response:
|
| 55 |
+
response_body = json.loads(response.read().decode('utf-8'))
|
| 56 |
+
|
| 57 |
+
logger.info(f"API Response: {response_body}") # 记录 API 响应
|
| 58 |
+
|
| 59 |
+
if 'images' not in response_body or not response_body['images']:
|
| 60 |
+
raise ValueError("No images in the response")
|
| 61 |
+
|
| 62 |
+
image_url = response_body['images'][0].get('url')
|
| 63 |
+
if not image_url:
|
| 64 |
+
raise ValueError("No URL in the image response")
|
| 65 |
+
|
| 66 |
+
unique_id = int(datetime.now().timestamp() * 1000)
|
| 67 |
+
current_timestamp = int(unique_id / 1000)
|
| 68 |
+
|
| 69 |
+
if stream:
|
| 70 |
+
response_payload = {
|
| 71 |
+
"id": unique_id,
|
| 72 |
+
"object": "chat.completion.chunk",
|
| 73 |
+
"created": current_timestamp,
|
| 74 |
+
"model": model,
|
| 75 |
+
"choices": [
|
| 76 |
+
{
|
| 77 |
+
"index": 0,
|
| 78 |
+
"delta": {
|
| 79 |
+
"content": f""
|
| 80 |
+
},
|
| 81 |
+
"finish_reason": "stop"
|
| 82 |
+
}
|
| 83 |
+
]
|
| 84 |
+
}
|
| 85 |
+
self.send_response(200)
|
| 86 |
+
self.send_header('Content-Type', 'text/event-stream')
|
| 87 |
+
self.end_headers()
|
| 88 |
+
self.wfile.write(f"data: {json.dumps(response_payload)}\n\n".encode('utf-8'))
|
| 89 |
+
else:
|
| 90 |
+
response_payload = {
|
| 91 |
+
"id": unique_id,
|
| 92 |
+
"object": "chat.completion",
|
| 93 |
+
"created": current_timestamp,
|
| 94 |
+
"model": model,
|
| 95 |
+
"choices": [
|
| 96 |
+
{
|
| 97 |
+
"index": 0,
|
| 98 |
+
"message": {
|
| 99 |
+
"role": "assistant",
|
| 100 |
+
"content": f""
|
| 101 |
+
},
|
| 102 |
+
"logprobs": None,
|
| 103 |
+
"finish_reason": "length"
|
| 104 |
+
}
|
| 105 |
+
],
|
| 106 |
+
"usage": {
|
| 107 |
+
"prompt_tokens": len(prompt),
|
| 108 |
+
"completion_tokens": len(image_url),
|
| 109 |
+
"total_tokens": len(prompt) + len(image_url)
|
| 110 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
}
|
| 112 |
+
self.send_response(200)
|
| 113 |
+
self.send_header('Content-Type', 'application/json')
|
| 114 |
+
self.end_headers()
|
| 115 |
+
self.wfile.write(json.dumps(response_payload).encode('utf-8'))
|
| 116 |
+
|
| 117 |
+
except Exception as e:
|
| 118 |
+
logger.error(f"Error occurred: {str(e)}")
|
| 119 |
+
self.send_error(500, f"Internal Server Error: {str(e)}")
|
| 120 |
|
| 121 |
def run(server_class=HTTPServer, handler_class=ChatCompletionsHandler, port=8000):
|
| 122 |
server_address = ('', port)
|
| 123 |
httpd = server_class(server_address, handler_class)
|
| 124 |
+
logger.info(f"Starting server on port {port}")
|
| 125 |
httpd.serve_forever()
|
| 126 |
|
| 127 |
if __name__ == '__main__':
|