Compare commits

...

9 Commits

2 changed files with 149 additions and 12 deletions

View File

@@ -1,7 +1,7 @@
local net = require("@lune/net")
local serde = require("@lune/serde")
export type model = 'grok-4' | 'grok-code-fast-1' | 'grok-4-fast-reasoning' | 'grok-4-fast-non-reasoning' | 'grok-3-mini' | 'grok-3' | 'grok-2-vision'
export type AvailableModels = 'grok-4' | 'grok-code-fast-1' | 'grok-4-fast-reasoning' | 'grok-4-fast-non-reasoning' | 'grok-3-mini' | 'grok-3' | 'grok-2-vision'
export type message = {
role: string,
@@ -15,8 +15,8 @@ export type message = {
name: string
},
id: string,
index: number?,
type: 'function'
index: number?, -- according to x.ai's documentation, this is 'integer | null' so i guess it can for some reason not exist?
type: 'function'?
}
}?
}
@@ -26,7 +26,7 @@ export type CompletionsResponse = {
object: 'chat.completion',
created: number,
model: string,
choices: {
choices: { {
index: number,
message: message,
finish_reason: "stop" | "length" | "function_call" | "content_filter" | "null",
@@ -48,7 +48,7 @@ export type CompletionsResponse = {
}
}
}?
},
} },
citations: { string }?,
system_fingerprint: string?,
usage: {
@@ -99,7 +99,7 @@ type rsssource = {
export type CompletionsRequest = {
messages: { message },
model: model,
model: AvailableModels,
stream: false?,
search_parameters: {
mode: 'auto' | 'on' | 'off',
@@ -162,14 +162,83 @@ export type CompletionsRequest = {
}?
}
type model = {
created: number,
id: string,
object: "model",
owned_by: string
}
export type ModelsResponse = {
data : {
model
},
object: "list"
}
type model_id = string
export type ModelRequest = model_id
export type ModelResponse = model
type modality = 'text' | 'image' | string
export type LanguageModel = {
aliases: {
string
},
cached_prompt_text_token_price: number,
completion_text_token_price: number,
created: number,
fingerprint: string,
id: string,
input_modalities: {
modality
},
object: string,
output_modalities: {
modality
},
owned_by: string,
prompt_image_token_price: number,
prompt_text_token_price: number,
search_price: number,
version: string
}
export type LanguageModelsResponse = {
models: {
LanguageModel
}
}
export type LanguageModelRequest = model_id
export type LanguageModelResponse = LanguageModel
type ApiResponse = {
success: boolean
}
export type SuccessfulApiResponse = ApiResponse & {
response: CompletionsResponse | ModelsResponse | ModelResponse | LanguageModelsResponse | LanguageModelResponse
}
export type FailedApiResponse = ApiResponse & {
status: number,
message: string
}
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): SuccessfulApiResponse | FailedApiResponse
request.stream = false
local config = {
url = "https://api.x.ai/v1/chat/completions",
url = `{baseUrl}/chat/completions`,
method = "POST",
headers = {
Authorization = "Bearer " .. api_key,
@@ -180,16 +249,84 @@ function xai.create(api_key: string)
local response = net.request(config)
if response.ok then
local decoded: CompletionsResponse = serde.decode("json", response.body)
return decoded
return { success = true, response = decoded } :: SuccessfulApiResponse
else
error(`API request failed with status {response.statusCode}: {response.statusMessage}`)
return { success = false, status = response.statusCode, message = response.body } :: FailedApiResponse
end
end
function client:models(): SuccessfulApiResponse | FailedApiResponse
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 { success = true, response = decoded } :: SuccessfulApiResponse
else
return { success = false, status = response.statusCode, message = response.body } :: FailedApiResponse
end
end
function client:model(model_id: ModelRequest): SuccessfulApiResponse | FailedApiResponse
local config = {
url = `{baseUrl}/models/{model_id}`,
method = "GET",
headers = {
["Content-Type"] = "application/json",
},
}
local response = net.request(config)
if response.ok then
local decoded: ModelResponse = serde.decode("json", response.body)
return { success = true, response = decoded } :: SuccessfulApiResponse
else
return { success = false, status = response.statusCode, message = response.body } :: FailedApiResponse
end
end
function client:language_models(): SuccessfulApiResponse | FailedApiResponse
local config = {
url = `{baseUrl}/language-models`,
method = "GET",
headers = {
["Content-Type"] = "application/json",
},
}
local response = net.request(config)
if response.ok then
local decoded: LanguageModelsResponse = serde.decode("json", response.body)
return { success = true, response = decoded } :: SuccessfulApiResponse
else
return { success = false, status = response.statusCode, message = response.body } :: FailedApiResponse
end
end
function client:language_model(model_id: LanguageModelRequest): SuccessfulApiResponse | FailedApiResponse
local config = {
url = `{baseUrl}/language-models/{model_id}`,
method = "GET",
headers = {
["Content-Type"] = "application/json",
},
}
local response = net.request(config)
if response.ok then
local decoded: LanguageModelResponse = serde.decode("json", response.body)
return { success = true, response = decoded } :: SuccessfulApiResponse
else
return { success = false, status = response.statusCode, message = response.body } :: FailedApiResponse
end
end
return client
end
function xai.completions(api_key: string, request: CompletionsRequest): CompletionsResponse
function xai.completions(api_key: string, request: CompletionsRequest): ApiResponse
return xai.create(api_key):completions(request)
end

View File

@@ -15,4 +15,4 @@ local response = xai.completions(apiKey, {
})
print(response)
print(response.choices[1].message.content == 'Working.')
print(response.response.choices[1].message.content == 'Working.')