A delightful Ruby way to work with AI. RubyLLM provides one beautiful, Ruby-like interface to interact with modern AI models. Chat, generate images, create embeddings, and use tools – all with clean, expressive code that feels like Ruby, not like patching together multiple services.
🤺 Battle tested at 💬 Chat with Work
Every AI provider comes with its own client library, its own response format, its own conventions for streaming, and its own way of handling errors. Want to use multiple providers? Prepare to juggle incompatible APIs and bloated dependencies.
RubyLLM fixes all that. One beautiful API for everything. One consistent format. Minimal dependencies — just Faraday and Zeitwerk. Because working with AI should be a joy, not a chore.
# Just ask questionschat=RubyLLM.chatchat.ask"What's the best way to learn Ruby?"# Analyze images, audio, documents, and text fileschat.ask"What's in this image?",with: "ruby_conf.jpg"chat.ask"Describe this meeting",with: "meeting.wav"chat.ask"Summarize this document",with: "contract.pdf"chat.ask"Explain this code",with: "app.rb"# Multiple files at once - types automatically detectedchat.ask"Analyze these files",with: ["diagram.png","report.pdf","notes.txt"]# Stream responses in real-timechat.ask"Tell me a story about a Ruby programmer"do |chunk|
printchunk.contentend# Generate imagesRubyLLM.paint"a sunset over mountains in watercolor style"# Create vector embeddingsRubyLLM.embed"Ruby is elegant and expressive"# Let AI use your codeclassWeather < RubyLLM::Tooldescription"Gets current weather for a location"param:latitude,desc: "Latitude (e.g., 52.5200)"param:longitude,desc: "Longitude (e.g., 13.4050)"defexecute(latitude:,longitude:)url="https://api.open-meteo.com/v1/forecast?latitude=#{latitude}&longitude=#{longitude}¤t=temperature_2m,wind_speed_10m"response=Faraday.get(url)data=JSON.parse(response.body)rescue=>e{error: e.message}endendchat.with_tool(Weather).ask"What's the weather in Berlin? (52.5200, 13.4050)"- 💬 Unified Chat: Converse with models from OpenAI, Anthropic, Gemini, Bedrock, OpenRouter, DeepSeek, Ollama, or any OpenAI-compatible API using
RubyLLM.chat. - 👁️ Vision: Analyze images within chats.
- 🔊 Audio: Transcribe and understand audio content.
- 📄 Document Analysis: Extract information from PDFs, text files, and other documents.
- 🖼️ Image Generation: Create images with
RubyLLM.paint. - 📊 Embeddings: Generate text embeddings for vector search with
RubyLLM.embed. - 🔧 Tools (Function Calling): Let AI models call your Ruby code using
RubyLLM::Tool. - 🚂 Rails Integration: Easily persist chats, messages, and tool calls using
acts_as_chatandacts_as_message. - 🌊 Streaming: Process responses in real-time with idiomatic Ruby blocks.
Add to your Gemfile:
gem'ruby_llm'Then bundle install.
Configure your API keys (using environment variables is recommended):
# config/initializers/ruby_llm.rb or similarRubyLLM.configuredo |config|
config.openai_api_key=ENV.fetch('OPENAI_API_KEY',nil)# Add keys ONLY for providers you intend to use# config.anthropic_api_key = ENV.fetch('ANTHROPIC_API_KEY', nil)# ... see Configuration guide for all options ...endSee the Installation Guide for full details.
Add persistence to your chat models effortlessly:
# app/models/chat.rbclassChat < ApplicationRecordacts_as_chat# Automatically saves messages & tool calls# ... your other model logic ...end# app/models/message.rbclassMessage < ApplicationRecordacts_as_message# ...end# app/models/tool_call.rb (if using tools)classToolCall < ApplicationRecordacts_as_tool_call# ...end# Now interacting with a Chat record persists the conversation:chat_record=Chat.create!(model_id: "gpt-4.1-nano")chat_record.ask("Explain Active Record callbacks.")# User & Assistant messages saved# Works seamlessly with file attachments - types automatically detectedchat_record.ask("What's in this file?",with: "report.pdf")chat_record.ask("Analyze these",with: ["image.jpg","data.csv","notes.txt"])Check the Rails Integration Guide for more.
Dive deeper with the official documentation:
- Installation
- Configuration
- Guides:
We welcome contributions! Please see CONTRIBUTING.md for details on setup, testing, and contribution guidelines.
Released under the MIT License.