Saturday, May 10, 2025
News PouroverAI
Visit PourOver.AI
No Result
View All Result
  • Home
  • AI Tech
  • Business
  • Blockchain
  • Data Science & ML
  • Cloud & Programming
  • Automation
  • Front-Tech
  • Marketing
  • Home
  • AI Tech
  • Business
  • Blockchain
  • Data Science & ML
  • Cloud & Programming
  • Automation
  • Front-Tech
  • Marketing
News PouroverAI
No Result
View All Result

Building a RAG chain using LangChain Expression Language (LCEL) | by Roshan Santhosh | Apr, 2024

April 13, 2024
in AI Technology
Reading Time: 5 mins read
0 0
A A
0
Share on FacebookShare on Twitter


QA RAG with Self Evaluation II

For this variation, a change is made to the evaluation procedure. Along with the question-answer pair, the retrieved context is also passed to the evaluator LLM.

To achieve this, an additional itemgetter function is added in the second RunnableParallel to collect the context string and pass it to the new qa_eval_prompt_with_context prompt template.

rag_chain = ( RunnableParallel(context = retriever | format_docs, question = RunnablePassthrough() ) |RunnableParallel(answer= qa_prompt | llm | retrieve_answer, question = itemgetter(“question”), context = itemgetter(“context”) ) |qa_eval_prompt_with_context | llm_selfeval |json_parser)

Implementation Flowchart :

One of the common pain points with using a chain implementation like LCEL is the difficulty in accessing the intermediate variables, which is important for debugging pipelines. We look at few options where we can still access any intermediate variables we are interested using manipulations of the LCEL

Using RunnableParallel to carry forward intermediate outputs

As we saw earlier, RunnableParallel allows us to carry multiple arguments forward to the next step in the chain. So we use this ability of RunnableParallel to carry forward the required intermediate values all the way till the end.

In the below example, the original self eval RAG chain is modified to output the retrieved context text along with the final self evaluation output. The primary change is that a RunnableParallel object is added to every step of the process to carry forward the context variable.

Additionally, the itemgetter function is used to clearly specify the inputs for the subsequent steps. For example, for the last two RunnableParallel objects, itemgetter(‘input’) is used to ensure that only the input argument from the previous step is passed on to the LLM/ Json parser objects.

rag_chain = ( RunnableParallel(context = retriever | format_docs, question = RunnablePassthrough() ) |RunnableParallel(answer= qa_prompt | llm | retrieve_answer, question = itemgetter(“question”), context = itemgetter(“context”) ) |RunnableParallel(input = qa_eval_prompt, context = itemgetter(“context”)) |RunnableParallel(input = itemgetter(“input”) | llm_selfeval , context = itemgetter(“context”) ) | RunnableParallel(input = itemgetter(“input”) | json_parser, context = itemgetter(“context”) ))

The output from this chain looks like the following :

A more concise variation:

rag_chain = ( RunnableParallel(context = retriever | format_docs, question = RunnablePassthrough() ) |RunnableParallel(answer= qa_prompt | llm | retrieve_answer, question = itemgetter(“question”), context = itemgetter(“context”) ) |RunnableParallel(input = qa_eval_prompt | llm_selfeval | json_parser, context = itemgetter(“context”)))

Using Global variables to save intermediate steps

This method essentially uses the principle of a logger. A new function is introduced that saves its input to a global variable, thus allowing access to the intermediate variable through the global variable

global context

def save_context(x):global contextcontext = xreturn x

rag_chain = ( RunnableParallel(context = retriever | format_docs | save_context, question = RunnablePassthrough() ) |RunnableParallel(answer= qa_prompt | llm | retrieve_answer, question = itemgetter(“question”) ) |qa_eval_prompt | llm_selfeval |json_parser)

Here a global variable called context and a function called save_context are defined. The save_context function saves its input value to the global context variable before returning the same input. In the chain, the save_context function is added as the last step of the context retrieval step.

This option allows access to any intermediate steps without making major changes to the chain.

Accessing intermediate variables using global variables

Using callbacks

Attaching callbacks to the chain is another common method used for logging intermediate variable values. More details on callbacks in LangChain will be covered in a separate post.



Source link

Tags: AprBuildingChainExpressionLangChainlanguageLCELRAGRoshanSanthosh
Previous Post

How Do You Stop Wood Rot From Spreading

Next Post

Dow Jones Futures: Stock Market Breaks Expectations As Iran Fears Spike; What To Do Now

Related Posts

How insurance companies can use synthetic data to fight bias
AI Technology

How insurance companies can use synthetic data to fight bias

June 10, 2024
From Low-Level to High-Level Tasks: Scaling Fine-Tuning with the ANDROIDCONTROL Dataset
AI Technology

