added baseUrl constant & /models endpoint
This commit is contained in:
@@ -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 xai = {}
|
||||||
|
local baseUrl = "https://api.x.ai/v1"
|
||||||
|
|
||||||
function xai.create(api_key: string)
|
function xai.create(api_key: string)
|
||||||
local client = {}
|
local client = {}
|
||||||
|
|
||||||
function client.completions(self, request: CompletionsRequest): CompletionsResponse
|
function client:completions(request: CompletionsRequest): CompletionsResponse
|
||||||
local config = {
|
local config = {
|
||||||
url = "https://api.x.ai/v1/chat/completions",
|
url = `{baseUrl}/completions`,
|
||||||
method = "POST",
|
method = "POST",
|
||||||
headers = {
|
headers = {
|
||||||
Authorization = "Bearer " .. api_key,
|
Authorization = "Bearer " .. api_key,
|
||||||
@@ -186,6 +197,23 @@ function xai.create(api_key: string)
|
|||||||
end
|
end
|
||||||
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
|
return client
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user