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 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,16 +162,25 @@ export type CompletionsRequest = {
}?
}
type model = {
created: number,
id: string,
object: "model",
owned_by: string
}
export type ModelsResponse = {
data : {
created: number,
id: string,
object: "model",
owned_by: string
model
},
object: "list"
}
type model_id = string
export type ModelRequest = model_id
export type ModelResponse = model
type modality = 'text' | 'image' | string
export type LanguageModel = {
@@ -203,12 +212,15 @@ export type LanguageModelsResponse = {
}
}
export type LanguageModelRequest = model_id
export type LanguageModelResponse = LanguageModel
type ApiResponse = {
success: boolean
}
export type SuccessfulApiResponse = ApiResponse & {
response: CompletionsResponse | ModelsResponse | LanguageModelsResponse
response: CompletionsResponse | ModelsResponse | ModelResponse | LanguageModelsResponse | LanguageModelResponse
}
export type FailedApiResponse = ApiResponse & {
@@ -223,8 +235,10 @@ function xai.create(api_key: string)
local client = {}
function client:completions(request: CompletionsRequest): SuccessfulApiResponse | FailedApiResponse
request.stream = false
local config = {
url = `{baseUrl}/completions`,
url = `{baseUrl}/chat/completions`,
method = "POST",
headers = {
Authorization = "Bearer " .. api_key,
@@ -237,7 +251,7 @@ function xai.create(api_key: string)
local decoded: CompletionsResponse = serde.decode("json", response.body)
return { success = true, response = decoded } :: SuccessfulApiResponse
else
return { success = false, status = response.status, message = response.body } :: FailedApiResponse
return { success = false, status = response.statusCode, message = response.body } :: FailedApiResponse
end
end
@@ -254,7 +268,24 @@ function xai.create(api_key: string)
local decoded: ModelsResponse = serde.decode("json", response.body)
return { success = true, response = decoded } :: SuccessfulApiResponse
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
@@ -271,7 +302,24 @@ function xai.create(api_key: string)
local decoded: LanguageModelsResponse = serde.decode("json", response.body)
return { success = true, response = decoded } :: SuccessfulApiResponse
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

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.')