Close Menu
    Facebook LinkedIn YouTube WhatsApp X (Twitter) Pinterest
    Trending
    • Portable water filter provides safe drinking water from any source
    • MAGA Is Increasingly Convinced the Trump Assassination Attempt Was Staged
    • NCAA seeks faster trial over DraftKings disputed March Madness branding case
    • AI Trusted Less Than Social Media and Airlines, With Grok Placing Last, Survey Says
    • Extragalactic Archaeology tells the ‘life story’ of a whole galaxy
    • Swedish semiconductor startup AlixLabs closes €15 million Series A to scale atomic-level etching technology
    • Republican Mutiny Sinks Trump’s Push to Extend Warrantless Surveillance
    • Yocha Dehe slams Vallejo Council over rushed casino deal approval process
    Facebook LinkedIn WhatsApp
    Times FeaturedTimes Featured
    Saturday, April 18
    • Home
    • Founders
    • Startups
    • Technology
    • Profiles
    • Entrepreneurs
    • Leaders
    • Students
    • VC Funds
    • More
      • AI
      • Robotics
      • Industries
      • Global
    Times FeaturedTimes Featured
    Home»Artificial Intelligence»LangGraph + SciPy: Building an AI That Reads Documentation and Makes Decisions
    Artificial Intelligence

    LangGraph + SciPy: Building an AI That Reads Documentation and Makes Decisions

    Editor Times FeaturedBy Editor Times FeaturedAugust 18, 2025No Comments12 Mins Read
    Facebook Twitter Pinterest Telegram LinkedIn Tumblr WhatsApp Email
    Share
    Facebook Twitter LinkedIn Pinterest Telegram Email WhatsApp Copy Link


    Introduction

    have at all times walked side-by-side, holding palms.

    I keep in mind listening to “Study Statistics to know what’s behind the algorithms” once I began learning Knowledge Science. Whereas all of that was fascinating to me, it was additionally actually overwhelming.

    The actual fact is that there are too many statistical ideas, exams, and distributions to maintain observe of. In the event you don’t know what I’m speaking about, simply go to the Scipy.stats web page, and you’ll perceive.

    If you’re sufficiently old within the Knowledge Science area, you most likely bookmarked (and even printed) a type of statistical check cheatsheets. They had been common for some time. However now, the Giant Language Fashions have gotten sort of a “second mind” for us, serving to us to shortly seek the advice of nearly any info we li,ke with the additional advantage of getting it summarized and tailored to our wants.

    With that in thoughts, my pondering was that selecting the best statistical check will be complicated as a result of it is dependent upon variable varieties, assumptions, and many others.

    So, I assumed I may get an assistant to assist with that. Then, my challenge took type.

    • I used LangGraph to construct a multi-step agent
    • The front-end was constructed with Streamlit
    • The Agent can shortly seek the advice of Scipy Stats documentation and retrieve the precise code for each particular scenario.
    • Then, it offers us a pattern Python code
    • It’s deployed in Streamlit Apps, in case you need to strive it.
    • App Hyperlink: https://ai-statistical-advisor.streamlit.app/

    Wonderful!

    Let’s dive in and learn to construct this agent.

    LangGraph

    LangGraph is a library that helps construct complicated, multi-step functions with giant language fashions (LLMs) by representing them as a graph. This graph structure permits the builders to create situations, loops, which make it helpful for creating refined brokers and chatbots that may determine what to do subsequent based mostly on the outcomes of a earlier step

    It basically turns a inflexible sequence of actions into a versatile, dynamic decision-making course of. In LangGraph, every node is a operate or software.

    Subsequent, let’s be taught extra in regards to the agent we’re going to create on this put up.

    Statistical Advisor Agent

    This agent is a Statistical Advisor. So, the principle concept is that:

    1. The bot receives a statistics-related query, akin to “Methods to evaluate the technique of two teams“.
    2. It checks the query and determines if it must seek the advice of Scipy’s documentation or simply give a direct reply.
    3. If wanted, the agent makes use of a RAG software on embedded SciPy documentation
    4. Returns a solution.
    5. If relevant, it returns a pattern Python code on how you can carry out the statistical check.

    Let’s shortly have a look at the Graph generated by LangGraph to point out this agent.

    Agent created with LangGraph. Picture by the writer.

    Nice. Now, let’s lower to the chase and begin coding!

    Code

    To make issues simpler, I’ll break the event down into modules. First, let’s set up the packages we are going to want.

    pip set up chromadb langchain-chroma langchain-community langchain-openai 
    langchain langgraph openai streamlit

    Chunk and Embed

    Subsequent, we are going to create the script to take our documentation and create chunks of textual content, in addition to embed these chunks. We do this to make it simpler for vector databases like ChromaDB to go looking and retrieve info.

    So, I created this operate embed_docs() that you may see within the GitHub repository linked here.

    • The operate takes Scipy’s documentation (which is open supply below BSD license)
    • Splits it into chinks of 500 tokens and overlap of fifty tokens.
    • Makes the embedding (rework textual content into numerical values for optimized vector db search) utilizing OpenAIEmbedding
    • Saves the embeddings in an occasion of ChromaDB

    Now the information is prepared as a data base for a Retrieval-Augmented Era (RAG). Nevertheless it wants a retriever that may search and discover the information. That’s what the retriever does.

    Retriever

    The get_doc_answer() operate will:

    • Load the ChromaDB occasion beforehand created.
    • Create an occasion of OpenAI GPT 4o
    • Create a retriever object
    • Glue all the things collectively in a retrieval_chain that will get a query from the person, sends it to the LLM
    • The mannequin makes use of the retriever to entry the ChromaDB occasion, get related knowledge about statistical exams, and return the reply to the person.

    Now we’ve got the RAG accomplished with the paperwork embedded and the retriever prepared. Let’s transfer on to the Agent nodes.

    Agent Nodes

    LangGraph has this fascinating structure that considers every node as a operate. Subsequently, now we should create the capabilities to deal with every a part of the agent.

    We’ll comply with the stream and begin with the classify_intent node. Since some nodes must work together with an LLM, we have to generate a consumer.

    from rag.retriever import get_doc_answer
    from openai import OpenAI
    import os
    from dotenv import load_dotenv
    load_dotenv()
    
    # Occasion of OpenAI
    consumer = OpenAI()

    As soon as we begin the agent, it should obtain a question from the person. So, this node will test the query and determine if the subsequent node shall be a easy response or if it wants to go looking Scipy’s documentation.

    def classify_intent(state):
        """Examine if the person query wants a doc search or will be answered straight."""
        query = state["question"]
    
        response = consumer.chat.completions.create(
            mannequin="gpt-4o",
            messages=[
                {"role": "system", "content": "You are an assistant that decides if a question about statistical tests needs document lookup or not. If it is about definitions or choosing the right test, return 'search'. Otherwise return 'simple'."},
                {"role": "user", "content": f"Question: {question}"}
            ]
        )
        resolution = response.decisions[0].message.content material.strip().decrease()
    
        return {"intent": resolution}  # "search" or "easy"

    If a query about statistical ideas or exams is requested, then the retrieve_info() node is activated. It performs the RAG within the documentation.

    def retrieve_info(state):
        """Use the RAG software to reply from embedded docs."""
        query = state["question"]
        reply = get_doc_answer(query=query)
        return {"rag_answer": reply}

    As soon as the correct chunk of textual content is retrieved from ChromaDB, the agent goes to the subsequent node to generate a solution.

    def reply(state):
        """Construct the ultimate reply."""
        if state.get("rag_answer"):
            return {"final_answer": state["rag_answer"]}
        else:
            return {"final_answer": "I am unsure how you can assist with that but."}

    Lastly, the final node is to generate a code, if that’s relevant. That means, if there’s a solution the place the check will be executed utilizing Scipy, there shall be a pattern code.

    def generate_code(state):
        """Generate Python code to carry out the beneficial statistical check."""
        query = state["question"]
        suggested_test = state.get("rag_answer") or "a statistical check"
    
        immediate = f"""
        You're a Python tutor. 
        Primarily based on the next person query, generate a brief Python code snippet utilizing scipy.stats that performs the suitable statistical check.
    
        Person query:
        {query}
    
        Reply given:
        {suggested_test}
    
        Solely output code. Do not embrace explanations.
        """
    
        response = consumer.chat.completions.create(
            mannequin="gpt-4o",
            messages=[{"role": "user", "content": prompt}]
        )
        
        return {"code_snippet": response.decisions[0].message.content material.strip()}

    Discover one thing vital right here: all capabilities in our nodes at all times have state as an argument as a result of the state is the only supply of reality for all the workflow. Every operate, or “node,” within the graph reads from and writes to this central state object.

    For instance:

    • The classify_intent operate reads the query from the state and provides an intent key.
    • The retrieve_info operate can learn the identical query and add a rag_answer, which the reply operate lastly reads to assemble the final_answer. This shared state dictionary is how the totally different steps within the agent’s reasoning and action-taking course of keep related.

    Subsequent, let’s put all the things collectively and construct our graph!

    Constructing the Graph

    The graph is the agent itself. So, what we’re doing right here is mainly telling LangGraph what the nodes are that we’ve got and the way they join to one another, so the framework could make the knowledge run in line with that stream.

    Let’s import the modules.

    from langgraph.graph import StateGraph, END
    from typing_extensions import TypedDict
    from langgraph_agent.nodes import classify_intent, retrieve_info, reply, generate_code

    Outline our state schema. Do not forget that dictionary that the agent makes use of to attach the steps of the method? That’s it.

    # Outline the state schema (only a dictionary for now)
    class TypedDictState(TypedDict):
        query: str
        intent: str
        rag_answer: str
        code_snippet: str
        final_answer: str

    Right here, we are going to create a operate that builds the graph.

    • To inform LangGraph what the steps (capabilities) within the course of are, we use add_node
    • As soon as we’ve got listed all of the capabilities, we begin creating the perimeters, that are the connections between the nodes.
    • We begin the method with set_entry_point. That is the primary operate for use.
    • We use add_edge to attach one node to a different, utilizing the primary argument because the operate from which the knowledge comes, and the second argument is the place it goes.
    • If we’ve got a situation to comply with, we use add_conditional_edges
    • We use END to complete the graph and compile to construct it.
    def build_graph():
        # Construct the LangGraph stream
        builder = StateGraph(TypedDictState)
    
        # Add nodes
        builder.add_node("classify_intent", classify_intent)
        builder.add_node("retrieve_info", retrieve_info)
        builder.add_node("reply", reply)
        builder.add_node("generate_code", generate_code)
    
        # Outline stream
        builder.set_entry_point("classify_intent")
    
        builder.add_conditional_edges(
            "classify_intent",
            lambda state: state["intent"],
            {
                "search": "retrieve_info",
                "easy": "reply"
            }
        )
    
        builder.add_edge("retrieve_info", "reply")
        builder.add_edge("reply", "generate_code")
        builder.add_edge("generate_code", END)
    
        return builder.compile()

    With our graph builder operate prepared, all we’ve got to do now’s create a phenomenal front-end the place we will work together with this agent.

    Let’s do this now.

    Streamlit Entrance-Finish

    The front-end is the ultimate piece of the puzzle, the place we create a Person Interface that permits us to simply enter a query in a correct textual content field and see the reply correctly formatted.

    I selected Streamlit as a result of it is vitally straightforward to prototype and deploy. Let’s start with the imports.

    import os
    import time
    import streamlit as st

    Then, we configure the web page’s look.

    # Config web page
    st.set_page_config(page_title="Stats Advisor Agent",
                       page_icon='🤖',
                       structure="vast",
                       initial_sidebar_state="expanded")

    Create a sidebar, the place the person can enter their OpenAI API key, together with a “Clear” session button.

    # Add a spot to enter the API key
    with st.sidebar:
        api_key = st.text_input("OPENAI_API_KEY", kind="password")
    
        # Save the API key to the surroundings variable
        if api_key:
            os.environ["OPENAI_API_KEY"] = api_key
    
        # Clear
        if st.button('Clear'):
            st.rerun()

    Subsequent, we arrange the web page title and directions and add a textual content field for the person to enter a query.

    # Title and Directions
    if not api_key:
        st.warning("Please enter your OpenAI API key within the sidebar.")
        
    st.title('Statistical Advisor Agent | 🤖')
    st.caption('This AI Agent is skilled to reply questions on statistical exams from the [Scipy](https://docs.scipy.org/doc/scipy/reference/stats.html) package deal.')
    st.caption('Ask questions like: "What's the finest statistical check to match two means".')
    st.divider()
    
    # Person query
    query = st.text_input(label="Ask me one thing:",
                             placeholder= "e.g. What's the finest check to match 3 teams means?")
    

    Lastly, we will run the graph builder and show the reply on display screen.

    # Run the graph
    if st.button('Search'):
        
        # Progress bar
        progress_bar = st.progress(0)
    
        with st.spinner("Considering..", show_time=True):
            
            from langgraph_agent.graph import build_graph
            progress_bar.progress(10)
            # Construct the graph
            graph = build_graph()
            consequence = graph.invoke({"query": query})
            
            # Progress bar
            progress_bar.progress(50)
    
            # Print the consequence
            st.subheader("📖 Reply:")
            
            # Progress bar
            progress_bar.progress(100)
    
            st.write(consequence["final_answer"])
    
            if "code_snippet" in consequence:
                st.subheader("💻 Prompt Python Code:")
                st.write(consequence["code_snippet"])
    

    Let’s see the consequence now.

    Wow, the result’s spectacular!

    • I requested: What’s the finest check to match two teams means?
    • Reply: To match the technique of two teams, probably the most acceptable check is usually the impartial two-sample t-test if the teams are impartial and the information is generally distributed. If the information just isn’t usually distributed, a non-parametric check just like the Mann-Whitney U check could be extra appropriate. If the teams are paired or associated, a paired pattern t-test can be acceptable.

    Mission achieved for what we proposed to create.

    Attempt It Your self

    Do you need to give this Agent a Attempt?

    Go forward and check the deployed model now!

    https://ai-statistical-advisor.streamlit.app

    Earlier than You Go

    It is a lengthy put up, I do know. However I hope it was value to learn it to the tip. We discovered loads about LangGraph. It makes us assume differently about creating AI brokers.

    The framework forces us to consider each step of the knowledge, from the second a query is prompted to the LLM till the reply that shall be displayed. Questions like these begin to pop in your thoughts throughout the improvement course of:

    • What occurs after the person asks the query?
    • Does the agent must confirm one thing earlier than transferring on?
    • Are there situations to think about throughout the interplay?

    This structure turns into a bonus as a result of it makes the entire course of cleaner and scalable, since including a brand new function will be so simple as including a brand new operate (node).

    Alternatively, LangGraph just isn’t as user-friendly as frameworks like Agno or CrewAI, which encapsulate many of those abstractions in easier strategies, making the method a lot simpler to be taught and develop, but in addition much less versatile.

    Ultimately, it’s all a matter of what drawback is being solved and the way versatile you want it to be.

    GitHub Repository

    https://github.com/gurezende/AI-Statistical-Advisor

    About Me

    In the event you preferred this content material and need to be taught extra about my work, right here is my web site, the place you can too discover all my contacts.

    https://gustavorsantos.me

    [1. LangGraph Docs] https://langchain-ai.github.io/langgraph/concepts/why-langgraph/

    [2. Scipy Stats] https://docs.scipy.org/doc/scipy/reference/stats.html

    [3. Streamlit Docs] https://docs.streamlit.io/

    [4. Statistical Advisor App] https://ai-statistical-advisor.streamlit.app/



    Source link

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Editor Times Featured
    • Website

    Related Posts

    A Practical Guide to Memory for Autonomous LLM Agents

    April 17, 2026

    You Don’t Need Many Labels to Learn

    April 17, 2026

    Beyond Prompting: Using Agent Skills in Data Science

    April 17, 2026

    6 Things I Learned Building LLMs From Scratch That No Tutorial Teaches You

    April 17, 2026

    Introduction to Deep Evidential Regression for Uncertainty Quantification

    April 17, 2026

    memweave: Zero-Infra AI Agent Memory with Markdown and SQLite — No Vector Database Required

    April 17, 2026

    Comments are closed.

    Editors Picks

    Portable water filter provides safe drinking water from any source

    April 18, 2026

    MAGA Is Increasingly Convinced the Trump Assassination Attempt Was Staged

    April 18, 2026

    NCAA seeks faster trial over DraftKings disputed March Madness branding case

    April 18, 2026

    AI Trusted Less Than Social Media and Airlines, With Grok Placing Last, Survey Says

    April 18, 2026
    Categories
    • Founders
    • Startups
    • Technology
    • Profiles
    • Entrepreneurs
    • Leaders
    • Students
    • VC Funds
    About Us
    About Us

    Welcome to Times Featured, an AI-driven entrepreneurship growth engine that is transforming the future of work, bridging the digital divide and encouraging younger community inclusion in the 4th Industrial Revolution, and nurturing new market leaders.

    Empowering the growth of profiles, leaders, entrepreneurs businesses, and startups on international landscape.

    Asia-Middle East-Europe-North America-Australia-Africa

    Facebook LinkedIn WhatsApp
    Featured Picks

    TikTok apologises after thousands of US users report app issues

    January 26, 2026

    Polish HRTech startup Global Work AI secures €2 million to enhance automated applications and launch its AI career assistant

    November 26, 2025

    Will 2025 Be the Year Real-Time Analytics Finally Goes Mainstream? | by Mahdi Karabiben | Feb, 2025

    February 3, 2025
    Categories
    • Founders
    • Startups
    • Technology
    • Profiles
    • Entrepreneurs
    • Leaders
    • Students
    • VC Funds
    Copyright © 2024 Timesfeatured.com IP Limited. All Rights.
    • Privacy Policy
    • Disclaimer
    • Terms and Conditions
    • About us
    • Contact us

    Type above and press Enter to search. Press Esc to cancel.