In the Foundation Models framework utilities package, the private method buildURLRequest in ChatCompletionsLanguageModel handles the construction of OpenAI-compatible API URLs:
private func buildURLRequest(for request: ChatCompletionRequest) throws -> URLRequest {
let isVersioned = baseURL.pathComponents.contains("v1")
let endpoint = isVersioned ? "/chat/completions" : "/v1/chat/completions"
let url = baseURL.appendingPathComponent(endpoint)
...
}
Problem
The current implementation hardcodes "v1" to determine if the baseURL already includes a version. This limits compatibility with API providers using alternative versioning schemes. For instance, Volcengine Ark uses "v3" in its Base URL, making it difficult to seamlessly integrate their services.
#Playground {
let baseURL = URL(string: "https://ark.cn-beijing.volces.com/api/v3")!
let modelName = "doubao-seed-2-0-mini-260428"
let headers: [String : String] = [
"Authorization" : "Bearer \(apiKey)"
]
let model = ChatCompletionsLanguageModel(name: modelName, url: baseURL, additionalHeaders: headers)
let session = LanguageModelSession(model: model)
do {
let result = try await session.respond(to: "Hello").content
} catch {
print(error.localizedDescription)
// HTTP error with status code 404:
}
}
#Playground {
let baseURL = URL(string: "https://ark.cn-beijing.volces.com/api/v3/responses")!
let modelName = "doubao-seed-2-0-mini-260428"
let headers: [String : String] = [
"Authorization" : "Bearer \(apiKey)"
]
let model = ChatCompletionsLanguageModel(name: modelName, url: baseURL, additionalHeaders: headers)
let session = LanguageModelSession(model: model)
do {
let result = try await session.respond(to: "Hello").content
} catch {
print(error.localizedDescription)
/*
HTTP error with status code 404:
{"error":{"code":"InvalidAction","message":"The specified action is invalid: /api/v3/responses/v1/chat/completions Request id: 021784381168842fdfd2e3c33d5b6eddad55ac385080e727cab08","param":"","type":"NotFound"}}
*/
}
}
Suggested Solution
To better accommodate different versioning conventions (e.g., v2, v3), we can leverage Swift's modern Regex (#/v\d+/#) to dynamically detect the version pattern in the path components.
Here is a recommended update for the isVersioned check:
let isVersioned = baseURL.pathComponents.contains { component in
component.wholeMatch(of: #/v\d+/#) != nil
}
Topic:
Machine Learning & AI
SubTopic:
Foundation Models
1
0
26