Langchain, Langchain.rb and RAG-based querying

Daniel Carvalho
Runtime Revolution
Published in
10 min readMar 27, 2024

--

LangChain is an open-source framework designed to streamline the development of applications that leverage large language models (LLMs). Think of it as a toolbox for programmers to build LLMs into useful applications.

Here are some key points about LangChain:

  • Focus on Applications: LangChain goes beyond just using LLMs for generating text. It provides tools to integrate LLMs with other data sources and functionalities, allowing developers to create full-fledged applications.
  • Modular Design: LangChain uses a modular approach where different components can be chained together to achieve a specific task. This allows for flexibility and customization.
  • External Data Access: LangChain allows LLMs to access and process information from external data sources, enhancing the accuracy and relevance of the applications.

Overall, LangChain aims to make it easier for developers to harness the power of LLMs and create intelligent applications.

Adding Langchain.rb to your project (using pgvector)

pgvector is an extension for PostgreSQL that allows you to store and perform efficient searches on vector data. Adding pgvector to your system involves two main steps:

  1. Installation: There are two main ways to install pgvector:
  • From source: This method involves downloading the source code from the pgvector project and compiling it yourself. This can be a bit more complex, but it gives you more control over the installation process. You can find instructions for installing pgvector from source on the pgvector GitHub repository.
  • Using a package manager: If you’re using a Linux distribution like Ubuntu or Debian, you may be able to install pgvector using your system’s package manager. This is the easiest way to install pgvector, as it will take care of all the dependencies for you.
  1. Enabling the extension: Once pgvector is installed, you need to enable it in your PostgreSQL database. You can do this by running the following command in your PostgreSQL client:
CREATE EXTENSION vector;

The specific installation steps will depend on your operating system and PostgreSQL setup.

https://github.com/pgvector/pgvector

Let’s start it …

gem 'langchainrb-rails'
  • Run bundle install to install the gem.

Create an initializer file:

  • In your Rails project’s config directory, create a new Ruby file named langchain.rb.
  • require 'langchainrb' # If using langchainrb-rails: # require 'langchainrb-rails'
# frozen_string_literal: true

LangchainrbRails.configure do |config|
# Initializes the vectorsearch configuration for Langchainrb Rails.
# It creates a new instance of Langchain::Vectorsearch::Pgvector and assigns it to the config.vectorsearch variable.
# The Langchain::Vectorsearch::Pgvector instance is initialized with an instance of Langchain::LLM::OpenAI,
# which is created with the provided OPENAI_API_KEY environment variable.
config.vectorsearch = Langchain::Vectorsearch::Pgvector.new(
llm: Langchain::LLM::OpenAI.new(api_key: ENV["OPENAI_KEY"],
default_options: {
temperature: 0.0,
chat_completion_model_name: 'gpt-4',
embeddings_model_name: "text-embedding-3-small"
})
)
endr

Database Embedding

An embedding, in machine learning, is a way to turn complex data, like text or images, into a simpler form that a computer can understand. It’s like creating a special code that captures the important aspects of the data.

Here’s a breakdown of how embeddings work:

  • Imagine a high-dimensional space: Think of a bunch of points scattered around in a space with many dimensions. This could represent all the unique words in a language.
  • Compressing the data: An embedding technique takes each word and translates it into a single point in a lower-dimensional space, like a 3D space.
  • Capturing relationships: The key is that this translation isn’t random. Words with similar meanings are placed close together in this new space, while words with different meanings are placed further apart. So, the embedding captures the relationships between the words.

By using embeddings, machine learning models can better understand the data they’re working with

Adding a embedding to our products

class AddVectorColumnToProducts < ActiveRecord::Migration[7.0]
def change
add_column :products, :embedding, :vector,
limit: LangchainrbRails
.config
.vectorsearch
.llm
.default_dimension
end
end

RAG-based querying (using langchain)

RAG-based querying, also known as Retrieval-Augmented Generation querying, isn’t exactly a traditional search method. It’s a technique used for tasks like question answering and content generation with large language models (LLMs).

Here’s how it works:

  1. Retrieval: Imagine a librarian. RAG first acts like a librarian, sifting through a vast amount of text data (like books in a library). It uses retrieval models, often semantic search, to find information relevant to your query based on meaning, not just keywords.
  2. Generation: Then, like a well-read scholar, RAG leverages this retrieved information to inform the LLM. The LLM uses this context to craft a response that aligns with your question and the retrieved data. This ensures the answer is relevant and comprehensive.

