1 of 28

πŸ’ŽπŸ”— Langchain.rb

For: NYC.rb

By: Andrei Bondarev

Date: July 12, 2023

2 of 28

About Me

  • 12 years of professional software development
  • Built software for Spree Commerce, Department of HHS, Gannett/USA Today, WeddingWire, FiscalNote, NPR, and others.
  • Running a software dev firm called Source Labs LLC since 2017
  • Architect/Engineering Manager/Fractional CTO on client projects

3 of 28

Background

  • Built a few (keyword) search applications
  • Dove into vector search capabilities
  • Discovered Langchain
  • Started working on Langchain.rb

4 of 28

πŸ’ŽπŸ”— Langchain.rb

  • github.com/andreibondarev/langchainrb
  • 2 months old
  • ⭐️ 400 stars
  • 23 contributors
  • 7k downloads
  • Discord community
  • Langchain.rb: an orchestration layer

5 of 28

Source: β€œEmerging Architectures for LLM Applications” (a16z blog)

6 of 28

Why Ruby?

  • Ruby is not going anywhere. Is Ruby Dead?
  • Pivot to AI Engineering
  • Python == Ruby
  • Pragmatism
  • SOLID / OOP principles
  • Monoliths are back in fashion

7 of 28

Use-cases

  1. Semantic search/Vector search
  2. Q&A over documents
  3. Chat bots
  4. *Agents (experimental)

8 of 28

Tools

  • Prompt management
  • Output parsing
  • Context length validator
  • Data chunking
  • File imports
  • Chat conversation memory
  • Agent Tools
  • ActiveRecord integration

9 of 28

Vector search

Definition: Vector search is a way to find related objects that have similar characteristics using machine learning models that detect semantic relationships between objects in an index.

Word embedding: a vector (array) of float numbers (coordinates) that represents properties of the word.

Vector size == Dimension.

Example: [0.15, 0.67, 0.54, 0.21, … ]

10 of 28

Vector search

Sentence embeddings: similar to word embeddings but converts the semantic meaning to a vector of numbers for the whole sentence

11 of 28

Vector search X LLM matrix

Chroma

Milvus

Pgvector

Pinecone

Qdrant

Weaviate

…

Cohere

βœ…

βœ…

βœ…

βœ…

βœ…

βœ…

βœ…

Google Palm

βœ…

βœ…

βœ…

βœ…

βœ…

βœ…

βœ…

Hugging Face

βœ…

βœ…

βœ…

βœ…

βœ…

βœ…

βœ…

Local Llama

βœ…

βœ…

βœ…

βœ…

βœ…

βœ…

βœ…

OpenAI

βœ…

βœ…

βœ…

βœ…

βœ…

βœ…

βœ…

…

βœ…

βœ…

βœ…

βœ…

βœ…

βœ…

βœ…

12 of 28

Benefits

  • Common DSL/APIs
  • Interoperability
  • Reduced vendor lock-in
  • Optionality
  • Best practices

13 of 28

Vector search

14 of 28

ActiveRecord integration

15 of 28

Vector search

16 of 28

Q&A on top of imported corporate β€œbenefits brochure” (2014)

17 of 28

Q&A over data

18 of 28

Chat bots

  • Interface into a service and/or data
  • Use-cases:
    • Improve UI
    • Increase the engagement
    • Guide user through information retrieval/collection
    • Business process automation

19 of 28

20 of 28

21 of 28

Agents (experimental)

  • Autonomous general purpose LLM-powered programs
  • Tons of emergent research: Chain of Thought (CoT) , Tree of Thought (ToT), Reasoning+Act (ReAct)
  • Can be used to automate workflows or execute multi-step tasks
  • Work better with powerful LLMs
  • Driven by carefully engineered prompts
  • Need lots of error handling and constraints around them (for resiliency)
  • Can use β€œTools” (API wrappers, similar to OpenAI Plugins)

22 of 28

Tools

  • Calculator
  • Wikipedia
  • Google Search
  • Weather
  • Ruby Code Interpreter
  • Database

23 of 28

Prompt used in Langchain::Agent::ReActAgent

24 of 28

Langchain::Agent::ReActAgent

25 of 28

Langchain::Agent::SQLQueryAgent

26 of 28

What’s next?!

  • Collecting feedback
  • Sharpening up the full end-to-end flows
  • Features:
    • Observability
    • Analytics
    • Deeper integration with Rails
    • Expanding into adjacent areas
    • Caching layer

27 of 28

References

28 of 28

Questions?