openrouter
HTTP Client for the OpenRouter API (https://openrouter.ai)
Installation
-
Add the dependency to your
shard.yml
:dependencies: openrouter: github: petterthowsen/openrouter
-
Run
shards install
Usage
require "openrouter"
# create a client instance
client = OpenRouter::Client.new "sk-or-v1-123456789..."
# get available models as an Array of OpenRouter::Model
models = client.get_models
# get a completion from a single text prompt:
response = client.complete("Hello, how are you?", "mistralai/mistral-7b-instruct:free")
# or use the CompletionRequest class to construct a more complex request with multiple messages:
request = OpenRouter::CompletionRequest.new(
model: "mistralai/mistral-7b-instruct:free",
messages: [
OpenRouter::Message.new(role: OpenRouter::Role::User, content: "If I have 10 apples..."),
OpenRouter::Message.new(role: OpenRouter::Role::Assistant, content: "You have 10 apples and... ?"),
OpenRouter::Message.new(role: OpenRouter::Role::User, content: "I eat half of them. How many do I have left?"),
]
)
response = client.complete(request)
Development
Pretty self-explanatory. The main class is the Client in ./src/openrouter/client.cr
.
It uses the CompletionRequest in ./src/openrouter/types/completion_request.cr
and Response in ./src/openrouter/types/response.cr
.
Contributing
- Fork it (https://github.com/petterthowsen/openrouter/fork)
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
Contributors
- Petter Thowsen - creator and maintainer