initialize

This commit is contained in:
2025-09-23 21:25:35 -06:00
commit 5b82c5381e
6 changed files with 292 additions and 0 deletions

196
src/init.luau Normal file
View File

@@ -0,0 +1,196 @@
local net = require("@lune/net")
local serde = require("@lune/serde")
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 message = {
role: string,
content: string?,
refusal: string?,
reasoning_content: string?,
tool_calls: {
{
func: {
arguments: string,
name: string
},
id: string,
index: number?,
type: 'function'
}
}?
}
export type CompletionsResponse = {
id: string,
object: 'chat.completion',
created: number,
model: string,
choices: {
index: number,
message: message,
finish_reason: "stop" | "length" | "function_call" | "content_filter" | "null",
logprobs: {
{
content: {
bytes: {
number
},
logprob: number,
token: string,
top_logprobs: {
bytes: {
number
},
logprob: number,
token: string
}
}
}
}?
},
citations: { string }?,
system_fingerprint: string?,
usage: {
completion_tokens: number,
completion_tokens_details: {
accepted_prediction_tokens: number,
audio_tokens: number,
reasoning_tokens: number,
rejected_prediction_tokens: number
},
num_sources_used: number,
prompt_tokens: number,
prompt_tokens_details: {
audio_tokens: number,
cached_tokens: number,
image_tokens: number,
text_tokens: number
},
total_tokens: number
},
}
type xsource = {
included_x_handles: { string }?,
excluded_x_handles: { string }?,
post_favourite_count: number?,
post_view_count: number?,
type: 'x'
}
type websource = {
excluded_websites: { string }?,
allowed_websites: { string }?,
country: string?,
safe_search: boolean?,
type: 'web'
}
type newssource = {
excluded_websites: { string }?,
country: string?,
safe_search: boolean?,
type: 'news'
}
type rsssource = {
links: { string }?,
type: 'rss'
}
export type CompletionsRequest = {
messages: { message },
model: model,
stream: false?,
search_parameters: {
mode: 'auto' | 'on' | 'off',
return_citations: boolean?,
from_date: string?,
to_date: string,
max_search_results: number?,
sources: {
xsource | websource | newssource | rsssource
}?
}?,
deferred: false | boolean?,
frequency_penalty: number?,
logprobs: false | boolean?,
max_completion_tokens: number?,
n: number?,
parallel_tool_calls: true | boolean?,
presence_penalty: number?,
reasoning_effort: 'low' | 'high'?,
response_format:{
type: 'text'
} | {
type: 'json_object'
} | {
type: 'json_schema',
json_schema: {any}
}?,
seed: number?,
stop: { string }?,
temperature: number?,
tool_choice: 'none' | 'auto' | 'required' | {
func: {
name: string
},
type: 'function'
}?,
tools: {
{
func: {
description: string?,
name: string,
parameters: string
},
type: string
} | {
sources: {
xsource | websource | newssource | rsssource
},
type: "live_search"
}
}?,
top_logprobs: number?,
top_p: number?,
user: string?,
web_search_options: {
{
search_context_size: 'medium' | string?,
user_location: string
}
}?
}
local xai = {}
function xai.create(api_key: string)
local client = {}
function client.completions(self, request: CompletionsRequest): CompletionsResponse
local config = {
url = "https://api.x.ai/v1/chat/completions",
method = "POST",
headers = {
Authorization = "Bearer " .. api_key,
["Content-Type"] = "application/json",
},
body = serde.encode("json", request),
}
local response = net.request(config)
if response.ok then
local decoded: CompletionsResponse = serde.decode("json", response.body)
return decoded
else
error(`API request failed with status {response.statusCode}: {response.statusMessage}`)
end
end
return client
end
function xai.responses(api_key: string, request: CompletionsRequest): CompletionsResponse
return xai.create(api_key):responses(request)
end
return xai

18
src/test.luau Normal file
View File

@@ -0,0 +1,18 @@
local stdio = require("@lune/stdio")
local xai = require('../src')
local apiKey: string = stdio.prompt("text", "Please provide the API key for testing")
local response = xai.responses(apiKey, {
model = 'grok-4-fast-non-reasoning',
messages = {
{
role = 'user',
content = 'Please reply with just "Working."'
}
},
stream = false
})
print(response)
print(response.choices[1].message.content == 'Working.')