OpenAI.luau - Simple OpenAI API wrapper

OpenAI.luau

GitHubRoblox MarketplaceWally¹ • Documentation


One day I wanted to use an external AI model for my project, but I wanted to use an API wrapper for it. Since I couldn’t find one like what I wanted, I decided to make one, and after I made it, I thought other people might find it useful, so I’m releasing it!

Example Code
local OpenAI = require("./path/to/openai")

-- Creating the OpenAI client
local openai = OpenAI.new({
	apiKey = "your-api-key",
	baseURL = "https://api.openai.com/v1",
	endpoints = {
		chatCompletion = "/chat/completions",
        listModels = "/models",
        moderation = "/moderations"
	}
})

-- Generating a chat completion
local response = openai:CreateChatCompletion({
	model = "gpt-4o-mini",
	messages = {
		{
			role = "user",
			content = "Hello, how are you?"
		}
	},
	temperature = 1,
	max_completion_tokens = 4096
})

print(response)

-- Moderation
local moderationResult = openai:CreateModeration({
	"I hope you die", "I love you so much!"
})
print(moderationResult.results[1].flagged) -- True
print(moderationResult.results[2].flagged) -- False

-- Grabbing models
local models = openai:GetAvailableModels()
print(models)

Todo:

  • Moderation API support
  • Getting available models
  • OpenAI assistant support

Please share any issues on the GitHub Issues page, or fork it to fix issues yourself!

¹ The Wally repo is sort of experimental as I’ve never used it before, so let me know how it goes.

Released under the MIT license because I love open source.

7 Likes

why is no one talking about this wrapper?

I wanna use this api because i hosted a deepseek model on deepinfra.com

i suggest that using rojo to build the wrapper. instead of directly putting the script on github.

1 Like

hey, it seems like i cant use this wrapper

sorry about that. you should be able to access it now. also about your rojo question, I have to get more familiar with rojo since I don’t use it that often but I’ll definitely check it out.

Completely rewrote this project using Rojo (thanks @VeryLiquidCold for suggesting this). Also uploaded the project to Wally, added moderation and model listing support, and completely rewrote the documentation. The new version is v0.1.1

1 Like

didnt know you were this quick but thanks!

1 Like