Skip to content

Commit 203d603

Browse files
committed
add openrouter compat client
1 parent af29c82 commit 203d603

4 files changed

Lines changed: 86 additions & 8 deletions

File tree

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,26 @@ end
524524
See the `examples/gemini/` directory for more examples including structured
525525
output with JSON schemas.
526526

527+
## Using with OpenRouter
528+
529+
[OpenRouter](https://openrouter.ai/) provides access to many AI models through a
530+
unified API. This library includes an `OpenRouter` client that uses their
531+
[OpenAI-compatible endpoint](https://openrouter.ai/docs/quickstart).
532+
533+
```lua
534+
local OpenRouter = require("openai.compat.openrouter")
535+
local client = OpenRouter.new(os.getenv("OPENROUTER_API_KEY"))
536+
537+
local status, response = client:create_chat_completion({
538+
{role = "user", content = "Hello, how are you?"}
539+
}, {
540+
model = "anthropic/claude-sonnet-4" -- default model is openai/gpt-4.1
541+
})
542+
```
543+
544+
The `OpenRouter` client extends `OpenAI` and supports all the same methods
545+
including chat completions, chat sessions, and streaming.
546+
527547
## Appendix
528548

529549
### Chat Session With Functions

lua-openai-dev-1.rockspec

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ dependencies = {
2020
"luasec",
2121
}
2222
build = {
23-
type = "builtin",
24-
modules = {
25-
["openai"] = "openai/init.lua",
26-
["openai.sse"] = "openai/sse.lua",
27-
["openai.chat_completions"] = "openai/chat_completions.lua",
28-
["openai.responses"] = "openai/responses.lua",
29-
["openai.compat.gemini"] = "openai/compat/gemini.lua"
30-
}
23+
type = "builtin",
24+
modules = {
25+
["openai.chat_completions"] = "openai/chat_completions.lua",
26+
["openai.compat.gemini"] = "openai/compat/gemini.lua",
27+
["openai.compat.openrouter"] = "openai/compat/openrouter.lua",
28+
["openai"] = "openai/init.lua",
29+
["openai.responses"] = "openai/responses.lua",
30+
["openai.sse"] = "openai/sse.lua",
31+
}
3132
}

openai/compat/openrouter.lua

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
local OpenAI
2+
OpenAI = require("openai").OpenAI
3+
local OpenRouter
4+
do
5+
local _class_0
6+
local _parent_0 = OpenAI
7+
local _base_0 = {
8+
api_base = "https://openrouter.ai/api/v1",
9+
default_model = "openai/gpt-4.1"
10+
}
11+
_base_0.__index = _base_0
12+
setmetatable(_base_0, _parent_0.__base)
13+
_class_0 = setmetatable({
14+
__init = function(self, ...)
15+
return _class_0.__parent.__init(self, ...)
16+
end,
17+
__base = _base_0,
18+
__name = "OpenRouter",
19+
__parent = _parent_0
20+
}, {
21+
__index = function(cls, name)
22+
local val = rawget(_base_0, name)
23+
if val == nil then
24+
local parent = rawget(cls, "__parent")
25+
if parent then
26+
return parent[name]
27+
end
28+
else
29+
return val
30+
end
31+
end,
32+
__call = function(cls, ...)
33+
local _self_0 = setmetatable({}, _base_0)
34+
cls.__init(_self_0, ...)
35+
return _self_0
36+
end
37+
})
38+
_base_0.__class = _class_0
39+
if _parent_0.__inherited then
40+
_parent_0.__inherited(_parent_0, _class_0)
41+
end
42+
OpenRouter = _class_0
43+
end
44+
return {
45+
OpenRouter = OpenRouter,
46+
new = OpenRouter
47+
}

openai/compat/openrouter.moon

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-- OpenRouter client using OpenAI compatibility layer
2+
-- https://openrouter.ai/docs/quickstart
3+
4+
import OpenAI from require "openai"
5+
6+
class OpenRouter extends OpenAI
7+
api_base: "https://openrouter.ai/api/v1"
8+
default_model: "openai/gpt-4.1"
9+
10+
{:OpenRouter, new: OpenRouter}

0 commit comments

Comments
 (0)