Skip to content

Commit b68067c

Browse files
committed
verify all the examples
1 parent f72aece commit b68067c

File tree

7 files changed

+60
-5
lines changed

7 files changed

+60
-5
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ local openai = require("openai")
1919
local client = openai.new(os.getenv("OPENAI_API_KEY"))
2020

2121
local status, response = client:chat({
22-
{role = "system", content = "You are a Lua programmer"}
22+
{role = "system", content = "You are a Lua programmer"},
2323
{role = "user", content = "Write a 'Hello world' program in Lua"}
2424
}, {
25-
model = "gpt-3.5-turbo" -- this is the default model
25+
model = "gpt-3.5-turbo", -- this is the default model
2626
temperature = 0.5
2727
})
2828

@@ -69,8 +69,8 @@ time, allowing you to display content in real time as it is generated.
6969
local openai = require("openai")
7070
local client = openai.new(os.getenv("OPENAI_API_KEY"))
7171

72-
openai:chat({
73-
{role = "system", content = "You work for Streak.Club, a website to track daily creative habits"}
72+
client:chat({
73+
{role = "system", content = "You work for Streak.Club, a website to track daily creative habits"},
7474
{role = "user", content = "Who do you work for?"}
7575
}, {
7676
stream = true

examples/example1.lua

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
local openai = require("openai")
2+
local client = openai.new(os.getenv("OPENAI_API_KEY"))
3+
4+
local status, response = client:chat({
5+
{role = "system", content = "You are a Lua programmer"},
6+
{role = "user", content = "Write a 'Hello world' program in Lua"}
7+
}, {
8+
model = "gpt-3.5-turbo", -- this is the default model
9+
temperature = 0.5
10+
})
11+
12+
if status == 200 then
13+
-- the JSON response is automatically parsed into a Lua object
14+
print(response.choices[1].message.content)
15+
end

examples/example2.lua

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
local openai = require("openai")
2+
local client = openai.new(os.getenv("OPENAI_API_KEY"))
3+
4+
local chat = client:new_chat_session({
5+
-- provide an initial set of messages
6+
messages = {
7+
{role = "system", content = "You are an artist who likes colors"}
8+
}
9+
})
10+
11+
12+
-- returns the string response
13+
print(chat:send("List your top 5 favorite colors"))
14+
15+
-- the chat history is sent on subsequent requests to continue the conversation
16+
print(chat:send("Excluding the colors you just listed, tell me your favorite color"))

examples/example3.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
local openai = require("openai")
2+
local client = openai.new(os.getenv("OPENAI_API_KEY"))
3+
4+
client:chat({
5+
{role = "system", content = "You work for Streak.Club, a website to track daily creative habits"},
6+
{role = "user", content = "Who do you work for?"}
7+
}, {
8+
stream = true
9+
}, function(chunk)
10+
io.stdout:write(chunk.content)
11+
io.stdout:flush()
12+
end)
13+
14+
print() -- print a newline

openai.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
return require("openai.init")

openai/init.lua

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
local VERSION = "1.0.0"
12
local ltn12 = require("ltn12")
23
local cjson = require("cjson")
34
local types
@@ -256,5 +257,10 @@ do
256257
})
257258
_base_0.__class = _class_0
258259
OpenAI = _class_0
259-
return _class_0
260260
end
261+
return {
262+
OpenAI = OpenAI,
263+
ChatSession = ChatSession,
264+
VERSION = VERSION,
265+
new = OpenAI
266+
}

openai/init.moon

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
VERSION = "1.0.0"
12

23
ltn12 = require "ltn12"
34
cjson = require "cjson"
@@ -238,3 +239,5 @@ class OpenAI
238239
@_http
239240

240241

242+
243+
{:OpenAI, :ChatSession, :VERSION, new: OpenAI}

0 commit comments

Comments
 (0)