langchainrb_rails

repository·main·Indexed 43 days ago

https://github.com/patterns-ai-core/langchainrb_rails

A toolkit for integrating AI capabilities into Ruby on Rails applications using Langchain.rb. It provides generators for vector search (supporting Pgvector, Pinecone, and Qdrant), prompt templating, and AI assistants. Key features include RAG (Retrieval-Augmented Generation) via `.ask` and `.similarity_search` methods on ActiveRecord models, and the ability to persist assistant sessions to the database.

Tokens
5.2K
Snippets
30
Records
32
Agent score
56%

What's inside langchainrb_rails

  1. Customize the vector representation of a record

    main

    By default, the gem calls to_json(except: :embedding) on your model instance to create the data used for embeddings. To customize which fields are included in the vector representation, override the #as_vector method in your model.

    Note: After modifying #as_vector, you must re-run Model.embed! to update the embeddings in the database.

    def as_vector
      { name: name, description: description, category: category.name }.to_json
    end
    
    # Then re-generate
    Product.embed!
  2. Use the Assistant Generator for chat capabilities

    main

    The langchainrb_rails:assistant generator adds the necessary ActiveRecord models, migrations, controllers, views, and routes to enable Langchain::Assistant features in your Rails app. This allows you to build and chat with AI assistants immediately.

    Command: rails generate langchainrb_rails:assistant --llm=openai

    Available LLM options: anthropic, cohere, google_palm, google_gemini, google_vertex_ai, hugging_face, llama_cpp, mistral_ai, ollama, openai, and replicate.

    To undo the installation, run: rails destroy langchainrb_rails:assistant

    rails generate langchainrb_rails:assistant --llm=openai
  3. Use the Prompt Generator for templating

    main

    The langchainrb_rails:prompt generator adds a Prompt ActiveRecord model and a migration to your project. This allows you to manage and render prompt templates with dynamic variables.

    Usage Example:

    # Create a template with placeholders in curly braces
    prompt = Prompt.create!(template: "Tell me a {adjective} joke about {subject}.")
    
    # Render the template with specific values
    prompt.render(adjective: "funny", subject: "elephants")
    # => "Tell me a funny joke about elephants."
    rails generate langchainrb_rails:prompt
  4. Configure vector search with Pgvector

    main

    To add vector search capabilities to an ActiveRecord model using pgvector:

    1. Run the generator specifying your model and preferred LLM: rails generate langchainrb_rails:pgvector --model=Product --llm=openai This creates the initializer, migrations, and updates your model.
    2. Install dependencies and migrate the database: bundle install && rails db:migrate
    3. Set your OPENAI_API_KEY environment variable.
    4. Generate embeddings for existing records: Product.embed!
    rails generate langchainrb_rails:pgvector --model=Product --llm=openai
    bundle install && rails db:migrate
    Product.embed!
  5. Generate an AI Assistant with the langchainrb_rails generator

    main

    Use the langchainrb_rails:assistant generator to scaffold the necessary models (Assistant, Message), database migrations, controllers, views, and routes to implement an AI assistant in your Rails application.

    After running the generator, you must complete these manual steps:

    1. Set the required environment variable for your chosen LLM (e.g., ENV['OPENAI_API_KEY']).
    2. Run rails db:migrate to create the assistants and messages tables.
    3. Navigate to /assistants in your browser to begin using the assistant.
    rails generate langchainrb_rails:assistant --llm=openai
  6. Integrate vector search into ActiveRecord models

    main

    To enable automatic vector search synchronization in your Rails models, include the LangchainrbRails::ActiveRecord::Hooks module and use the vectorsearch class method. You should typically use ActiveRecord callbacks like after_save and after_destroy to keep your vector provider in sync with your database records.

    To control how your model is converted into text for embedding, override the as_vector instance method. By default, it returns the record as JSON (excluding the embedding field), but you can customize it to join specific attributes into a single string.

    class Recipe < ActiveRecord::Base
      # Enable vector search functionality
      vectorsearch
    
      # Automatically sync with the vector provider on save/destroy
      after_save :upsert_to_vectorsearch
      after_destroy :destroy_from_vectorsearch
    
      # Customize the text representation used for embeddings
      def as_vector
        [
          "Title: #{title}",
          "Description: #{description}"
        ].compact.join("\n")
      end
    end
  7. Generate Pinecone vectorsearch integration

    main

    Use the langchainrb_rails:pinecone Rails generator to automatically set up Pinecone integration for a specific ActiveRecord model.

    This generator performs the following actions:

    1. Creates a config/initializers/langchainrb_rails.rb initializer file.
    2. Injects the vectorsearch class method and an after_save :upsert_to_vectorsearch callback into your specified ActiveRecord model.
    3. Adds the pinecone gem to your Gemfile.
    rails generate langchainrb_rails:pinecone --model=Product --llm=openai
  8. Generate Pgvector integration for an ActiveRecord model

    main

    Use the langchainrb_rails:pgvector generator to automate the setup of vector search integration for a specific Rails model. This generator performs the following actions:

    1. Database Migrations: Creates migrations to enable the vector extension and add a vector column to your model's table.
    2. Dependencies: Adds neighbor and ruby-openai to your Gemfile.
    3. Configuration: Creates config/initializers/langchainrb_rails.rb.
    4. Model Integration: Injects vectorsearch and an after_save :upsert_to_vectorsearch callback into your model file.

    Required flags:

    • --model: The name of the ActiveRecord model you want to enable vector search for.
    • --llm: The LLM provider to use (e.g., openai). This determines the vector dimensions used for the column.
    rails generate langchainrb_rails:pgvector --model=Product --llm=openai
  9. Perform Question and Answering

    main

    Use the .ask method on your model to perform natural language question-answering. The method generates an embedding for the question, finds similar records via cosine similarity, and passes the records (as their #as_vector representation) as context to the LLM to generate a response.

    Returns a String containing the answer.

    Product.ask("list the brands of shoes that are in stock")