RubyLLM - 一个单一、优雅的Ruby框架,适用于所有主流AI提供商

Lobsters Hottest 工具

摘要

RubyLLM是一个统一的Ruby框架,用于与多个AI提供商交互,支持聊天机器人、智能体、RAG等,并提供一致的API。

<p><a href="https://lobste.rs/s/ph49qi/rubyllm_single_beautiful_ruby_framework">评论</a></p>
查看原文
查看缓存全文

缓存时间: 2026/06/24 15:59

# RubyLLM 来源:https://rubyllm.com/ ## RubyLLM 一个简洁优雅的 Ruby 框架,统一覆盖所有主流 AI 提供商。轻松构建聊天机器人、AI 智能体、RAG 应用、内容生成器,以及你能想到的任何 AI 工作流。 已在 Chat with Work(https://chatwithwork.com/)经过实战检验 *— 完全私密的 AI 工作助手* ## https://rubyllm.com/#build-a-working-ruby-ai-chat-in-two-minutes两分钟构建可运行的 Ruby AI 聊天应用 正在使用 RubyLLM?分享你的故事(https://tally.so/r/3Na02p)!只需 5 分钟。 --- ## https://rubyllm.com/#why-rubyllm为什么选择 RubyLLM? 每个 AI 提供商都推出自家的臃肿客户端。不同的 API。不同的响应格式。不同的规范。令人厌烦。 RubyLLM 为它们提供一个统一而优雅的框架。无论使用 GPT、Claude 还是本地 Ollama,接口完全一致。仅依赖三个库:Faraday、Zeitwerk 和 Marcel。仅此而已。 ## https://rubyllm.com/#show-me-the-code看代码 ``` # 直接提问 chat = RubyLLM.chat chat.ask "学习 Ruby 的最佳方式是什么?" ``` ``` # 分析任意文件类型 chat.ask "这张图片里有什么?", with: "ruby_conf.jpg" chat.ask "这个视频里发生了什么?", with: "video.mp4" chat.ask "描述这次会议", with: "meeting.wav" chat.ask "总结这份文档", with: "contract.pdf" chat.ask "解释这段代码", with: "app.rb" ``` ``` # 一次分析多个文件 chat.ask "分析这些文件", with: ["diagram.png", "report.pdf", "notes.txt"] ``` ``` # 流式响应 chat.ask "给我讲一个关于 Ruby 的故事" do |chunk| print chunk.content end ``` ``` # 生成图片 RubyLLM.paint "一幅水彩风格的夕阳山景" ``` ``` # 创建嵌入向量 RubyLLM.embed "Ruby 优雅且富有表现力" ``` ``` # 音频转文字 RubyLLM.transcribe "meeting.wav" ``` ``` # 安全内容审核 RubyLLM.moderate "检查这段文本是否安全" ``` ``` # 让 AI 调用你的代码 class Weather < RubyLLM::Tool desc "获取当前天气" 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 "柏林的天气怎么样?" ``` ``` # 定义带指令和工具的智能体 class WeatherAssistant < RubyLLM::Agent model "gpt-5-nano" instructions "保持简洁,始终使用工具获取天气信息。" tools Weather end WeatherAssistant.new.ask "柏林的天气怎么样?" ``` ``` # 获取结构化输出 class ProductSchema < RubyLLM::Schema string :name number :price array :features do string end end response = chat.with_schema(ProductSchema).ask "分析这个产品", with: "product.txt" ``` ## https://rubyllm.com/#features功能特性 - **聊天:** 使用 `RubyLLM.chat` 进行对话式 AI - **视觉:** 分析图像和视频 - **音频:** 使用 `RubyLLM.transcribe` 转写和理解语音 - **文档:** 从 PDF、CSV、JSON 等任意文件类型中提取内容 - **图像生成:** 使用 `RubyLLM.paint` 创建图像 - **嵌入向量:** 使用 `RubyLLM.embed` 生成嵌入 - **内容审核:** 使用 `RubyLLM.moderate` 进行安全审核 - **工具:** 让 AI 调用你的 Ruby 方法 - **智能体:** 使用 `RubyLLM::Agent` 构建可复用的助手 - **结构化输出:** 开箱即用的 JSON Schema - **流式响应:** 通过块实现实时响应 - **Rails:** 通过 `acts_as_chat` 与 ActiveRecord 集成 - **异步:** 基于 Fiber 的并发 - **模型注册表:** 800+ 模型,支持能力检测和定价 - **扩展思考:** 控制、查看和持久化模型推理过程 - **提供商:** OpenAI、xAI、Anthropic、Gemini、VertexAI、Bedrock、DeepSeek、Mistral、Ollama、OpenRouter、Perplexity、GPUStack 以及任意兼容 OpenAI 的 API ## https://rubyllm.com/#installation安装 添加到你的 Gemfile: 然后执行 `bundle install`。 配置你的 API 密钥: ``` # config/initializers/ruby_llm.rb RubyLLM.configure do |config| config.openai_api_key = ENV['OPENAI_API_KEY'] end ``` ## https://rubyllm.com/#railsRails ``` # 安装 Rails 集成 bin/rails generate ruby_llm:install bin/rails db:migrate bin/rails ruby_llm:load_models # v1.13+ # 添加聊天界面(可选) bin/rails generate ruby_llm:chat_ui ``` ``` class Chat < ApplicationRecord acts_as_chat end chat = Chat.create! model: "claude-sonnet-4" chat.ask "这个文件里有什么?", with: "report.pdf" ``` 访问 `http://localhost:3000/chats` 即可获得即时可用的聊天界面!

相似文章

使用 llama.cpp 在本地运行的自动化 AI 研究员

Reddit r/LocalLLaMA

ml-intern 是一个面向 AI 代理的工具,它与 Hugging Face 的库集成,现在支持通过 llama.cpp 或 ollama 运行本地模型,使得自动化 AI 研究员可以在笔记本电脑上全天候运行。

LLM Gateway Chat

Product Hunt

LLM Gateway Chat 是一个平台,提供对多种AI模型的访问,用于聊天、图像、视频和音频生成。