Compare commits

..

6 Commits

2 changed files with 64 additions and 16 deletions

View File

@@ -1,7 +1,7 @@
local net = require("@lune/net") local net = require("@lune/net")
local serde = require("@lune/serde") 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 = { export type message = {
role: string, role: string,
@@ -15,8 +15,8 @@ export type message = {
name: string name: string
}, },
id: string, id: string,
index: number?, index: number?, -- according to x.ai's documentation, this is 'integer | null' so i guess it can for some reason not exist?
type: 'function' type: 'function'?
} }
}? }?
} }
@@ -26,7 +26,7 @@ export type CompletionsResponse = {
object: 'chat.completion', object: 'chat.completion',
created: number, created: number,
model: string, model: string,
choices: { choices: { {
index: number, index: number,
message: message, message: message,
finish_reason: "stop" | "length" | "function_call" | "content_filter" | "null", finish_reason: "stop" | "length" | "function_call" | "content_filter" | "null",
@@ -48,7 +48,7 @@ export type CompletionsResponse = {
} }
} }
}? }?
}, } },
citations: { string }?, citations: { string }?,
system_fingerprint: string?, system_fingerprint: string?,
usage: { usage: {
@@ -99,7 +99,7 @@ type rsssource = {
export type CompletionsRequest = { export type CompletionsRequest = {
messages: { message }, messages: { message },
model: model, model: AvailableModels,
stream: false?, stream: false?,
search_parameters: { search_parameters: {
mode: 'auto' | 'on' | 'off', mode: 'auto' | 'on' | 'off',
@@ -162,16 +162,25 @@ export type CompletionsRequest = {
}? }?
} }
type model = {
created: number,
id: string,
object: "model",
owned_by: string
}
export type ModelsResponse = { export type ModelsResponse = {
data : { data : {
created: number, model
id: string,
object: "model",
owned_by: string
}, },
object: "list" object: "list"
} }
type model_id = string
export type ModelRequest = model_id
export type ModelResponse = model
type modality = 'text' | 'image' | string type modality = 'text' | 'image' | string
export type LanguageModel = { export type LanguageModel = {
@@ -203,12 +212,15 @@ export type LanguageModelsResponse = {
} }
} }
export type LanguageModelRequest = model_id
export type LanguageModelResponse = LanguageModel
type ApiResponse = { type ApiResponse = {
success: boolean success: boolean
} }
export type SuccessfulApiResponse = ApiResponse & { export type SuccessfulApiResponse = ApiResponse & {
response: CompletionsResponse | ModelsResponse | LanguageModelsResponse response: CompletionsResponse | ModelsResponse | ModelResponse | LanguageModelsResponse | LanguageModelResponse
} }
export type FailedApiResponse = ApiResponse & { export type FailedApiResponse = ApiResponse & {
@@ -223,8 +235,10 @@ function xai.create(api_key: string)
local client = {} local client = {}
function client:completions(request: CompletionsRequest): SuccessfulApiResponse | FailedApiResponse function client:completions(request: CompletionsRequest): SuccessfulApiResponse | FailedApiResponse
request.stream = false
local config = { local config = {
url = `{baseUrl}/completions`, url = `{baseUrl}/chat/completions`,
method = "POST", method = "POST",
headers = { headers = {
Authorization = "Bearer " .. api_key, Authorization = "Bearer " .. api_key,
@@ -237,7 +251,7 @@ function xai.create(api_key: string)
local decoded: CompletionsResponse = serde.decode("json", response.body) local decoded: CompletionsResponse = serde.decode("json", response.body)
return { success = true, response = decoded } :: SuccessfulApiResponse return { success = true, response = decoded } :: SuccessfulApiResponse
else else
return { success = false, status = response.status, message = response.body } :: FailedApiResponse return { success = false, status = response.statusCode, message = response.body } :: FailedApiResponse
end end
end end
@@ -254,7 +268,24 @@ function xai.create(api_key: string)
local decoded: ModelsResponse = serde.decode("json", response.body) local decoded: ModelsResponse = serde.decode("json", response.body)
return { success = true, response = decoded } :: SuccessfulApiResponse return { success = true, response = decoded } :: SuccessfulApiResponse
else else
return { success = false, status = response.status, message = response.body } :: FailedApiResponse 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
end end
@@ -271,7 +302,24 @@ function xai.create(api_key: string)
local decoded: LanguageModelsResponse = serde.decode("json", response.body) local decoded: LanguageModelsResponse = serde.decode("json", response.body)
return { success = true, response = decoded } :: SuccessfulApiResponse return { success = true, response = decoded } :: SuccessfulApiResponse
else else
return { success = false, status = response.status, message = response.body } :: FailedApiResponse 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
end end

View File

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