Timothy Rock

January 31, 2025

Langchain.rb Rails Integration

This latest post provides insight into how you can utilize the Lainchain.rb Rails gem in your Rails application.


Langchain Ruby on Rails Install

1.) If you don't already have a PostgreSQL install that has vector available you can enable using the following command, you will have to be a superuser to perform this step.

    CREATE EXTENSION vector;


1b.) If you do not have a PostgreSQL install that is has the ability to add vector you can add a docker image, if you already have a docker image:

    docker pull ankane/pgvector
    docker run -e POSTGRES_PASSWORD=mypassword docker.io/ankane/pgvector:latest

1c.) You will then need to finish setting up your container, you will need to find the container using the docker ps command which will give you the id, which you will pass to the exec function as a -it parameter and open a console:

    docker ps
    docker exec -it 0a7dcd46be8c /bin/bash

1d.) Login into PostgreSQL from the root console and setup your database you would use the name of your database instead of langchaindemo, superuser is required to setup the vector database extension, you can later turn it off:

    psql -U postgres
    create role langchaindemo_development;
    \password langchaindemo_development;
    alter role langchaindemo_development with createdb;
    alter role langchaindemo_development with login;
    alter role langchaindemo_development with connection limit 100;
    alter role langchaindemo_development with superuser;
    create database langchaindemo_development owner = langchaindemo_development
    create extension vector;
    \q

1e.) Setup your database so that it will allow external connections. You will need to install an editor, and find the location of your PostgreSQL configuration files, and will also need to find out the IP address of your container.

    apt-get install vim
    find / -iname pg_hba.conf
    ifconfig

1f.) Once you know the location of your files we need to edit two files to configure your environment, the first file you will edit is your pg_hba.conf which will tell postgres what kind of connections are allowed and the authentication process.

    vi pb_hba.conf

You can set the IP address that it listens to the IP address of your container and allow for your newly added user to authenticate, in my case the address returned by ifconfig was 172.17.0.3/16 or you can use 0.0.0.0/0:

    host    all        all    0.0.0.0/0    md5

I'm using 0.0.0.0/0 although not as secure works better with dynamic IP addressing that occurs in a docker environment.

1g.) Next you must update your standard postgres configuration so that it knows to listen on port 5432. The standard configuration file will have that line commented out, just find it and remove the comment '#':


    vi postgresql.conf


1f.) You should now be done, now you can restart your container, if you forgot the container id can rerun the docker ps command:

    docker ps
    docker container restart 0a7dcd46be8c

Wow, that was a lot.

2.) Setup your langchaindemo rails application:


    rails new langchaindemo -d postgresql -c tailwind
    db:migrate

3.) Lets scaffold a sample controller up:


  rails generate scaffold post title:string body:text
  rails db:migrate

4.) Lets create some sample data:


    rails c
    Post.create!(title: "Langchain demo", body: "Langchain is all the rage, maybe, not sure.")
    Post.create!(title: "Nanobots", body: "Nanobots, and cartridges, what could be better?")


5.) Lets add the lainchain.rb rails gem:

    bundle add langchainrb_rails
    gem install langchainrb_rails

6.) Lets add langchainrb AI sprinkles to the post model:

    rails generate langchainrb_rails:pgvector --model=Post --llm=openai

7.) This step will create some database migrations that need to be migrated:

    rails db:migrate
    bundle install

8.) If you do not already have an environmental variable setup, you will need to setup OPENAI_API_KEY with your key or you can setup your key directly into your config/initializers/langchainrb_rails.rb file.

```
export OPENAI_API_KEY=YOURKEY
```

9.) Next we setup the vectors for our existing posts, from now on this will be done for us automatically when we update our post records


   Post.embed!


10.) Finally, we can get started with utilizing the tool, try playing around in the console with the following commands:

    Post.ask("Tell me about langchain")
    Post.similarity_search("nanobot")

Later I will provide more information on where langchain is really interesting and powerful, and that's utilizing assistants. Following this guide will ensure that you have langchain installed to the point where we can build on top of it without difficulty. Good luck!