RAG essentially combines the strengths of retrieval models and LLMs. Retrieval models find relevant info, and LLMs use that info to generate informative answers. This is beneficial because:

  • Improved Relevance: RAG focuses on understanding the intent behind your question, leading to more on-point responses.
  • Context-aware Responses: By incorporating retrieved data, RAG ensures the LLM’s response is factually accurate and well-grounded.

Overall, RAG-based querying isn’t a standalone search method, but a powerful technique for enhancing question answering and content generation with LLMs.

RAG-based querying with Langchain.rb

RAG- Retriever-Augmented Generation for Querying with Langchain.rb

RAG-based querying leverages a Retriever-Augmented Generation approach to answer your questions. Langchain.rb provides tools to build such systems, but it currently only supports OpenAI as the Large Language Model (LLM). Here’s an example to illustrate the concept:

Retriever (InMemory example):

This is a simplified in-memory retriever. In practice, you’d likely use a database or external service.

retriever = ->(query) { memory.find(query) }

RAG Function:

This function takes a question and retrieves relevant passages from the memory using the retriever. It then feeds the question and retrieved passages to the LLM for answer generation.

rag_query = ->(question) do
passages = retriever.call(question) || []
prompt = "Question: #{question}\n\nPassages:\n#{passages.join("\n")}\n\nAnswer:"
OpenAI.completion(engine: 'text-davinci-003', prompt: prompt).choices.first.text.strip
end
question = "What is the capital of France?"
# Add some example data to the memory (replace with your data source)
memory.add('What is the capital of France?', 'Paris is the capital of France.')
answer = rag_query.call(question)
puts answer # Output: Paris

This is a basic example. Real-world RAG systems involve more complex retrieval strategies and LLM prompts.

For further exploration, refer to the Langchain.rb documentation https://github.com/langchain-ai/langchain.

Usage Example with our previous example of Products:

