Support OpenAI-compatible inference endpoints
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -364,3 +364,26 @@ final class GenerationConfigurationTests: XCTestCase {
|
||||
XCTAssertThrowsError(try DockerCommand.arguments("docker logs 'unclosed"))
|
||||
}
|
||||
}
|
||||
|
||||
final class CompatibleEndpointTests: XCTestCase {
|
||||
func testRequiresHTTPSForRemoteAndRejectsEmbeddedCredentials() throws {
|
||||
XCTAssertThrowsError(try CompatibleEndpoint(endpoint: "http://example.com/v1"))
|
||||
XCTAssertThrowsError(try CompatibleEndpoint(endpoint: "https://user:[email protected]/v1"))
|
||||
XCTAssertNoThrow(try CompatibleEndpoint(endpoint: "http://127.0.0.1:1234/v1"))
|
||||
}
|
||||
|
||||
func testAuthenticatedDiscoveryAndStreaming() async throws {
|
||||
guard let endpoint = ProcessInfo.processInfo.environment["AISTUDIO_TEST_COMPATIBLE_URL"] else {
|
||||
throw XCTSkip("Set AISTUDIO_TEST_COMPATIBLE_URL to the compatible endpoint fixture.")
|
||||
}
|
||||
let client = try CompatibleEndpoint(endpoint: endpoint, token: "fixture-token")
|
||||
let models = try await client.models()
|
||||
XCTAssertEqual(models, ["fixture-model"])
|
||||
let output = try await collect(client.generate(InferenceRequest(model: "fixture-model", messages: [
|
||||
Message(role: .system, content: "Silent instruction"),
|
||||
Message(role: .user, content: "Hello")
|
||||
], thinking: .automatic, parameters: GenerationParameters(temperature: 0, maxTokens: 32))))
|
||||
XCTAssertEqual(output, [.backend("Compatible API · fixture-model"),
|
||||
.thinking("Thinking"), .answer("Fixture response")])
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user