RubyLLM a fait de 2026 l'année des agents IA Ruby. Les équipes Rails qui construisent des fonctionnalités d'agent dans des applications existantes veulent des intégrations HTTP-first, pas des SDK lourds. Ce tutoriel connecte RubyLLM et Scavio dans une application Rails avec un outil de recherche fonctionnel.
Prérequis
- Rails 8+
- Ruby 3.3+
- Une clé API Scavio
- Le gem ruby_llm
Parcours
Étape 1: Ajoutez le gem ruby_llm
RubyLLM est livré avec un DSL d'agent supportant les outils.
bundle add ruby_llm
bundle add faradayÉtape 2: Créez une classe d'outil Scavio
Tout objet Ruby appelable fonctionne comme un outil RubyLLM.
# app/agents/tools/scavio_search.rb
class ScavioSearch
include RubyLLM::Tool
description 'Search the web across Google, Reddit, and YouTube.'
param :query, type: :string
def execute(query:)
res = Faraday.post('https://api.scavio.dev/api/v1/search',
{ query: query }.to_json,
{ 'x-api-key' => ENV['SCAVIO_API_KEY'], 'Content-Type' => 'application/json' })
JSON.parse(res.body).dig('organic_results') || []
end
endÉtape 3: Définissez l'agent
Le DSL d'agent RubyLLM lie les outils à la boucle de chat.
# app/agents/research_agent.rb
class ResearchAgent < RubyLLM::Agent
model 'claude-sonnet-4-6'
tools ScavioSearch
system_prompt 'Research agent. Cite every claim.'
endÉtape 4: Appelez l'agent depuis un contrôleur
Fonctionne dans un contrôleur Rails ou un job en arrière-plan.
# app/controllers/research_controller.rb
class ResearchController < ApplicationController
def create
result = ResearchAgent.chat(params[:question])
render json: { answer: result }
end
endÉtape 5: Configurez une route de test
Vérifiez de bout en bout dans le navigateur.
# config/routes.rb
post '/research', to: 'research#create'
# curl: curl -XPOST localhost:3000/research -d 'question=what is rubyllm'Exemple Python
# Ruby tutorial; Python parity call:
import os, requests
API_KEY = os.environ['SCAVIO_API_KEY']
r = requests.post('https://api.scavio.dev/api/v1/search',
headers={'x-api-key': API_KEY},
json={'query': 'rubyllm 2026'})
print(r.json().get('organic_results', [])[:3])Exemple JavaScript
// Ruby tutorial; JS parity call:
const API_KEY = process.env.SCAVIO_API_KEY;
const r = await fetch('https://api.scavio.dev/api/v1/search', {
method: 'POST',
headers: { 'x-api-key': API_KEY, 'Content-Type': 'application/json' },
body: JSON.stringify({ query: 'rubyllm 2026' })
});
console.log(((await r.json()).organic_results || []).slice(0, 3));Sortie attendue
Rails endpoint returns agent answers grounded in Scavio search results. Works inside synchronous controllers or Sidekiq jobs.