Product.create!(
ProductID: row['ProductID'],
name: row['ProductName'],
brand: row['ProductBrand'],
gender: row['Gender'],
price: row['Price'].to_f,
NumImages: row['NumImages'],
description: row['Description'],
PrimaryColor: row['PrimaryColor']
)
# Persisted Data
[
{
"id": 1,
"ProductID": 10017413,
"name": "DKNY Unisex Black & Grey Printed Medium Trolley Bag",
"brand": "DKNY",
"gender": "Unisex",
"price": 11745.0,
"NumImages": 7,
"description": "Black and grey printed medium trolley bag, secured with a TSA lockOne handle on the top and one on the side, has a trolley with a retractable handle on the top and four corner mounted inline skate wheelsOne main zip compartment, zip lining, two compression straps with click clasps, one zip compartment on the flap with three zip pocketsWarranty: 5 yearsWarranty provided by Brand Owner / Manufacturer",
"PrimaryColor": " Black",
"created_at": "2024-03-19 11:47:22.104885",
"updated_at": "2024-03-19 11:47:22.104885",
"embedding": "[-0.004040977,0.01586862,-0.026935568,0.016895713,-0.050712816,-0.023032606,-0.030581756,0.04727205,-0.003687913,-0.035075296,-0.016921392,-0.013223848,-0.041802768,-0.018513389,0.013056946,0.024085378,-0.040544577,0.02113248,0.0010150588,0.040570255,0.014533395,0.044575926,0.022377834,-0.024804346,0.00332843,-0.016574746,0.002211464,-0.057414614,0.051611528,0.0038869127,-0.022891382,-0.008634018,0.013133978,-0.020683127,0.03599968,0.0024602136,-0.01604836,0.0130312685,0.0121903345,0.033842783,-0.018770162,-0.033354912,0.016086876,-0.034073878,0.0040762834,0.033329234,-0.006733892,-0.034690134,-0.007279536,0.039312065,0.04999385,0.00793431,0.005555942,-0.00042126945,-0.024406346,0.0051001688,-0.026678793,6.293966e-05,0.019322226,0.017627519,-0.006040603,-0.07194801,0.029528983,0.03687271,0.013429267,-0.060136415,0.000658384,0.021479126,-0.056028035,-0.029272208,0.014276621,0.022249447,-0.014161073,-0.047015276,0.03307246,0.036436196,-0.029734401,-0.03512665,0.053563006,0.0093080485,-0.030350659,0.0060919574,-0.06655576,0.010335144,-0.0440367,0.002710568,-0.041700058,-0.028296469,-0.05813358,-0.016035521,0.0044774925,0.014738814,0.042701475,-0.0013592961,0.012671785,0.00036851046,-0.009661113,0.016074037,-0.053511653,0.044961087,-0.010104047,0.04912082,0.04336909,0.064809695,-0.009808757,-0.04745179,0.0029833901,-0.009622597,0.0032931236,-0.019309387,-0.084016375,0.011991335,0.023533314,0.018705968,-0.0024056493,-0.0075876648,0.03209672,0.05138043,-0.00043290452,-0.037360583,-0.049300563,-0.020888545,0.0062075057,-0.061214864,-0.04747747,0.015085459,0.020837191,-0.01654907,-0.064963765,0.0037232195,0.0030299304,-0.017345067,-0.033663042,-0.014327976,0.018474871,-0.0007843636,0.008146147,0.01733223,-0.016895713,-0.01869313,-0.017755905,-0.043805603,0.053768426,0.0031679464,-0.016523391,0.009051275,-0.025317892,0.09069249,0.0075427294,0.020927062,-0.018256614,0.019463452,0.03592265,-0.03923503,0.019617515,-0.07549149,0.016163908,0.06825046,-0.0040152995,0.01262043,0.01027737,0.01986145,-0.030889884,0.023713056,0.0074913744,0.040364835,0.0015245943,-0.06953433,-0.054641455,-0.015111136,-0.0028999387,-0.014315138,-0.010502047,0.02183861,0.0042046704,-0.010206757,-0.0051804106,-0.009173242,-0.028553242,-0.009821597,-0.016626101,0.008338728,0.0004052211,-0.021941317,-0.055257715,0.017832939,0.010399337,-0.02574157,0.008409341,-0.013557655,-0.006531683,0.021299383,-0.01839784,-0.01223527,-0.031198012,-0.0044132993,-0.028681628,-0.00968679,0.029734401,0.013557655,0.045628697,-0.0036140906,-0.0031005433,-0.012017012,-0.019951321,-0.0505074,-0.0152266845,-0.015008426,0.032712977,-0.014725976,0.009211759,-0.02955466,-0.0283735,0.00792147,0.0037617355,0.010694628,-0.009738144,0.018718807,0.003797042,0.026961245,-0.029195176,0.0084157605,0.00694573,-0.027731566,-0.020952739,0.019219516,0.036359165,0.00063471263,-0.030581756,-0.049480304,0.0011434456,0.05127772,-0.008114051,-0.031968333,0.039260708,-0.0010519701,0.02055474,-0.03533207,0.0017540858,-0.0094621135,-0.020131063,0.003944687,-0.0070484397,0.033021107,0.04385696,-0.025985505,-0.021581834,-0.025125312,-0.012967075,0.016510554,-0.02847621,0.04668147,0.016330812,0.016459199,0.039132323,0.023661703,0.004394041,0.027628858,-0.011728141,-0.061677057,0.03181427,-0.017730229,-0.024521895,0.00068767223,-0.048453208,0.01556049,0.022082543,0.0029512935,-0.0041436865,-0.002055795,-0.00958408,-0.013275203,-0.011176078,0.029349241,-0.015290878,0.011471367,0.005703587,0.016022682,-0.019155324,-0.025985505,-0.024085378,0.00062187394,0.028039696,-0.021350738,0.021646028,-0.0116767865,0.005472491,0.025446279,-0.036924068,0.011227433,0.009629016,-0.014456362,0.00052839227,-0.02330222,-0.01801268,-0.034767166,-0.015111136,0.029606014,-0.005344104,-0.03720652,0.03415091,0.028116727,0.040287804,-0.015046942,-0.05037901,0.034587424,0.0610608,-0.0010262927,0.010912885,0.026730148,-0.035357747,0.045243535,-0.010399337,-0.05705513,-0.015881456,-0.029041113,0.018860033,0.027526148,0.026293634,-0.011317303,0.02377725,0.006127264,0.074926585,-0.034921233,0.001217268,-0.020131063,0.023533314,0.0027522938,-0.008146147,-0.03941477,-0.021363577,-0.019989837,-0.022031188,-0.042008188,0.034484718,-0.042419024,0.053511653,0.009680371,-0.04609089,0.025792925,0.005687539,-0.07328323,-0.008820179,0.018911388,0.020118224,-0.011221013,0.02426512,0.020054031,-0.006451441,-0.013775912,-0.008486372,0.033714395,0.016202424,0.009956403,-0.04354883,-0.032712977,-0.016728811,0.015290878,-0.0049717817,-0.00091234926,0.023315057,0.067736916,0.029374918,0.008338728,0.020991255,0.0334833,-0.028707307,0.0014323161,-0.017832939,-0.0009227807,0.047195017,0.023045445,-0.011952818,0.016844358,-0.045500312,-0.03150614,-0.005986038,0.06054725,0.017576164,-0.019604677,0.060906738,-0.025908472,-0.023815766,0.006050232,0.03258459,-0.0010327119,-0.01653623,0.027731566,0.02339209,-0.01987429,-0.026858535,-0.04609089,-0.013583332,-0.039363418,-0.05556584,-0.0012421431,0.043035284,0.009064114,-0.020824352,-0.04442186,-0.012581915,-0.03687271,-0.012954236,0.012748817,0.0049717817,0.016574746,0.020965578,0.013365074,-0.0061304737,-0.013917138,-0.009109049,-0.062190603,-0.0037360583,-0.020490546,-0.01017466,0.043882634,0.010219595,0.05782545,0.025305055,0.016934229,-0.012267366,-0.008627598,0.0040281382,0.010110467,-0.02827079,0.05325488,-0.010238854,-0.0051900395,-0.041623026,-0.048247788,0.015367909,0.010694628,0.06388531,0.0034471878,0.012614011,-0.019206677,-0.0047214273,-0.009757403,-0.0052478136,-0.038105227,-0.0011803568,-0.023456283,-0.018218098,-0.004092332,0.00034403673,0.042958252,0.0082616955,0.028758662,0.01575307,-0.041545995,0.0003424319,0.06116351,-0.07523471,0.0074143424,-0.07318052,0.054179262,-0.036410518,-0.043086637,0.020721642,-0.023109639,0.03797684,0.04901811,0.04362586,0.011503465,0.0049782014,0.040955417,-0.04216225,-0.00043370694,0.06183112,-0.038207937,-0.038926903,-0.048273467,0.02191564,0.019360742,0.017499132,0.03445904,-0.006676118,-0.012472785,-0.01860326,-0.0038227194,-0.009423597,0.016767327,-0.07086956,0.002885495,-0.037257873,0.068764016,-0.003581994,0.047888305,-0.0060052965,-0.004461444,-0.02524086,0.00087463565,0.0008569824,-0.01389146,-0.07225613,0.020952739,0.0025982296,-0.038130905,-0.024200927,-0.070510074,0.016228102,0.008383663,0.046912566,-0.036179423,0.025767246,0.016793003,0.025459118,-0.033123814,0.013698881,-0.008794501,0.012934978,0.0112466905,0.0010760425,0.006900795,-0.018770162,-0.021093965,0.009346565,-0.00079920836,-0.042213608,-0.00959692,-0.029734401,-0.041956834,-0.024483379,-0.032815687,-0.004297751,-0.030196594,-0.0043298476,-0.06578544,-0.029528983,0.014238105,0.0045769922,0.0019707386,-0.016908552,0.022146737,0.027911307,0.0046540243,0.012697463,-0.029914143,0.0035755746,-0.031762913,0.050250623,-0.033534653,0.0021729479,-0.04092974,-0.044858377,-0.0044935406,0.0112980455,-0.009365823,0.0028614227,0.037617356,0.010033435,0.022223769,0.03415091,-0.007362988,0.004239977,0.020092547,0.023430606,-0.00022949155,0.02965737,-0.0348442,-0.014276621,0.0252537,0.036847036,-0.008916469,0.050558753,-0.00073862576,0.017409261,-0.008383663,-0.012479205,-0.018834356,-0.020888545,0.0152652,0.0055848295,-0.05078985,0.027063955,0.03951748,0.020580417,-0.036333486,0.018705968,0.0011025224,-0.0128258495,0.010951401,0.020195257,-0.006175409,-0.07487523,0.006136893,0.06917485,-0.014417847,-0.021748738,0.027551824,0.033688717,-0.019733064,0.0039414773,0.020400675,-0.005177201,0.030864207,-0.02534357,-0.005469281,-0.015316555,-7.2317926e-05,0.0020076497,-0.0505074,-0.020233773,-0.019103969,-0.0038676548,0.009166824,0.037899807,0.013596171,-0.031352077,0.020734482,0.0055302647,0.015008426,0.007895793,-0.012511302,-0.0034632361,0.04629631,-0.0055431034,-0.002426512,-0.008094793,0.019103969,-0.03289272,-0.033406265,0.028424855,-0.034792844,0.018564744,0.030941239,0.024919894,-0.069226205,-0.033560332,0.0009011154,0.010104047,-0.036128066,0.055822615,-0.0063872472,-0.0026624228,0.02114532,-0.0022403512,0.0012694253,-0.004256025,-0.016895713,0.011304465,0.028296469,0.052304816,-0.0062010866,0.015496297,0.0064129247,0.01458475,-0.008852275,-0.03376575,0.012209592,0.03171156,-0.0348442,0.015778748,-0.019835774,0.0015237918,0.01898842,-0.012248108,0.023135316,0.008556985,0.013788751,-0.01644636,0.078726836,-0.0066183438,0.001585578,0.022377834,0.014892878,0.008640437,-0.0023478752,-0.0022050447,0.017524809,0.037694387,-0.012845107,-0.016805843,0.017165326,-0.0062492313,0.019604677,-0.019155324,0.0011466553,-0.03032498,0.01556049,0.02162035,0.0005123439,-0.011060529,-0.0004224731,0.01565036,-0.019977,-0.0021312223,-0.008935727,-0.015342232,0.011901464,-0.006470699,0.029939821,-0.0067082145,0.017036939,-0.013827267,-0.012768075,0.011574077,-0.009648274,-0.0093594035,-0.018808678,-0.011997754,0.019578999,-0.05412791,0.011811593,-0.026832858,0.011034853,0.015393587,-0.0017139649,0.047220696,-0.037514646,-0.006351941,-0.013146817,-0.033354912,0.011503465,0.013198172,-0.0291695,0.017191004,-0.002739455,0.013621848,-0.0018519808,0.015573328,0.041905478,-0.0034696555,-0.013775912,0.010925723,-0.023238026,0.029041113,-0.009186082,0.044447538,-0.044396184,-0.017768744,0.0035884134,0.032918397,0.02006687,-0.027295051,0.008017761,-0.02788563,-0.023623185,-0.017268036,-0.02046487,-0.006400086,-0.035974003,-0.031043949,0.015329394,0.01653623,0.014109719,0.047117986,-0.013557655,-0.07169124,0.015830103,0.0010503652,0.009872952,-0.02231364,0.012068367,-0.030068208,0.021748738,0.033560332,0.03689839,-0.02046487,0.06290957,0.007311633,0.011073369,-0.027834276,0.0046829116,0.007369407,-0.021992672,-0.025202345,0.04365154,-0.02955466,0.018616097,0.005411507,0.018783001,0.01722952,-0.020580417,-0.0024891007,-0.0059635704,0.0037938324,0.021607513,0.009128307,-0.009866532,-0.0028630274,0.010938562,-0.0017942067,0.016253779,0.022005511,-0.03985129,-0.012588333,0.0024120684,0.023225186,0.022454865,-0.0021793672,-0.0101489825,-0.01046995,0.010585498,-0.009725306,0.0056747003,-0.034895554,-0.08560837,-0.009352984,0.020028353,-0.022801511,-0.0074656974,-0.0497114,0.019566162,0.019591838,-0.0016176747,-0.01927087,-0.022878543,-0.015085459,-0.04072432,-0.024624603,0.020734482,0.019412097,0.03307246,0.0071768267,-0.009757403,-0.010065531,-0.018770162,-0.0026672375,-0.025266537,0.005427555,-0.0033252202,-0.0037745743,0.012119722,0.045294892,0.03299543,-0.023828605,0.025330732,-0.0011972077,0.016484875,0.028732983,-0.01458475,0.008550567,-0.031865623,-0.018808678,0.028989758,-0.025767246,0.01937358,-0.012382914,-0.0278086,0.01595849,-0.015817264,-0.030247949,-0.017845776,-0.0060245544,-0.015188168,-0.01018108,0.052150752,0.014032686,0.036256455,-0.04570573,0.027911307,-0.01223527,-0.010944982,-0.05001953,-0.011901464,-0.00606628,0.020734482,-0.024932733,-0.025857117,-0.013852945,0.0041533154,0.004098751,-0.060906738,-0.0013649131,0.040262125,-0.0017187793,0.015817264,0.020593256,0.008563405,0.02584428,-0.0066183438,0.028810017,-0.0037135906,0.031095302,0.0016120578,0.0020814722,0.016613262,0.018937064,0.017537648,0.032071043,0.007818761,0.006281328,-0.002681681,-0.009038436,0.00675315,-0.016395004,0.036821358,0.017101133,-0.00430417,0.01898842,-0.012325141,0.012748817,0.0022467703,0.01586862,0.026113892,-0.011066949,-0.010187499,-0.01722952,-0.027346406,0.014815846,-0.039876964,-0.012614011,0.0039286385,-0.0060117156,0.042932574,0.0035210103,0.012119722,0.022467704,0.048042372,0.025176667,-0.011875787,0.040185094,0.019335065,0.018230937,-0.004952524,-0.025587505,0.028424855,0.02631931,-8.0693164e-05,0.0085184695,-0.0101489825,0.0020204885,-0.020618932,-0.021106804,-0.01516249,0.014173912,-0.0046861214,-0.0104057565,-0.010688208,-0.03463878,0.007786664,-0.0205419,0.04080135,0.010193918,0.01937358,0.005825555,-0.024650281,0.011201755,-0.029939821,0.027577503,0.01046995,0.02466312,-0.0028132775,0.0046347664,0.005616926,-0.0010543773,-0.010104047,0.025818601,0.014854362,0.0518683,0.011972076,0.016202424,-0.023931315,0.017460616,-0.016215263,0.051816944,-0.0132880425,-0.0016240941,0.016882874,-0.038028196,0.0059924577,-0.019656032,-0.034869876,-0.000963704,0.0044004605,-0.008390083,-0.004843395,0.04113516,-0.039799932,-0.0049011693,0.03443336,0.0278086,0.0067146337,-0.014931395,0.013172494,0.019514807,0.010142564,0.026601762,-0.014199589,-0.02102977,-0.00024172843,0.054949585,-0.026396343,-0.032071043,0.0013938,0.033046782,0.018256614,0.0033348491,0.029580338,-0.02631931,0.04342044,0.024637442,-0.0012654132,0.0033508977,0.059366092,0.011689626,0.069431625,-0.024226604,0.0044165086,0.0016866827,-0.012434269,0.01780726,0.051329076,-0.007581245,0.0012798568,0.025548989,0.021979835,0.011715303,0.0042528156,-0.032379173,0.009853693,0.021055449,-0.032327816,0.005209298,-0.043600183,0.0070548593,-0.018179582,0.0032080673,0.036359165,0.041725736,-0.0014949047,0.018115388,0.015509135,0.01792281,0.0060919574,0.023058284,-0.00239923,0.03533207,-0.0026961246,0.03592265,-0.0076582776,-0.02260893,0.013942815,-0.014161073,0.022146737,-0.007093375,-0.03422794,0.016600424,0.012318721,-0.010996336,-0.002367133,-0.029220855,0.0054436037,-0.0029240113,-0.02201835,0.036410518,0.037360583,-0.006351941,-0.03168588,-0.0091026295,0.02770589,-0.038413353,0.0076582776,0.0045545246,-0.018551905,-0.0031358495,-0.0046893307,-0.027526148,0.038824193,0.016035521,-0.03941477,0.0034086716,0.026447697,-0.006204296,-0.012761656,0.002779576,0.009930725,0.00040481988,0.0013584937,0.005068072,0.016664617,0.021106804,-0.008088374,0.019026935,0.029965498,-0.016780166,-0.031968333,0.011362239,-0.003472865,-0.0062203445,0.061420284,-0.015971327,-0.00518683,-0.027962662,0.035974003,-0.012748817,-0.007780245,0.020837191,-0.014533395,0.010823014,0.0029625273,0.012941398,0.03602536,0.009019178,0.007786664,-0.004801669,0.037155163,0.025330732,-0.0060791187,-0.00948779,0.021453448,-0.004939685,-0.01594565,-0.009096211,-0.037026778,0.01253056,-0.010347983,-0.01674165,0.017666034,-0.005851232,0.012671785,0.01771739,0.010688208,0.011336561,0.022185253,0.004092332,0.010546982,0.0075619873,0.011497045,-0.026576085,0.03181427,0.009917887,0.033123814,-0.0063166344,-0.0069842464,-0.008435018,-0.013583332,-0.004243186,0.024149572,0.010636853,-0.012151819,-0.041931156,-0.027834276,0.005401878,0.025022604,-0.035768583,-0.005145104,-0.017152488,0.0009075348,0.009423597,-0.027628858,0.015791586,0.015303716,0.039157998,0.000739027,-0.0022082543,0.07692942,0.0063744085,0.027834276,0.0071126334,0.033817105,-0.021325061,-0.007228181,-0.023854282,-0.016215263,0.03181427,-0.011240272,0.010977078,0.0024329314,0.0009773452,-0.0025019394,0.008306631,-0.02778292,-0.021119641,0.028168082,-0.010887207,0.018924227,-0.0040538157,0.009500629,-0.019989837,0.009892209,-0.0084157605,0.008165406,-0.010450692,0.03063311,0.0113494005,0.007651858,-0.013365074,0.051123656,0.00067523477,0.0636799,0.00479204,-0.022095382,-0.016844358,0.020914223,-0.018153906,0.0019033356,-0.027140986,0.023032606,0.019052614,0.026935568,0.014931395,-0.018166743,-0.029092466,0.04334341,-0.063936666,0.010386499,0.020927062,-0.051046625,-0.031377755,-0.013711719,0.024021186,0.01986145,0.0012710302,-0.051893976,-0.016215263,0.014623266,0.025664538,-0.032122396,0.012883623,0.010136144,-0.038978256,0.018924227,0.003434349,-0.0067082145,0.025985505,0.007221762,-0.00045577344,-0.013686041,-0.049249206,0.009744564,0.031377755,-0.004471073,0.010912885,-0.028168082,0.026178084,-0.013050526,0.02046487,0.022454865,0.020580417,-0.018372163,-0.04362586,-0.035691552,-0.02123519,-0.013942815,0.030170918,0.0019996257,-0.028938403,-0.0057356837,0.009468532,0.033688717,-0.0018551904,-0.033714395,-0.004471073,-0.00860834,0.045628697,0.0051643625,0.03122369,-0.044652957,-0.006939311,0.0060759094,0.013827267,-0.009282372,-0.0047824113,-0.012260947,-0.011355819,-0.018115388,-0.0018230937,-0.017473454,-0.021389255,-0.008634018,-0.018423518,0.004930056,0.0021376417,-0.008152567,-0.020965578,0.002318988,-0.027320728,-0.017036939,0.033046782,-0.012376496,-0.01184369,0.00528312,0.023353573,-0.013005591,0.00015827695,0.02426512,0.017383583,0.010084789,-0.010091209,0.060804028,-0.0019322225,-0.00724102,-0.011464949,0.026447697,0.01644636,0.018051196,0.009391501,-0.024483379,-0.04372857,0.0034985426,0.049172174,-0.0010327119,0.014559072,0.01233156,0.020182418,0.039774254,0.024971249,0.003140664,-0.03140343,0.027526148,0.009186082,-0.0007639019,-0.008280953,0.001664215,0.009949983,-0.038926903,0.008178244,-0.06280686,-0.0009861718,0.028732983,-0.013647526,0.00054123095,-0.01643352,0.023931315,0.013724557,-0.021440608,0.041828446,0.004609089,0.046219278,0.011959238,0.0027153825,0.011240272,0.02114532,-0.024213767,0.0035787844,-0.029914143,-0.018872872,-0.044267796,-0.022647446,0.010778079,0.0025115684,-0.0060694898,-0.032225106,0.01789713,-0.011715303,0.03112098,-0.013172494,-0.0043202187,0.006830182,-0.009699629,-0.008268115,0.0278086,0.009057694,-0.01909113,0.018410679,-0.00025938163,0.017460616,0.01448204,-0.0008020168,0.02976008,-0.054641455,-0.0084671145,-0.0036654454,-0.007844439,0.0020975207,0.028450534,0.024200927,0.015740233,0.015817264,-0.008852275,0.007594084,0.022544736,0.02965737,0.00090352265,-0.05654158,-0.00163934,-0.046142247,0.007882955,-0.0034985426,-0.044216443,0.01203627,0.012896462,0.040082384,0.023636024,-0.01654907,-0.014340814,-0.010078371,0.039029613,-0.019694548,-0.013108301,-0.022095382,-0.04493541,0.020323643,0.0121389795,-0.04113516,-0.030812852,-0.014610427,-0.018526226,-0.0032417688,-0.0005091342,0.0069714077,-0.0061304737,-0.03032498,0.0014427475,0.03199401,-0.0270126,0.019938482,-0.018616097,0.014443524,-0.00802418,0.03153182,-0.006695376,-0.002535641,-0.027243696,-0.052689977,-0.031095302,-0.0050648623,0.005831974,-0.0025484795,-0.005767781,0.019001259,-0.0051033786,-0.028322145,-0.02006687,-0.004936476,-0.027372083,0.016587585,0.008839437,-0.0010254902,-0.012864365,-0.0017605051,-0.038618773,-0.01839784,0.0067274724,0.027962662,-0.029143821,-0.0016802633,-0.025677375,-0.0064129247,0.009539145,0.026653117,-0.043317735,0.022364995,-0.008505631,-0.005068072,-0.0048594433,-0.0071126334,-0.0116767865,-0.01233156,0.04786263,0.0077545675,0.04342044,-0.002927221,-0.0015615055,0.0007145532,-0.029965498,-0.003864445,0.036179423,-0.020246612,1.8806672e-08,-0.010399337,0.020105386,0.0119335605,-0.02690989,-0.0017059407,-0.016215263,0.0127744945,0.005077701,-0.046707146,0.0129799135,-0.006486747,-0.0010920909,-0.005892958,0.01164469,0.010598337,0.005588039,-0.0065413117,0.0010006153,-0.03474149,-0.024675958,-0.0004148501,-0.048992433,0.02183861,0.032815687,0.0305304,0.03843903,-0.015676038,-0.0072859554,-0.039568838,0.017884292,0.0061625703,0.013724557,0.023725895,0.008178244,-0.01252414,-0.0044100895,0.015611845,0.025651699,0.017409261,-0.018821517,0.045089472,0.021106804,-0.021517642,-0.0006246824,0.035383426,-0.0014884854,0.011265948,-0.057568677,0.030119563,0.040852707,0.012890043,0.03160885,0.017653197,-0.014007009,-0.020580417,0.004949314,0.0035498973,-0.00063471263,-0.0013239897,0.013159655,0.009949983,0.000499104,0.011060529,-0.007850857,0.013480622,-0.020618932,-0.00010451495,-0.003610881,-0.0007101399,0.0084671145,-0.042752832,-0.013531977,-0.002094311,-0.006547731,0.026884213,0.01634365,-0.039979674,-0.008293793,0.011586916,0.018192422,0.0070420206,0.024380669,-0.008043438,-0.023956992]"
}
]
3.2.0 :001 > Product.ask('Tell me about DKNY Unisex Black & Grey Printe')
=> "The DKNY Unisex Black & Grey Printed Medium Trolley Bag is a black
and grey printed medium trolley bag that is secured with a TSA lock.
It has one handle on the top and one on the side, and a trolley
with a retractable handle on the top and four corner mounted inline
skate wheels. The bag has one main zip compartment, zip lining, two
compression straps with click clasps, one zip compartment on the
flap with three zip pockets. The bag comes with a 5-year warranty
provided by the brand owner or manufacturer. It is priced at 11745.0."

Conclusion

Here’s a concise breakdown of the pros and cons of RAG-based querying:

Pros:

  • High Accuracy: RAG conditioning allows the model to leverage external knowledge sources, potentially improving the accuracy of its responses to complex or challenging queries.
  • Improved Reasoning: By incorporating external knowledge, RAG can reason over information and potentially provide more comprehensive and informative answers.
  • Flexibility: The RAG approach can be adapted to incorporate different types of knowledge sources, making it adaptable to various domains and tasks.

Cons:

  • Increased Complexity: RAG systems are more complex than traditional retrieval-based methods, requiring additional training and computational resources.
  • Potential for Errors: The accuracy of RAG outputs depends on the quality and relevance of the external knowledge source. Errors or biases in the knowledge source can be reflected in the model’s responses.
  • Interpretability Challenges: It can be challenging to understand how the model reasons using external knowledge, making it difficult to debug or assess the trustworthiness of its answers.

Overall, RAG-based querying offers promising advancements in accuracy and reasoning capabilities. However, it’s essential to consider the increased complexity and potential biases introduced by external knowledge sources.

(This article was partially written with the help of Google’s Gemini)

--

--