Skip to content

Commit 225f112

Browse files
committed
rename an example, add image generation example
1 parent 61354a0 commit 225f112

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

examples/image_generation.lua

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
3+
local openai = require("openai")
4+
local client = openai.new(os.getenv("OPENAI_API_KEY"))
5+
6+
local status, response = client:image_generation({
7+
model = "gpt-image-1.5",
8+
prompt = "Design a modern logo for MoonScript, the language that compiles to Lua"
9+
})
10+
11+
12+
if status == 200 then
13+
print("Size", response.size)
14+
print("Background", response.background)
15+
print("Output format", response.output_format)
16+
print("Quality", response.quality)
17+
18+
-- output the image
19+
local filename = "image_generation.png"
20+
local mime = require("mime")
21+
local image_bytes = mime.unb64(response.data[1].b64_json)
22+
23+
local file = io.open(filename, "wb")
24+
if file then
25+
file:write(image_bytes)
26+
file:close()
27+
print("Saved image to: " .. filename)
28+
else
29+
print("Failed to save image to: " .. filename)
30+
end
31+
else
32+
local json = require("cjson")
33+
print(json.encode(response))
34+
end
35+
36+

0 commit comments

Comments
 (0)