Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from http.server import BaseHTTPRequestHandler, HTTPServer
- import random
- import time
- import json
- class RequestHandler(BaseHTTPRequestHandler):
- def do_GET(self):
- if self.path == '/v1/models':
- self.send_response(200)
- self.send_header('Content-type', 'application/json')
- self.end_headers()
- #response = {"message": "This is response from endpoint A"}
- #self.wfile.write(json.dumps(response).encode())
- self.wfile.write(b'{"data":[{"id":"DeepSeek-Coder-V2-Instruct","name":"DeepSeek-Coder-V2-Instruct"}],"object":"list"}')
- else:
- self.send_response(404)
- self.end_headers()
- self.wfile.write(b'404 Not Found')
- def do_POST(self):
- if self.path == '/v1/chat/completions':
- clen = int(self.headers["Content-Length"])
- print(self.rfile.read(clen).decode())
- self.send_response(200)
- self.send_header('Content-type', 'text/event-stream')
- self.send_header('Cache-control', 'no-cache')
- self.send_header('Connection', 'keep-alive')
- self.end_headers()
- #response = {"message": "This is response from endpoint B/C"}
- u1 = random.randint(0x0, 0xffffffff)
- u2 = random.randint(0x0, 0xffff)
- u3 = random.randint(0x0, 0xffff)
- u4 = random.randint(0x0, 0xffff)
- u5 = random.randint(0x0, 0xffffffffffff)
- uuid = "%08X-%04X-%04X-%04X-%012X"%(u1, u2, u3, u4, u5)
- #self.wfile.write(json.dumps(response).encode())
- now = int(time.time())
- 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)
- self.wfile.write(response.encode())
- time.sleep(1)
- #self.send_response(206)
- #self.send_header('Content-type', 'application/json')
- #self.end_headers()
- #now = int(time.time())
- #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)
- 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)
- self.wfile.write(response.encode())
- time.sleep(1)
- #self.send_response(200)
- #self.send_header('Content-type', 'application/json')
- #self.end_headers()
- #now = int(time.time())
- #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)
- 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)
- self.wfile.write(response.encode())
- else:
- self.send_response(404)
- self.end_headers()
- self.wfile.write(b'404 Not Found')
- def run(server_class=HTTPServer, handler_class=RequestHandler, port=8000):
- server_address = ('', port)
- httpd = server_class(server_address, handler_class)
- print(f'Starting server on port {port}...')
- httpd.serve_forever()
- if __name__ == '__main__':
- run()
Advertisement
Add Comment
Please, Sign In to add comment