added baseUrl constant & /models endpoint

This commit is contained in:
2025-10-05 00:15:14 -06:00
parent 9f881c6372
commit 557106f1be

View File

@@ -162,14 +162,25 @@ export type CompletionsRequest = {
}?
}
export type ModelsResponse = {
data : {
created: number,
id: string,
object: "model",
owned_by: string
},
object: "list"
}
local xai = {}
local baseUrl = "https://api.x.ai/v1"
function xai.create(api_key: string)
local client = {}
function client.completions(self, request: CompletionsRequest): CompletionsResponse
function client:completions(request: CompletionsRequest): CompletionsResponse
local config = {
url = "https://api.x.ai/v1/chat/completions",
url = `{baseUrl}/completions`,
method = "POST",
headers = {
Authorization = "Bearer " .. api_key,
@@ -186,6 +197,23 @@ function xai.create(api_key: string)
end
end
function client:models(): ModelsResponse
local config = {
url = `{baseUrl}/models`,
method = "GET",
headers = {
["Content-Type"] = "application/json",
},
}
local response = net.request(config)
if response.ok then
local decoded: ModelsResponse = serde.decode("json", response.body)
return decoded
else
error(`API request failed with status {response.statusCode}: {response.statusMessage}`)
end
end
return client
end