<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Justin DJAKAS on Medium]]></title>
        <description><![CDATA[Stories by Justin DJAKAS on Medium]]></description>
        <link>https://medium.com/@justindjakas?source=rss-66abbb206fb4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*NtrKTYrfQ7Nq-_9eIcBu9Q.png</url>
            <title>Stories by Justin DJAKAS on Medium</title>
            <link>https://medium.com/@justindjakas?source=rss-66abbb206fb4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 17:22:21 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@justindjakas/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Building AI Agents with LangChain & LangGraph]]></title>
            <link>https://blog.gopenai.com/building-ai-agents-with-langchain-langgraph-be69e48dbe6c?source=rss-66abbb206fb4------2</link>
            <guid isPermaLink="false">https://medium.com/p/be69e48dbe6c</guid>
            <category><![CDATA[ai-agent]]></category>
            <category><![CDATA[langgraph]]></category>
            <category><![CDATA[langsmith]]></category>
            <category><![CDATA[langchain]]></category>
            <category><![CDATA[generative-ai-tools]]></category>
            <dc:creator><![CDATA[Justin DJAKAS]]></dc:creator>
            <pubDate>Tue, 17 Nov 2026 07:01:02 GMT</pubDate>
            <atom:updated>2026-11-17T12:57:30.635Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aD5rbNNXpWkMRxBiJF2ypw.png" /></figure><p><strong>Master the fundamentals of LLM engineering and build the foundations of production-ready AI agents.</strong></p><h3>Part 1: Introduction &amp; LangChain Fundamentals</h3><p><em>From a single LLM call to composable pipelines: the building blocks every AI agent is made of.</em></p><p>In the previous post, I announced a <strong>7-part technical series</strong> on building AI agents with LangChain and LangGraph, covering everything from LLM fundamentals to production-ready multi-agent systems.</p><p>Today, the journey begins.</p><p>This first article lays the foundation for everything that follows. Before an AI agent can use tools, maintain conversational memory, or collaborate with other agents, you need to understand a few fundamental concepts:</p><ul><li>How a framework structures an LLM application.</li><li>How prompts are transformed into reusable templates.</li><li>How individual components are connected to build reliable, composable pipelines.</li></ul><p>By the end of this article, you will have:</p><ul><li>A working LangChain development environment.</li><li>A solid understanding of LangChain&#39;s core concepts.</li><li>Hands-on experience building reusable prompt templates.</li><li>Practical experience composing multiple components into chains.</li><li>A small multi-step project demonstrating these concepts.</li></ul><h3>Where We Are in the Series</h3><ul><li><strong>Part 1: Introduction &amp; LangChain Fundamentals</strong> (You are here)</li><li>Part 2: Memory &amp; Vector Databases (Upcoming)</li><li>Part 3: AI Agents &amp; Tools (Upcoming)</li><li>Part 4: LangGraph Workflows (Upcoming)</li><li>Part 5: Multi-Agent Systems &amp; Real-World Applications (Upcoming)</li><li>Part 6: Memory, Feedback &amp; Human-in-the-Loop (Upcoming)</li><li>Part 7: Production, Integration &amp; Optimization (Upcoming)</li></ul><p><strong>Let&#39;s get started!</strong></p><h3>What You Will Learn</h3><p>By the end of this article, you will understand:</p><ul><li>What an LLM application really is, beyond simply calling a model.</li><li>Why frameworks like LangChain and LangGraph exist.</li><li>The four building blocks of any LLM application.</li><li>How to set up a clean, reproducible development environment.</li><li>How to build <strong>prompt templates</strong>, <strong>chains</strong>, and <strong>output parsers</strong>.</li><li>What a <strong>Runnable</strong> is and why it is the core abstraction of the framework.</li><li>How to compose, parallelize, stream, and batch your pipelines.</li><li>How to handle errors, retries, and fallbacks.</li><li>How to trace your chains with <strong>LangSmith</strong>.</li></ul><p><strong>Prerequisites:</strong></p><ul><li>Python 3.10+ (this series uses Python 3.12).</li><li>Basic knowledge of Python.</li><li>An API key from an LLM provider (OpenAI in our examples).</li></ul><h3>1. What Are LLM Applications?</h3><p>In recent years, Large Language Models (LLMs) such as ChatGPT, Gemini, and Claude have transformed the way we interact with computers. Instead of writing complex rules and conditions, we can simply ask questions in natural language and receive useful answers.</p><p>An <strong>LLM application</strong> is any piece of software that uses such a model to:</p><ul><li>Understand and generate human-like text.</li><li>Automate reasoning tasks.</li><li>Act as a digital assistant for a specific purpose.</li></ul><h3>Real-World Examples</h3><ul><li>A chatbot that answers questions about your company&#39;s documents.</li><li>An AI tutor that explains coding concepts.</li><li>An assistant that summarizes emails or generates weekly reports.</li><li>A support agent that classifies tickets and drafts replies.</li></ul><p>The key insight is this:</p><p><strong>An LLM application is never just a model.</strong></p><p>It is a combination of:</p><p><strong>AI Model + Logic + Data + Tools</strong></p><p>Think of the model as the engine, but the application as the entire car.</p><h3>2. Why We Need Frameworks Like LangChain and LangGraph</h3><p>For a small prototype, calling a model API directly is perfectly fine. However, as soon as your application requires any of the following, things can quickly become complicated:</p><ul><li><strong>Multiple steps:</strong> Summarize → Analyze → Generate a report.</li><li><strong>Memory:</strong> Remembering what was said earlier.</li><li><strong>Tool use:</strong> Searching the web or querying a database.</li><li><strong>Error handling and retries:</strong> Managing failures and recovering from unexpected errors.</li><li><strong>Provider flexibility:</strong> Switching from one model provider to another.</li><li><strong>Observability:</strong> Understanding what happened when something goes wrong.</li></ul><p>This is where <strong>LangChain</strong> and <strong>LangGraph</strong> come in.</p><h3>2.1. LangChain: The Toolbox</h3><p>LangChain helps you build structured workflows around LLMs using reusable components such as:</p><ul><li>Prompts</li><li>Models</li><li>Output parsers</li><li>Chains</li><li>Tools</li><li>Agents</li></ul><p>Think of LangChain as a <strong>toolbox for building AI-powered applications and pipelines</strong>.</p><h3>2.2. LangGraph: The Control Flow</h3><p>LangGraph builds on the LangChain ecosystem. It allows you to design <strong>graph-based workflows</strong> where each step (a <em>node</em>) processes information, while <em>edges</em> determine what happens next.</p><p>It is particularly useful for:</p><ul><li><strong>Multi-agent systems:</strong> Researcher → Writer → Editor.</li><li><strong>Stateful applications:</strong> Maintaining information throughout a workflow.</li><li><strong>Error-tolerant workflows:</strong> Implementing loops, conditional logic, and recovery mechanisms.</li></ul><p>Imagine a flowchart where each box represents a function or an AI agent, and the arrows determine how information moves between them.</p><blockquote><strong>Mental Model</strong><br><strong>LangChain</strong> provides the components.<br><strong>LangGraph</strong> provides the control flow between them.</blockquote><p>In this article, we will focus on LangChain&#39;s core components. LangGraph will take center stage in Article <strong>4</strong>.</p><h3>3. The Four Key Components of an LLM Application</h3><p>Every LLM application, from a simple Python script to a production-ready AI agent, is built around four fundamental ingredients.</p><h3>3.1. Prompt</h3><p>The <strong>prompt</strong> is the input text or instruction provided to the model.</p><p><em>Example: </em>&quot;Summarize this article in three sentences.&quot;</p><h3>3.2. Model</h3><p>The <strong>model</strong> is the LLM responsible for processing the input and generating a response.</p><p><em>Examples:</em> GPT-4o, Gemini, Claude.</p><h3>3.3. Memory</h3><p><strong>Memory</strong> allows an application to retain information from previous interactions and use it in subsequent steps.</p><p><em>Example:</em> Remembering a user&#39;s name throughout a conversation.</p><h3>3.4. Tool / Function</h3><p>A <strong>tool</strong> or <strong>function</strong> allows an LLM application to interact with external systems and perform actions.</p><p><em>Examples:</em></p><ul><li>Searching the web.</li><li>Retrieving information from a database.</li><li>Calling an external API.</li><li>Executing a Python function.</li></ul><p>LangChain and LangGraph help you assemble these components into structured applications without having to rewrite the same boilerplate code every time.</p><h3>4. A Simple Example Without LangChain</h3><p>Before introducing LangChain, let&#39;s see what a direct API call looks like using the <strong>OpenAI Python SDK</strong>.</p><p>This example sends a message directly to an OpenAI model and prints the generated response.</p><pre># Import the OpenAI client<br>from openai import OpenAI<br><br><br><br># Initialize the client<br># The client automatically reads OPENAI_API_KEY from the environment<br>client = OpenAI()<br><br># Send a request to the chat completion API<br>response = client.chat.completions.create(<br>    # Specify the language model to use<br>    model=&quot;gpt-4o-mini&quot;,<br><br>    # Provide the conversation messages<br>    messages=[<br>        {<br>            &quot;role&quot;: &quot;user&quot;,<br>            &quot;content&quot;: &quot;Explain LangChain in simple terms&quot;<br>        }<br>    ],<br>)<br><br># Extract and print the generated response<br>print(response.choices[0].message.content)</pre><p><em>Output:</em></p><pre>LangChain is a framework designed to help developers build applications that utilize language models, like those from OpenAI. It simplifies the process of integrating these AI models into various types of applications, such as chatbots, question-answering systems, or any software that requires understanding and generating human language.<br><br>Here’s a simple breakdown:<br><br>1. **Connecting Language Models**: LangChain allows you to connect to language models easily. You can use APIs from different models without worrying about the technical details.<br><br>2. **Structuring Workflows**: It helps you create complex workflows that can involve multiple steps, like taking user input, processing it with a language model, and then responding back in a coherent way.<br><br>3. **Handling Data**: LangChain can manage interactions with different types of data sources (like databases or web APIs), making it easier to enrich the language model’s responses with real-world information.<br><br>4. **Versatile Use Cases**: Whether you want to build a simple assistant or a complex application that requires nuanced understanding and interaction, LangChain provides the tools to do so.<br><br>In short, LangChain is like a toolkit for developers to build intelligent language-based applications more efficiently.</pre><p>This works perfectly. However, imagine that you now want to:</p><ul><li>Add a <strong>summarization step</strong> before generating the answer.</li><li><strong>Store the chat history</strong> to maintain conversational context.</li><li>Call a <strong>web API</strong> to fetch fresh data.</li><li><strong>Switch to another model provider</strong> next month.</li></ul><p>Each of these requirements introduces additional code, and that code can quickly become difficult to manage.</p><p>Prompts get duplicated, parsing logic becomes scattered across the application, and switching providers may require rewriting several parts of your code.</p><p>This is exactly the problem <strong>LangChain</strong> was built to solve: keeping LLM applications <strong>organized, modular, and scalable</strong>.</p><h3>5. Setting Up Your Environment</h3><p>A reproducible development environment saves hours of debugging later. Here is the setup we will use throughout this series.</p><h3>Requirements</h3><ul><li>Python 3.12</li><li>pip (Python package installer)</li><li>An API key from OpenAI (or another LLM provider)</li></ul><h3>Create and Activate a Virtual Environment</h3><pre>python3 -m venv .venv-agentic-ai<br>source .venv-agentic-ai/bin/activate      # on Windows: .venv-agentic-ai\Scripts\activate<br><br>python --version<br># Python 3.12.3</pre><p>Install the core packages:</p><pre>python -m pip install -U \<br>    &quot;langchain&gt;=1.0,&lt;2.0&quot; \<br>    &quot;langchain-openai&quot; \<br>    &quot;langgraph&gt;=1.0,&lt;2.0&quot; \<br>    &quot;langsmith&gt;=0.3.0&quot; \<br>    python-dotenv</pre><p>Here is what each package does:</p><ul><li>langchain is the main framework (v1.x): provides models, prompts, chains, and agents.</li><li>langchain-openai is the integration package for OpenAI models.</li><li>langgraph is the graph-based orchestration engine(used from Article 4).</li><li>langsmith is the tracing and debugging platform.</li><li>python-dotenv loads environment variables from a .env file.</li></ul><blockquote><strong>A Note on Versions</strong></blockquote><blockquote>LangChain 1.0 is a major release: its API is now stable and focused on a smaller set of core abstractions.<br><br>Many older tutorials still use patterns from versions 0.1 and 0.2 (LLMChain, ConversationBufferMemory, initialize_agent, etc.) that are now deprecated or have been moved to the legacy package.<br><br>Everything in this series targets <strong>LangChain 1.x</strong>, so you can copy and run the examples with confidence. Pinning versions below &lt;2.0 helps protect your environment from breaking changes.</blockquote><h3>Store Your Secrets in a .env File</h3><p>Never hard-code API keys in your source code.</p><p>Create a .env file at the root of your project:</p><pre>OPENAI_API_KEY=your-openai-key</pre><p>Add .env to your .gitignore, then load it at the top of your scripts:</p><pre>from dotenv import load_dotenv<br><br>load_dotenv()</pre><p><em>Output:</em></p><pre>True</pre><p><strong>Verify your installation</strong></p><p>If every package prints a version number (with langchain and langgraph at 1.x), you are ready to go.</p><pre>from importlib.metadata import version<br><br>for pkg in [&quot;langchain&quot;, &quot;langchain-core&quot;, &quot;langchain-openai&quot;, &quot;langgraph&quot;, &quot;langsmith&quot;]:<br>    print(f&quot;{pkg}: {version(pkg)}&quot;)</pre><p><em>Output:</em></p><pre>langchain: 1.4.2<br>langchain-core: 1.6.5<br>langchain-openai: 1.6.6<br>langgraph: 1.2.12<br>langsmith: 0.14.0</pre><h3>6. The Building Blocks of LangChain</h3><p>Think of LangChain as a <strong>Lego set for AI applications</strong>. Here are the main building blocks you will work with:</p><ul><li><strong>Chat model:</strong> The AI model (OpenAI, Anthropic, Google, etc.), wrapped in a standard interface.</li><li><strong>Messages:</strong> Structured conversation turns with roles (SystemMessage, HumanMessage, AIMessage).</li><li><strong>Prompt template:</strong> A reusable prompt with placeholders, such as &quot;Summarize the text: {content}&quot;.</li><li><strong>Output parser:</strong> Transforms the model&#39;s response into a usable format (a string, JSON, or a Python object).</li><li><strong>Chain:</strong> A sequence of steps combining prompts, models, parsers, and custom logic.</li><li><strong>Tool:</strong> An action the model can request, such as an API call, a calculator, or a database query.</li><li><strong>Agent:</strong> A decision-maker that chooses which tool to use and when.</li><li><strong>Memory:</strong> Stores previous messages or results.</li></ul><p>In this first article, we focus on the first five building blocks.</p><p>Tools and agents arrive in <strong>Article 3</strong>, while memory is covered in <strong>Articles 2 and 6</strong>.</p><blockquote><strong>About Memory in LangChain 1.x</strong><br><br>You may find older tutorials using ConversationBufferMemory. That class belongs to the legacy API.<br><br>In modern LangChain, conversational memory is handled through <strong>LangGraph&#39;s persistence layer (checkpointers)</strong>, which is more powerful and production-ready.<br><br>We will cover it properly in Articles 2 and 6.</blockquote><h3>7. Your First LangChain Example: A Text Summarizer</h3><p>Let&#39;s build a simple text summarizer step by step.</p><h3>Step 1: Import the Components</h3><pre>from dotenv import load_dotenv<br>from langchain_openai import ChatOpenAI<br>from langchain_core.prompts import ChatPromptTemplate<br>from langchain_core.output_parsers import StrOutputParser<br><br>load_dotenv()</pre><p>langchain_openai provides the integration with OpenAI models, while langchain_core contains the framework&#39;s fundamental abstractions: prompts, parsers, messages, and runnables.</p><h3>Step 2: Create the Model</h3><pre>llm = ChatOpenAI(model=&quot;gpt-4o-mini&quot;, temperature=0)</pre><p>The temperature parameter controls the creativity of the model&#39;s responses. A value of 0 makes the output as deterministic as possible, which is ideal for tasks such as extraction, classification, and summarization. Higher values (0.7–1.0) are better suited for creative writing.</p><p>Let&#39;s call the model directly to see what a chat model returns:</p><pre>response = llm.invoke(&quot;Explain what an LLM is in one sentence.&quot;)<br><br>print(type(response))        # &lt;class &#39;langchain_core.messages.ai.AIMessage&#39;&gt;<br>print(response.content)      # the generated text<br>print(response.usage_metadata)  # token usage: input, output, total</pre><p><em>Output:</em></p><pre>&lt;class &#39;langchain_core.messages.ai.AIMessage&#39;&gt;<br>An LLM, or Large Language Model, is an advanced artificial intelligence system designed to understand and generate human-like text based on vast amounts of data and complex algorithms.<br>{&#39;input_tokens&#39;: 17, &#39;output_tokens&#39;: 33, &#39;total_tokens&#39;: 50, &#39;input_token_details&#39;: {&#39;audio&#39;: 0, &#39;cache_read&#39;: 0}, &#39;output_token_details&#39;: {&#39;audio&#39;: 0, &#39;reasoning&#39;: 0}}</pre><p>Notice that the result is an <strong>AIMessage</strong> object, not a plain string. It contains the generated text (.content) as well as metadata such as token usage, which is useful for monitoring API costs.</p><h3>Step 3: Create a Prompt Template</h3><p>A prompt template defines <em>what you want the model to do</em>, using placeholders that you can fill in later.</p><pre>prompt = ChatPromptTemplate.from_template(<br>    &quot;Summarize this text in one sentence:\n\n{text}&quot;<br>)</pre><p>You can inspect what the template produces before sending it to any model:</p><pre>messages = prompt.invoke({<br>    &quot;text&quot;: &quot;LangChain is a powerful framework designed to simplify the development of applications powered by large language models. It provides developers with reusable components for building complex workflows, integrating external tools, connecting to data sources, and managing interactions with AI models. By combining these capabilities, developers can create intelligent applications such as chatbots, document assistants, and autonomous AI agents more efficiently.&quot;<br>})<br><br>print(messages)</pre><p><em>Output:</em></p><pre>messages=[HumanMessage(content=&#39;Summarize this text in one sentence:\n\nLangChain is a powerful framework designed to simplify the development of applications powered by large language models. It provides developers with reusable components for building complex workflows, integrating external tools, connecting to data sources, and managing interactions with AI models. By combining these capabilities, developers can create intelligent applications such as chatbots, document assistants, and autonomous AI agents more efficiently.&#39;, additional_kwargs={}, response_metadata={})]</pre><p>This is a useful debugging habit: <strong>always inspect the prompt before blaming the model.</strong> Checking the prompt helps you verify that the instructions and input variables are correctly formatted before sending them to the LLM.</p><h3>Step 4: Combine Everything into a Chain</h3><p>LangChain uses the pipe operator (|) to connect different components into a single workflow. This syntax is called the <strong>LangChain Expression Language (LCEL)</strong>, and it allows you to build modular, reusable chains by combining prompts, language models, and output parsers.</p><pre>chain = prompt | llm | StrOutputParser()</pre><h3>What Happens When the Chain Runs?</h3><p>When the chain is executed, LangChain processes the data through each component in sequence:</p><ol><li><strong>prompt</strong> fills in the template with the provided input variables and formats the messages for the model.</li><li><strong>llm</strong> sends these messages to the language model and returns an AIMessage object containing the generated response and associated metadata.</li><li><strong>StrOutputParser()</strong> extracts the text content from the AIMessage and converts it into a plain Python string.</li></ol><h3>Step 5: Run the Chain</h3><pre>summary = chain.invoke({<br>    &quot;text&quot;: &quot;LangChain helps developers build AI applications faster by &quot;<br>            &quot;simplifying prompt management and chaining logic.&quot;<br>})<br><br>print(summary)</pre><p><em>Output:</em></p><pre>LangChain accelerates AI application development by streamlining prompt management and chaining logic for developers.</pre><p>That is your first working LangChain pipeline.</p><h3>8. How It Works Internally</h3><p>Behind the single line of code prompt | llm | StrOutputParser(), several operations take place in sequence. Each component processes the data and passes its output to the next component.</p><p>The following diagram illustrates how data flows through the chain:</p><pre>{&quot;text&quot;: &quot;...&quot;}<br>      │<br>      ▼<br>[ChatPromptTemplate]  → Formats the input variables into messages<br>      │<br>      ▼<br>[ChatOpenAI]          → Sends the messages to the model and returns an AIMessage<br>      │<br>      ▼<br>[StrOutputParser]     → Extracts the text content and returns a Python string<br>      │<br>      ▼<br>&quot;LangChain simplifies building ...&quot;</pre><h3>9. Going Further with Prompt Templates</h3><p>In real-world applications, prompts often need to do more than provide a simple instruction to a language model. They may need to define the model&#39;s role, specify behavioral constraints, and handle different types of user input.</p><p>LangChain provides flexible prompt templates that make it easier to structure, customize, and reuse these instructions.</p><p>Let&#39;s explore three useful patterns.</p><h3>Pattern 1: System + Human Messages</h3><p>A <strong>system message</strong> defines the model&#39;s role, behavior, and constraints, while a <strong>human message</strong> carries the actual user input.</p><p>Separating these two types of messages helps organize instructions and gives you greater control over how the model responds.</p><pre>prompt = ChatPromptTemplate.from_messages([<br>    (&quot;system&quot;, &quot;You are a concise technical assistant. Answer in at most {max_sentences} sentences.&quot;),<br>    (&quot;human&quot;, &quot;{question}&quot;),<br>])<br><br>chain = prompt | llm | StrOutputParser()<br><br>print(chain.invoke({<br>    &quot;max_sentences&quot;: 2,<br>    &quot;question&quot;: &quot;What is a vector database?&quot;,<br>}))</pre><p><em>Output:</em></p><pre>A vector database is a specialized database designed to store, index, and query high-dimensional vectors, often used in machine learning and AI applications for tasks like similarity search and recommendation systems. It enables efficient retrieval of data based on vector representations, such as embeddings from neural networks.</pre><p>Separating the <em>behavior</em> (system message) from the <em>input</em> (human message) is a recommended best practice when designing prompts. It makes your prompts easier to maintain, customize, and reuse across different applications.</p><h3>Pattern 2: Multiple Variables, One Template</h3><p>Prompt templates can accept multiple input variables, allowing you to dynamically customize their content without changing the overall structure.</p><p>This means you can define a single reusable template and use it for different tasks by simply providing different values for each variable.</p><pre>translator_prompt = ChatPromptTemplate.from_messages([<br>    (&quot;system&quot;, &quot;You are a professional translator. Keep the tone {tone}.&quot;),<br>    (&quot;human&quot;, &quot;Translate the following text from {source_lang} to {target_lang}:\n\n{text}&quot;),<br>])<br><br>translator = translator_prompt | llm | StrOutputParser()<br><br>print(translator.invoke({<br>    &quot;tone&quot;: &quot;formal&quot;,<br>    &quot;source_lang&quot;: &quot;English&quot;,<br>    &quot;target_lang&quot;: &quot;French&quot;,<br>    &quot;text&quot;: &quot;Our new feature will be available next week.&quot;,<br>}))</pre><p><em>Output:</em></p><pre>Notre nouvelle fonctionnalité sera disponible la semaine prochaine.</pre><h3>Pattern 3: Question Answering</h3><p>The same approach can also be used to build question-answering applications. You can define a reusable prompt template that instructs the model to answer questions based on a given context.</p><p>This is particularly useful for applications such as document assistants, knowledge-based chatbots, and Retrieval-Augmented Generation (RAG) systems.</p><pre>qa_prompt = ChatPromptTemplate.from_template(<br>    &quot;Answer the question briefly.\n\nQuestion: {question}&quot;<br>)<br><br>qa_chain = qa_prompt | llm | StrOutputParser()<br><br>print(qa_chain.invoke({&quot;question&quot;: &quot;What is LangChain?&quot;}))</pre><p><em>Output:</em></p><pre>LangChain is a framework designed for building applications that utilize language models. It provides tools and components to facilitate the integration of language models with various data sources, APIs, and workflows, enabling developers to create more complex and interactive applications that leverage natural language processing capabilities.</pre><p>The lesson: the pipeline stays the same, only the template changes. That is the power of reusable prompts.</p><h3>10. Structured Output: From Text to Data</h3><p>Free-form text is useful for human-readable responses, but real-world applications often need <strong>structured data</strong> that Python can process directly.</p><p>Instead of manually extracting information from generated text, LangChain allows you to define an expected output structure and use the model&#39;s structured output capabilities to return data in a predictable format.</p><p>This makes it easier to validate, access, and integrate model responses into your Python applications.</p><pre>from typing import Literal<br>from pydantic import BaseModel, Field<br><br><br>class TicketAnalysis(BaseModel):<br>    &quot;&quot;&quot;Analysis of a customer support ticket.&quot;&quot;&quot;<br><br>    category: Literal[&quot;billing&quot;, &quot;technical&quot;, &quot;account&quot;, &quot;other&quot;] = Field(<br>        description=&quot;The main category of the ticket&quot;<br>    )<br>    urgency: Literal[&quot;low&quot;, &quot;medium&quot;, &quot;high&quot;] = Field(<br>        description=&quot;How urgent the ticket is&quot;<br>    )<br>    summary: str = Field(description=&quot;A one-sentence summary of the problem&quot;)<br><br><br>structured_llm = llm.with_structured_output(TicketAnalysis)<br><br>prompt = ChatPromptTemplate.from_messages([<br>    (&quot;system&quot;, &quot;You analyze customer support tickets.&quot;),<br>    (&quot;human&quot;, &quot;{ticket}&quot;),<br>])<br><br>ticket_chain = prompt | structured_llm<br><br>result = ticket_chain.invoke({<br>    &quot;ticket&quot;: &quot;I was charged twice this month and I need a refund before Friday!&quot;<br>})<br><br>print(result)<br>print(result.category, result.urgency)</pre><p><em>Output:</em></p><pre>category=&#39;billing&#39; urgency=&#39;high&#39; summary=&#39;Customer was charged twice and requests a refund before Friday.&#39;<br>billing high</pre><p>The result is a real TicketAnalysis object, so you can write result.urgency == &quot;high&quot; in your code without any string parsing. This pattern is one of the most useful tools you will use when building agents, as we will see in the next articles.</p><h3>11. Why Chains Are Powerful: Composing Multiple Steps</h3><p>One of the main advantages of LangChain is the ability to combine multiple processing steps into a single workflow. Instead of asking the model to perform everything in one prompt, you can break a complex task into smaller, independent steps and connect them together.</p><p>For example, consider a pipeline that performs the following operations:</p><ol><li><strong>Step 1 – Summarization:</strong> Summarize the original text.</li><li><strong>Step 2 – Keyword Extraction:</strong> Identify the main keywords from the generated summary.</li><li><strong>Step 3 – Title Generation:</strong> Generate a concise and relevant title based on the summary.</li></ol><p>By connecting these steps, you can build a modular pipeline where the output of one step becomes the input of the next.</p><h3>A Common Pitfall</h3><p>A naive approach is to pipe two chains together directly. However, this can lead to unexpected results if the output of the first chain does not match the input expected by the second chain.</p><pre># ⚠️ This does NOT work as expected<br>final_chain = summary_chain | keyword_chain</pre><p>Why does it fail? summary_chain outputs a string, but keyword_chain starts with a prompt template that expects a dictionary such as {&quot;summary&quot;: &quot;...&quot;}. The data shape does not match.</p><p><strong>The right way: map the output to the next input</strong></p><pre>summary_prompt = ChatPromptTemplate.from_template(<br>    &quot;Summarize this text in two sentences:\n\n{text}&quot;<br>)<br>keyword_prompt = ChatPromptTemplate.from_template(<br>    &quot;Extract 5 keywords from this summary, as a comma-separated list:\n\n{summary}&quot;<br>)<br><br>parser = StrOutputParser()<br><br>summary_chain = summary_prompt | llm | parser<br><br># Wrap the first chain in a dict so its output becomes the &quot;summary&quot; variable<br>keyword_chain = {&quot;summary&quot;: summary_chain} | keyword_prompt | llm | parser<br><br>print(keyword_chain.invoke({<br>    &quot;text&quot;: &quot;LangChain is an open-source framework for building LLM applications. &quot;<br>            &quot;It provides standard interfaces for models, prompts, and tools, &quot;<br>            &quot;and integrates with hundreds of providers.&quot;<br>}))</pre><p><em>Output:</em></p><pre>LangChain, open-source, framework, large language models, integration</pre><p>In LCEL, a dictionary of runnables is automatically converted into a <strong>RunnableParallel</strong>. This means that each runnable is executed independently, and its output is collected into a single Python dictionary.</p><p>This is particularly useful when different steps require the same input but produce different outputs. It allows you to combine their results and pass them to subsequent steps in your pipeline.</p><h3>Running Steps in Parallel</h3><p>When two steps are <strong>independent</strong> of each other, you can execute them simultaneously rather than waiting for one to finish before starting the other.</p><p>This can improve execution efficiency, especially when working with multiple LLM calls or independent processing tasks.</p><pre>from langchain_core.runnables import RunnableParallel<br><br>keywords_prompt = ChatPromptTemplate.from_template(<br>    &quot;Extract 5 keywords from this text, comma-separated:\n\n{text}&quot;<br>)<br>sentiment_prompt = ChatPromptTemplate.from_template(<br>    &quot;Is the tone of this text positive, neutral, or negative? Answer with one word:\n\n{text}&quot;<br>)<br><br>analysis = RunnableParallel(<br>    summary=summary_chain,<br>    keywords=keywords_prompt | llm | parser,<br>    sentiment=sentiment_prompt | llm | parser,<br>)<br><br>result = analysis.invoke({&quot;text&quot;: &quot;LangChain makes building LLM apps a joy. The docs are great!&quot;})<br>print(result)<br># {&#39;summary&#39;: &#39;...&#39;, &#39;keywords&#39;: &#39;...&#39;, &#39;sentiment&#39;: &#39;Positive&#39;}</pre><p><em>Output:</em></p><pre>{&#39;summary&#39;: &#39;LangChain simplifies the process of creating applications using large language models (LLMs). The documentation is excellent, enhancing the overall experience.&#39;, &#39;keywords&#39;: &#39;LangChain, building, LLM, apps, docs&#39;, &#39;sentiment&#39;: &#39;Positive&#39;}</pre><p>All three branches receive the same input and execute concurrently. This reduces the overall execution time compared to running each step sequentially, since the independent operations do not need to wait for one another.</p><h3>Adding Your Own Python Logic</h3><p>Not every step in a LangChain pipeline requires a language model. You can also integrate your own Python functions into a chain using <strong>RunnableLambda</strong>.</p><p>This allows you to combine traditional Python logic with LLM-powered components, making your pipelines more flexible and suitable for a wider range of tasks.</p><pre>from langchain_core.runnables import RunnableLambda<br><br>def to_keyword_list(raw: str) -&gt; list[str]:<br>    return [k.strip().lower() for k in raw.split(&quot;,&quot;) if k.strip()]<br><br>keyword_list_chain = (<br>    keywords_prompt | llm | parser | RunnableLambda(to_keyword_list)<br>)<br><br>print(keyword_list_chain.invoke({&quot;text&quot;: &quot;LangChain simplifies building LLM apps.&quot;}))<br># [&#39;langchain&#39;, &#39;llm&#39;, &#39;apps&#39;, &#39;framework&#39;, &#39;python&#39;]</pre><p><em>Output:</em></p><pre>[&#39;langchain&#39;, &#39;simplifies&#39;, &#39;building&#39;, &#39;llm&#39;, &#39;apps&#39;]</pre><p>This is how AI steps and deterministic code live side by side in the same pipeline.</p><h3>12. Understanding Runnables</h3><p>You have already been working with Runnables since your very first LangChain example, even if you haven&#39;t explicitly encountered the concept by name.</p><p>In LangChain, <strong>every component follows a common interface called </strong><strong>Runnable</strong>. This includes prompt templates, language models, output parsers, chains, and even custom Python functions wrapped with RunnableLambda.</p><p>This is one of the most important abstractions in the framework. Because all these components share the same interface, <strong>they can be easily connected and combined</strong>, allowing you to build complex workflows from smaller, reusable building blocks. In fact, every chain you create is itself a runnable.</p><p>Every runnable provides a consistent set of core methods that allow you to execute operations, process multiple inputs, stream results, and inspect intermediate outputs:</p><pre>chain.invoke(input)      # run on one input<br>chain.batch(inputs)      # run on a list of inputs<br>chain.stream(input)      # stream the output as it is generated<br>await chain.ainvoke(input)  # async version of invoke</pre><h3>Creating a Chain</h3><p>Let&#39;s create a simple chain that summarizes a text. We will use this chain to explore the different Runnable methods.</p><pre>prompt = ChatPromptTemplate.from_messages([<br>    (&quot;system&quot;, &quot;You are a concise summarization assistant.&quot;),<br>    (&quot;human&quot;, &quot;Summarize the following text in one sentence:\n\n{text}&quot;),<br>])<br><br>chain = prompt | llm | StrOutputParser()</pre><p><strong>invoke: one input, one output</strong></p><pre>chain.invoke({&quot;text&quot;: &quot;Hello world&quot;})</pre><p><em>Output:</em></p><pre>&#39;A simple greeting to the world.&#39;</pre><p><strong>batch: many inputs, processed in parallel</strong></p><pre>texts = [<br>    {&quot;text&quot;: &quot;LangChain is a framework for building LLM apps.&quot;},<br>    {&quot;text&quot;: &quot;LangGraph lets you build stateful, graph-based agents.&quot;},<br>    {&quot;text&quot;: &quot;LangSmith helps you trace and debug your chains.&quot;},<br>]<br><br>summaries = chain.batch(texts, config={&quot;max_concurrency&quot;: 3})<br><br>for s in summaries:<br>    print(&quot;-&quot;, s)</pre><p><em>Output:</em></p><pre>- LangChain is a framework designed for developing applications using large language models (LLMs).<br>- LangGraph enables the creation of stateful agents that utilize graph-based structures.<br>- LangSmith assists in tracking and troubleshooting your workflows.</pre><p>The batch() method allows you to process multiple inputs in a single call. By default, LangChain can execute these inputs concurrently, while the max_concurrency parameter lets you control the maximum number of simultaneous executions. This is particularly useful for managing performance and staying within your model provider&#39;s rate limits.</p><p><strong>stream(): Display Output as It Is Generated</strong></p><p>The stream() method allows you to receive the model&#39;s response progressively instead of waiting for the entire output to be generated.</p><p>This makes applications feel faster and more responsive because users can start reading the response as it is being generated, rather than waiting for the complete result.</p><pre>for chunk in chain.stream({&quot;text&quot;: &quot;Explain what LangChain is in three sentences.&quot;}):<br>    print(chunk, end=&quot;&quot;, flush=True)</pre><p><em>Output:</em></p><pre>LangChain is a framework designed for developing applications powered by language models. It provides tools and components to facilitate the integration of language models with various data sources and APIs. By streamlining the process of building language-based applications, LangChain enhances the capabilities of developers in leveraging AI-driven text generation and processing.</pre><p><strong>ainvoke: async execution</strong></p><p>In async applications (such as a FastAPI backend, covered in Article 7).</p><p>This unified interface is why LangChain components are so easy to connect, test, and run in parallel. It is also the same interface that LangGraph relies on.</p><h3>13. Swapping Models Without Rewriting Your Code</h3><p>Because all chat models follow the same Runnable interface, you can switch between different providers with minimal code changes.</p><p>LangChain provides a provider-agnostic helper called init_chat_model, which makes it easier to initialize and switch between language models.</p><pre>from langchain.chat_models import init_chat_model<br><br>llm = init_chat_model(&quot;gpt-4o-mini&quot;, model_provider=&quot;openai&quot;, temperature=0)<br><br># Later, switch providers by changing one line (after installing the matching<br># integration package, e.g. `pip install langchain-anthropic`):<br># llm = init_chat_model(&quot;claude-sonnet-4-5&quot;, model_provider=&quot;anthropic&quot;, temperature=0)</pre><p>The rest of your chain (prompt, parser, orchestration) remains untouched. This is the practical payoff of standard interfaces: your application logic does not depend on a single vendor.</p><h3>14. Handling Errors and Invalid Inputs</h3><p>LLM calls can fail for several reasons, including network issues, API rate limits, empty inputs, or unexpected responses. Proper error handling helps make your LangChain applications more reliable and robust.</p><h3>Basic Error Handling</h3><p>Let&#39;s see how to use Python&#39;s try and except blocks to catch errors when executing a chain.</p><pre>try:<br>    result = chain.invoke({&quot;text&quot;: &quot;&quot;})<br>    print(result)<br>except Exception as e:<br>    print(&quot;Error:&quot;, e)</pre><p><em>Output:</em></p><pre>Please provide the text you would like me to summarize.</pre><h3>Built-in Retries</h3><p>LangChain provides a built-in with_retry() method for Runnables. It automatically retries an operation when a failure occurs, helping make your chains more resilient to temporary errors such as network issues or API rate limits.</p><pre>reliable_chain = chain.with_retry(stop_after_attempt=3)<br><br>result = reliable_chain.invoke({&quot;text&quot;: &quot;Retry me if something goes wrong.&quot;})</pre><h3>Fallbacks</h3><p>If your primary model fails, fall back to another:</p><pre>primary = ChatOpenAI(model=&quot;gpt-4o-mini&quot;)<br>backup = ChatOpenAI(model=&quot;gpt-4o&quot;)<br><br>resilient_llm = primary.with_fallbacks([backup])<br><br>resilient_chain = prompt | resilient_llm | StrOutputParser()</pre><h3>Validate inputs before calling the model</h3><p>Cheap checks save money and avoid weird outputs:</p><pre>def validate_text(inputs: dict) -&gt; dict:<br>    text = inputs.get(&quot;text&quot;, &quot;&quot;).strip()<br>    if len(text) &lt; 10:<br>        raise ValueError(&quot;Please provide a text with at least 10 characters.&quot;)<br>    return {&quot;text&quot;: text}<br><br>safe_chain = RunnableLambda(validate_text) | chain</pre><p>Retries, fallbacks, and validation are small additions that make the difference between a demo and an application you can rely on.</p><h3>15. Seeing What Happens: Tracing with LangSmith</h3><p>When a chain contains multiple steps, it is important to understand what happens at each stage. <strong>LangSmith</strong> helps you monitor and debug your LangChain applications by recording traces of each execution, including inputs, outputs, latency, and token usage.</p><p>To enable tracing, add the following environment variables to your .env file. You can create a free account at <a href="https://smith.langchain.com/">smith.langchain.com</a> to obtain your API key.</p><pre>LANGSMITH_TRACING=true<br>LANGSMITH_API_KEY=your-langsmith-key<br>LANGSMITH_PROJECT=agentic-ai-series</pre><p>That’s all. No code changes are needed: run any chain and its trace appears in your LangSmith project. You will be able to see exactly what prompt was sent, what the model returned, and how long each step took.</p><p>We will rely heavily on this tool when we debug agents and graphs in the next parts.</p><h3>16. Best Practices</h3><p>Here are some best practices to keep in mind when building LangChain applications:</p><ul><li><strong>Keep prompts short and clear.</strong> Clear instructions help improve the accuracy of model responses.</li><li><strong>Reuse prompt templates.</strong> Use one template with different inputs to simplify maintenance and debugging.</li><li><strong>Chain small, testable steps.</strong> Each component should have one responsibility, making the workflow easier to test and debug.</li><li><strong>Test each component with </strong><strong>.invoke() before chaining.</strong> This helps identify issues early and makes debugging easier.</li><li><strong>Use </strong><strong>temperature=0 for deterministic tasks.</strong> Tasks such as extraction and classification benefit from consistent outputs.</li><li><strong>Prefer structured output over string parsing.</strong> Pydantic schemas provide more reliable and predictable results than regular expressions.</li><li><strong>Pin your package versions.</strong> Using langchain&gt;=1.0,&lt;2.0 helps avoid unexpected breaking changes.</li><li><strong>Never hard-code secrets.</strong> Store API keys in .env files and exclude them from version control.</li><li><strong>Enable tracing from day one.</strong> Use LangSmith early to monitor executions and identify issues.</li></ul><h3>17. Mini Project: A Content Analyzer</h3><p>Let&#39;s put everything together in a small, practical project.</p><p>We will build a content analyzer that takes an article as input and performs the following operations:</p><ol><li><strong>Generate a summary</strong> of the article.</li><li><strong>Extract keywords</strong> to identify its main topics.</li><li><strong>Run both steps in parallel</strong> to reduce execution time.</li><li><strong>Generate a catchy title</strong> using the results of the previous steps.</li></ol><pre># 1. Import libraries and load environment variables<br>from dotenv import load_dotenv<br>from langchain_openai import ChatOpenAI<br>from langchain_core.prompts import ChatPromptTemplate<br>from langchain_core.output_parsers import StrOutputParser<br>from langchain_core.runnables import RunnableParallel, RunnablePassthrough<br><br>load_dotenv()<br><br><br># 2. Initialize the language model and output parser<br>llm = ChatOpenAI(model=&quot;gpt-4o-mini&quot;, temperature=0.3)<br>parser = StrOutputParser()<br><br><br># 3. Define individual chains<br><br># Chain 1: Generate a summary<br>summary_chain = (<br>    ChatPromptTemplate.from_template(<br>        &quot;Summarize the following text in two sentences:\n\n{text}&quot;<br>    )<br>    | llm<br>    | parser<br>)<br><br># Chain 2: Extract keywords<br>keywords_chain = (<br>    ChatPromptTemplate.from_template(<br>        &quot;Extract 5 relevant keywords from the following text. &quot;<br>        &quot;Return them as a comma-separated list:\n\n{text}&quot;<br>    )<br>    | llm<br>    | parser<br>)<br><br># Chain 3: Generate a title<br>title_chain = (<br>    ChatPromptTemplate.from_template(<br>        &quot;Write a catchy title (max 10 words) for an article with this summary &quot;<br>        &quot;and these keywords.\n\nSummary: {summary}\nKeywords: {keywords}\n\n&quot;<br>        &quot;Return only the title.&quot;<br>    )<br>    | llm<br>    | parser<br>)<br><br><br># 4. Build the complete pipeline<br><br># Run summary and keyword extraction in parallel<br># Then generate a title using both results<br>pipeline = (<br>    RunnableParallel(<br>        summary=summary_chain,<br>        keywords=keywords_chain<br>    )<br>    | RunnablePassthrough.assign(title=title_chain)<br>)<br><br><br># 5. Provide the input article<br>article = &quot;&quot;&quot;<br>LangChain is an open-source framework designed to simplify the development of<br>applications powered by large language models. It provides standard interfaces<br>for models, prompts, output parsers and tools, and lets developers compose them<br>into reusable pipelines. Built on top of it, LangGraph adds support for stateful,<br>graph-based workflows, which makes it possible to build agents that can loop,<br>branch, and collaborate.<br>&quot;&quot;&quot;<br><br><br># 6. Execute the pipeline<br>result = pipeline.invoke({&quot;text&quot;: article})<br><br><br># 7. Display the results<br>print(&quot;TITLE   :&quot;, result[&quot;title&quot;])<br>print(&quot;SUMMARY :&quot;, result[&quot;summary&quot;])<br>print(&quot;KEYWORDS:&quot;, result[&quot;keywords&quot;])</pre><p><em>Output:</em></p><pre>TITLE   : &quot;Revolutionize AI Development with LangChain and LangGraph Workflows&quot;<br>SUMMARY : LangChain is an open-source framework that streamlines the development of applications using large language models by offering standard interfaces for various components, enabling the creation of reusable pipelines. LangGraph enhances this framework by introducing stateful, graph-based workflows, allowing for the creation of agents capable of looping, branching, and collaborating.<br>KEYWORDS: LangChain, open-source, large language models, pipelines, workflows</pre><p>Look at how little code is needed and how clearly the data flows through the pipeline:</p><ul><li><strong>RunnableParallel</strong> executes independent steps concurrently.</li><li><strong>RunnablePassthrough.assign()</strong> preserves the previous results and adds the generated title.</li><li><strong>Each chain</strong> is small, independently testable, and easy to replace.</li></ul><p>Try extending the pipeline. Add a sentiment branch, convert the keywords into a Python list using RunnableLambda, or replace the output with a Pydantic model. This is how real-world applications evolve: by combining small, reusable components.</p><h3>Summary</h3><ul><li><strong>LLM applications</strong> combine models, logic, data, and tools. A model alone is not a complete application.</li><li><strong>LangChain</strong> provides the building blocks (models, prompts, parsers, and chains), while <strong>LangGraph</strong> manages the control flow between them.</li><li><strong>Prompt templates</strong> provide reusable prompt structures, and the <strong>pipe operator (</strong><strong>|)</strong> connects components into chains using LCEL.</li><li>Every component is a <strong>Runnable</strong>, sharing a common interface: invoke, batch, stream, and ainvoke.</li><li>Use <strong>structured output</strong> instead of manual string parsing, <strong>RunnableParallel</strong> for independent tasks, and <strong>RunnableLambda</strong> for custom Python logic.</li><li><strong>Retries, fallbacks, and validation</strong> improve reliability, while <strong>LangSmith</strong> helps monitor and debug your applications.</li></ul><h3>What’s Next?</h3><p>In <strong>Article 2 — Memory &amp; Vector Databases</strong>, we’ll explore:</p><ul><li>Conversational memory: how to make applications remember previous interactions.</li><li>Embeddings: how to transform text into numerical representations.</li><li>Vector databases: how to store and retrieve information efficiently.</li><li>Retrieval-Augmented Generation (RAG): how to build applications that answer questions using external documents.</li></ul><p>By the end, you’ll know how to build AI applications that remember conversations and answer questions about your own documents.</p><p>The complete code for this article is available on GitHub: <a href="https://github.com/Just1919/building-ai-agents-langchain-langgraph">https://github.com/Just1919/building-ai-agents-langchain-langgraph</a></p><p>If you found this article useful, feel free to <strong>follow me on Medium</strong> for the next article in this series!</p><p>You can also connect with me:</p><ul><li><strong>LinkedIn:</strong> <a href="https://linkedin.com/in/yawo-justin-djakas">https://linkedin.com/in/yawo-justin-djakas</a></li><li><strong>Medium:</strong> <a href="https://medium.com/@justindjakas">https://medium.com/@justindjakas</a></li><li><strong>GitHub:</strong> <a href="https://github.com/Just1919">https://github.com/Just1919</a></li></ul><p>#AI #LangChain #LangGraph #AIAgents #GenerativeAI</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=be69e48dbe6c" width="1" height="1" alt=""><hr><p><a href="https://blog.gopenai.com/building-ai-agents-with-langchain-langgraph-be69e48dbe6c">Building AI Agents with LangChain &amp; LangGraph</a> was originally published in <a href="https://blog.gopenai.com/">GoPenAI</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building AI Agents with LangChain & LangGraph: A 7-Part Technical Series]]></title>
            <link>https://blog.gopenai.com/building-ai-agents-with-langchain-langgraph-a-7-part-technical-series-e72826c3271f?source=rss-66abbb206fb4------2</link>
            <guid isPermaLink="false">https://medium.com/p/e72826c3271f</guid>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[agentic-rag]]></category>
            <category><![CDATA[ai-agent]]></category>
            <category><![CDATA[ai]]></category>
            <dc:creator><![CDATA[Justin DJAKAS]]></dc:creator>
            <pubDate>Thu, 15 Oct 2026 07:57:46 GMT</pubDate>
            <atom:updated>2026-10-15T11:35:10.522Z</atom:updated>
            <content:encoded><![CDATA[<p><em>From LLM fundamentals to production-ready multi-agent systems.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mp0xwLB9JEGK821Byxzjxw.png" /></figure><p>Artificial intelligence is evolving rapidly. We’re moving beyond simple interactions with Large Language Models (LLMs) toward autonomous agents capable of using tools, managing state, collaborating with other agents, and executing complex workflows.</p><p>But how do we build these systems in practice?</p><p>I’m launching a new <strong>7-part technical article series</strong> dedicated to building AI agents with <strong>LangChain and LangGraph</strong> — exploring the concepts, implementation patterns, and engineering practices needed to build practical AI applications.</p><h3>What will this series cover?</h3><p>Throughout these seven articles, we’ll progressively explore the foundations, architectures, and production considerations behind modern AI agent systems.</p><p><strong>01 — Introduction &amp; LangChain Fundamentals</strong></p><p>Understanding LLM components, prompt templates, chains, and runnables.</p><p><strong>02 — Memory &amp; Vector Databases</strong></p><p>Exploring conversational memory, embeddings, vector databases, and Retrieval-Augmented Generation (RAG).</p><p><strong>03 — AI Agents &amp; Tools</strong></p><p>Building agents that use custom tools, perform function calling, and interact with external capabilities.</p><p><strong>04 — LangGraph Workflows</strong></p><p>Understanding state, nodes, edges, conditional logic, and the differences between LangChain and LangGraph.</p><p><strong>05 — Multi-Agent Systems &amp; Real-World Applications</strong></p><p>Designing collaborative systems with specialized agents such as researchers, writers, and reviewers.</p><p><strong>06 — Memory, Feedback &amp; Human-in-the-Loop</strong></p><p>Implementing persistent state, feedback loops, and human oversight for more reliable agent workflows.</p><p><strong>07 — Production, Integration &amp; Optimization</strong></p><p>Exploring deployment and integration with FastAPI, Gradio, Docker, databases, and external services, alongside debugging and security considerations.</p><h3>What can you expect?</h3><p>This series is designed to be practical and progressive.</p><p>Each article will focus on technical explanations, implementation examples, architectural decisions, and lessons that can help developers move from experimentation to building more robust AI applications.</p><p>Whether you’re a Data Scientist , a Python developer, an AI engineer, or simply exploring the world of AI agents, this series will provide a structured path through the key concepts and tools.</p><h3>Coming soon on Medium</h3><p>The first article will introduce the fundamentals of LangChain and establish the foundation for the rest of the series.</p><p>The following articles will be published progressively in the coming days.</p><p><strong>Follow me on Medium</strong> to stay updated, explore each new installment, and follow the journey from fundamental concepts to production-ready AI agent systems.</p><p>Let’s build something useful with AI.</p><p><em>Connect with me:</em></p><ul><li><strong>LinkedIn:</strong> <a href="https://www.linkedin.com/in/yawo-justin-djakas/">Justin D</a>JAKAS</li><li><strong>Medium:</strong> <a href="https://medium.com/@justindjakas">Justin Djakas</a></li><li><strong>GitHub:</strong> <a href="https://github.com/Just1919">Just1919</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e72826c3271f" width="1" height="1" alt=""><hr><p><a href="https://blog.gopenai.com/building-ai-agents-with-langchain-langgraph-a-7-part-technical-series-e72826c3271f">Building AI Agents with LangChain &amp; LangGraph: A 7-Part Technical Series</a> was originally published in <a href="https://blog.gopenai.com/">GoPenAI</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building a Plan-and-Execute AI Agent for Analysis Report Generation: A Step-by-Step Guide]]></title>
            <link>https://blog.gopenai.com/building-a-plan-and-execute-ai-agent-for-analysis-report-generation-a-step-by-step-guide-6cd6078d3e7d?source=rss-66abbb206fb4------2</link>
            <guid isPermaLink="false">https://medium.com/p/6cd6078d3e7d</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[agentic-ai]]></category>
            <category><![CDATA[ai-agent]]></category>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[rags]]></category>
            <dc:creator><![CDATA[Justin DJAKAS]]></dc:creator>
            <pubDate>Fri, 07 Aug 2026 13:52:48 GMT</pubDate>
            <atom:updated>2026-08-13T11:17:12.045Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qRMtfs4GqHRPg9WwSZRpqw.png" /></figure><p>Learn how to design AI agents that reason, retrieve information, and automate complex analysis workflows.</p><p>Large Language Models (LLMs) are powerful at generating text, but many real-world AI applications require more than a simple prompt-response interaction. Enterprise use cases such as research assistants, document intelligence, automated reporting, and knowledge management often require an AI system to plan tasks, retrieve information from external knowledge bases, use specialized tools, and synthesize the results into meaningful insights.</p><p>In this project, we will build a <strong>production-ready Plan-and-Execute AI agent</strong> using <strong>LangGraph</strong>, <strong>LangChain</strong>, <strong>OpenAI</strong>, and <strong>Deep Lake</strong>. Rather than answering a query directly, the agent first analyzes the user’s request, creates a structured execution plan, retrieves relevant information from a vector database, executes each step using the appropriate tools, and finally generates a comprehensive analysis report.</p><p>The agent follows a modular architecture composed of two key components:</p><ul><li><strong>Planner</strong> — analyzes the user’s request and decomposes it into a sequence of clear and actionable steps.</li><li><strong>Executor</strong> — carries out each step by selecting and using the appropriate tools, such as a document retrieval system backed by Deep Lake.</li></ul><p>This separation of planning and execution makes the workflow more reliable, modular, and easier to extend with additional tools and capabilities, making it well suited for enterprise AI applications.</p><p>The complete workflow is illustrated below:</p><pre>                User Query<br>                      │<br>                      ▼<br>                 ┌─────────┐<br>                 │ Planner │<br>                 └────┬────┘<br>                      │<br>              Structured Plan<br>                      │<br>                      ▼<br>                ┌──────────┐<br>                │ Executor │<br>                └────┬─────┘<br>                     │<br>                     ▼<br>          Search Private Docs<br>                     │<br>                     ▼<br>                 Deep Lake<br>                     │<br>                     ▼<br>          Relevant Documents<br>                     │<br>                     ▼<br>              LLM Synthesis<br>                     │<br>                     ▼<br>              Final Analysis Report</pre><p>Throughout this guide, we will implement each component step by step using the latest LangChain ecosystem. By the end of the tutorial, you will have built a modular agentic workflow that can serve as the foundation for enterprise applications such as intelligent search, document analysis, research automation, and AI-powered reporting.</p><h3>Environment Setup</h3><p>Before we begin, we need to configure the required API credentials as environment variables:</p><ul><li><strong>OpenAI API Key</strong> — used to access the language model.</li><li><strong>Activeloop API Key</strong> — used to authenticate with Deep Lake and manage the vector database.</li></ul><p>For security and portability, these credentials are loaded from environment variables instead of being hard-coded in the notebook. This approach follows best practices for developing production-ready AI applications.</p><h3>Collecting AI Articles</h3><p>Before building the retrieval system, we first need to create a knowledge base for our agent.</p><p>In this step, we collect a set of publicly available <strong>AI-related articles</strong> covering topics such as artificial intelligence, regulation, and industry developments. We use the requests library to retrieve web pages and newspaper3k to extract the article content and metadata.</p><p>The extracted documents are stored together with their source URLs and will later be processed, embedded, and uploaded to <strong>Deep Lake</strong>, which will serve as the vector database powering our agent’s retrieval capabilities.</p><pre># Install the newspaper3k package for web article scraping and text extraction<br>!pip -q install -U newspaper3k</pre><pre>import time<br>import requests<br>from newspaper import Article<br><br># Define HTTP headers to simulate a standard web browser<br>headers = {<br>    &quot;User-Agent&quot;: (<br>        &quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) &quot;<br>        &quot;AppleWebKit/537.36 (KHTML, like Gecko) &quot;<br>        &quot;Chrome/89.0.4389.82 Safari/537.36<br>    )<br>}<br><br># List of AI news articles that will populate the knowledge base<br><br>article_urls = [<br><br># EU AI Act and AI regulation<br>&quot;https://digital-strategy.ec.europa.eu/en/policies/regulatory-framework-ai&quot;,<br><br># NIST AI standards and governance<br>&quot;https://www.nist.gov/artificial-intelligence&quot;,<br><br># OECD AI Principles<br>&quot;https://oecd.ai/en/ai-principles&quot;,<br><br># Google DeepMind AI safety and research<br>&quot;https://deepmind.google/discover/blog/&quot;,<br><br># Microsoft AI governance<br>&quot;https://www.microsoft.com/en-us/ai/responsible-ai&quot;,<br><br># IBM AI governance<br>&quot;https://www.ibm.com/topics/ai-governance&quot;,<br><br>]<br><br># Create an HTTP session to reuse the connection across requests<br>session = requests.Session()<br><br># Store the extracted articles<br>pages_content = []<br><br>for url in article_urls:<br>    try:<br>        # Respect the target server by limiting the request rate<br>        time.sleep(2)<br><br>        # Request the web page<br>        response = session.get(<br>            url,<br>            headers=headers,<br>            timeout=10<br>        )<br><br>        if response.status_code == 200:<br><br>            # Download and parse the article content<br>            article = Article(url)<br>            article.download()<br>            article.parse()<br><br>            # Store the article URL and extracted text<br>            pages_content.append({<br>                &quot;url&quot;: url,<br>                &quot;text&quot;: article.text<br>            })<br><br>            print(f&quot;Successfully scraped: {url}&quot;)<br><br>        else:<br>            print(f&quot;Failed to fetch {url}: HTTP {response.status_code}&quot;)<br><br>    except Exception as e:<br>        print(f&quot;Error while fetching {url}: {e}&quot;)</pre><pre>Successfully scraped: https://digital-strategy.ec.europa.eu/en/policies/regulatory-framework-ai<br>Successfully scraped: https://www.nist.gov/artificial-intelligence<br>Successfully scraped: https://oecd.ai/en/ai-principles<br>Successfully scraped: https://deepmind.google/discover/blog/<br>Successfully scraped: https://www.microsoft.com/en-us/ai/responsible-ai<br>Successfully scraped: https://www.ibm.com/topics/ai-governance</pre><h3>Installing Dependencies</h3><p>To ensure a stable environment, we first remove potentially conflicting packages and install a compatible set of dependencies.</p><p>The project relies on the LangChain ecosystem, LangGraph for agent orchestration, OpenAI models for embeddings and reasoning capabilities, and Deep Lake as the vector database.</p><p>Using fixed package versions helps avoid compatibility issues and ensures reproducibility of the workflow.</p><pre><br> # Remove potentially conflicting packages to ensure a clean environment<br>!pip uninstall -q -y \<br>    langchain \<br>    langchain-core \<br>    langchain-community \<br>    langchain-text-splitters \<br>    langchain-openai \<br>    langchain-deeplake \<br>    deeplake \<br>    langgraph \<br>    langgraph-sdk \<br>    langgraph-prebuilt \<br>    openai<br><br><br># Install compatible versions of the LangChain ecosystem and required dependencies<br>!pip install -q \<br>    &quot;langchain==0.3.25&quot; \<br>    &quot;langchain-core==0.3.65&quot; \<br>    &quot;langchain-openai==0.3.16&quot; \<br>    &quot;langchain-community==0.3.23&quot; \<br>    &quot;langchain-text-splitters==0.3.8&quot; \<br>    &quot;langgraph==0.2.60&quot; \<br>    &quot;langgraph-sdk==0.1.48&quot; \<br>    &quot;langgraph-prebuilt==0.1.8&quot; \<br>    &quot;langchain-deeplake&quot; \<br>    &quot;deeplake&quot; \<br>    &quot;tiktoken&quot; \<br>    &quot;openai&gt;=1.68.0,&lt;2.0.0&quot;</pre><h3>Creating the Embedding Model and Vector Database</h3><p>Before storing our documents, we need to transform their textual content into numerical representations called <strong>embeddings</strong>. These embeddings capture the semantic meaning of the documents and enable similarity-based search.</p><p>We use the <strong>OpenAIEmbeddings</strong> model to generate vector representations and <strong>Deep Lake</strong> as the vector database to store both the document chunks and their corresponding embeddings.</p><p>The Deep Lake vector store is initialized with a dataset path and the embedding model. This configuration allows the system to automatically generate embeddings when documents are added and enables efficient retrieval of relevant information during the agent execution workflow.</p><pre># Store the documents and their embeddings in Deep Lake<br><br>from langchain_openai import OpenAIEmbeddings<br><br># Make sure your OpenAI API key is available in the OPENAI_API_KEY environment variable<br><br># Initialize the embedding model<br>embeddings = OpenAIEmbeddings(<br>    model=&quot;text-embedding-3-small&quot;<br>)</pre><pre>from langchain_deeplake import DeeplakeVectorStore<br><br># Define Deep Lake dataset path<br># Replace with your Activeloop organization ID<br>my_activeloop_org_id = &quot;&lt;YOUR-ACTIVELOOP-ORG-ID&gt;&quot;<br><br>my_activeloop_dataset_name = &quot;langchain_project_analysis_outline&quot;<br><br>dataset_path = (<br>    f&quot;hub://{my_activeloop_org_id}/{my_activeloop_dataset_name}&quot;<br>)<br><br># Initialize the Deep Lake vector store<br># Documents and their embeddings will be stored in this dataset<br>db = DeeplakeVectorStore(<br>    dataset_path=dataset_path,<br>    embedding_function=embeddings<br>)</pre><h3>Splitting Documents into Chunks</h3><p>Before storing the articles in the vector database, we split their content into smaller chunks. This step improves document processing and enables more efficient retrieval during the RAG workflow.</p><p>We use RecursiveCharacterTextSplitter with a chunk size of <strong>1,000 characters</strong> and an overlap of <strong>100 characters</strong> between consecutive chunks. The overlap helps preserve context between chunks and reduces information loss.</p><p>The generated chunks are then added to the <strong>Deep Lake vector store</strong>, making them available for the retrieval component of our AI agent.</p><pre>from langchain_text_splitters import RecursiveCharacterTextSplitter<br><br><br># Initialize the text splitter to create smaller document chunks<br>text_splitter = RecursiveCharacterTextSplitter(<br>    chunk_size=1000,<br>    chunk_overlap=100<br>)<br><br><br># Store all generated chunks<br>all_texts = []<br><br><br># Split each document into smaller chunks<br>for document in pages_content:<br>    chunks = text_splitter.split_text(document[&quot;text&quot;])<br>    all_texts.extend(chunks)<br><br><br># Display the total number of generated chunks<br>print(f&quot;Number of chunks: {len(all_texts)}&quot;)</pre><pre>Number of chunks: 11</pre><h3>Storing Document Chunks in Deep Lake</h3><p>After splitting the documents into smaller chunks, we store them in the <strong>Deep Lake vector store</strong>.</p><p>During this step, each chunk is converted into an embedding using the configured embedding model and stored together with its vector representation. These embeddings will later enable semantic search when the agent retrieves relevant information.</p><pre># Add document chunks to the Deep Lake vector store<br>db.add_texts(all_texts)</pre><p><em>The</em> Deep Lake dataset setup is complete.</p><p>Next, we build the <strong>Plan-and-Execute agent</strong> using our knowledge base. We create a <strong>retriever</strong> from the Deep Lake vector store and define a custom <strong>retrieval tool</strong> to fetch the most relevant documents based on the user’s query.</p><pre># Create the retriever and configure it to return the 3 most relevant documents<br>retriever = db.as_retriever(<br>    search_kwargs={&quot;k&quot;: 3}<br>)<br><br># Separator used to combine retrieved documents<br>CUSTOM_TOOL_DOCS_SEPARATOR = &quot;\n---------------\n&quot;<br><br><br># Custom retrieval tool<br>def retrieve_n_docs_tool(query: str) -&gt; str:<br>    &quot;&quot;&quot;<br>    Retrieve the most relevant documents for a given query<br>    and combine their contents into a single string.<br>    &quot;&quot;&quot;<br>    docs = retriever.invoke(query)<br><br>    texts = [doc.page_content for doc in docs]<br><br>    texts_merged = (<br>        &quot;---------------\n&quot;<br>        + CUSTOM_TOOL_DOCS_SEPARATOR.join(texts)<br>        + &quot;\n---------------&quot;<br>    )<br><br>    return texts_merged</pre><p>First, we configure the <strong>retriever</strong> to return the three most relevant documents from Deep Lake for each query.</p><p>We then create a custom retrieve_n_docs_tool function that retrieves relevant documents and combines their content into a single context. This tool enables the <strong>Plan-and-Execute agent</strong> to access and analyze relevant information during execution.</p><pre>from langchain_core.tools import Tool<br># We create the tool that uses the &quot;retrieve_n_docs_tool&quot; function<br>tools = [<br>    Tool(<br>        name=&quot;Search Private Docs&quot;,<br>        func=retrieve_n_docs_tool,<br>        description=&quot;&quot;&quot;useful for when you need to answer questions about<br>current events about Artificial Intelligence&quot;&quot;&quot;<br>    )<br>]</pre><p>The <strong>“Search Private Docs”</strong> tool uses the retrieve_n_docs_tool function to search and retrieve relevant documents from Deep Lake. It is particularly useful for gathering information and insights from the document repository to support the agent’s analysis.</p><h3>Initialize the Language Model</h3><p>We use an OpenAI chat model as the reasoning engine for both the <strong>Planner</strong> and the <strong>Executor</strong> components.</p><p>The temperature is set to 0 to obtain more deterministic outputs, which is important for reliable and structured agent workflows.</p><pre>from langchain_openai import ChatOpenAI<br><br>model = ChatOpenAI(<br>    model=&quot;gpt-4o-mini&quot;,<br>    temperature=0<br>)</pre><h3>Define the Planning Schema</h3><p>The planner should generate a structured output instead of free-form text. We use <strong>Pydantic</strong> to define the expected format of the plan.</p><p>Each plan contains a sequence of actionable steps that the executor can interpret and execute.</p><pre><br>from typing import List<br>from pydantic import BaseModel, Field<br><br><br>class Plan(BaseModel):<br>    &quot;&quot;&quot;Structured execution plan generated by the planning model.&quot;&quot;&quot;<br><br>    steps: List[str] = Field(<br>        description=(<br>            &quot;A list of clear and actionable steps required &quot;<br>            &quot;to answer the user&#39;s request.&quot;<br>        )<br>    )</pre><h3>Build the Planner</h3><p>The <strong>Planner</strong> analyzes the user’s request and transforms it into a structured sequence of actionable steps.</p><p>By generating a predefined plan format, the workflow can reliably pass tasks to the executor for further processing.</p><pre>planner = model.with_structured_output(Plan)</pre><h3>Generate an Execution Plan</h3><p>We provide the Planner with a user request and let it analyze the task complexity.</p><p>The model then generates a structured sequence of steps that will guide the Executor during the execution phase.</p><pre>Generated plan:<br>1. Define the scope of the research: Focus on AI regulations from the last 2-3 years, including key regions such as the EU, US, and Asia.<br>2. Identify key sources of information: Look for government publications, legal journals, news articles, and reports from reputable organizations like OECD, IEEE, and others.<br>3. Search for recent legislation: Use legal databases and government websites to find new laws or proposed regulations related to AI.<br>4. Review academic literature: Search for recent studies or papers discussing AI regulations in journals and conferences.<br>5. Analyze case studies: Look for specific examples of AI regulations being implemented or challenged in courts.<br>6. Summarize findings: Create a structured overview that includes key regulations, their objectives, and implications for stakeholders.<br>7. Stay updated: Set up alerts for new developments in AI regulations to keep the overview current.</pre><h3>Document Retrieval Tool</h3><p>The retrieval tool gives the <strong>Executor</strong> access to the knowledge base stored in <strong>Deep Lake</strong>.</p><p>For each query, it retrieves the most relevant document chunks and provides their content as context for the agent’s reasoning and report generation.</p><pre>from langchain_core.tools import Tool<br><br>search_tool = Tool(<br>    name=&quot;Search_Private_Docs&quot;,<br>    func=retrieve_n_docs_tool,<br>    description=(<br>        &quot;Search the Deep Lake knowledge base for relevant documents. &quot;<br>        &quot;Use this tool when information must be retrieved from the &quot;<br>        &quot;document repository.&quot;<br>    ),<br>)<br><br>tools = [search_tool]</pre><h3>Build the Executor</h3><p>The executor is responsible for carrying out the steps generated by the planner.</p><p>Instead of hard-coding which tool should be used for each step, we allow the language model to select the appropriate tool dynamically. This makes the workflow easier to extend when additional tools are introduced.</p><pre>from langchain_core.messages import HumanMessage, SystemMessage<br><br>executor_model = model.bind_tools(tools)</pre><h3>Define the Agent State</h3><p>LangGraph uses a shared state to pass information between workflow nodes.</p><p>The state contains the original user query, the generated plan, the execution results, and the final report. This provides a clear and traceable flow of information throughout the agent.</p><pre>from typing import TypedDict, List<br><br><br># Define the shared state used across the agent workflow<br>class AgentState(TypedDict):<br>    query: str              # User request<br>    plan: List[str]         # Steps generated by the planner<br>    current_step: int       # Current step being executed<br>    results: List[str]      # Retrieved information<br>    final_answer: str       # Generated final response</pre><h3>Planner Node</h3><p>The planner node receives the user’s query and generates a structured execution plan.</p><p>The plan is stored in the shared state so that the executor can process each step sequentially.</p><pre>def planner_node(state: AgentState):<br>    plan = planner.invoke(<br>        f&quot;&quot;&quot;<br>Create a step-by-step research plan to answer the following request:<br><br>{state[&quot;query&quot;]}<br>&quot;&quot;&quot;<br>    )<br><br>    return {<br>        &quot;plan&quot;: plan.steps,<br>        &quot;current_step&quot;: 0,<br>        &quot;results&quot;: [],<br>        &quot;final_answer&quot;: &quot;&quot;<br>    }</pre><h3>Executor Node</h3><p>The executor processes the current step of the plan.</p><p>The language model determines whether a tool is required to complete the step. When necessary, it can call the document retrieval tool to obtain relevant information from Deep Lake</p><pre>def executor_node(state: AgentState):<br>    current_step = state[&quot;current_step&quot;]<br>    step = state[&quot;plan&quot;][current_step]<br><br>    messages = [<br>        SystemMessage(<br>            content=(<br>                &quot;You are an execution agent. &quot;<br>                &quot;Execute the given task using the available tools when necessary. &quot;<br>                &quot;Use retrieved information to support your reasoning.&quot;<br>            )<br>        ),<br>        HumanMessage(content=step)<br>    ]<br><br>    response = executor_model.invoke(messages)<br><br>    return {<br>        &quot;results&quot;: state[&quot;results&quot;] + [response.content],<br>        &quot;current_step&quot;: current_step + 1<br>    }</pre><h3>Control the Execution Flow</h3><p>After each execution step, the workflow checks whether additional steps remain in the plan.</p><p>If more steps are available, the workflow returns to the executor. Otherwise, the process moves to the final report generation stage.</p><pre>def route_after_execution(state: AgentState):<br>    # Continue execution if there are remaining steps in the plan<br>    if state[&quot;current_step&quot;] &lt; len(state[&quot;plan&quot;]):<br>        return &quot;executor&quot;<br><br>    # Generate the final report when all steps are completed<br>    return &quot;report&quot;</pre><h3>Generate the Final Analysis Report</h3><p>Once all planned steps have been executed, the collected information is passed to the language model.</p><p>The model synthesizes the retrieved information into a structured analysis report while keeping the original user request as the main objective.</p><pre>def report_node(state: AgentState):<br>    context = &quot;\n\n&quot;.join(<br>        f&quot;Step {i + 1}:\n{result}&quot;<br>        for i, result in enumerate(state[&quot;results&quot;])<br>    )<br><br>    prompt = f&quot;&quot;&quot;<br>You are a professional research analyst.<br><br>User request:<br>{state[&quot;query&quot;]}<br><br>Execution results:<br>{context}<br><br>Produce a clear and structured analysis report based on the<br>information collected during the execution process.<br><br>Organize the report logically and avoid introducing unsupported<br>information.<br>&quot;&quot;&quot;<br><br>    response = model.invoke(<br>        [HumanMessage(content=prompt)]<br>    )<br><br>    return {<br>        &quot;final_answer&quot;: response.content<br>    }</pre><h3>Build the Plan-and-Execute Workflow</h3><p>We now connect the planner, executor, and reporting components into a LangGraph workflow.</p><p>The graph provides explicit control over the execution process and makes the agent architecture easier to monitor, test, and extend.</p><pre>from langgraph.graph import StateGraph, END<br><br><br>workflow = StateGraph(AgentState)<br><br># Add workflow nodes<br>workflow.add_node(&quot;planner&quot;, planner_node)<br>workflow.add_node(&quot;executor&quot;, executor_node)<br>workflow.add_node(&quot;report&quot;, report_node)<br><br># Define the entry point<br>workflow.set_entry_point(&quot;planner&quot;)<br><br># Planner → Executor<br>workflow.add_edge(&quot;planner&quot;, &quot;executor&quot;)<br><br># Executor → Executor or Report<br>workflow.add_conditional_edges(<br>    &quot;executor&quot;,<br>    route_after_execution,<br>    {<br>        &quot;executor&quot;: &quot;executor&quot;,<br>        &quot;report&quot;: &quot;report&quot;<br>    }<br>)<br><br># Report → End<br>workflow.add_edge(&quot;report&quot;, END)<br><br># Compile the graph<br>agent = workflow.compile()</pre><h3>Run the Plan-and-Execute Agent</h3><p>The workflow is now ready to process a research request.</p><p>The agent first generates a plan, executes each step, retrieves relevant information when required, and finally synthesizes the collected information into an analysis report.</p><pre># Define the user&#39;s request<br>query = &quot;&quot;&quot;<br>Provide an overview of recent developments in AI regulations.<br>&quot;&quot;&quot;<br><br><br># Execute the Plan-and-Execute agent workflow<br>result = agent.invoke({<br>    &quot;query&quot;: query,<br>    &quot;plan&quot;: [],<br>    &quot;current_step&quot;: 0,<br>    &quot;results&quot;: [],<br>    &quot;final_answer&quot;: &quot;&quot;<br>})<br><br><br># Display the generated report<br>print(result[&quot;final_answer&quot;])</pre><pre># Overview of Recent Developments in AI Regulations<br><br>## Introduction<br>The rapid advancement of artificial intelligence (AI) technologies has prompted governments and regulatory bodies worldwide to establish frameworks aimed at ensuring the ethical and responsible use of AI. This report provides an overview of recent developments in AI regulations, highlighting key initiatives, legislative proposals, and international collaborations.<br><br>## 1. European Union AI Act<br>The European Union (EU) is at the forefront of AI regulation with its proposed AI Act, which aims to create a comprehensive legal framework for AI technologies. Key features include:<br>- **Risk-Based Classification**: AI systems are categorized into four risk levels: unacceptable, high, limited, and minimal risk. Unacceptable risk systems are banned, while high-risk systems face stringent requirements.<br>- **Transparency Requirements**: Developers of high-risk AI systems must provide clear documentation and ensure transparency in their algorithms.<br>- **Compliance and Enforcement**: The Act proposes the establishment of national supervisory authorities to oversee compliance and enforce regulations.<br><br>## 2. United States Initiatives<br>In the U.S., AI regulation is evolving through various initiatives:<br>- **Executive Orders**: The Biden administration has issued executive orders focusing on promoting trustworthy AI, emphasizing the need for ethical guidelines and public engagement.<br>- **Legislative Proposals**: Several bills have been introduced in Congress aimed at addressing AI safety, privacy, and accountability, including the Algorithmic Accountability Act, which seeks to require companies to assess the impact of their algorithms.<br>- **Federal Agency Guidelines**: Agencies like the National Institute of Standards and Technology (NIST) are developing frameworks for AI risk management and best practices.<br><br>## 3. Global Collaborations<br>Internationally, there is a growing trend towards collaboration on AI regulations:<br>- **OECD AI Principles**: The Organization for Economic Cooperation and Development (OECD) has established principles for AI that promote responsible stewardship and international cooperation.<br>- **G7 and G20 Discussions**: These forums have initiated discussions on AI governance, focusing on ethical considerations and the need for a coordinated global approach to regulation.<br><br>## 4. National Regulations<br>Several countries have begun implementing their own AI regulations:<br>- **China**: The Chinese government has introduced regulations that require AI developers to ensure their systems align with socialist values and national security interests.<br>- **United Kingdom**: The UK has proposed a regulatory framework that emphasizes innovation while ensuring safety and accountability in AI deployment.<br><br>## 5. Ethical Considerations<br>Ethical considerations are central to AI regulation:<br>- **Bias and Fairness**: Regulators are increasingly focusing on addressing bias in AI systems to ensure fairness and prevent discrimination.<br>- **Privacy and Data Protection**: Regulations are being developed to safeguard personal data used in AI training and deployment, aligning with existing frameworks like the General Data Protection Regulation (GDPR).<br><br>## Conclusion<br>The landscape of AI regulation is rapidly evolving, with significant developments occurring at both national and international levels. As AI technologies continue to advance, the need for comprehensive and adaptive regulatory frameworks will be crucial in ensuring their ethical and responsible use. Stakeholders, including governments, industry leaders, and civil society, must collaborate to create regulations that balance innovation with public safety and ethical considerations.<br><br>## Recommendations<br>- **Engagement with Stakeholders**: Regulators should engage with industry experts, researchers, and the public to develop informed and effective regulations.<br>- **Continuous Monitoring**: As AI technologies evolve, regulations should be regularly reviewed and updated to address emerging challenges and opportunities.<br>- **International Cooperation**: Countries should work together to harmonize AI regulations to facilitate global trade and innovation while ensuring ethical standards are met.</pre><p>In this project, we built a <strong>Plan-and-Execute agent</strong> capable of decomposing complex queries, retrieving relevant information from a Deep Lake knowledge base, and generating structured analysis reports.</p><p>By combining <strong>LangChain, LangGraph, OpenAI, and Deep Lake</strong>, we designed a modular <strong>agentic AI workflow</strong> where planning, execution, retrieval, and generation are clearly separated.</p><p>This architecture provides a strong foundation for enterprise applications such as research assistants, internal knowledge systems, and automated reporting solutions.</p><p>The complete implementation is available on my <strong>GitHub repository</strong>:<br><a href="https://github.com/Just1919/plan-and-execute-ai-agent">https://github.com/Just1919/plan-and-execute-ai-agent</a></p><p>Feel free to connect with me on <strong>LinkedIn</strong>:<br><a href="https://www.linkedin.com/in/yawo-justin-djakas/">https://www.linkedin.com/in/yawo-justin-djakas/</a></p><p>Follow me for upcoming articles about <strong>AI agents, RAG systems, Generative AI, and applied Machine Learning</strong>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6cd6078d3e7d" width="1" height="1" alt=""><hr><p><a href="https://blog.gopenai.com/building-a-plan-and-execute-ai-agent-for-analysis-report-generation-a-step-by-step-guide-6cd6078d3e7d">Building a Plan-and-Execute AI Agent for Analysis Report Generation: A Step-by-Step Guide</a> was originally published in <a href="https://blog.gopenai.com/">GoPenAI</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Build Your First RAG-Powered Customer Support Chatbot]]></title>
            <link>https://blog.gopenai.com/build-your-first-rag-powered-customer-support-chatbot-ada7bffdf50e?source=rss-66abbb206fb4------2</link>
            <guid isPermaLink="false">https://medium.com/p/ada7bffdf50e</guid>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[chatbots]]></category>
            <category><![CDATA[vector-embeddings]]></category>
            <category><![CDATA[vector-database]]></category>
            <category><![CDATA[rags]]></category>
            <dc:creator><![CDATA[Justin DJAKAS]]></dc:creator>
            <pubDate>Fri, 15 May 2026 07:53:36 GMT</pubDate>
            <atom:updated>2026-05-15T10:58:22.106Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QN42lojD-2ux1NR0tUXdkA.png" /></figure><p>Fundamentals of RAG and vector databases: building a customer support chatbot using external documents for accurate, context-aware responses</p><p>In this article, we will explore the fundamentals of Retrieval-Augmented Generation (RAG) and vector databases. We will then build a practical Customer Support Q&amp;A chatbot powered by RAG to integrate external documents and generate more accurate and context-aware responses.</p><h3>Retrieval-Augmented Generation</h3><p>Retrieval-Augmented Generation (RAG) is a technique developed to improve the accuracy and reliability of Large Language Models (LLMs) by reducing hallucinations and enhancing contextual understanding. Introduced by Meta’s FAIR team, RAG combines information retrieval with text generation by fetching relevant information from external knowledge sources before the model generates a response.</p><p>Instead of relying solely on the model’s internal training data, RAG enriches prompts with relevant external content such as PDFs, private documents, code repositories, websites, or SQL databases. This enables chatbots and AI assistants to provide more precise, up-to-date, and context-aware answers.</p><p>One of the major advantages of RAG is its ability to reference or cite the sources used to generate responses, which improves transparency and user trust. Additionally, RAG makes it easier to integrate domain-specific or frequently updated information without the need for expensive and complex fine-tuning of the LLM.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-aysWR8Wqcb4gTLh9Dz17A.png" /><figcaption><strong>RAG System Workflow</strong></figcaption></figure><p>The diagram illustrates the overall workflow of a Retrieval-Augmented Generation (RAG) system. It shows how a user’s question is first processed by a retriever, which searches a vector database or knowledge base to find the most relevant information. The retrieved context is then combined with the original question and sent to the Large Language Model (LLM), enabling it to generate more accurate, contextual, and reliable responses.</p><h4><strong>The </strong>Impact of Larger Context Windows</h4><p>Modern LLMs with larger context windows can process much more text at once, which raises an important question: should we provide the entire document set or only the most relevant information?</p><p>Although providing complete documents gives the model access to broader context, it also introduces several drawbacks:</p><ul><li>Higher latency due to the processing of large amounts of text.</li><li>Reduced accuracy when relevant information is buried inside long documents.</li><li>Higher computational costs and inefficient resource usage, especially with large datasets.</li></ul><p>For this reason, retrieving only the most relevant information is often more efficient and effective for RAG systems.</p><p>Libraries such as LangChain and LlamaIndex provide user-friendly tools for building retrieval systems on custom data sources. In this article, we focus on LangChain’s indexing capabilities to create a simple RAG pipeline for uploading, indexing, and querying documents.</p><p>In the next article, we will explore LlamaIndex for more advanced retrieval-augmented generation workflows.</p><h3>LangChain’s Indexes and Retrievers</h3><p>In LangChain, an <strong>index</strong> organizes and stores data for fast and efficient search, while a <strong>retriever</strong> uses this index to find the most relevant documents for a given query.</p><p>Most LangChain indexes rely on <strong>vector databases</strong> and <strong>embeddings</strong> to handle unstructured data efficiently. Retrievers then extract relevant information and pass it to the LLM to generate accurate and context-aware responses.</p><h4>Let’s see how they work with a practical application:</h4><p>Install the required Python packages, then use the TextLoader class to load text files and create LangChain Document objects</p><pre>!pip install langchain deeplake openai tiktoken</pre><pre>from langchain.document_loaders import TextLoader<br><br># Text adapted from general information about Large Language Models (LLMs)<br># inspired by OpenAI, Google DeepMind, and Meta AI documentation<br><br>text = &quot;&quot;&quot;<br>Large Language Models (LLMs) are advanced AI systems trained on massive<br>amounts of text data to understand and generate human language.<br>Models such as GPT, Gemini, and LLaMA can perform a wide range of tasks,<br>including text summarization, translation, question answering, code<br>generation, and conversational assistance.<br><br>LLMs rely on deep learning and transformer architectures to capture<br>context and relationships between words. Their ability to generate<br>coherent and context-aware responses has transformed many industries,<br>including education, healthcare, finance, and customer support.<br><br>Despite their impressive capabilities, LLMs may sometimes generate<br>incorrect or outdated information. Techniques such as Retrieval-Augmented<br>Generation (RAG) help improve their accuracy by connecting them to<br>external and up-to-date knowledge sources.<br>&quot;&quot;&quot;<br><br># Write text to a local file<br>with open(&quot;llm_text.txt&quot;, &quot;w&quot;) as file:<br>    file.write(text)<br><br># Load the text file using TextLoader<br>loader = TextLoader(&quot;llm_text.txt&quot;)<br>docs_from_file = loader.load()<br><br># Print the number of loaded documents<br>print(len(docs_from_file))</pre><pre>#Output<br>1</pre><h4>Use <strong>CharacterTextSplitter</strong> to divide documents into smaller text segments called <strong>chunks</strong>.</h4><p>The <strong>chunk_overlap</strong> parameter defines how many characters are shared between consecutive chunks, helping preserve context and maintain coherence by preventing important information from being split at chunk boundaries.</p><pre>from langchain.text_splitter import CharacterTextSplitter<br># create a text splitter<br>text_splitter = CharacterTextSplitter(chunk_size=200,<br>chunk_overlap=20)<br># split documents into chunks<br>docs = text_splitter.split_documents(docs_from_file)<br>print(len(docs))</pre><pre>#Output<br>3</pre><p><strong>Note:</strong> Some generated chunks may slightly exceed the specified chunk_size. This occurs when the text splitter preserves complete sentences or separators to maintain better context and readability.</p><p>You can resolve this issue by specifying better separators or reducing the chunk size strategy. The most common solution is to use <strong>RecursiveCharacterTextSplitter</strong>, which splits text more intelligently.</p><pre>from langchain.text_splitter import RecursiveCharacterTextSplitter<br><br>text_splitter = RecursiveCharacterTextSplitter(<br>    chunk_size=200,<br>    chunk_overlap=20,<br>    separators=[&quot;\n\n&quot;, &quot;\n&quot;, &quot; &quot;, &quot;&quot;]<br>)<br><br>chunks = text_splitter.split_documents(docs_from_file)<br><br>print(len(chunks))</pre><pre>#Output<br>6</pre><h4>Create a vector embedding for each text snippet.</h4><p>A vector embedding is a numerical representation of text that captures its semantic meaning in a high-dimensional space. Similar pieces of text have similar vector representations, which makes it possible to perform semantic search.</p><p>Generate a vector embedding for each text chunk. These embeddings enable semantic search by identifying documents or text segments that are most relevant to a query based on their meaning and contextual similarity. In this example, we use OpenAI’s embedding model to create the embeddings.</p><p>Before running the code, make sure you have set up an OpenAI API account and generated an API key to access the embedding model. You can create your API key from OpenAI Platform .</p><pre>from langchain_openai import OpenAIEmbeddings<br># Before executing the following code, make sure to have<br># your OpenAI key saved in the &quot;OPENAI_API_KEY&quot; environment<br>embeddings = OpenAIEmbeddings(model=&quot;text-embedding-ada-002&quot;)</pre><h4><strong>Create a Vector Store</strong></h4><p>A <strong>vector store</strong> is a database designed to store and retrieve vector embeddings efficiently for semantic search and similarity matching. In this example, we use <strong>Deep Lake</strong>, a cloud-based vector store developed by Activeloop, although other solutions such as ChromaDB can also be used.</p><p>Next, we create a Deep Lake dataset and generate embeddings using the <strong>embedding_function</strong>.</p><p>To follow along, create a free Activeloop account at Activeloop .</p><pre>#Create a vector store: Deep Lake<br><br>my_activeloop_dataset_name = &quot;YOUR_DATASET_NAME&quot;<br>my_activeloop_org_id= &quot;&lt;YOUR-ACTIVELOOP-ORG-ID&gt;&quot;<br><br>dataset_path=f&quot;hub://{my_activeloop_org_id}/{my_activeloop_dataset_name}&quot;<br><br><br>db = DeeplakeVectorStore(dataset_path=dataset_path,embedding_function=embeddings)<br><br>#Add documents to the Deep Lake dataset<br>db.add_documents(docs)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2R4xZXSc1Sb85orUYMIExQ.png" /><figcaption><strong>Dataset Preview with Generated Text Chunks</strong></figcaption></figure><p>The next step is to create a LangChain retriever by calling the .as_retriever() method on the vector store instance.</p><pre># create retriever from db<br>retriever = db.as_retriever()</pre><p>Once we have the retriever, we can use the RetrievalQA class to define a question answering chain using external data source and start with question-answering.</p><pre>from langchain.chains import RetrievalQA<br>from langchain_openai import OpenAI<br># create a retrieval chain<br>qa_chain = RetrievalQA.from_chain_type(<br>    llm=OpenAI(model=&quot;gpt-3.5-turbo&quot;),<br>    chain_type= &quot;stuff&quot;,<br>    retriever=retriever)</pre><p>We can query our document about a specific topic found in the documents.</p><pre><br><br># Create the LLM<br>llm = ChatOpenAI(<br>    model_name=&quot;gpt-3.5-turbo&quot;,<br>    temperature=0<br>)<br><br># Create the QA chain<br>qa_chain = RetrievalQA.from_chain_type(<br>    llm=llm,<br>    chain_type=&quot;stuff&quot;,<br>    retriever=retriever<br>)<br><br># Query<br>query = &quot;What are Large Language Models (LLMs) and how does RAG improve their accuracy?&quot;<br><br># Generate response<br>response = qa_chain.invoke(query)<br><br>print(response[&quot;result&quot;])</pre><pre>#Output<br>Large Language Models (LLMs) are advanced AI systems trained on vast amounts of text data to understand and generate human language. They can perform various tasks like text summarization, translation, question answering, code generation, and conversational assistance. LLMs use deep learning and transformer architectures to capture context and relationships between words, enabling them to generate coherent and context-aware responses.<br>Retrieval-Augmented Generation (RAG) is a technique used to enhance the accuracy of LLMs by connecting them to external and up-to-date knowledge sources. By incorporating additional information from these external sources, LLMs can generate more accurate and relevant responses, reducing the chances of producing incorrect or outdated information. This helps improve the overall performance and reliability of Large Language Models.</pre><h4>How It Works Internally?</h4><p>Using chain_type=”stuff” means that all retrieved documents are directly inserted into the prompt before being sent to the LLM. This approach is simple and effective for small datasets, but it can become limited by the context window size of the model.</p><p>The system also performs a semantic similarity search using embeddings to retrieve the most relevant document chunks related to the user query. This allows the LLM to receive focused and context-aware information instead of processing entire documents.</p><p>This example highlights how retrievers, embeddings, and vector databases such as Deep Lake improve the ability of LLMs to work efficiently with external knowledge sources.</p><p>🎉 Congratulations — you have just built your first Retrieval-Augmented Generation (RAG) system!</p><h4>A Potential Problem</h4><p>Even when using RecursiveCharacterTextSplitter, a potential challenge remains when working with large datasets. Although this splitter improves chunk creation by using better separators and reducing oversized chunks, some retrieved chunks may still contain information that is not fully relevant to the user’s query.</p><p>Including unrelated content in the LLM prompt can be problematic because:</p><ul><li>It may distract the model from the most important information.</li><li>It consumes valuable context space that could be used for more relevant chunks.</li></ul><h4>Possible Solution</h4><p>A possible solution is to use a DocumentCompressor to filter and keep only the most relevant information from retrieved documents. Instead of sending complete chunks directly to the LLM, the system compresses the content based on the user query.</p><p>In LangChain, the ContextualCompressionRetriever combines a retriever with a compressor to return only the most relevant document sections. A common approach is to use LLMChainExtractor, which leverages an LLM to extract the sentences or passages that best match the query.</p><p>This helps reduce irrelevant context, improve response quality, and optimize prompt usage.</p><p>Once the compression_retriever is created, we can use it to retrieve the most relevant compressed documents for a given query.</p><pre># Retrieving compressed documents<br>retrieved_docs = compression_retriever.invoke(<br>    &quot;What are Large Language Models (LLMs) and how does RAG improve their accuracy?&quot;<br>)<br><br>print(retrieved_docs[0].page_content)</pre><pre>#Output<br>Techniques such as Retrieval-Augmented Generation (RAG) help improve their accuracy by connecting them to external and up-to-date knowledge sources.</pre><p>Compressors help optimize RAG systems by sending only the most relevant information to the LLM. This reduces unnecessary context, improves response quality, and allows the model to focus on essential content. It also enables the retriever to return more documents while relying on the compressor to keep only the most useful information.</p><p>In this example, we created a retriever from a simple .txt file, but in real-world applications, data can come from many different sources. Fortunately, LangChain provides loaders for PDFs, websites, Google Drive, databases, and many other formats that we will explore in future articles.</p><p>🎉 🎉🎉🎉🎉Thank you for making it this far! You have now discovered the core architecture and workflow behind a basic RAG-powered LLM system.</p><p>If you have followed this article up to this point, you now have a solid foundation in building a basic RAG architecture. Now, let’s move from theory to practice by exploring a real-world business use case: building a Customer Support Q&amp;A Chatbot powered by RAG.</p><h3><strong>A Customer Support Q&amp;A Chatbot</strong></h3><p>In this project, we will build a Customer Support Q&amp;A chatbot powered by GPT and Retrieval-Augmented Generation (RAG) to answer questions directly from business documents.</p><p>Traditionally, chatbots relied on predefined intents and scripted responses tailored to specific business scenarios. For example, in an e-commerce platform, customers might ask questions such as:</p><ul><li>“Where is my order?”</li><li>“How can I return a product?”</li><li>“Can I get a refund?”</li></ul><p>The chatbot would then match the query to a predefined intent and return a fixed response.</p><p>While this approach works well for repetitive and structured queries, it becomes limited when users ask unexpected or more complex questions.</p><p>With the rise of Large Language Models (LLMs), modern chatbots can now understand context, retrieve information from external knowledge sources, and generate more natural and accurate responses. In this section, we will build a simple RAG-based customer support assistant capable of answering questions from custom documents.</p><ul><li>Install the necessary Python packages and use the TextLoader class to load text files and create a LangChain Document objec</li></ul><pre>#Install langchain<br>!pip uninstall -q -y langchain langchain-core langchain-community langchain-text-splitters langchain-openai langchain-deeplake deeplake<br>!pip install -q -U &quot;langchain&lt;1.0&quot; &quot;langchain-openai&quot; &quot;langchain-community&quot; &quot;langchain-text-splitters&quot; &quot;langchain-deeplake&quot; &quot;deeplake&quot; &quot;tiktoken&quot;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fzNDtUJ3Z69_GOso4B8hPQ.png" /><figcaption>Customer Support QA Chatbot Workflow</figcaption></figure><h4>Workflow</h4><p>The first step is to extract content from online articles, split it into smaller chunks, generate embeddings, and store them in Deep Lake. When a user submits a query, the system retrieves the most relevant chunks and includes them in the prompt sent to the LLM to generate the final response.</p><p>Before starting, configure the OPENAI_API_KEY and ACTIVELOOP_TOKEN environment variables with your API credentials.</p><p>To load web content, we will use the WebBaseLoader class from LangChain.</p><pre>from langchain_openai import OpenAIEmbeddings<br>from langchain_deeplake.vectorstores import DeeplakeVectorStore<br>from langchain_text_splitters import CharacterTextSplitter, RecursiveCharacterTextSplitter<br>from langchain_openai import  OpenAI<br>from langchain_community.document_loaders import UnstructuredMarkdownLoader,SeleniumURLLoader,WebBaseLoader<br>from langchain import PromptTemplate<br>from langchain.retrievers import ContextualCompressionRetriever<br>from langchain.retrievers.document_compressors import LLMChainExtractor<br>from langchain.retrievers import ContextualCompressionRetriever<br>from langchain.retrievers.document_compressors import LLMChainExtractor<br>from langchain_openai import ChatOpenAI<br>from langchain_core.messages import HumanMessage</pre><p>These libraries provide the core functionalities required to build a context-aware question-answering system, including generating embeddings, managing vector storage, splitting text into chunks, and interacting with the OpenAI API. Together, they enable the integration of information retrieval with LLM-based response generation.</p><p>In this example, we use technical support articles as the chatbot knowledge base. However, the same architecture can be applied to many other domains, such as healthcare, finance, e-commerce, legal documents, or internal company documentation.</p><pre># we&#39;ll use information from the following articles<br>urls = [<br>&#39;https://beebom.com/what-is-nft-explained/&#39;,<br>&#39;https://beebom.com/how-delete-spotify-account/&#39;,<br>&#39;https://beebom.com/how-download-gif-twitter/&#39;,<br>&#39;https://beebom.com/how-use-chatgpt-linux-terminal/&#39;,<br>&#39;https://beebom.com/how-delete-spotify-account/&#39;,<br>&#39;https://beebom.com/how-save-instagram-story-with-music/&#39;,<br>&#39;https://beebom.com/how-install-pip-windows/&#39;,<br>&#39;https://beebom.com/how-check-disk-usage-linux/&#39;]</pre><ol><li>Use Selenium scraper or WebBaseLoader to load the documents</li></ol><p>Load the content from the provided URLs and split it into smaller chunks using CharacterTextSplitter with a chunk_size of 1000 and no overlap between chunks.</p><pre># Initialize the WebBaseLoader with the list of URLs<br>loader = WebBaseLoader(urls)<br><br># Load the content from the web pages<br>docs_not_splitted = loader.load()</pre><pre>print(f&#39;Number of documents={len(docs_not_splitted)}&#39;)</pre><pre>#Output<br>Number of documents=8</pre><p>2. Split the documents into chunks and compute</p><pre>#Split the documents into smaller chunks<br>text_splitter = RecursiveCharacterTextSplitter(<br>    chunk_size=1000,<br>    chunk_overlap=100<br>)<br>docs=text_splitter.split_documents(docs_not_splitted)<br>print(len(docs))</pre><pre>#Output<br>148</pre><p>Next, generate embeddings using OpenAIEmbeddings and store them in a cloud-based Deep Lake vector store. In production environments, this approach allows applications to search across large collections of documents using a centralized and scalable dataset.</p><p>Update the code below with your Activeloop organization ID (by default, it is your username).</p><p>3. Create a vector embedding for each text snippet</p><pre># Initialize the OpenAI embedding model<br># This model converts text chunks into numerical vector embeddings<br>embeddings = OpenAIEmbeddings(model=&quot;text-embedding-ada-002&quot;)</pre><pre>#Create a vector store: Deep Lake<br><br>my_activeloop_dataset_name=&quot;MY_ACTIVEELOOP_DATASET_NAME&quot;<br>my_activeloop_org_id= &quot;&lt;YOUR-ACTIVELOOP-ORG-ID&gt;&quot;<br>dataset_path=f&quot;hub://{my_activeloop_org_id}/{my_activeloop_dataset_name}&quot;<br><br><br>db = DeeplakeVectorStore(dataset_path=dataset_path,embedding_function=embeddings)<br><br>#Add documents to the Deep Lake dataset<br>db.add_documents(docs)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZCzEV0ytUZy8HhTy_Tg-Xg.png" /><figcaption><strong>Dataset Preview</strong></figcaption></figure><p>To retrieve the most similar chunks to a given query, we can use the similarity_search method of the Deep Lake vector store:</p><pre># let&#39;s see the top relevant documents to a specific query<br>query = &quot;how to check disk usage in linux?&quot;<br>docs = db.similarity_search(query)<br>print(docs[0].page_content)</pre><pre>#Output<br>There may be times when you need to download some important files or transfer some photos to your Linux system, but face a problem of insufficient disk space. You head over to your file manager to delete the large files which you no longer require, but you have no clue which of them are occupying most of your disk space. In this article, we will show some easy methods to check disk usage in Linux from both the terminal and the GUI application.<br>Check Disk Space Using the df Command<br>In Linux, there are many commands to check disk usage, the most common being the df command. The df stands for “Disk Filesystem” in the command, which is a handy way to check the current disk usage and the available disk space in Linux. The syntax for the df command in Linux is as follows:<br>df &lt;options&gt; &lt;file_system&gt;<br>The options to use with the df command are:</pre><p>4. Craft a prompt for GPT-3 using the suggested strategies</p><p>For this chatbot, we will create a prompt template that combines role prompting, retrieved knowledge base context, and the user’s question to generate accurate and context-aware responses.</p><pre># let&#39;s write a prompt for a customer support chatbot that<br># answer questions using information extracted from our db<br>template = &quot;&quot;&quot;You are a helpful and professional customer support assistant.<br><br>Your goal is to answer customer questions using ONLY the provided context.<br><br>Guidelines:<br>- Be polite, clear, and concise.<br>- If the answer is not in the context, say: &quot;I’m sorry, I don’t have enough information to answer that.&quot;<br>- Do NOT make up information.<br>- Use simple language.<br><br>Context:<br>{chunks_formatted}<br><br>Customer question:<br>{query}<br><br>Answer:&quot;&quot;&quot;<br><br>prompt = PromptTemplate(<br>    input_variables=[&quot;chunks_formatted&quot;, &quot;query&quot;],<br>    template=template,<br>)</pre><p>The template defines the chatbot as an advanced customer support assistant and relies on two main inputs: chunks_formatted, containing relevant document segments, and query, representing the user’s question. The goal is to generate accurate, context-based responses while avoiding fabricated information.</p><p>5. Utilize the GPT-3 model with a temperature of 0 for text generation</p><pre><br># Initialize the chatbot model<br>llm = ChatOpenAI(<br>    model=&quot;gpt-3.5-turbo&quot;,<br>    temperature=0<br>)<br><br>print(&quot;Hi! I&#39;m your AI technical support assistant. Ask me questions about Linux, Windows, Spotify, ChatGPT, and more.&quot;)<br>print(&quot;Type &#39;exit&#39; to stop the chatbot.\n&quot;)<br><br># Chat loop<br>while True:<br><br>    # User input<br>    query = input(&quot;You: &quot;)<br><br>    # Exit condition<br>    if query.lower() == &quot;exit&quot;:<br>        print(&quot;Chatbot: Goodbye!&quot;)<br>        break<br><br>    # Retrieve relevant chunks from the vector database<br>    docs = db.similarity_search(query)<br><br>    # Extract text content from retrieved documents<br>    retrieved_chunks = [doc.page_content for doc in docs]<br><br>    # Format retrieved context<br>    chunks_formatted = &quot;\n\n&quot;.join(retrieved_chunks)<br><br>    # Build the final prompt<br>    prompt_formatted = prompt.format(<br>        chunks_formatted=chunks_formatted,<br>        query=query<br>    )<br><br>    # Generate chatbot response<br>    answer = llm.invoke([<br>        HumanMessage(content=prompt_formatted)<br>    ])<br><br>    # Display response<br>    print(&quot;\nChatbot:&quot;, answer.content)<br>    print(&quot;-&quot; * 80)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/801/1*tP4vLc44ks3W6U7eGnutJw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/966/1*53UP1A5ahZKd7SZdkZuPaQ.png" /></figure><p>🎉 Congratulations! We have successfully built our first RAG-powered Customer Support Q&amp;A chatbot.</p><p>Throughout this project, we explored the complete workflow behind a retrieval-augmented AI application: loading external documents, splitting them into chunks, generating embeddings, storing them in a vector database, retrieving relevant information through semantic search, and finally using an LLM to generate contextual answers.</p><p>We also discovered how tools such as LangChain, OpenAI embeddings, and Deep Lake work together to build intelligent systems capable of interacting with external knowledge sources instead of relying solely on the model’s internal memory.</p><p>However, building a reliable chatbot is not only about generating answers. One of the main challenges of LLM-based systems remains hallucinations — cases where the model generates incorrect or unsupported information. In the next article, we will explore techniques to improve retrieval quality and build more reliable RAG architectures.</p><p>Thank you for reading this article! 🚀<br> Be sure to follow me on Medium for upcoming articles about data ingestion pipelines and more advanced RAG architectures using LlamaIndex.</p><p>The complete project code is available on GitHub:<a href="https://github.com/Just1919/Customer-Support-Q-A-Chatbot">link</a></p><p>Feel free to connect with me on LinkedIn for more content on AI, Data Science, and LLM engineering:<a href="https://www.linkedin.com/in/yawo-justin-djakas"> yawo-justin-djakas</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ada7bffdf50e" width="1" height="1" alt=""><hr><p><a href="https://blog.gopenai.com/build-your-first-rag-powered-customer-support-chatbot-ada7bffdf50e">Build Your First RAG-Powered Customer Support Chatbot</a> was originally published in <a href="https://blog.gopenai.com/">GoPenAI</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mastering LangChain (Part 2): Advanced Techniques & Developing LLM-Powered Applications]]></title>
            <link>https://blog.gopenai.com/mastering-langchain-part-2-advanced-techniques-developing-llm-powered-applications-dcd75c03e36f?source=rss-66abbb206fb4------2</link>
            <guid isPermaLink="false">https://medium.com/p/dcd75c03e36f</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[generative-ai-tools]]></category>
            <dc:creator><![CDATA[Justin DJAKAS]]></dc:creator>
            <pubDate>Thu, 02 Apr 2026 11:21:08 GMT</pubDate>
            <atom:updated>2026-04-03T06:43:15.369Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pGV15Qj2s8VCZgaoN8-s9Q.png" /></figure><h4>Build real-world AI applications with LangChain: news summarization, QA systems, and practical LLM workflows.</h4><p>In the first article — <a href="https://medium.com/@justindjakas/mastering-langchain-part-1-core-concepts-and-your-first-llm-application-ecbf48b6f246">”M<strong>astering LangChain — Part 1: Core Concepts and Your First LLM Application”</strong></a>— we explored the fundamental concepts of LangChain and its core components. (<a href="https://medium.com/@justindjakas/mastering-langchain-part-1-core-concepts-and-your-first-llm-application-ecbf48b6f246">Link</a>)</p><p>In this second part, we move from theory to practice by focusing on real-world use cases. You will learn how to build applications such as document access and summarization, question-answering (QA) chains, and a news article summarizer.</p><p><strong>If you find this article helpful, feel free to follow me on Medium to stay updated and support more high-quality content 🚀</strong></p><p>In your Python script or Jupyter Notebook, set your API key as an environment variable named `OPENAI_API_KEY`. This ensures better security and prevents exposing sensitive information in your code.</p><p>If you are using a `.env` file, you can load the environment variables using the `load_dotenv` function:</p><pre>import json<br>from dotenv import load_dotenv<br><br># Load environment variables from the .env file<br>load_dotenv()</pre><h3><strong>Summarization Chain</strong></h3><p>summarization chain is a process in LangChain that enables automatic generation of concise summaries from large amounts of text by structuring the interaction with a language model.</p><p>In this example, the summarization chain is used to process an external document (a PDF) and produce a clear and structured summary. The goal is to load the document, extract its content, and pass it to a language model for summarization.</p><p>To do this, the language model is initialized with ChatOpenAI and a temperature of 0, ensuring deterministic and consistent outputs. Then, the PyPDFLoader class is used to load the PDF file page by page into a format that can be easily processed. All pages are then combined into a single text string.</p><p>A prompt is defined using PromptTemplate to instruct the model to summarize the content clearly and concisely. The chain is created using LangChain Expression Language (prompt | llm) and executed with the invoke() method.</p><p>For this example, the PDF used is the Python course , downloaded from the <a href="https://static.realpython.com/python-cheatsheet.pdf">link</a> <a href="https://static.realpython.com/python-cheatsheet.pdf.">.</a></p><p>It is important to install the pypdf package before running this code.</p><pre>#Import necesseray modules<br>from langchain_openai import ChatOpenAI<br>from langchain_core.prompts import PromptTemplate<br>from langchain_community.document_loaders import PyPDFLoader</pre><pre># Initialize language model<br>llm = ChatOpenAI(model=&quot;gpt-3.5-turbo&quot;, temperature=0)<br><br># Load the document using PyPDFLoader<br>document_loader = PyPDFLoader(&quot;/content/drive/MyDrive/llms_for_production/python-cheatsheet.pdf&quot;)<br>documents = document_loader.load()<br><br># Combine all pages into one text<br>text = &quot;\n\n&quot;.join(doc.page_content for doc in documents)<br><br># Create the prompt template<br>prompt = PromptTemplate.from_template(<br>    &quot;Summarize the following PDF content in a clear and concise way:\n\n{text}&quot;<br>)<br><br># Create the chain<br>chain = prompt | llm<br><br># Summarize the document<br>response = chain.invoke({&quot;text&quot;: text})<br><br># Print the summary<br>print(response.content)</pre><pre>The PDF provides a pocket reference for Python beginners, covering topics such as getting started, comments, data types, variables, strings, numbers, conditionals, loops, functions, classes, exceptions, collections, comprehensions, file I/O, imports &amp; modules, virtual environments, packages, and miscellaneous Pythonic constructs. It also encourages further learning on realpython.com with in-depth tutorials, examples, and expert guidance.</pre><h3>QA Chain</h3><p>LangChain allows prompts to be structured in different ways, including asking general questions to language models.</p><p>⚠️ However, it is important to be aware of potential hallucinations, where the model may generate incorrect or non-factual information. To address this issue, a Retrieval-Augmented Generation (RAG) approach can be implemented. In the next article, we will explore how LangChain facilitates the implementation of such systems using a Retrieval Chain.</p><p>In this example, we define a custom prompt template using the PromptTemplate class. The template includes a {question} placeholder, which represents the user’s input, followed by a newline and the label “Answer:”.</p><p>The input_variables parameter specifies the variables expected in the template — in this case, the question. These variables are dynamically replaced with actual input values when the chain is executed.</p><pre>from langchain_core.prompts import PromptTemplate<br>from langchain_openai import ChatOpenAI<br><br># Create the prompt with input_variables<br>prompt = PromptTemplate(<br>    template=&quot;Question: {question}\nAnswer:&quot;,<br>    input_variables=[&quot;question&quot;]<br>)</pre><p>Next, an instance of the OpenAI model gpt-3.5-turbo is initialized with a temperature set to 0, ensuring fully deterministic outputs. This model is created using the ChatOpenAI class, where the model and temperature parameters are specified.</p><p>After that, a question-answering chain is constructed by combining the language model with the previously defined prompt template. In earlier versions of LangChain, this was typically done using the LLMChain class, which takes two main arguments: the language model (llm) and the prompt template (prompt).</p><p>In modern LangChain usage, this process is simplified by directly composing the prompt and the model using the expression:</p><pre># Initialize the model<br>llm = ChatOpenAI(<br>    model=&quot;gpt-3.5-turbo&quot;,<br>    temperature=0<br>)<br><br># Create the chain<br>chain = prompt | llm</pre><p>By following these steps, input questions can be processed efficiently using the custom question-answering chain. This approach enables the generation of relevant and coherent answers by combining the capabilities of the OpenAI model with a well-defined prompt template.</p><pre><br># Invoke the chain<br>response = chain.invoke({&quot;question&quot;: &quot;does money bring happiness?&quot;})<br><br>print(response.content)</pre><pre>Money can contribute to happiness by providing financial security, access to resources, and the ability to enjoy experiences and opportunities. However, it is not the sole determinant of happiness. Other factors such as relationships, health, personal fulfillment, and a sense of purpose also play a significant role in overall happiness. Ultimately, the relationship between money and happiness varies for each individual and depends on how money is used and valued in their life.</pre><p>This example illustrates how LangChain facilitates the integration of prompt templates in question-answering applications. The framework can be further extended by incorporating additional components such as retrieval-augmented generation, agents, or memory mechanisms, enabling the development of more advanced and intelligent applications.</p><h3>News Article Summarizer with LangChain</h3><p>This project walks you through the process of building a News Articles Summarizer using OpenAI’s GPT-4 model and LangChain. It demonstrates how to scrape online articles, extract their titles and content, and generate concise, meaningful summaries.</p><h4>Workflow</h4><p>Here’s what we are going to do in this project:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MVllhvny0obXBTUy5_V3Qw.png" /><figcaption>News Articles Summarization Pipeline</figcaption></figure><h4>News Articles Summarization Pipeline</h4><ol><li><strong>Install Libraries: </strong>Install the required packages: `requests`, `newspaper3k`, and `langchain`.</li></ol><p>2. <strong>Fetch Articles: </strong>Use the `requests` library to retrieve article content from URLs.</p><p>3. <strong>Extract Content: </strong>Parse the HTML using `newspaper3k` to extract the title and main text.</p><p>4. <strong>Preprocess Text: </strong>Clean and prepare the extracted text for the language model.</p><p>5. <strong>Generate Summaries: </strong>Use GPT-4 to produce concise summaries of the articles.</p><p>6. <strong>Present Results: </strong>Display the generated summaries alongside their corresponding titles.</p><h3>Steps to Build a News Article Summarizer</h3><p>To get started, obtain your OpenAI API key from the OpenAI website. You will need to create an account and enable access to the API. Once logged in, navigate to the API Keys section and securely copy your key.</p><p>Next, install the required Python packages using up-to-date versions to ensure compatibility and stability:</p><pre># Fix lxml compatibility issue with newspaper3k, then install<br>!pip install -q lxml_html_clean</pre><pre># Install required libraries silently:<br># - newspaper3k: for scraping and parsing online news articles<br># - python-dotenv: for loading environment variables from a .env file<br>!pip install -q newspaper3k python-dotenv</pre><p>We selected a set of representative news article URLs in order to generate summaries. The following code demonstrates how to use the requests library to fetch the content of each URL while specifying a custom User-Agent header to mimic a real browser.</p><p>The newspaper3k library is then used to parse each article and extract its title and main text content.</p><pre>import requests<br>from newspaper import Article<br><br># Define headers to mimic a real browser (helps avoid blocking)<br>headers = {<br>    &#39;User-Agent&#39;: (<br>        &#39;Mozilla/5.0 (Windows NT 10.0; Win64; x64) &#39;<br>        &#39;AppleWebKit/537.36 (KHTML, like Gecko) &#39;<br>        &#39;Chrome/120.0.0.0 Safari/537.36&#39;<br>    )<br>}<br><br># Target article URL<br>article_url =&quot;https://blog.python.org/2026/03/the-python-insider-blog-has-moved/&quot;<br><br><br><br># Create a session object for better performance and connection reuse<br>session = requests.Session()<br><br>try:<br>    # Send a GET request with headers and timeout<br>    response = session.get(article_url, headers=headers, timeout=10)<br><br>    # Check if the request was successful (HTTP 200 OK)<br>    if response.status_code == 200:<br>        <br>        # Extract the raw HTML content from the response<br>        html = response.text<br><br>        # Initialize the Article object with the URL<br>        article = Article(article_url)<br><br>        # IMPORTANT: Set the HTML manually to avoid a second request<br>        article.set_html(html)<br><br>        # Parse the article content (title, text, etc.)<br>        article.parse()<br><br>        # Print extracted information<br>        print(&quot;Title:&quot;, article.title)<br>        print(&quot;Text:&quot;, article.text)<br><br>    else:<br>        # Print error code if request failed<br>        print(f&quot;Failed to fetch article (status code: {response.status_code})&quot;)<br><br># Handle timeout errors<br>except requests.exceptions.Timeout:<br>    print(&quot;Request timed out&quot;)<br><br># Handle connection errors (DNS issues, no internet, etc.)<br>except requests.exceptions.ConnectionError:<br>    print(&quot;Connection error occurred&quot;)<br><br># Catch any other unexpected errors<br>except Exception as e:<br>    print(f&quot;Unexpected error: {e}&quot;)<br><br># Always close the session to free resources<br>finally:<br>    session.close()</pre><pre>Title: The Python Insider Blog Has Moved!<br>Text: Python Insider now lives at blog.python.org, backed by a Git repository. All 307 posts from the Blogger era have been migrated over, and old URLs redirect to the new ones automatically. Your RSS readers should pick up the new feed without any action on your part, but if something looks off, the new feed URL is blog.python.org/rss.xml.<br><br>Why we moved<br><br>Blogger worked fine for a long time, but contributing to the blog meant having a Google account and using Blogger’s editor. That’s a higher bar than it needs to be. The new setup is just Markdown files in a Git repo. If you can open a pull request, you can write a post.<br><br>Posts live in content/posts/{slug}/index.md with YAML frontmatter for the title, date, authors, and tags. Images go right next to the post in the same directory. No special tooling required beyond a text editor.<br><br>Contributing<br><br>Want to write about a Python release, core sprint, governance update, or anything else that belongs on the official Python blog? Here’s the short version:<br><br>Fork python/python-insider-blog Create a new directory under content/posts/ with your post slug Add an index.md with your content (and optionally upload your images) Open a PR<br><br>The repo README has more detail on frontmatter fields and local development if you want to preview your post before submitting.<br><br>What’s new on the site<br><br>Beyond the content itself, the new site has a few features the old Blogger setup never had. Here’s a live look:<br><br>Browse &amp; filter posts<br><br>All posts are browsable with pagination, a year filter, and a tag sidebar. Click any tag or year to narrow things down.<br><br>Every author has a page<br><br>See who’s been writing, how much they’ve contributed, and browse their posts individually.<br><br>Every tag across the archive, ranked by how often it appears. Great for finding all the release announcements or security updates in one place.<br><br>Search everything<br><br>Hit Ctrl+K (or Cmd+K on Mac) from any page to open the command palette. It searches across all 307+ posts by title, author, tags, and description. There are also keyboard chord shortcuts for quick navigation.<br><br>Command Palette Press ⌘K to try it Search posts, tags, authors... Go to Go to Home G then H Go to Blog G then B Go to Tags G then T ↑↓ Navigate ↵ Open Esc Close<br><br>And more<br><br>RSS feed at blog.python.org/rss.xml, compatible with the old Blogger feed URL so existing subscribers don’t need to change anything.<br><br>at blog.python.org/rss.xml, compatible with the old Blogger feed URL so existing subscribers don’t need to change anything. Dark mode that follows your system preference (try the toggle in the header).<br><br>that follows your system preference (try the toggle in the header). Open Graph images generated automatically for every post, so links shared on social media get proper preview cards.<br><br>What’s under the hood<br><br>The site is built with Astro and deployed as fully static HTML. There’s a Keystatic CMS available in dev mode if you prefer a visual editor over raw Markdown, but it’s entirely optional. Tailwind handles the styling. The whole thing builds and deploys through GitHub Actions.<br><br>If you spot broken links, missing images, or formatting issues from the migration, file an issue on the repo. PRs are welcome too.</pre><p>The following code imports the required classes and functions from LangChain and initializes a ChatOpenAI model with a temperature set to 0, ensuring deterministic and consistent responses.</p><p>It also imports the message schema classes used for chat-based interactions, allowing efficient handling and structuring of conversational tasks.</p><p>Finally, the code defines a prompt and fills it with the content of the article to guide the model in generating the desired output.</p><pre>from langchain_core.messages import (<br>    AIMessage,<br>    HumanMessage,<br>    SystemMessage<br>)<br># Retrieve article data from the scraping step<br>article_title = article.title<br>article_text = article.text<br><br># Define a prompt template for summarization<br>template = &quot;&quot;&quot;<br>You are an advanced AI assistant specialized in summarizing online articles.<br><br>Here is the article to summarize:<br>==================<br>Title: {article_title}<br><br>{article_text}<br>==================<br><br>Provide a clear and concise summary of the article.<br>&quot;&quot;&quot;<br><br># Format the prompt with the article content<br>prompt = template.format(<br>    article_title=article_title,<br>    article_text=article_text<br>)<br><br># Create a message object to send to the model<br>messages = [HumanMessage(content=prompt)]</pre><p>The HumanMessage class represents a structured format for handling user inputs in chat-based interactions. In this context, the ChatOpenAI model is used to communicate with the AI system, while HumanMessage provides a standardized way to encapsulate user prompts.</p><p>The prompt template defined in this setup contains placeholders for the article’s title and content. These placeholders are dynamically replaced with the actual values of article_title and article_text at runtime.</p><p>This approach enables the efficient creation of dynamic and reusable prompts, making the interaction with the language model more flexible and scalable.</p><pre>from langchain_openai import ChatOpenAI<br>#Load the model<br>chat=ChatOpenAI(model=&quot;gpt-4-turbo&quot;,temperature=0)</pre><p>Initialize the model and set the temperature to 0 to ensure deterministic and consistent outputs. To generate the summary, send the formatted request — encapsulated in a HumanMessage object — to the chat model instance.</p><p>Once the prompt is processed by the AI model, it returns a concise summary of the article.</p><pre>#Generate the summary<br>summary=chat.invoke(messages)<br>print(summary.content)</pre><pre>The Python Insider blog has relocated to a new URL at blog.python.org, transitioning from Blogger to a Git repository-based system. This move was made to simplify the contribution process, allowing anyone who can open a pull request to write a post using Markdown files. All previous posts from Blogger have been migrated, and old URLs automatically redirect to the new ones. The new site features enhanced navigation and search capabilities, including filters by year, tags, and authors, and a command palette for quick access. It also supports RSS feeds, dark mode, and automatically generated Open Graph images for social media sharing. The site is built with Astro, styled with Tailwind, and uses GitHub Actions for deployment. Issues such as broken links or formatting errors can be reported on the repository, where contributions are also welcomed.</pre><p>You can also modify the prompt to generate a numbered list:</p><pre>1. The Python Insider Blog has relocated to a new URL: blog.python.org, now supported by a Git repository.<br>2. All 307 previous posts from the Blogger platform have been transferred to the new site, with automatic redirection from old URLs to new ones.<br>3. The new blog setup uses Markdown files in a Git repository, simplifying the contribution process as it no longer requires a Google account or the use of Blogger’s editor.<br>4. Contributors can now write posts by forking the repository, creating a directory and Markdown file for their post, and submitting a pull request.<br>5. The new site features enhanced navigation and search capabilities, including filters by year, tags, and a command palette for quick access using keyboard shortcuts.<br>6. Additional features include a dark mode option, automatic generation of Open Graph images for social media previews, and compatibility with existing RSS feed URLs.<br>7. The site is built using Astro, styled with Tailwind, and includes optional Keystatic CMS for those who prefer a visual editor. It is deployed through GitHub Actions.<br>8. Users are encouraged to report any issues such as broken links or formatting errors by filing an issue on the repository, with contributions via PRs welcomed.</pre><p>If you would like the summary to be generated in French, you can explicitly instruct the model to produce the output in that language. However, it is important to keep in mind that English is the primary training language of GPT-4. Although the model supports multiple languages, the quality and consistency of responses in languages other than English may vary.</p><p>Below is an example of how to modify the prompt to generate a summary in French.</p><pre># Define a prompt template<br>template = &quot;&quot;&quot;<br>You are an advanced AI assistant specialized in summarizing online articles.<br><br>Here is the article to summarize:<br>==================<br>Title: {article_title}<br><br>{article_text}<br>==================<br><br>Now, provide a summarized version of the article in french<br>&quot;&quot;&quot;<br><br>#Format prompt <br>prompt = template.format(<br>    article_title=article_title,<br>    article_text=article_text<br>)<br><br>#Create a message object to send to thez model<br>messages = [HumanMessage(content=prompt)]<br><br>#Generate the summary<br>summary=chat.invoke(messages)<br>print(summary.content)  </pre><pre>Titre: Le blog Python Insider a déménagé !<br><br>Le blog Python Insider se trouve désormais sur blog.python.org, soutenu par un dépôt Git. Les 307 articles de l&#39;époque Blogger ont été transférés, et les anciennes URL redirigent automatiquement vers les nouvelles. Les lecteurs de flux RSS devraient récupérer le nouveau flux sans intervention de votre part, mais si quelque chose semble incorrect, la nouvelle URL du flux est blog.python.org/rss.xml.<br><br>Raison du déménagement<br><br>Blogger fonctionnait bien, mais contribuer au blog nécessitait un compte Google et l&#39;utilisation de l&#39;éditeur de Blogger, ce qui était une contrainte inutile. La nouvelle configuration utilise simplement des fichiers Markdown dans un dépôt Git, facilitant la contribution par le biais de pull requests.<br><br>Contributions<br><br>Pour écrire sur une nouvelle version de Python, un sprint du noyau, une mise à jour de la gouvernance, ou tout autre sujet pertinent pour le blog officiel de Python, il suffit de forker le dépôt python/python-insider-blog, créer un nouveau répertoire sous content/posts/ avec votre slug, ajouter un index.md avec votre contenu, et ouvrir une PR.<br><br>Nouveautés sur le site<br><br>Le nouveau site offre des fonctionnalités telles que la navigation et le filtrage des articles par année et par tags, des pages dédiées pour chaque auteur, et une fonction de recherche complète accessible via un raccourci clavier. Il inclut également un mode sombre qui s&#39;adapte à la préférence de système de l&#39;utilisateur, et des images Open Graph générées automatiquement pour chaque post.<br><br>Sous le capot<br><br>Le site est construit avec Astro et déployé en HTML statique. Il utilise Tailwind pour le style et se construit et se déploie via GitHub Actions. Des outils comme Keystatic CMS sont disponibles en mode développement pour ceux qui préfèrent un éditeur visuel.<br><br>Pour toute erreur de lien, image manquante ou problème de formatage suite à la migration, il est possible de soumettre un problème ou une PR sur le dépôt.</pre><p>The approach presented here combines LangChain and GPT-4 to interpret natural language instructions and generate human-like text. This enables intuitive interaction with the model, allowing users to perform complex tasks — such as summarizing an article into structured bullet points in French — with ease and precision.</p><p>The process starts by collecting the article’s title and content, which are then inserted into a prompt template designed to guide the model. The GPT-4 model is initialized using the ChatOpenAI class, with the temperature set to 0 to ensure consistent and deterministic outputs.</p><p>Once the prompt is submitted, the model analyzes the input and generates a concise summary based on its understanding of the text. Leveraging its training on diverse data, it can effectively produce structured summaries in French.</p><p>This approach highlights how natural language can be used to guide AI systems efficiently, offering a practical and powerful solution for real-world applications.</p><p>In upcoming articles, we will explore the **LlamaIndex** framework and how to implement <strong>RAG (Retrieval-Augmented Generation) systems in real-world scenarios</strong>.</p><p>🚀 Stay Connected</p><p>💻<strong> </strong>Complete code on my GitHub<strong>:</strong> <a href="https://github.com/Just1919/Buildng-LLMs-for-production/blob/main/Building_LLM_Powered_Applications_with_LangChain.ipynb"><strong>Just1919</strong></a></p><p>🔗 <strong>LinkedIn: </strong><a href="https://www.linkedin.com/in/yawo-justin-djakas/"><strong>Yawo Justin DJAKAS</strong></a></p><p>If you enjoyed this article, follow me for more content on A<strong>I and Data Science</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dcd75c03e36f" width="1" height="1" alt=""><hr><p><a href="https://blog.gopenai.com/mastering-langchain-part-2-advanced-techniques-developing-llm-powered-applications-dcd75c03e36f">Mastering LangChain (Part 2): Advanced Techniques &amp; Developing LLM-Powered Applications</a> was originally published in <a href="https://blog.gopenai.com/">GoPenAI</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mastering LangChain (Part 1): Core Concepts and Your First LLM Application]]></title>
            <link>https://blog.gopenai.com/mastering-langchain-part-1-core-concepts-and-your-first-llm-application-ecbf48b6f246?source=rss-66abbb206fb4------2</link>
            <guid isPermaLink="false">https://medium.com/p/ecbf48b6f246</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[langchain]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[llm]]></category>
            <dc:creator><![CDATA[Justin DJAKAS]]></dc:creator>
            <pubDate>Thu, 19 Mar 2026 11:12:27 GMT</pubDate>
            <atom:updated>2026-03-20T07:51:32.474Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PCGEUaTt8QToZnHv0b5yGg.png" /></figure><p>Large Language Models (LLMs) are transforming how we build intelligent systems. However, working with them efficiently in production can quickly become complex.</p><p>This is where <strong>LangChain</strong> comes in.</p><p>LangChain is an open-source framework that simplifies the development, orchestration, and deployment of LLM-powered applications. It provides modular components and abstractions that help developers move from experimentation to production faster.</p><p>In this first part, we’ll explore the core concepts of LangChain and build a simple LLM-powered application.</p><h3>Key Features of LangChain</h3><p>LangChain is designed to make LLM development more structured and scalable. Its main strengths include:</p><ul><li><strong>Abstractions &amp; LCEL (LangChain Expression Language)</strong><br> A modern way to compose chains using a clean, modular syntax.</li><li><strong>Third-party integrations</strong><br> Easy connection with external tools, APIs, and data sources.</li><li><strong>Chains, Agents &amp; Retrieval</strong><br> Enable the creation of intelligent systems capable of reasoning and decision-making.</li><li><strong>LangGraph</strong><br> Designed for building stateful, multi-agent workflows.</li><li><strong>LangServe</strong><br> Allows deployment of LLM applications as REST APIs.</li></ul><h3>The LangChain Ecosystem</h3><p>LangChain is more than just a library — it’s an ecosystem.</p><p><strong>LangSmith</strong><br> A platform for:</p><ul><li>Debugging</li><li>Testing</li><li>Evaluating</li><li>Monitoring LLM applications</li></ul><h3>LangChain and Retrieval-Augmented Generation (RAG)</h3><p>One major limitation of LLMs is <strong>hallucination</strong> — generating incorrect or fabricated information.</p><p><strong>Retrieval-Augmented Generation (RAG)</strong> solves this problem.</p><p>Instead of relying only on training data, RAG:</p><ul><li>Retrieves real information from external sources</li><li>Injects it into the model before generation</li></ul><p>Result:</p><ul><li>More accurate outputs</li><li>More reliable answers</li><li>Better contextual understanding</li></ul><h3>Why RAG Matters</h3><p>LLMs generate responses based on learned patterns, not real-time knowledge.</p><p>RAG improves this by:</p><ul><li>Connecting models to documents or databases</li><li>Providing factual grounding</li></ul><h3>LangChain for RAG</h3><p>LangChain makes RAG easy to implement through:</p><ul><li>Document loaders</li><li>Retrievers</li><li>Vector stores</li></ul><p>This enables:</p><ul><li>Context-aware responses</li><li>Knowledge-grounded applications</li></ul><h3>Core LangChain Components</h3><p>To build LLM applications, LangChain provides several key building blocks:</p><ul><li><strong>Prompts</strong> → Structure inputs to the model</li><li><strong>Output Parsers</strong> → Format model outputs</li><li><strong>Retrievers</strong> → Fetch relevant documents</li><li><strong>Document Loaders</strong> → Load external data</li><li><strong>Text Splitters</strong> → Chunk large text</li><li><strong>Embeddings &amp; Vector Stores</strong> → Enable semantic search</li><li><strong>Chains</strong> → Combine multiple steps</li><li><strong>Agents</strong> → Add reasoning and decision-making</li><li><strong>Tools</strong> → Connect to external systems</li><li><strong>Memory</strong> → Store conversation history</li></ul><h3>Agents and Tools Overview</h3><p>LangChain agents act as <strong>decision-makers</strong>.</p><p>They:</p><ul><li>Choose actions</li><li>Decide which tools to use</li><li>Execute multi-step workflows</li></ul><h3>Types of Agents</h3><ul><li><strong>Zero-shot ReAct</strong> → Tool usage based on descriptions</li><li><strong>Structured ReAct</strong> → Handles multiple inputs</li><li><strong>OpenAI Functions Agent</strong> → Uses function calling</li><li><strong>Self-Ask with Search</strong> → Breaks questions into sub-questions</li><li><strong>Plan-and-Execute</strong> → Handles complex workflows</li></ul><p>Trade-off: more power = more latency.</p><h3>Tools in LangChain</h3><p>LangChain provides built-in tools such as:</p><ul><li><strong>Python Tool</strong> → Execute Python code</li><li><strong>JSON Tool</strong> → Work with large JSON files</li><li><strong>CSV Tool</strong> → Query structured data</li></ul><h3>Custom Tools</h3><p>You can also build your own tools to:</p><ul><li>Extend agent capabilities</li><li>Handle domain-specific tasks</li><li>Create complex workflows</li></ul><h3>Building Your First LLM Application: Movie / TV series Assistant</h3><p>Let’s build a simple <strong>movie assistant</strong> using LangChain.</p><pre>!pip install langchain langchain-openai langchain-community tiktoken</pre><p>Set your API key:</p><pre>export OPENAI_API_KEY=&quot;your_api_key&quot;</pre><p>Import necessary modules</p><pre>#Import necesseray modules<br>from langchain_openai import ChatOpenAI<br>from langchain_core.prompts import (<br>    ChatPromptTemplate,<br>    SystemMessagePromptTemplate,<br>    HumanMessagePromptTemplate,<br>)</pre><pre><br><br># Define the system template<br>template = &quot;You are an assistant that helps users find information about movies.&quot;<br>system_message_prompt = SystemMessagePromptTemplate.from_template(template)<br><br># Define the human template<br>human_template = &quot;Find information about the movie {movie_title}.&quot;<br>human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)<br><br># Create the chat prompt<br>chat_prompt = ChatPromptTemplate.from_messages([<br>    system_message_prompt,<br>    human_message_prompt<br>])<br><br># Invoke the model<br>response = chat.invoke(<br>    chat_prompt.format_prompt(movie_title=&quot;Silicon Valley&quot;).to_messages()<br>)<br><br># Print the result<br>print(response.content)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/933/1*rz1ybu2T-ewo8jsEPQ9OpA.png" /></figure><h3>What You Just Built</h3><p>With just a few lines of code, you:</p><ul><li>Structured a prompt</li><li>Defined system behavior</li><li>Passed dynamic input</li><li>Generated a response using an LLM</li></ul><p>This is the foundation of <strong>all LLM applications</strong>.</p><h3>What’s Next?</h3><p>In the next parts, we will go deeper into:</p><ul><li>Building a <strong>RAG system with PDFs</strong></li><li>Creating <strong>custom agents with tools</strong></li><li>Deploying LLM applications</li><li>Real-world projects for production</li></ul><h3>Final Thoughts</h3><p>LangChain is not just a library — it’s a <strong>framework for building intelligent systems</strong>.</p><p>Mastering it means understanding:</p><ul><li>How to structure prompts</li><li>How to connect data</li><li>How to orchestrate reasoning</li></ul><p>And that’s exactly what we’ll build step by step in this series.</p><p>👉 Follow me on LinkedIn: <a href="https://www.linkedin.com/in/yawo-justin-djakas/">yawo-justin-djakas/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ecbf48b6f246" width="1" height="1" alt=""><hr><p><a href="https://blog.gopenai.com/mastering-langchain-part-1-core-concepts-and-your-first-llm-application-ecbf48b6f246">Mastering LangChain (Part 1): Core Concepts and Your First LLM Application</a> was originally published in <a href="https://blog.gopenai.com/">GoPenAI</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building Smarter Applications with Cohere Large Language Models(LLMs)]]></title>
            <link>https://medium.com/@justindjakas/building-smarter-applications-with-cohere-large-language-models-llms-1ca91354d9d3?source=rss-66abbb206fb4------2</link>
            <guid isPermaLink="false">https://medium.com/p/1ca91354d9d3</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[large-language-models]]></category>
            <category><![CDATA[llm]]></category>
            <dc:creator><![CDATA[Justin DJAKAS]]></dc:creator>
            <pubDate>Mon, 02 Mar 2026 12:49:56 GMT</pubDate>
            <atom:updated>2026-03-02T12:49:56.620Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VlEd0EyzABnJzcB1pjWFjw.png" /></figure><p>Explore how developers and enterprises can use Cohere’s Command, Rerank, and Embed models to build AI-powered apps.</p><p>Cohere is an AI company founded in 2019 by former Google Brain researchers, <strong>Aidan Gomez, Nick Frosst, and Ivan Zhang</strong>.<br>Their mission is to build <strong>large language models (LLMs)</strong> that empower developers and enterprises to create AI-powered applications with ease and efficiency.</p><p>Cohere’s platform provides state-of-the-art models optimized for <strong>conversational AI, semantic search, and data embedding</strong>, making it a versatile tool for a variety of use cases</p><h3>Cohere Model Categories</h3><p>Cohere’s LLMs are organized into <strong>three main categories</strong>, each tailored to specific tasks:</p><p><strong>1. Command Models</strong></p><ul><li>Designed for chatbots, conversational AI, and long-context reasoning tasks.</li></ul><p><strong>2. Rerank Models</strong></p><ul><li>Optimize search results and information retrieval by ranking inputs based on semantic relevance.</li></ul><p><strong>3. Embed Models</strong></p><ul><li>Generate high-quality vector embeddings for semantic search, clustering, and retrieval systems.</li></ul><h3>Command R: Cohere’s Advanced Model</h3><p>One of Cohere’s most advanced offerings is the <strong>Command R model</strong>, comparable to GPT-4.</p><ul><li><strong>Training Data:</strong> Large-scale internet datasets.</li><li><strong>Optimized For:</strong> Retrieval-Augmented Generation (RAG) workflows and tool-use scenarios.</li><li><strong>Context Window:</strong> Up to <strong>128,000 tokens</strong>, allowing extremely long documents or conversations.</li><li><strong>Multilingual Support:</strong> Strong performance across ten major languages.</li></ul><p>Command R is perfect for <strong>enterprise applications</strong> that require both <strong>context understanding</strong> and <strong>integration with external data or tools</strong>.</p><h3>Getting Started with Cohere</h3><p>Developers can quickly experiment with Cohere LLMs by <a href="https://dashboard.cohere.com/"><strong>creating a free account</strong></a> and obtaining an <strong>API key</strong>.</p><ul><li>The free trial has <strong>no strict time limits</strong>.</li><li>Allows up to <strong>100 API requests per minute</strong>, sufficient for prototyping and experimentation.</li></ul><p><strong>Security Best Practice:</strong> Store your API key in a .env file instead of hardcoding it:</p><pre>COHERE_API_KEY=&quot;your-api-key-here&quot;</pre><p>You can now generate text with Cohere as follows.</p><pre># Import the Cohere library<br>import cohere<br><br># Directly set your Cohere API key<br>cohere_key = &quot;API KEY&quot; <br><br># Initialize the Cohere client<br>co = cohere.Client(cohere_key)</pre><h3>Command Model (Chat / Conversation)</h3><pre><br># Perform a chat with context<br># chat_history contains previous conversation turns<br># message is the new question we want the model to answer<br>response = co.chat(<br>    chat_history=[<br>        {<br>            &quot;role&quot;: &quot;USER&quot;,<br>            &quot;message&quot;: &quot;Who discovered gravity?&quot;<br>        },<br>        {<br>            &quot;role&quot;: &quot;CHATBOT&quot;,<br>            &quot;message&quot;: &quot;The man who is widely credited with discovering gravity is Sir Isaac Newton.&quot;<br>        }<br>    ],<br>    message=&quot;What year was he born?&quot;<br>)<br><br># Print the chatbot&#39;s response<br>print(response.text)</pre><pre>Command Model reply: Sir Isaac Newton was born on January 4, 1643. However, it&#39;s worth noting that this date is according to the Julian calendar, which was in use in England at the time. In the Gregorian calendar, which is the standard calendar used today, his birth date is December 25, 1642.</pre><ul><li><strong>Use case:</strong> Chatbots, question answering, conversational AI</li><li><strong>Key feature:</strong> Understands previous messages (chat_history) to answer in context</li></ul><h3>Rerank Model (Semantic Ranking)</h3><pre>query = &quot;Who discovered gravity?&quot;<br>texts = [<br>    &quot;Isaac Newton was born in 1643 in England.&quot;,<br>    &quot;Albert Einstein developed the theory of relativity.&quot;,<br>    &quot;Galileo Galilei made significant discoveries in astronomy.&quot;<br>]<br><br># Use a current rerank model like rerank-v3.5<br>rerank_response = co.rerank(<br>    model=&quot;rerank-v3.5&quot;,   # valid model<br>    query=query,<br>    documents=texts,<br>    top_n=3<br>)<br><br>print(&quot;Reranked documents by relevance:&quot;)<br>for result in rerank_response.results:<br>    idx = result.index                   # index of the document in &#39;texts&#39;<br>    score = result.relevance_score       # relevance score<br>    print(f&quot;{texts[idx]} - score: {score:.4f}&quot;)</pre><pre>Reranked documents by relevance:<br>Isaac Newton was born in 1643 in England. - score: 0.2755<br>Galileo Galilei made significant discoveries in astronomy. - score: 0.1572<br>Albert Einstein developed the theory of relativity. - score: 0.1354</pre><ul><li><strong>Use case:</strong> Improve search engines or document retrieval systems</li><li><strong>Key feature</strong>: Returns relevance scores for each text relative to the query</li></ul><h3>Embed Model (Text Embeddings / Semantic Search)</h3><pre># Embed model: create vector embeddings for semantic search or clustering<br>texts = [<br>    &quot;Isaac Newton was a physicist.&quot;,<br>    &quot;Albert Einstein developed relativity.&quot;,<br>    &quot;Galileo studied astronomy.&quot;<br>]<br><br>embedding_response = co.embed(<br>    model=&quot;embed-english-v2.0&quot;,<br>    texts=texts<br>)<br><br>print(&quot;Embeddings for each text (vector length):&quot;)<br>for i, emb in enumerate(embedding_response.embeddings):<br>    print(f&quot;Text: {texts[i]}, Vector length: {len(emb)}&quot;)</pre><pre>Embeddings for each text (vector length):<br>Text: Isaac Newton was a physicist., Vector length: 4096<br>Text: Albert Einstein developed relativity., Vector length: 4096<br>Text: Galileo studied astronomy., Vector length: 4096</pre><ul><li><strong>Use case</strong>: Semantic search, clustering, recommendations</li><li><strong>Key feature</strong>: Converts text into numerical vectors that can be compared for similarity</li></ul><p>Cohere LLMs offer developers and enterprises <strong>powerful tools for building AI-driven applications</strong>, from chatbots to semantic search and document retrieval.</p><p>With <strong>Command, Rerank, and Embed models</strong>, strong multilingual support, and advanced long-context capabilities, Cohere enables both <strong>prototype development</strong> and <strong>enterprise-grade AI solutions</strong>.</p><p>Including examples like co.chat() demonstrates how easy it is to <strong>interact with LLMs in context</strong>, making it practical for <strong>real-world applications</strong>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1ca91354d9d3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Customer Segmentation in Real Marketing: How K-Means Can Boost Your Campaigns?]]></title>
            <link>https://medium.com/@justindjakas/customer-segmentation-in-real-marketing-how-k-means-can-boost-your-campaigns-826e1e8d72aa?source=rss-66abbb206fb4------2</link>
            <guid isPermaLink="false">https://medium.com/p/826e1e8d72aa</guid>
            <category><![CDATA[marketing]]></category>
            <category><![CDATA[business-improvement]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Justin DJAKAS]]></dc:creator>
            <pubDate>Thu, 26 Feb 2026 13:22:30 GMT</pubDate>
            <atom:updated>2026-02-26T13:37:41.291Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QVKbMOiuUT5oE6fGaCGp5w.png" /></figure><p>Ever wondered why some customers spend a fortune while others barely open your emails? In marketing, understanding your customers is gold — and data science can help.</p><p>In this article, I’ll show how to segment customers using K-Means, not on the classic Iris dataset, but on a real-life business scenario: marketing and customer targeting.</p><p>By the end, you’ll see how clustering can turn raw data into actionable business insights.</p><h4>The Business Problem</h4><p>A retail company wants to:</p><ul><li>Increase sales without annoying customers</li><li>Reward loyal buyers</li><li>Understand which customers respond best to campaigns</li></ul><p><strong>Goal</strong>: Segment customers in a way that marketing teams can target effectively.</p><p><strong>Preparing the Data</strong></p><pre>#Import librairies<br>import pandas as pd<br>import numpy as np<br>from sklearn.cluster import KMeans<br>from sklearn.preprocessing import StandardScaler,LabelEncoder<br>import matplotlib.pyplot as plt<br><br>#Dataset<br>customers = pd.read_csv(&quot;/content/drive/MyDrive/Costumers.csv&quot;)<br><br>#Display the 5 fives rows<br>customers.head()<br></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/570/1*dM4nDB2sGtOWyO2476_iSw.png" /></figure><p>We start with typical customer data:</p><ul><li><strong>Gender (Male/Female)</strong> — The customer’s gender.</li><li><strong>Age</strong> — How old the customer is.</li><li><strong>Annual Income</strong> — The customer’s yearly income in thousands of dollars.</li><li><strong>Spending Score (1–100)</strong> — A measure of the customer’s purchasing behavior and engagement.</li></ul><p>Machine learning can only handle numbers, so we encode the gender:</p><pre>df=customers.copy()<br>encoder=LabelEncoder()<br>df[&#39;Gender&#39;]=encoder.fit_transform(df[&#39;Gender&#39;])<br>df.head()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/561/1*NMZvyxJhDHuVTtdRN-JgrQ.png" /></figure><p>We extract the gender, age, annual income, and spending score columns. Then we use the elbow method to determine the optimum number of clusters based on these features:</p><pre>points = df.iloc[:, 1:5].values<br>inertias = []<br>for i in range(1, 15):<br>  kmeans = KMeans(n_clusters=i, random_state=0)<br>  kmeans.fit(points)<br>  inertias.append(kmeans.inertia_)<br>  print(i, kmeans.inertia_)<br>  print(&quot;   &quot;)<br><br>plt.plot(range(1, 15), inertias)<br>plt.xlabel(&#39;Number of Clusters&#39;)<br>plt.ylabel(&#39;Inertia&#39;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/597/1*TBA_oWjD6kspvP6IF8j8Ew.png" /></figure><p>As we can see<strong> 6 appears</strong> to be the reasonable number</p><p>Segment the customers into six clusters and we add a column named Cluster containing the index of the cluster (0–5) to which the customer was assigned:</p><pre>kmeans = KMeans(n_clusters=6, random_state=0)<br>kmeans.fit(points)<br>df[&#39;Cluster&#39;]=kmeans.predict(points)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/621/1*QMV3IgUpzl4XF6qLpo2DTQ.png" /></figure><p>We have assigned a cluster number to each customer, but what does it tell us?</p><p>We can’t visualize gender, age, annual income, and spending score together in a two-dimensional chart the way we can for just annual income and spending score. However, we can calculate the mean (average) of these attributes for each cluster using the cluster centroids. Let’s create a new DataFrame with columns for average age, average income, and so on, and then present the results in a table:</p><pre># First, we ensure the number of clusters matches the model<br>n_clusters = kmeans.n_clusters  # Get the number of clusters actually used<br><br># Create an empty DataFrame to store cluster summaries<br>results = pd.DataFrame(columns=[<br>    &#39;Cluster&#39;, &#39;Average Age&#39;, &#39;Average Income&#39;, &#39;Average Spending Index&#39;, <br>    &#39;Number of Females&#39;, &#39;Number of Males&#39;<br>])<br><br># Iterate over each cluster<br>for i in range(n_clusters):<br>    # Get all customers belonging to this cluster<br>    gdf = df[df[&#39;Cluster&#39;] == i]<br>    <br>    # Calculate averages directly from the cluster&#39;s data<br>    age = gdf[&#39;Age&#39;].mean()<br>    income = gdf[&#39;Annual Income (k$)&#39;].mean()<br>    spend = gdf[&#39;Spending Score (1-100)&#39;].mean()<br>    <br>    # Count females and males<br>    females = gdf[gdf[&#39;Gender&#39;] == 0].shape[0]  # Assuming 0 = Female<br>    males = gdf[gdf[&#39;Gender&#39;] == 1].shape[0]    # Assuming 1 = Male<br>    <br>    # Add row to results<br>    results.loc[i] = [i, age, income, spend, females, males]<br><br># Display the summary table<br>results.head(10)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/780/1*aUDGiMXh234kzf6RavnTkw.png" /></figure><p>To make it clear for marketing teams, we can assign business-oriented labels to each cluster:</p><pre>def segment_label(row):<br>    if row[&#39;Average Income&#39;] &gt; 80 and row[&#39;Average Spending Index&#39;] &gt; 70:<br>        return &#39;Premium High-Spender&#39;<br>    elif row[&#39;Average Income&#39;] &gt; 80:<br>        return &#39;High Income, Low-Spender&#39;<br>    elif row[&#39;Average Spending Index&#39;] &gt; 70<br>        return &#39;Young High-Spender&#39;<br>    elif row[&#39;Average Age&#39;] &lt; 30:<br>        return &#39;Young Moderate-Spender&#39;<br>    else:<br>        return &#39;Regular Customer&#39;<br><br>results[&#39;Segment_Label&#39;] = results.apply(segment_label, axis=1)<br><br>results.head()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dVDBBCrJfguDQJJ3F7PeuA.png" /></figure><p>Now each cluster has a clear label that marketing teams can immediately understand.</p><p>A simple plot can help communicate the insights visually.</p><pre>df[&#39;Gender&#39;] = df[&#39;Gender&#39;].map({0:&#39;Male&#39;, 1:&#39;Female&#39;})<br>markers={&#39;Male&#39;:&#39;o&#39;, &#39;Female&#39;:&#39;X&#39;}<br><br># Agrandir le graphique<br>plt.figure(figsize=(12, 8))  # largeur=12, hauteur=8<br><br># Scatterplot<br>sns.scatterplot(<br>    data=df, <br>    x=&#39;Annual Income (k$)&#39;, <br>    y=&#39;Spending Score (1-100)&#39;,<br>    hue=&#39;Segment_Label&#39;,   # Couleur par segment<br>    style=&#39;Gender&#39;,        # Forme par genre<br>    markers={&#39;Male&#39;:&#39;o&#39;, &#39;Female&#39;:&#39;X&#39;},  # Choix des symboles<br>    palette=&#39;Set2&#39;, <br>    s=150  # Taille des points<br>)<br><br># Titres et axes<br>plt.title(&quot;Customer Segmentation - Business Insight&quot;, fontsize=16)<br>plt.xlabel(&quot;Annual Income (k$)&quot;, fontsize=14)<br>plt.ylabel(&quot;Spending Score (1-100)&quot;, fontsize=14)<br><br># Légende<br>plt.legend(title=&quot;Segment / Gender&quot;, fontsize=12, title_fontsize=12)<br><br># Affichage<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1010/1*w9GbyA9IO7baU06_qZcVcA.png" /></figure><p><strong>💡 Interpretation:</strong> High-income but low-spending clusters are perfect targets for upsell campaigns, while young high-spenders are ideal for loyalty programs.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/717/1*eof7ayyd6-mGcbUf4XTZBg.png" /></figure><p>Imagine a retail company launching its summer campaign. Using these segments, they could target ‘High Income, Low-Spender’ with premium offers, reward ‘Young High-Spender’ with loyalty perks, and re-engage moderate spenders. The result? Higher conversion rates, happier customers, and smarter marketing spend.</p><p><strong>Key Takeaway</strong></p><p>Customer segmentation with K-Means is not just about numbers — it’s about turning data into actionable business strategy. Every campaign, promotion, or loyalty program can now be precision-targeted, increasing ROI and customer satisfaction. Real data science, real impact.</p><p>For those interested in the <strong>complete code and implementation</strong>, feel free to check out my GitHub: <a href="https://github.com/Just1919/Customer-Segmentation-in-Real-Marketing-with-K-Means">Customer Segmentation with K-Means</a>.</p><p>Connect with me on LinkedIn for more insights: <a href="https://www.linkedin.com/in/yawo-justin-djakas/">Yawo Justin DJAKAS</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=826e1e8d72aa" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building a Recommender System Step-by-Step: From Basics to Business Impact]]></title>
            <link>https://medium.com/@justindjakas/building-a-recommender-system-step-by-step-ebbe7e7d7fc1?source=rss-66abbb206fb4------2</link>
            <guid isPermaLink="false">https://medium.com/p/ebbe7e7d7fc1</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[recommender-systems]]></category>
            <dc:creator><![CDATA[Justin DJAKAS]]></dc:creator>
            <pubDate>Sun, 27 Jul 2025 21:01:39 GMT</pubDate>
            <atom:updated>2025-07-27T21:06:40.105Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ULDsLoQND-jLuUHtGC2JAg.png" /></figure><p>One important part of machine learning today is recommender systems. These are systems that suggest products, services, or content to users, based on their interests. Many big companies use recommender systems to increase their sales and keep users active.</p><p>For example:</p><p>1. Amazon makes 35% of its sales from its recommendation engine.</p><p>2. Netflix says that 75% of the shows people watch come from its recommendations.</p><p>3. Spotify reports that 30% of songs played are from recommended playlists.</p><p>4. YouTube shows that 70% of the videos watched are from its recommendations.</p><p>5. Alibaba uses recommendation systems for 20% of its product sales.</p><p>These numbers show how powerful recommendation systems are. They help companies offer the right product to the right customer at the right time.</p><p>The good news is that you don’t need to be Amazon or Netflix to build a recommender system. With some basic knowledge, even small businesses can create one.</p><p>There are different types of recommender systems:</p><p>* Popularity-based systems: They show items that are popular with everyone. For example, “Top trending videos on TikTok this week” or “Most booked homes on Airbnb today.” These systems do not need machine learning.</p><p>* Collaborative filtering: They suggest items based on what other users chose. For example, “People who listened to this album on Spotify also listened to these artists” or “Shoppers who bought this jacket on Zalando also bought these shoes.” This method looks at the choices of similar users.</p><p>* Content-based systems: These systems are more advanced and use machine learning. They recommend items based on the product’s details or features. For example, “If you watched Frozen 2 on Disney+, you might also like Moana” or “If you booked a beach house on Airbnb, you might like these seaside apartments too.”</p><p>1. A way to convert product features into numbers (vectorize them). You can use a Count Vectorizer to count the words in product descriptions.</p><p>2. A way to measure similarity between products. A simple method is cosine similarity, which compares two products and tells you how close they are.</p><p>Once you understand these two ideas, you can start building your own recommender system to boost your business.</p><h3><strong>Converting Text to Numbers: Vectorization Methods</strong></h3><p>Machine learning models cannot work directly with raw text data. Similarly, to calculate similarity between two vectors representing product attributes, we first need to <strong>vectorize</strong> the text — that is, convert it into numerical form. Several techniques exist to perform this transformation. Scikit-learn provides three main classes that simplify text cleaning and vectorization:</p><h4>CountVectorizer</h4><p>Builds a vocabulary dictionary from the text corpus and creates a matrix of word counts.</p><h4>HashingVectorizer</h4><p>Uses hashing functions instead of storing a vocabulary in memory, making it more memory-efficient while producing word count vectors.</p><h4>TfidfVectorizer</h4><p>Builds a vocabulary and produces a matrix of TF-IDF (Term Frequency-Inverse Document Frequency) scores, which reflect the importance of words relative to the corpus.</p><pre>from sklearn.feature_extraction.text import CountVectorizer, HashingVectorizer, TfidfVectorizer<br><br># Sample documents<br>documents = [<br>    &quot;I love data science and machine learning&quot;,<br>    &quot;Machine learning models need vectorized input&quot;,<br>    &quot;Data science is fun and powerful&quot;<br>]</pre><pre># 1. CountVectorizer example<br>count_vec = CountVectorizer()<br>count_matrix = count_vec.fit_transform(documents)<br>print(&quot;CountVectorizer matrix:\n&quot;, count_matrix.toarray())<br>print(&quot;Feature names:&quot;, count_vec.get_feature_names_out())</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/666/1*V8wtH0lLkjiJU4liE14q6w.png" /></figure><pre><br># 2. HashingVectorizer example<br>hash_vec = HashingVectorizer(n_features=10)  # specify number of features (hash buckets)<br>hash_matrix = hash_vec.transform(documents)<br>print(&quot;HashingVectorizer matrix:\n&quot;, hash_matrix.toarray())</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*TqVH1C9fatGooS16eqaUXA.png" /></figure><pre><br># 3. TfidfVectorizer example<br>tfidf_vec = TfidfVectorizer()<br>tfidf_matrix = tfidf_vec.fit_transform(documents)<br>print(&quot;TfidfVectorizer matrix:\n&quot;, tfidf_matrix.toarray())<br>print(&quot;Feature names:&quot;, tfidf_vec.get_feature_names_out())</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/692/1*On0pF68LEDM_IQCcCklONA.png" /></figure><p>As for which technique is preferable or considered the best, that will be the subject of another article. For now, after converting text into numerical representations, we will see how to compute their similarity.</p><h3><strong>Cosine Similarity</strong></h3><p>Cosine similarity is a technique used to measure how similar two vectors are — that is, how closely two sets of numbers align in space. The core idea is to take each numerical feature in a data sample — like the frequency of certain words in a document — and treat them as coordinates that form a vector starting from the origin (0,0,…). You repeat this for another sample, and then you compute the cosine of the angle between these vectors in an m-dimensional space, where m represents the number of features.</p><p>If two vectors are identical, they will point in exactly the same direction, making the angle between them zero. In this case, the cosine similarity equals 1, meaning they are perfectly similar. As the vectors differ more, the angle increases, and the cosine similarity decreases towards 0.</p><p>Let’s take a simple example in 2D space to visualize this. Imagine you have three data entries, each represented by two numbers:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/160/1*4Kio_yG92q5-bxTwqnVqUg.png" /></figure><p>Now, you want to find out whether Row 2 is more similar to Row 1 or Row 3. At first glance, it’s not obvious just by looking at the numbers. For instance, if you just added up the numbers in each row:</p><p>Row 1 sum = 6</p><p>Row 2 sum = 11</p><p>Row 3 sum = 8</p><p>You might mistakenly think Row 2 is closer to Row 3 because 11 and 8 are near each other. However, this ignores the direction of the vectors.</p><p>If we visualize these rows as vectors starting at (0,0):</p><p>Row 1: (0, 0) → (2, 4)</p><p>Row 2: (0, 0) → (4, 7)</p><p>Row 3: (0, 0) → (6, 2)</p><pre>import matplotlib.pyplot as plt<br>import numpy as np<br><br># Vecteurs définis<br>vectors = {<br>    &quot;Row 1&quot;: np.array([2, 4]),<br>    &quot;Row 2&quot;: np.array([4, 7]),<br>    &quot;Row 3&quot;: np.array([6, 2])<br>}<br><br>colors = {<br>    &quot;Row 1&quot;: &#39;r&#39;,<br>    &quot;Row 2&quot;: &#39;g&#39;,<br>    &quot;Row 3&quot;: &#39;b&#39;<br>}<br><br>def cosine_similarity(v1, v2):<br>    return np.dot(v1, v2) / (np.linalg.norm(v1) * np.linalg.norm(v2))<br><br>def angle_between(v1, v2):<br>    cos_sim = cosine_similarity(v1, v2)<br>    cos_sim = np.clip(cos_sim, -1, 1)<br>    angle_rad = np.arccos(cos_sim)<br>    angle_deg = np.degrees(angle_rad)<br>    return angle_deg<br><br>plt.figure(figsize=(7,7))<br>ax = plt.gca()<br><br># Tracer les vecteurs<br>for label, vec in vectors.items():<br>    ax.quiver(0, 0, vec[0], vec[1], angles=&#39;xy&#39;, scale_units=&#39;xy&#39;, scale=1, color=colors[label], label=label)<br><br># Limites du graphique<br>plt.xlim(0, 7)<br>plt.ylim(0, 7)<br>plt.xlabel(&#39;X axis&#39;)<br>plt.ylabel(&#39;Y axis&#39;)<br>plt.title(&#39;Visualization of vectors with angles between them&#39;)<br>plt.grid(True)<br><br># Calcul et annotation des angles entre chaque paire avec notation θ_i,j<br>pairs = [(&quot;Row 1&quot;, &quot;Row 2&quot;), (&quot;Row 1&quot;, &quot;Row 3&quot;), (&quot;Row 2&quot;, &quot;Row 3&quot;)]<br>positions = [(3,5), (4,2), (5,4)]  # positions pour afficher les textes des angles<br><br>for (i, (v1_label, v2_label)) in enumerate(pairs):<br>    angle = angle_between(vectors[v1_label], vectors[v2_label])<br>    x, y = positions[i]<br>    # Affichage avec indices en bas pour i,j<br>    ax.text(x, y, f&#39;θ_{{{i+1},{i+2}}} = {angle:.1f}°&#39;, fontsize=12, bbox=dict(facecolor=&#39;white&#39;, alpha=0.7))<br><br>plt.legend()<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/605/1*k2cOriuX25ramLH8BwKMTQ.png" /></figure><p>You can now visualize each row as a vector, calculate the cosine of the angles formed between vectors 1 and 2, as well as between vectors 2 and 3, and conclude that row 2 is more similar to row 1 than to row 3. This is essentially how cosine similarity works.</p><p>Cosine similarity is not limited to two dimensions — it also applies to higher-dimensional spaces. To simplify computing cosine similarities regardless of the number of dimensions, Scikit-learn provides the cosine_similarity function.</p><p>Here’s an example that calculates the cosine similarities among the three vectors from the example above:</p><pre>from sklearn.metrics.pairwise import cosine_similarity<br><br>data = [[2, 4], [4, 7], [6, 2]]<br>similarity_matrix = cosine_similarity(data)<br>print(similarity_matrix)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/348/1*VuvldUY6o-JXr_egjyOjqA.png" /></figure><p>The output above is a similarity matrix that shows the cosine values for each pair of vectors. The matrix is square, with both width and height equal to the number of samples.</p><p>From this, you can see the similarity between row 1 and row 2 is approximately 0.997, indicating they are very close. The similarity between row 2 and row 3 is about 0.812, which is higher than that between row 1 and row 3 (around 0.728). This confirms that row 2 is more similar to row 1 than to row 3.</p><h3>Building a Movie Recommendation System</h3><p>Let’s now apply cosine similarity to build a content-based movie recommender system. Begin by Downloading the dataset from this <a href="https://drive.google.com/file/d/1QPNqhizJvjQfiN0Afdo1LOsPhfo6thbU/view?usp=drive_link">link</a>. This dataset, sourced from Kaggle, contains detailed information on approximately 4,800 movies, including attributes such as title, budget, genres, keywords, cast, and more. Once downloaded, save the CSV file in the ‘Data’ subdirectory of your Jupyter notebooks. Afterward, load the dataset into your environment and take a moment to explore its structure and contents.</p><pre> import pandas as pd<br> data = pd.read_csv(&#39;/content/drive/MyDrive/movies  dataset/movies.csv&#39;)<br> data.head()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nIK8oI5LiB-6NbXBsQnHUA.png" /></figure><p>The dataset contains 24 columns, only a few of which are needed to describe a movie. Use the following statements to extract key columns such as title and genres and fill missing values with empty strings:</p><pre><br>#Make a copy<br>df=data.copy()<br># Select only the relevant columns from the dataset: &#39;title&#39;, &#39;genre&#39;, &#39;keywords&#39;, &#39;cast&#39;, and &#39;director&#39;<br>df = df[[&#39;title&#39;, &#39;genres&#39;, &#39;keywords&#39;, &#39;cast&#39;, &#39;director&#39;]]<br><br># Replace any missing (NaN) values in these columns with an empty string<br>df = df.fillna(&quot;&quot;)<br><br># Display the first 5 rows of the resulting DataFrame to verify the selection and cleaning<br>df.head()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jcicg6UrgKHYFrHR3yJgkg.png" /></figure><p>Next, create a new column called <strong><em>features</em></strong><em> </em>that merges the text from the other columns into a single combined string.</p><pre> df[&#39;features&#39;] = df[&#39;title&#39;] + &#39; &#39; + df[&#39;genres&#39;] + &#39; &#39; + df[&#39;keywords&#39;] + &#39; &#39; + df[&#39;cast&#39;] + &#39; &#39; + df[&#39;director&#39;]</pre><p>Use CountVectorizer to vectorize the text in the features column</p><pre>from sklearn.feature_extraction.text import CountVectorizer<br><br>vectorizer = CountVectorizer(stop_words=&#39;english&#39;, min_df=20)<br>word_matrix = vectorizer.fit_transform(df[&#39;features&#39;])<br>word_matrix.shape</pre><p>The table of word counts contains 4,803 rows — one for each movie — and 918 col umns. The next task is to compute cosine similarities for each row pair.</p><pre>from sklearn.metrics.pairwise import cosine_similarity<br>sim = cosine_similarity(word_matrix)</pre><p>The primary objective of this system is to take a movie title as input and retrieve the n most similar movies based on their content. To achieve this, we’ll define a function called<strong><em> get_recommendations</em></strong> that takes four parameters: the movie title, a DataFrame with movie details, a precomputed similarity matrix, and the number of similar movie titles to return.</p><pre>def get_recommendations(title, df, similarity_matrix, top_n=10):<br>    &quot;&quot;&quot;<br>    Returns the top N most similar movies to the given title based on the similarity matrix.<br><br>    Parameters:<br>    - title (str): The title of the movie to search for.<br>    - df (DataFrame): The DataFrame containing movie information.<br>    - similarity_matrix (array): The cosine similarity matrix.<br>    - top_n (int): Number of similar movie titles to return (default is 10).<br><br>    Returns:<br>    - List of recommended movie titles.<br>    &quot;&quot;&quot;<br>    # Find the index of the movie title (case-insensitive search)<br>    indices = df.index[df[&#39;title&#39;].str.lower() == title.lower()]<br><br>    # If the title is not found, return an empty list<br>    if len(indices) == 0:<br>        return []<br><br>    movie_index = indices[0]<br><br>    # Retrieve the similarity scores for the given movie<br>    similarity_scores = list(enumerate(similarity_matrix[movie_index]))<br><br>    # Sort movies by similarity score in descending order<br>    similarity_scores = sorted(similarity_scores, key=lambda x: x[1], reverse=True)<br><br>    # Exclude the first result (which is the movie itself with similarity 1.0)<br>    top_similar_indices = similarity_scores[1:top_n + 1]<br><br>    # Extract the movie titles corresponding to the top similar indices<br>    recommended_titles = [df.iloc[i[0]][&#39;title&#39;] for i in top_similar_indices]<br><br>    return recommended_titles</pre><p>This function sorts the cosine similarities in descending order to identify the count movies most like the one identified by the title parameter. Then it returns the titles of those movies.</p><p>Now use get_recommendations to search the database for similar movies. First ask for the 10 movies that are most similar to the James Bond thriller Skyfall.</p><pre>get_recommendations(&#39;Skyfall&#39;, df, sim)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/388/1*Ui40OX_Kp3UbXV-vsphAMQ.png" /></figure><p>Call get_recommendations again to list movies that are like</p><pre>get_recommendations(&quot;John Carter&quot;,df,sim)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/432/1*BxXnGv4AShCtNNC9ZXBBBw.png" /></figure><pre>get_recommendations(&quot;The Dark Knight Rises&quot;,df,sim)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/478/1*FYB5rvxDfaruLdk3mmDVHQ.png" /></figure><pre>get_recommendations(&quot;Pirates of the Caribbean: At World&#39;s End&quot;,df,sim)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/482/1*n8FO1GQ4A9IcGS8La3czug.png" /></figure><p>I believe you’ll agree that this system does a remarkably good job at identifying similar movies.In just few lines of code.</p><p>Of course, before vectorizing our text, there are essential preprocessing steps that should be considered. These typically include converting text to lowercase, removing punctuation symbols, and optionally eliminating stop words — common words like “the”, “and”, or “that” which often carry little significance in determining similarity.</p><p>Once the text is cleaned, it is then tokenized, i.e., broken down into individual words. Additionally, techniques such as stemming and lemmatization can be applied to reduce words to their root forms before vectorization. All these preprocessing techniques are fundamental and will be covered in detail in a future article.</p><p>You can access the full code implementation on my GitHub repository: <a href="https://github.com/Just1919/Recommender_Systems">https://github.com/Just1919/Recommender_Systems</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ebbe7e7d7fc1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Cross-Validation vs. Train/Test Split: When and Why You Should Use Each?]]></title>
            <link>https://medium.com/@justindjakas/cross-validation-vs-train-test-split-when-and-why-you-should-use-each-1900b95e08e6?source=rss-66abbb206fb4------2</link>
            <guid isPermaLink="false">https://medium.com/p/1900b95e08e6</guid>
            <category><![CDATA[data-preprocessing]]></category>
            <category><![CDATA[k-fold-cross-validation]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[Justin DJAKAS]]></dc:creator>
            <pubDate>Sun, 01 Jun 2025 21:09:12 GMT</pubDate>
            <atom:updated>2025-06-01T21:09:12.653Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dVkhtMzPNjxkAYoIkhwlNA.png" /></figure><p>As you progress on your machine learning journey, you’ll learn that building a model isn’t just about training — it’s also about testing. Testing helps us evaluate how well a model performs on unseen data. But here’s the catch: if your test data isn’t truly representative, or if your dataset is too small, you might end up with misleading results.</p><p>In this article, we’ll dive deep into:</p><ul><li>The limitations of train_test_split</li><li>Why small datasets are especially sensitive</li><li>How cross-validation comes to the rescue</li><li>When to use each strategy in practice</li></ul><p><strong>The Basics:</strong> Why Do We Split the Data?</p><p>Whenever you train a model, it’s crucial to evaluate it on a separate dataset. This helps you gauge how well it generalizes to new, unseen data not just the examples it was trained on.</p><p>In Scikit-Learn, this is typically done using:</p><pre>from sklearn.model_selection import train_test_split</pre><p>It randomly splits the data into training and test sets. For example, you might allocate 80% of your data for training and 20% for testing.</p><p><strong>A Real Example:</strong> California Housing Dataset</p><p>arrow_upwardarrow_downwardlinkcommenteditdeletemore_vert</p><p><strong>Let’s see how a simple model’s accuracy can vary just by changing how the data is split.</strong></p><pre>from sklearn.utils import shuffle<br>from sklearn.datasets import fetch_california_housing<br><br>df = fetch_california_housing(as_frame=True).frame<br>df = shuffle(df, random_state=0)<br>df = df.head(1200)  # Only use 1,200 rows</pre><p>Let’s train a linear regression model:</p><pre>from sklearn.linear_model import LinearRegression<br>from sklearn.model_selection import train_test_split<br><br>x = df.drop([&#39;MedHouseVal&#39;], axis=1)<br>y = df[&#39;MedHouseVal&#39;]<br><br>x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=0)<br><br>model = LinearRegression()<br>model.fit(x_train, y_train)<br>print(model.score(x_test, y_test))  # Output: ~0.68</pre><p>Now change the seed:</p><pre>x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=1)<br><br>model = LinearRegression()<br>model.fit(x_train, y_train)<br>print(model.score(x_test, y_test))  # Output: ~0.70</pre><p>Two different seeds, two different scores. So… is the model 68% accurate or 70% accurate?</p><p>The truth is: <strong>the smaller the dataset, the more sensitive your results are to how it’s split.</strong></p><h3>Cross-Validation to the Rescue</h3><p>Enter <strong>k-fold cross-validation</strong>. Instead of splitting once, we split the data into k equal parts <strong>(commonly 5 or 10)</strong>, train the model on k-1 parts, and test it on the remaining part.</p><p>This is repeated k times, and the scores are averaged.</p><pre>from sklearn.model_selection import cross_val_score<br><br>scores = cross_val_score(model, x, y, cv=5)<br>print(scores.mean())  # Output: ~0.64</pre><p>This averaged score is more reliable because it smooths out the randomness of a single split.</p><p><strong>Bonus:</strong> It uses all your data</p><p>You don’t have to train a model before cross-validating it since cross_val_score trains it for you. However, cross_val_score trains a copy of the model, not the model itself, so once you’ve used cross-validation to gauge the model’s accuracy, you still need to call fit before making predictions:</p><pre>model.fit(x, y)</pre><p>If you have a validation or production test set, still test your model on completely unseen data at the very end to validate its performance.</p><p>A model’s performance metric is only as trustworthy as the data used to test it. For small datasets, relying on a single train/test split can lead to false confidence — or unwarranted pessimism. Cross-validation gives you a broader, more stable estimate of your model’s capabilities.</p><p>You can find all the code and examples from this article on my GitHub repository: <a href="https://github.com/Just1919"><strong>github.com/Just1919</strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1900b95e08e6" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>