A simple agent using LangGraph with RAG context and web search
Sushma Rao
Expert Vetted freelancer on Upwork(Top 1%) | Backend & GenAI | Langchain Langgraph LLM| AI ML development/Automation | Algorithms expert| Cloud development I help clients get more business through software development
Aim: To create a study assistant that can help in the preparation, notes, cheat sheets, guides, and much more. Along with this, It helps with giving you the required mock test to further strengthen the learning.
View the entire article here:?https://wp.me/pccXal-AX
Github code: https://lnkd.in/gH4u7qdR
Please view the link above if you want more details on the implementation.
Here is a brief.
Step 1: Create a routing logic
This step contains a question router chain that combines the LLM with the prompt and structures the output which returns whether the question requires an RAG context or a web search to generate an answer. Review the code below and It returns “vectorstore” or “web_search”.
Step 2: Web search tool
Tavily Web search tool is one of the tools used in langchain community.
Step 3: Create a chain to generate the answers
Create a rag_chain which is a combination of LLM+ prompt template+ output parser. OutputParser that parses LLMResult into the top likely string. Prompt Template is one of the templates that is in the lang chain hub.
Step 4: Define the graph state
Each node in the graph contains a data structure that has a question, generation, and documents when there is a RAG context.
class GraphState(TypedDict):
"""
Represents the state of our graph.
Attributes:
question: question
generation: LLM generation
documents: list of documents
"""
question: str
generation: str
documents: List[str]
Step 5: Retrieve from the vector database and generate answers
The following code generates based on the documents that are retrieved from the db retriever. The retriever uses a Maximum marginal relevance retrieval mechanism to retrieve the documents.
领英推荐
Step 6: Web search tool invocation
Invoke the web search tool with the questions. Retrieve and generate answers.
Step 7: Graph design
The start invokes route_question where the question is classified as a vectorStore or web_search. Based on the result it invokes either the vectorstore node or web_search node. which ends the workflow after generating the results.
Step 8: Queries
Query 1: Give me a brief note on AVL trees.
Query 2: “Tutor me about binary search trees in 300 words.“
Query 3: “Give me 2 MCQ interview questions along with 4 options in Binary Search tree topic from the web. State the correct answer. Let the difficulty level be Easy“
Thanks for reading this article till the end.
Production and Engineering
5 个月Great madam. ??