RubyLLM:一个支持所有主流 AI 提供商、简洁优美的 Ruby 框架
RubyLLM: A Ruby framework for all major AI providers

原始链接: https://rubyllm.com/

RubyLLM 是一个统一的 Ruby 框架,旨在通过为所有主流 AI 提供商(包括 OpenAI、Claude、Gemini、Mistral 以及通过 Ollama 运行的本地模型)提供一致的接口,从而简化 AI 集成过程。 通过屏蔽不同 API 和响应格式的复杂性,RubyLLM 允许开发人员使用统一且简洁的语法构建复杂的 AI 工作流,例如聊天机器人、RAG 应用和自主智能体。该框架非常轻量,仅依赖三个核心组件(Faraday、Zeitwerk 和 Marcel),并提供丰富的功能,包括图像生成、音频转录、文件分析、结构化 JSON 输出以及自定义工具定义。 RubyLLM 专为生产环境设计,支持异步处理、通过 `acts_as_chat` 与 Rails 集成,以及持久化的模型深度思考。无论你是进行简单的内容审核,还是构建复杂的多智能体系统,RubyLLM 都能简化开发流程,让你在无需重写代码库的情况下轻松切换模型或提供商。

**RubyLLM** 是一个开源的 Ruby 框架,旨在为各大 AI 提供商提供统一的接口。它因其易用性在 Hacker News 上获得了积极反响,用户将其易用性与 Vercel 的 AI 框架相媲美。 该项目旨在平衡“开箱即用”的功能与灵活性,通过简洁、一致的 API 让开发者能够控制温度(temperature)和“思考”强度等参数。尽管一些用户指出了在特定服务商的缓存和参数调优方面存在挑战,但项目作者正在积极解决这些问题,包括在 2.0 版本中进行更新。 该框架在 Ruby 社区广受好评,开发者们称赞其在扩大 Ruby 在 AI 生态系统中影响力方面所做的努力。作者目前仍在持续优化该库,并欢迎社区参与贡献,以进一步提升其功能。
相关文章

原文

A single, beautiful Ruby framework for all major AI providers. Easily build chatbots, AI agents, RAG applications, content generators, and every AI workflow you can think of.

Battle tested at - Fully private work AI

Build a working Ruby AI chat in two minutes

Using RubyLLM? Share your story! Takes 5 minutes.


Why RubyLLM?

Every AI provider ships their own bloated client. Different APIs. Different response formats. Different conventions. It’s exhausting.

RubyLLM gives you one beautiful framework for all of them. Same interface whether you’re using GPT, Claude, or your local Ollama. Just three dependencies: Faraday, Zeitwerk, and Marcel. That’s it.

Show me the code

# Just ask questions
chat = RubyLLM.chat
chat.ask "What's the best way to learn Ruby?"
# Analyze any file type
chat.ask "What's in this image?", with: "ruby_conf.jpg"
chat.ask "What's happening in this video?", with: "video.mp4"
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
chat.ask "Analyze these files", with: ["diagram.png", "report.pdf", "notes.txt"]
# Stream responses
chat.ask "Tell me a story about Ruby" do |chunk|
  print chunk.content
end
# Generate images
RubyLLM.paint "a sunset over mountains in watercolor style"
# Create embeddings
RubyLLM.embed "Ruby is elegant and expressive"
# Transcribe audio to text
RubyLLM.transcribe "meeting.wav"
# Moderate content for safety
RubyLLM.moderate "Check if this text is safe"
# Let AI use your code
class Weather < RubyLLM::Tool
  desc "Get current weather"

  def execute(latitude:, longitude:)
    url = "https://api.open-meteo.com/v1/forecast?latitude=#{latitude}&longitude=#{longitude}&current=temperature_2m,wind_speed_10m"
    JSON.parse(Faraday.get(url).body)
  end
end

chat.with_tool(Weather).ask "What's the weather in Berlin?"
# Define an agent with instructions + tools
class WeatherAssistant < RubyLLM::Agent
  model "gpt-5-nano"
  instructions "Be concise and always use tools for weather."
  tools Weather
end

WeatherAssistant.new.ask "What's the weather in Berlin?"
# Get structured output
class ProductSchema < RubyLLM::Schema
  string :name
  number :price
  array :features do
    string
  end
end

response = chat.with_schema(ProductSchema).ask "Analyze this product", with: "product.txt"

Features

  • Chat: Conversational AI with RubyLLM.chat
  • Vision: Analyze images and videos
  • Audio: Transcribe and understand speech with RubyLLM.transcribe
  • Documents: Extract from PDFs, CSVs, JSON, any file type
  • Image generation: Create images with RubyLLM.paint
  • Embeddings: Generate embeddings with RubyLLM.embed
  • Moderation: Content safety with RubyLLM.moderate
  • Tools: Let AI call your Ruby methods
  • Agents: Reusable assistants with RubyLLM::Agent
  • Structured output: JSON schemas that just work
  • Streaming: Real-time responses with blocks
  • Rails: ActiveRecord integration with acts_as_chat
  • Async: Fiber-based concurrency
  • Model registry: 800+ models with capability detection and pricing
  • Extended thinking: Control, view, and persist model deliberation
  • Providers: OpenAI, xAI, Anthropic, Gemini, VertexAI, Bedrock, DeepSeek, Mistral, Ollama, OpenRouter, Perplexity, GPUStack, and any OpenAI-compatible API

Installation

Add to your Gemfile:

Then bundle install.

Configure your API keys:

# config/initializers/ruby_llm.rb
RubyLLM.configure do |config|
  config.openai_api_key = ENV['OPENAI_API_KEY']
end

Rails

# Install Rails Integration
bin/rails generate ruby_llm:install
bin/rails db:migrate
bin/rails ruby_llm:load_models # v1.13+

# Add Chat UI (optional)
bin/rails generate ruby_llm:chat_ui
class Chat < ApplicationRecord
  acts_as_chat
end

chat = Chat.create! model: "claude-sonnet-4"
chat.ask "What's in this file?", with: "report.pdf"

Visit http://localhost:3000/chats for a ready-to-use chat interface!


联系我们 contact @ memedata.com