RubyLLM:一种令人愉悦的用 Ruby 操作 AI 的方式
RubyLLM: A delightful Ruby way to work with AI

原始链接: https://github.com/crmne/ruby_llm

RubyLLM 简化了 Ruby 中的 AI 集成,提供了一个统一的 API 接口,支持 OpenAI、Anthropic、Gemini 和 DeepSeek 等多个提供商。它可以处理文本、图像、音频和 PDF,支持文档分析、图像生成和嵌入等任务。 其主要特性包括简化的 API 密钥配置、无缝的多轮对话、流式响应以及在对话中切换模型的能力。RubyLLM 还支持允许 AI 与 Ruby 代码交互的工具,例如获取天气数据或搜索数据库。 Rails 集成通过 ActiveRecord 简化,使用 `acts_as_chat`、`acts_as_message` 和 `acts_as_tool_call` 来持久化聊天记录和工具交互。流式响应可以使用 Turbo 广播。凭借最少的依赖项和对表达性代码的关注,RubyLLM 使在 Ruby 中使用 AI 变得轻松愉快。

Hacker News 上对 RubyLLM 非常热议,这是一个新的 Ruby 库,旨在简化 AI 交互。一些用户赞扬它简洁的语法和比 Langchain 等替代方案更好的开发者体验,但也有人担心由于 Ruby 的阻塞特性,并发和潜在的性能瓶颈问题。该库在文档示例中使用 `eval()` 也引发了讨论,并促使开发者迅速修复了这个问题。 讨论围绕着 Ruby 是否适合异步 AI 工作负载以及开发者体验和可维护性之间的权衡。一些人认为 Ruby 的表达能力优先考虑了作者的体验,而另一些人则强调 Go 关注的是可维护性和健壮的系统。目前正在使用 async-http-faraday 添加 AsyncIO 支持,这可能会显著提高 AI 工作负载的性能。总的来说,社区承认 Ruby 在创建简洁易读的代码方面的优势,但也承认需要仔细处理并发问题以及元编程的潜在陷阱。
相关文章
  • (评论) 2025-03-16
  • Show HN:基于Ollama的开源文档AI 2025-03-09
  • (评论) 2024-08-22
  • (评论) 2024-08-04
  • (评论) 2025-03-09

  • 原文

    RubyLLM

    A delightful Ruby way to work with AI. No configuration madness, no complex callbacks, no handler hell – just beautiful, expressive Ruby code.

    Gem Version Ruby Style Guide Gem Downloads codecov

    🤺 Battle tested at 💬 Chat with Work

    The problem with AI libraries

    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.

    • 💬 Chat with OpenAI, Anthropic, Gemini, and DeepSeek models
    • 👁️ Vision and Audio understanding
    • 📄 PDF Analysis for analyzing documents
    • 🖼️ Image generation with DALL-E and other providers
    • 📊 Embeddings for vector search and semantic analysis
    • 🔧 Tools that let AI use your Ruby code
    • 🚂 Rails integration to persist chats and messages with ActiveRecord
    • 🌊 Streaming responses with proper Ruby patterns
    # Just ask questions
    chat = RubyLLM.chat
    chat.ask "What's the best way to learn Ruby?"
    
    # Analyze images
    chat.ask "What's in this image?", with: { image: "ruby_conf.jpg" }
    
    # Analyze audio recordings
    chat.ask "Describe this meeting", with: { audio: "meeting.wav" }
    
    # Analyze documents
    chat.ask "Summarize this document", with: { pdf: "contract.pdf" }
    
    # Generate images
    RubyLLM.paint "a sunset over mountains in watercolor style"
    
    # Create vector embeddings
    RubyLLM.embed "Ruby is elegant and expressive"
    
    # Let AI use your code
    class Weather < RubyLLM::Tool
      description "Gets current weather for a location"
      param :latitude, desc: "Latitude (e.g., 52.5200)"
      param :longitude, desc: "Longitude (e.g., 13.4050)"
    
      def execute(latitude:, longitude:)
        url = "https://api.open-meteo.com/v1/forecast?latitude=#{latitude}&longitude=#{longitude}&current=temperature_2m,wind_speed_10m"
    
        response = Faraday.get(url)
        data = JSON.parse(response.body)
      rescue => e
        { error: e.message }
      end
    end
    
    chat.with_tool(Weather).ask "What's the weather in Berlin? (52.5200, 13.4050)"
    # In your Gemfile
    gem 'ruby_llm'
    
    # Then run
    bundle install
    
    # Or install it yourself
    gem install ruby_llm

    Configure with your API keys:

    RubyLLM.configure do |config|
      config.openai_api_key = ENV['OPENAI_API_KEY']
      config.anthropic_api_key = ENV['ANTHROPIC_API_KEY']
      config.gemini_api_key = ENV['GEMINI_API_KEY']
      config.deepseek_api_key = ENV['DEEPSEEK_API_KEY'] # Optional
    end
    # Start a chat with the default model (GPT-4o-mini)
    chat = RubyLLM.chat
    
    # Or specify what you want
    chat = RubyLLM.chat(model: 'claude-3-7-sonnet-20250219')
    
    # Simple questions just work
    chat.ask "What's the difference between attr_reader and attr_accessor?"
    
    # Multi-turn conversations are seamless
    chat.ask "Could you give me an example?"
    
    # Stream responses in real-time
    chat.ask "Tell me a story about a Ruby programmer" do |chunk|
      print chunk.content
    end
    
    # Understand content in multiple forms
    chat.ask "Compare these diagrams", with: { image: ["diagram1.png", "diagram2.png"] }
    chat.ask "Summarize this document", with: { pdf: "contract.pdf" }
    chat.ask "What's being said?", with: { audio: "meeting.wav" }
    
    # Need a different model mid-conversation? No problem
    chat.with_model('gemini-2.0-flash').ask "What's your favorite algorithm?"

    Rails integration that makes sense

    # app/models/chat.rb
    class Chat < ApplicationRecord
      acts_as_chat
    
      # Works great with Turbo
      broadcasts_to ->(chat) { "chat_#{chat.id}" }
    end
    
    # app/models/message.rb
    class Message < ApplicationRecord
      acts_as_message
    end
    
    # app/models/tool_call.rb
    class ToolCall < ApplicationRecord
      acts_as_tool_call
    end
    
    # In your controller
    chat = Chat.create!(model_id: "gpt-4o-mini")
    chat.ask("What's your favorite Ruby gem?") do |chunk|
      Turbo::StreamsChannel.broadcast_append_to(
        chat,
        target: "response",
        partial: "messages/chunk",
        locals: { chunk: chunk }
      )
    end
    
    # That's it - chat history is automatically saved

    Creating tools is a breeze

    class Search < RubyLLM::Tool
      description "Searches a knowledge base"
    
      param :query, desc: "The search query"
      param :limit, type: :integer, desc: "Max results", required: false
    
      def execute(query:, limit: 5)
        # Your search logic here
        Document.search(query).limit(limit).map(&:title)
      end
    end
    
    # Let the AI use it
    chat.with_tool(Search).ask "Find documents about Ruby 3.3 features"

    Check out the guides at https://rubyllm.com for deeper dives into conversations with tools, streaming responses, embedding generations, and more.

    Released under the MIT License.

    联系我们 contact @ memedata.com