From Low-Level to High-Level Tasks: Scaling Fine-Tuning with the ANDROIDCONTROL Dataset

June 10, 2024
Decoding Decoder-Only Transformers: Insights from Google DeepMind’s Paper
AI Technology

Decoding Decoder-Only Transformers: Insights from Google DeepMind’s Paper

June 9, 2024
How Game Theory Can Make AI More Reliable
AI Technology

How Game Theory Can Make AI More Reliable

June 9, 2024
Buffer of Thoughts (BoT): A Novel Thought-Augmented Reasoning AI Approach for Enhancing Accuracy, Efficiency, and Robustness of LLMs
AI Technology

Buffer of Thoughts (BoT): A Novel Thought-Augmented Reasoning AI Approach for Enhancing Accuracy, Efficiency, and Robustness of LLMs

June 9, 2024
Deciphering Doubt: Navigating Uncertainty in LLM Responses
AI Technology

Deciphering Doubt: Navigating Uncertainty in LLM Responses

June 9, 2024
Next Post
Dow Jones Futures: Stock Market Breaks Expectations As Iran Fears Spike; What To Do Now

Dow Jones Futures: Stock Market Breaks Expectations As Iran Fears Spike; What To Do Now

Iran launches cruise missiles on Israel alongside ‘Shaheed’ drones as the war in the Middle East escalates

Iran launches cruise missiles on Israel alongside 'Shaheed' drones as the war in the Middle East escalates

Evaluating World Knowledge and Memorization in Machine Learning: A Study by the University of Tübingen

Evaluating World Knowledge and Memorization in Machine Learning: A Study by the University of Tübingen

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Trending
  • Comments
  • Latest
Is C.AI Down? Here Is What To Do Now

Is C.AI Down? Here Is What To Do Now

January 10, 2024
Porfo: Revolutionizing the Crypto Wallet Landscape

Porfo: Revolutionizing the Crypto Wallet Landscape

October 9, 2023
A Complete Guide to BERT with Code | by Bradney Smith | May, 2024

A Complete Guide to BERT with Code | by Bradney Smith | May, 2024

May 19, 2024
How To Build A Quiz App With JavaScript for Beginners

How To Build A Quiz App With JavaScript for Beginners

February 22, 2024
Saginaw HMI Enclosures and Suspension Arm Systems from AutomationDirect – Library.Automationdirect.com

Saginaw HMI Enclosures and Suspension Arm Systems from AutomationDirect – Library.Automationdirect.com

December 6, 2023
Part 1: ABAP RESTful Application Programming Model (RAP) – Introduction

Part 1: ABAP RESTful Application Programming Model (RAP) – Introduction

November 20, 2023
Can You Guess What Percentage Of Their Wealth The Rich Keep In Cash?

Can You Guess What Percentage Of Their Wealth The Rich Keep In Cash?

June 10, 2024
AI Compared: Which Assistant Is the Best?

AI Compared: Which Assistant Is the Best?

June 10, 2024
How insurance companies can use synthetic data to fight bias

How insurance companies can use synthetic data to fight bias

June 10, 2024
5 SLA metrics you should be monitoring

5 SLA metrics you should be monitoring

June 10, 2024
From Low-Level to High-Level Tasks: Scaling Fine-Tuning with the ANDROIDCONTROL Dataset

From Low-Level to High-Level Tasks: Scaling Fine-Tuning with the ANDROIDCONTROL Dataset

June 10, 2024
UGRO Capital: Targeting to hit milestone of Rs 20,000 cr loan book in 8-10 quarters: Shachindra Nath

UGRO Capital: Targeting to hit milestone of Rs 20,000 cr loan book in 8-10 quarters: Shachindra Nath

June 10, 2024
Facebook Twitter LinkedIn Pinterest RSS
News PouroverAI

The latest news and updates about the AI Technology and Latest Tech Updates around the world... PouroverAI keeps you in the loop.

CATEGORIES

  • AI Technology
  • Automation
  • Blockchain
  • Business
  • Cloud & Programming
  • Data Science & ML
  • Digital Marketing
  • Front-Tech
  • Uncategorized

SITEMAP

  • Disclaimer
  • Privacy Policy
  • DMCA
  • Cookie Privacy Policy
  • Terms and Conditions
  • Contact us

Copyright © 2023 PouroverAI News.
PouroverAI News

No Result
View All Result
  • Home
  • AI Tech
  • Business
  • Blockchain
  • Data Science & ML
  • Cloud & Programming
  • Automation
  • Front-Tech
  • Marketing

Copyright © 2023 PouroverAI News.
PouroverAI News

Welcome Back!

Login to your account below

Forgotten Password? Sign Up

Create New Account!

Fill the forms bellow to register

All fields are required. Log In

Retrieve your password

Please enter your username or email address to reset your password.

Log In