{"id":13877936,"url":"https://github.com/patterns-ai-core/langchainrb_rails","last_synced_at":"2025-05-01T14:33:54.997Z","repository":{"id":203000658,"uuid":"657622522","full_name":"patterns-ai-core/langchainrb_rails","owner":"patterns-ai-core","description":null,"archived":false,"fork":false,"pushed_at":"2024-04-29T23:03:55.000Z","size":125,"stargazers_count":123,"open_issues_count":7,"forks_count":17,"subscribers_count":9,"default_branch":"main","last_synced_at":"2024-04-30T04:42:52.232Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/patterns-ai-core.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.YML","license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["andreibondarev"]}},"created_at":"2023-06-23T13:14:16.000Z","updated_at":"2024-05-01T12:57:38.998Z","dependencies_parsed_at":"2024-01-04T03:24:17.231Z","dependency_job_id":"a3fac280-3f7c-490b-8a17-188d8771bec4","html_url":"https://github.com/patterns-ai-core/langchainrb_rails","commit_stats":null,"previous_names":["andreibondarev/langchainrb_rails","patterns-ai-core/langchainrb_rails"],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patterns-ai-core%2Flangchainrb_rails","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patterns-ai-core%2Flangchainrb_rails/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patterns-ai-core%2Flangchainrb_rails/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patterns-ai-core%2Flangchainrb_rails/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/patterns-ai-core","download_url":"https://codeload.github.com/patterns-ai-core/langchainrb_rails/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224262982,"owners_count":17282456,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-06T08:01:35.346Z","updated_at":"2025-05-01T14:33:54.990Z","avatar_url":"https://github.com/patterns-ai-core.png","language":"Ruby","funding_links":["https://github.com/sponsors/andreibondarev"],"categories":["Ruby","🤖 **AI**"],"sub_categories":[],"readme":"💎🔗 Langchain.rb for Rails\n---\nThe fastest way to sprinkle AI ✨ on top of your Rails app. Add OpenAI-powered question-and-answering in minutes.\n\nAvailable for paid consulting engagements! [Email me](mailto:andrei@sourcelabs.io).\n\n![Tests status](https://github.com/andreibondarev/langchainrb_rails/actions/workflows/ci.yml/badge.svg?branch=main)\n[![Gem Version](https://badge.fury.io/rb/langchainrb_rails.svg)](https://badge.fury.io/rb/langchainrb_rails)\n[![Docs](http://img.shields.io/badge/yard-docs-blue.svg)](http://rubydoc.info/gems/langchainrb_rails)\n[![License](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/andreibondarev/langchainrb_rails/blob/main/LICENSE.txt)\n[![](https://dcbadge.vercel.app/api/server/WDARp7J2n8?compact=true\u0026style=flat)](https://discord.gg/WDARp7J2n8)\n[![X](https://img.shields.io/twitter/url/https/twitter.com/cloudposse.svg?style=social\u0026label=Follow%20%40rushing_andrei)](https://twitter.com/rushing_andrei)\n\n## Dependencies\n\n* Ruby 3.0+\n* Postgres 11+\n\n## Table of Contents\n\n- [Installation](#installation)\n- [Generators](#rails-generators)\n\n## Installation\n\nInstall the gem and add to the application's Gemfile by executing:\n```bash\nbundle add langchainrb_rails\n```\n\nIf bundler is not being used to manage dependencies, install the gem by executing:\n```bash\ngem install langchainrb_rails\n```\n\n## Configuration w/ [Pgvector](https://github.com/pgvector/pgvector) (requires Postgres 11+)\n\n1. Run the Rails generator to add vectorsearch to your ActiveRecord model\n```bash\nrails generate langchainrb_rails:pgvector --model=Product --llm=openai\n```\n\nThis adds required dependencies to your Gemfile, creates the `config/initializers/langchainrb_rails.rb` initializer file, database migrations, and adds the necessary code to the ActiveRecord model to enable vectorsearch.\n\n2. Bundle and migrate\n```bash\nbundle install \u0026\u0026 rails db:migrate\n```\n\n3. Set the env var `OPENAI_API_KEY` to your OpenAI API key: https://platform.openai.com/account/api-keys\n```ruby\nENV[\"OPENAI_API_KEY\"]= \n```\n\n5. Generate embeddings for your model\n```ruby\nProduct.embed!\n```\n\nThis can take a while depending on the number of database records.\n\n## Usage\n\n### Question and Answering\n```ruby\nProduct.ask(\"list the brands of shoes that are in stock\")\n```\n\nReturns a `String` with a natural language answer. The answer is assembled using the following steps:\n\n1. An embedding is generated for the passed in `question` using the selected LLM.\n2. We calculate a [cosine similarity](https://en.wikipedia.org/wiki/Cosine_similarity) to find records that most closely match your question's embedding.\n3. A prompt is created using the question and the above records (their `#as_vector` representation )are added as context.\n4. This prompt is passed to the LLM to generate an answer\n\n### Similarity Search\n```ruby\nProduct.similarity_search(\"t-shirt\")\n```\n\nReturns ActiveRecord relation that most closely matches the `query` using vector search.\n\n## Customization\n\n### Changing the vector representation of a record\n\nBy default, embeddings are generated by calling the following method on your model instance:\n```ruby\nto_json(except: :embedding)\n```\n\nYou can override this by defining an `#as_vector` method in your model:\n```ruby\ndef as_vector\n  { name: name, description: description, category: category.name, ... }.to_json\nend\n```\n\nRe-generate embeddings after modifying this method:\n\n```ruby\nProduct.embed!\n```\n\n## Rails Generators\n\n### Pgvector Generator\n\n```bash\nrails generate langchainrb_rails:pgvector --model=Product --llm=openai\n```\n\n### Pinecone Generator - adds vectorsearch to your ActiveRecord model\n```bash\nrails generate langchainrb_rails:pinecone --model=Product --llm=openai\n```\n\n### Qdrant Generator - adds vectorsearch to your ActiveRecord model\n```bash\nrails generate langchainrb_rails:qdrant --model=Product --llm=openai\n```\n\nAvailable `--llm` options: `cohere`, `google_palm`, `hugging_face`, `llama_cpp`, `ollama`, `openai`, and `replicate`. The selected LLM will be used to generate embeddings and completions.\n\nThe `--model` option is used to specify which ActiveRecord model vectorsearch capabilities will be added to.\n\nPinecone Generator does the following:\n1. Creates the `config/initializers/langchainrb_rails.rb` initializer file\n2. Adds necessary code to the ActiveRecord model to enable vectorsearch\n3. Adds `pinecone` gem to the Gemfile\n\n### Prompt Generator - adds prompt templating capabilities to your ActiveRecord model\n```bash\nrails generate langchainrb_rails:prompt\n```\n\nThis generator adds the following files to your Rails project:\n1. An ActiveRecord `Prompt` model at `app/models/prompt.rb`\n2. A rails migration to create the `prompts` table\n\nYou can then use the `Prompt` model to create and manage prompts for your model.\n\nExample usage:\n```ruby\nprompt = Prompt.create!(template: \"Tell me a {adjective} joke about {subject}.\")\nprompt.render(adjective: \"funny\", subject: \"elephants\")\n# =\u003e \"Tell me a funny joke about elephants.\"\n```\n\n### Assistant Generator - adds Langchain::Assistant capabilities to your Rails app\n\nThis generator adds Langchain::Assistant-related ActiveRecord models, migrations, controllers, views and route to your Rails app. You can start creating assistants and chatting with them in immediately.\n\n```bash\nrails generate langchainrb_rails:assistant --llm=openai\n```\n\nAvailable `--llm` options: `anthropic`, `cohere`, `google_palm`, `google_gemini`, `google_vertex_ai`, `hugging_face`, `llama_cpp`, `mistral_ai`, `ollama`, `openai`, and `replicate`. The selected LLM will be used to generate completions.\n\nTo remove the generated files, run:\n\n```bash\nrails destroy langchainrb_rails:assistant\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpatterns-ai-core%2Flangchainrb_rails","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpatterns-ai-core%2Flangchainrb_rails","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpatterns-ai-core%2Flangchainrb_rails/lists"}