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

5
.luaurc Normal file
View File

@@ -0,0 +1,5 @@
{
"aliases": {
"lune": "~/.lune/.typedefs/0.10.2/"
}
}

7
LICENSE Normal file
View File

@@ -0,0 +1,7 @@
Copyright (c) 2025 cyclic (cyclic@luau.software)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

59
README.md Normal file
View File

@@ -0,0 +1,59 @@
# x-ai SDK
A simple Luau SDK for the x.ai API.
## Installation
Install Rokit. Then install Lune like so:
```
rokit install
```
Then require the SDK in your Luau code.
## Usage
First, get your API key from [x.ai](https://x.ai).
Create a client instance:
```luau
local xai = require("./src/init")
local client = xai.create("your-api-key-here")
```
Make a chat completion request:
```luau
local response = client:responses({
model = "grok-4",
messages = {
{ role = "user", content = "Hello, Grok!" }
}
})
print(response.choices[1].message.content)
```
For backward compatibility, you can also use the static method:
```luau
local response = xai.responses("your-api-key", request)
```
## Types
The module exports several types for requests and responses:
- `model`: Available models like 'grok-4', etc.
- `message`: Structure for chat messages.
- `CompletionsResponse`: The response from the API.
- `CompletionsRequest`: The request structure.
Refer to `src/init.luau` for full type definitions.
## License
MIT.
See `LICENSE` for details.

7
rokit.toml Normal file
View File

@@ -0,0 +1,7 @@
# This file lists tools managed by Rokit, a toolchain manager for Roblox projects.
# For more information, see https://github.com/rojo-rbx/rokit
# New tools can be added by running `rokit add <tool>` in a terminal.
[tools]
lune = "lune-org/lune@0.10.2"

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