Guest User

Untitled

a guest
Jun 8th, 2025
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.33 KB | Source Code | 0 0
  1. from http.server import BaseHTTPRequestHandler, HTTPServer
  2. import random
  3. import time
  4. import json
  5.  
  6. class RequestHandler(BaseHTTPRequestHandler):
  7.     def do_GET(self):
  8.         if self.path == '/v1/models':
  9.             self.send_response(200)
  10.             self.send_header('Content-type', 'application/json')
  11.             self.end_headers()
  12.             #response = {"message": "This is response from endpoint A"}
  13.             #self.wfile.write(json.dumps(response).encode())
  14.             self.wfile.write(b'{"data":[{"id":"DeepSeek-Coder-V2-Instruct","name":"DeepSeek-Coder-V2-Instruct"}],"object":"list"}')
  15.         else:
  16.             self.send_response(404)
  17.             self.end_headers()
  18.             self.wfile.write(b'404 Not Found')
  19.    
  20.     def do_POST(self):
  21.         if self.path == '/v1/chat/completions':
  22.             clen = int(self.headers["Content-Length"])
  23.             print(self.rfile.read(clen).decode())
  24.             self.send_response(200)
  25.             self.send_header('Content-type', 'text/event-stream')
  26.             self.send_header('Cache-control', 'no-cache')
  27.             self.send_header('Connection', 'keep-alive')
  28.             self.end_headers()
  29.             #response = {"message": "This is response from endpoint B/C"}
  30.             u1 = random.randint(0x0, 0xffffffff)
  31.             u2 = random.randint(0x0, 0xffff)
  32.             u3 = random.randint(0x0, 0xffff)
  33.             u4 = random.randint(0x0, 0xffff)
  34.             u5 = random.randint(0x0, 0xffffffffffff)
  35.             uuid = "%08X-%04X-%04X-%04X-%012X"%(u1, u2, u3, u4, u5)
  36.             #self.wfile.write(json.dumps(response).encode())
  37.             now = int(time.time())
  38.             response = '{"id":"%s","object":"chat.completion.chunk","created":%d,"model":"DeepSeek-Coder-V2-Instruct","choices":[{"index":0,"delta":{"role":"assistant","content":"<think>\\n"},"logprobs":null,"finish_reason":null}]}'%(uuid, now)
  39.             self.wfile.write(response.encode())
  40.  
  41.             time.sleep(1)
  42.  
  43.             #self.send_response(206)
  44.             #self.send_header('Content-type', 'application/json')
  45.             #self.end_headers()
  46.             #now = int(time.time())
  47.             #response = '{"id":"%s","choices":[{"finish_reason":"stop","index":0,"logprobs":null,"message":{"content":"<think>\\nthink\\n</think>\\n\\ncontent","refusal":null,"role":"assistant","annotations":null,"audio":null,"function_call":null,"tool_calls":null}}],"created":%d,"model":"DeepSeek-Coder-V2-Instruct","object":"chat.completion","service_tier":null,"system_fingerprint":null,"usage":{"completion_tokens":null,"prompt_tokens":null,"total_tokens":null,"completion_tokens_details":null,"prompt_tokens_details":null}}'%(uuid, now)
  48.             response = '{"id":"%s","object":"chat.completion.chunk","created":%d,"model":"DeepSeek-Coder-V2-Instruct","choices":[{"index":0,"delta":{"content":"think\\n</think>\\n\\ncontent"},"logprobs":null,"finish_reason":null}]}'%(uuid, now)
  49.             self.wfile.write(response.encode())
  50.  
  51.             time.sleep(1)
  52.  
  53.             #self.send_response(200)
  54.             #self.send_header('Content-type', 'application/json')
  55.             #self.end_headers()
  56.             #now = int(time.time())
  57.             #response = '{"id":"%s","choices":[{"finish_reason":"stop","index":0,"logprobs":null,"message":{"content":"<think>\\nthink\\n</think>\\n\\ncontent","refusal":null,"role":"assistant","annotations":null,"audio":null,"function_call":null,"tool_calls":null}}],"created":%d,"model":"DeepSeek-Coder-V2-Instruct","object":"chat.completion","service_tier":null,"system_fingerprint":null,"usage":{"completion_tokens":null,"prompt_tokens":null,"total_tokens":null,"completion_tokens_details":null,"prompt_tokens_details":null}}'%(uuid, now)
  58.             response = '{"id":"%s","object":"chat.completion.chunk","created":%d,"model":"DeepSeek-Coder-V2-Instruct","choices":[{"index":0,"delta":{},"logprobs":null,"finish_reason":“stop”}]}'%(uuid, now)
  59.             self.wfile.write(response.encode())
  60.         else:
  61.             self.send_response(404)
  62.             self.end_headers()
  63.             self.wfile.write(b'404 Not Found')
  64.  
  65. def run(server_class=HTTPServer, handler_class=RequestHandler, port=8000):
  66.     server_address = ('', port)
  67.     httpd = server_class(server_address, handler_class)
  68.     print(f'Starting server on port {port}...')
  69.     httpd.serve_forever()
  70.  
  71. if __name__ == '__main__':
  72.     run()
Advertisement
Add Comment
Please, Sign In to add comment