A simple agent using LangGraph with RAG context and web search

A simple agent using LangGraph with RAG context and web search

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.

LangGraph

Step 8: Queries

Query 1: Give me a brief note on AVL trees.

https://telestreak.com/tech/ai/a-simple-agent-using-langgraph-with-rag-context-and-web-search/#Query_1_%E2%80%9CGive_me_a_brief_note_on_AVL_trees%E2%80%9D

Query 2: “Tutor me about binary search trees in 300 words.“

https://telestreak.com/tech/ai/a-simple-agent-using-langgraph-with-rag-context-and-web-search/#Query_2_%E2%80%9CTutor_me_about_binary_search_trees_in_300_words%E2%80%9C

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“

https://telestreak.com/tech/ai/a-simple-agent-using-langgraph-with-rag-context-and-web-search/#Query_3_%E2%80%9CGive_me_2_MCQs_interview_questions_along_with_4_options_in_Binary_Search_tree_topic_from_the_web_State_the_correct_answer_Let_the_difficulty_level_be_Easy%E2%80%9C

Thanks for reading this article till the end.




Nayapati Venkat Raghunath Rao

Production and Engineering

5 个月

Great madam. ??

要查看或添加评论,请登录

Sushma Rao的更多文章

社区洞察

其他会员也浏览了