Support OpenAI-compatible inference endpoints

This commit is contained in:
Mark Gregory
2026-11-16 13:58:43 +00:00
parent afeba270f4
commit caa0a8733b
9 changed files with 309 additions and 30 deletions
+22
View File
@@ -24,6 +24,22 @@ class Handler(BaseHTTPRequestHandler):
message = self.body()
method = message.get('method')
args = message.get('params', {})
if self.path == '/compatible/v1/chat/completions':
if self.headers.get('Authorization') != 'Bearer fixture-token':
self.reply(401)
return
assert message['stream'] and message['model'] == 'fixture-model'
assert message['messages'][0] == {'role': 'system', 'content': 'Silent instruction'}
assert message['temperature'] == 0 and message['max_tokens'] == 32
assert 'options' not in message and 'think' not in message
frames = [
{'choices': [{'delta': {'reasoning_content': 'Thinking'}}]},
{'choices': [{'delta': {'content': 'Fixture response'}}]},
{'choices': [{'delta': {}, 'finish_reason': 'stop'}]},
]
payload = ''.join(f'data: {json.dumps(frame)}\n\n' for frame in frames)
self.reply(200, (payload + 'data: [DONE]\n\n').encode(), 'text/event-stream')
return
if self.path == '/gateway/api/chat':
if self.headers.get('Authorization') != 'Bearer fixture-token':
self.reply(401)
@@ -80,6 +96,12 @@ class Handler(BaseHTTPRequestHandler):
self.reply(200, envelope, headers=headers)
def do_GET(self):
if self.path == '/compatible/v1/models':
if self.headers.get('Authorization') != 'Bearer fixture-token':
self.reply(401)
return
self.reply(200, {'data': [{'id': 'fixture-model'}, {'id': 'fixture-model'}]})
return
if self.path == '/gateway/api/tags':
if self.headers.get('Authorization') != 'Bearer fixture-token':
self.reply(401)