Ruby API Reference¶
The Ruby gem wraps the Rust core via Magnus. All request/response data is passed as JSON strings -- use JSON.parse and JSON.generate for conversion.
Installation¶
Or in your Gemfile:
Client¶
Constructor¶
require 'liter_llm'
client = LiterLlm::LlmClient.new('sk-...',
base_url: 'https://api.openai.com/v1', # optional
model_hint: 'groq/llama3-70b', # optional
max_retries: 3, # default: 3
timeout_secs: 60 # default: 60
)
| Parameter | Type | Default | Description |
|---|---|---|---|
api_key |
String |
required | API key (positional) |
base_url: |
String? |
nil |
Override provider base URL |
model_hint: |
String? |
nil |
Provider auto-detection hint |
max_retries: |
Integer |
3 |
Retries on 429/5xx |
timeout_secs: |
Integer |
60 |
Request timeout in seconds |
Methods¶
All methods are synchronous (they block on the Tokio runtime internally). Methods that accept requests take a JSON string and return a JSON string.
chat(request_json)¶
Send a chat completion request.
response_json = client.chat(JSON.generate(
model: 'gpt-4',
messages: [{ role: 'user', content: 'Hello' }]
))
response = JSON.parse(response_json)
embed(request_json)¶
Send an embedding request.
list_models¶
List available models. Takes no arguments.
image_generate(request_json)¶
Generate an image from a text prompt.
speech(request_json)¶
Generate speech audio from text. Returns a base64-encoded string of the audio bytes.
transcribe(request_json)¶
Transcribe audio to text.
moderate(request_json)¶
Check content against moderation policies.
rerank(request_json)¶
Rerank documents by relevance to a query.
response_json = client.rerank(JSON.generate(
model: 'rerank-v1', query: 'q', documents: ['a', 'b']
))
create_file(request_json)¶
Upload a file.
retrieve_file(file_id)¶
Retrieve metadata for a file by ID.
delete_file(file_id)¶
Delete a file by ID.
list_files(query_json)¶
List files. Pass nil or a JSON string with query parameters.
file_content(file_id)¶
Retrieve raw file content as a base64-encoded string.
create_batch(request_json) / retrieve_batch(batch_id) / list_batches(query_json) / cancel_batch(batch_id)¶
Batch management operations.
create_response(request_json) / retrieve_response(response_id) / cancel_response(response_id)¶
Responses API operations.
Error Handling¶
Errors are raised as Ruby exceptions:
ArgumentError-- invalid request JSONRuntimeError-- network, auth, provider, or serialization errors
begin
response = JSON.parse(client.chat(JSON.generate(model: 'gpt-4', messages: [])))
rescue ArgumentError => e
puts "Bad request: #{e.message}"
rescue RuntimeError => e
puts "Error: #{e.message}"
end