From 5b82c5381eef62de6b274dcfa3f5d4e1e24e7761 Mon Sep 17 00:00:00 2001 From: cyclic Date: Tue, 23 Sep 2025 21:25:35 -0600 Subject: [PATCH] initialize --- .luaurc | 5 ++ LICENSE | 7 ++ README.md | 59 +++++++++++++++ rokit.toml | 7 ++ src/init.luau | 196 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/test.luau | 18 +++++ 6 files changed, 292 insertions(+) create mode 100644 .luaurc create mode 100644 LICENSE create mode 100644 README.md create mode 100644 rokit.toml create mode 100644 src/init.luau create mode 100644 src/test.luau diff --git a/.luaurc b/.luaurc new file mode 100644 index 0000000..e5fd924 --- /dev/null +++ b/.luaurc @@ -0,0 +1,5 @@ +{ + "aliases": { + "lune": "~/.lune/.typedefs/0.10.2/" + } +} diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..c207471 --- /dev/null +++ b/LICENSE @@ -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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..b8a7389 --- /dev/null +++ b/README.md @@ -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. \ No newline at end of file diff --git a/rokit.toml b/rokit.toml new file mode 100644 index 0000000..a1d4b0b --- /dev/null +++ b/rokit.toml @@ -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 ` in a terminal. + +[tools] +lune = "lune-org/lune@0.10.2" diff --git a/src/init.luau b/src/init.luau new file mode 100644 index 0000000..3330d58 --- /dev/null +++ b/src/init.luau @@ -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 diff --git a/src/test.luau b/src/test.luau new file mode 100644 index 0000000..033a7af --- /dev/null +++ b/src/test.luau @@ -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.')