<?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 Reema Kumaritech on Medium]]></title>
        <description><![CDATA[Stories by Reema Kumaritech on Medium]]></description>
        <link>https://medium.com/@reema.kumaritech?source=rss-cb321e7882e1------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*3g6ZFpGz0nRvj6p5</url>
            <title>Stories by Reema Kumaritech on Medium</title>
            <link>https://medium.com/@reema.kumaritech?source=rss-cb321e7882e1------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 16:39:21 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@reema.kumaritech/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[Claude certification]]></title>
            <link>https://medium.com/@reema.kumaritech/claude-certification-f6a27349af5d?source=rss-cb321e7882e1------2</link>
            <guid isPermaLink="false">https://medium.com/p/f6a27349af5d</guid>
            <category><![CDATA[anthropic-claude]]></category>
            <category><![CDATA[claude]]></category>
            <dc:creator><![CDATA[Reema Kumaritech]]></dc:creator>
            <pubDate>Thu, 19 Nov 2026 10:38:01 GMT</pubDate>
            <atom:updated>2026-11-19T10:38:01.724Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sn5_VG-OQZoywGXoSZRPDw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CMcPS7lxrmFMxsBoMpVMzg.png" /></figure><p>&lt;notes&gt;<br>&lt;critical&gt;<br>Below are notes from a video course about working with the Claude language model.<br>Use these notes as a resource to answer the user’s question.<br>Write your answer as a standalone response — do not refer directly to these notes unless specifically requested by the user.<br>&lt;/critical&gt;</p><p>&lt;note title=”Overview of Claude Models”&gt;<br>Claude has three model families optimized for different priorities:</p><p>Opus = highest intelligence model for complex, multi-step tasks requiring deep reasoning and planning. Trade-off: higher cost and latency.</p><p>Sonnet = balanced model with good intelligence, speed, and cost efficiency. Strong coding abilities and precise code editing. Best for most practical use cases.</p><p>Haiku = fastest model optimized for speed and cost efficiency. No reasoning capabilities like Opus/Sonnet. Best for real-time user interactions and high-volume processing.</p><p>Selection framework: Intelligence priority → Opus. Speed priority → Haiku. Balanced requirements → Sonnet.</p><p>Common approach = use multiple models in same application based on specific task requirements rather than single model selection.</p><p>All models share core capabilities: text generation, coding, image analysis. Main difference is optimization focus.<br>&lt;/note&gt;</p><p>&lt;note title=”Accessing the API”&gt;<br>API Access Flow = 5-step process from user input to response display</p><p>Step 1: Client sends user text to developer’s server (never access Anthropic API directly from client apps to keep API key secret)</p><p>Step 2: Server makes request to Anthropic API using SDK (Python, TypeScript, JavaScript, Go, Ruby) or plain HTTP. Required parameters = API key + model name + messages list + max_tokens limit</p><p>Step 3: Text generation process has 4 stages:<br>- Tokenization = breaking input into tokens (words/word parts/symbols/spaces)<br>- Embedding = converting tokens to number lists representing all possible word meanings<br>- Contextualization = adjusting embeddings based on neighboring tokens to determine precise meaning<br>- Generation = output layer produces probabilities for next word, model selects using probability + randomness, adds selected word, repeats process</p><p>Step 4: Model stops when max_tokens reached or special end_of_sequence token generated</p><p>Step 5: API returns response with generated text + usage counts + stop_reason to server, server sends to client for display</p><p>Token = text chunk (word/part/symbol)<br>Embedding = numerical representation of word meanings<br>Contextualization = meaning refinement using neighboring words<br>Max_tokens = generation length limit<br>Stop_reason = why model stopped generating<br>&lt;/note&gt;</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*b11h7c97ceQEc1s7WX3vYQ.png" /></figure><p>This guide will walk you through the process of creating an API key.</p><h3>Step One: Navigate to the Anthropic API Console</h3><p>In your browser, navigate to <a href="https://console.anthropic.com/">https://console.anthropic.com/</a> and log in to your Anthropic account. You’ll then see a page like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ZLUMt7ZreKF9437u.png" /></figure><h3>Step Two: Click the ‘Get API Keys’ button</h3><p>This button can be found towards the top right of the main dashboard page.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*SWsF2JKBhR0NN0DG.png" /></figure><h3>Step Three: Click the ‘Create Key’ button</h3><p>At the top right of the page, find the ‘Create Key’ button and click it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*v47o3MuSKilZ7dDY.png" /></figure><h3>Step Four: Enter a workspace and name for your key</h3><p>Create the key in workspace ‘Default’ and enter a name for your key. This name is used to help you identify the keys you generate. Let’s use a name of ‘Anthropic Course’.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*6dGZYCTVcwqeTWxf.png" /></figure><h3>Step Five: Copy the Key</h3><p>Your API key will then be displayed in a pop up window. Copy this key and hold onto it — we will use it in the next video. This key will only be displayed once, so make sure you copy it!</p><p>If you accidentally close the window, delete the old key and generate it again.</p><h3>Setting Up Your Environment</h3><p>Before making any API calls, you need to install the required packages and configure your API key securely.</p><p>First, install the necessary dependencies in your Jupyter notebook:</p><pre>%pip install anthropic python-dotenv</pre><p>Next, create a <strong>.env</strong> file in the same directory as your notebook to store your API key securely:</p><pre>ANTHROPIC_API_KEY=&quot;your-api-key-here&quot;</pre><p>This approach keeps your API key out of your code and prevents accidentally committing it to version control. Always add <strong>.env</strong> to your <strong>.gitignore</strong> file.</p><p>Load the environment variables and create your API client:</p><pre>from dotenv import load_dotenv<br>load_dotenv()</pre><pre>from anthropic import Anthropic</pre><pre>client = Anthropic()<br>model = &quot;claude-sonnet-4-0&quot;</pre><h3>The Create Function</h3><p>The core of making API requests is the <strong>client.messages.create()</strong> function. This function requires three key parameters:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*RicKSUx6JT0ApzDt.png" /></figure><ul><li>model — The name of the Claude model you want to use</li><li>max_tokens — A safety limit on response length (not a target)</li><li>messages — The conversation history you’re sending to Claude</li></ul><p>The <strong>max_tokens</strong> parameter acts as a safety mechanism. If you set it to 1000, Claude will stop generating after 1000 tokens even if it has more to say. Claude doesn&#39;t try to reach this limit - it just writes what it thinks is appropriate and stops if it hits the maximum.</p><h3>Understanding Messages</h3><p>Messages represent the conversation between you and Claude, similar to a chat application. There are two types of messages:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*bIrUi6Pc0JRkeMJH.png" /></figure><ul><li>User messages — Content you want to send to Claude (written by humans)</li><li>Assistant messages — Responses that Claude has generated</li></ul><p>Each message is a dictionary with a <strong>role</strong> (either &quot;user&quot; or &quot;assistant&quot;) and <strong>content</strong> (the actual text).</p><h3>Making Your First Request</h3><p>Here’s a complete example of making a request to Claude:</p><pre>message = client.messages.create(<br>    model=model,<br>    max_tokens=1000,<br>    messages=[<br>        {<br>            &quot;role&quot;: &quot;user&quot;,<br>            &quot;content&quot;: &quot;What is quantum computing? Answer in one sentence&quot;<br>        }<br>    ]<br>)</pre><p>When you run this code, Claude will process your request and return a response object containing the generated text along with metadata about the request.</p><h3>Extracting the Response</h3><p>The response object contains a lot of information, but you usually just want the generated text. Access it using:</p><pre>message.content[0].text</pre><p>This gives you clean, readable output like: “Quantum computing is a type of computation that leverages quantum mechanics principles like superposition and entanglement to process information using quantum bits (qubits), potentially solving certain complex problems exponentially faster than classical computers.”</p><p>With these basics in place, you can start experimenting with different prompts and building more complex interactions with Claude.</p><p>When working with the Anthropic API and Claude, there’s a crucial concept you need to understand: Claude doesn’t store any of your conversation history. Each request you make is completely independent, with no memory of previous exchanges.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*kJTJyhkI0Zt463bQ.png" /></figure><p>This means if you want to have a multi-turn conversation where Claude remembers context from earlier messages, you need to handle the conversation state yourself.</p><h3>The Problem with Stateless Conversations</h3><p>Let’s say you ask Claude “What is quantum computing?” and get a good response. Then you follow up with “Write another sentence” — Claude has no idea what you’re referring to. It will write a sentence about something completely random because it has no memory of the quantum computing discussion.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*nczcJ_fagCLp4Jrr.png" /></figure><h3>How Multi-Turn Conversations Work</h3><p>To maintain conversation context, you need to do two things:</p><ul><li>Manually maintain a list of all messages in your code</li><li>Send the complete message history with every request</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Wy6xYMKLDbEE5of7.png" /></figure><p>Here’s the flow that actually works:</p><ol><li>Send your initial user message to Claude</li><li>Take Claude’s response and add it to your message list as an assistant message</li><li>Add your follow-up question as another user message</li><li>Send the entire conversation history to Claude</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*iOREZv8w0iir4G0h.png" /></figure><h3>Building Helper Functions</h3><p>To make conversation management easier, you can create three helper functions:</p><pre>def add_user_message(messages, text):<br>    user_message = {&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: text}<br>    messages.append(user_message)</pre><pre>def add_assistant_message(messages, text):<br>    assistant_message = {&quot;role&quot;: &quot;assistant&quot;, &quot;content&quot;: text}<br>    messages.append(assistant_message)</pre><pre>def chat(messages):<br>    message = client.messages.create(<br>        model=model,<br>        max_tokens=1000,<br>        messages=messages,<br>    )<br>    return message.content[0].text</pre><h3>Putting It All Together</h3><p>Here’s how you use these functions to maintain a conversation:</p><pre># Start with an empty message list<br>messages = []</pre><pre># Add the initial user question<br>add_user_message(messages, &quot;Define quantum computing in one sentence&quot;)</pre><pre># Get Claude&#39;s response<br>answer = chat(messages)</pre><pre># Add Claude&#39;s response to the conversation history<br>add_assistant_message(messages, answer)</pre><pre># Add a follow-up question<br>add_user_message(messages, &quot;Write another sentence&quot;)</pre><pre># Get the follow-up response with full context<br>final_answer = chat(messages)</pre><p>Now Claude will understand that “Write another sentence” refers to expanding on the quantum computing definition, because you’ve provided the complete conversation context.</p><p>These helper functions will be useful throughout your work with Claude, making it much easier to build applications that can maintain meaningful conversations over multiple exchanges.</p><p>System prompts are a powerful way to customize how Claude responds to user input. Instead of getting generic answers, you can shape Claude’s tone, style, and approach to match your specific use case.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*t9Ct83sgRpuq4h9o.png" /></figure><h3>Why System Prompts Matter</h3><p>Consider building a math tutor chatbot. When a student asks “How do I solve 5x + 2 = 3 for x?”, you want Claude to act like a real tutor, not just spit out the answer. A good math tutor should:</p><ul><li>Initially give hints rather than complete solutions</li><li>Patiently walk students through problems step by step</li><li>Show solutions for similar problems as examples</li></ul><p>You definitely don’t want Claude to:</p><ul><li>Immediately give direct answers</li><li>Tell students to just use a calculator</li></ul><h3>How System Prompts Work</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*alo3PHpF97U8sIcY.png" /></figure><p>System prompts provide Claude with guidance on how to respond. You define them as plain strings and pass them into the create function call. The key benefits are:</p><ul><li>System prompts provide Claude guidance on how to respond</li><li>Claude will try to respond in the same way someone in the specified role would respond</li><li>Helps keep Claude on task</li></ul><p>Here’s the basic structure:</p><pre>system_prompt = &quot;&quot;&quot;<br>You are a patient math tutor.<br>Do not directly answer a student&#39;s questions.<br>Guide them to a solution step by step.<br>&quot;&quot;&quot;</pre><pre>client.messages.create(<br>    model=model,<br>    messages=messages,<br>    max_tokens=1000,<br>    system=system_prompt<br>)</pre><h3>Seeing the Difference</h3><p>Without a system prompt, Claude gives a complete step-by-step solution immediately. This might be helpful, but it doesn’t encourage the student to think through the problem themselves.</p><p>With the math tutor system prompt, Claude’s response changes dramatically. Instead of providing the full solution, Claude asks guiding questions like “What do you think would be a good first step to isolate x? Consider what operation we might need to perform on both sides to start moving terms around.”</p><h3>Building a Flexible Chat Function</h3><p>Rather than hard-coding system prompts, you can make your chat function more reusable by accepting system prompts as parameters:</p><pre>def chat(messages, system=None):<br>    params = {<br>        &quot;model&quot;: model,<br>        &quot;max_tokens&quot;: 1000,<br>        &quot;messages&quot;: messages,<br>    }<br>    <br>    if system:<br>        params[&quot;system&quot;] = system<br>    <br>    message = client.messages.create(**params)<br>    return message.content[0].text</pre><p>This approach handles an important detail: Claude’s API doesn’t accept <strong>system=None</strong>, so you need to conditionally include the system parameter only when it&#39;s provided.</p><p>Now you can call your chat function with or without a system prompt:</p><pre># Without system prompt<br>answer = chat(messages)</pre><pre># With system prompt<br>system = &quot;&quot;&quot;<br>You are a patient math tutor.<br>Do not directly answer a student&#39;s questions.<br>Guide them to a solution step by step.<br>&quot;&quot;&quot;<br>answer = chat(messages, system=system)</pre><p>System prompts are essential for creating AI applications that behave consistently and appropriately for their intended purpose. They transform generic AI responses into specialized, role-appropriate interactions.</p><p>Temperature is a powerful parameter that controls how predictable or creative Claude’s responses will be. Understanding how to use it effectively can dramatically improve your AI applications.</p><h3>How Claude Generates Text</h3><p>Before diving into temperature, it helps to understand Claude’s text generation process. When you send Claude a prompt like “What do you think?”, it goes through three key steps:</p><ul><li>Tokenization — Breaking your input into smaller chunks</li><li>Prediction — Calculating probabilities for possible next words</li><li>Sampling — Choosing a token based on those probabilities</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*6NmPJEzxAPtFIVdU.png" /></figure><p>In this example, Claude might assign a 30% probability to “about”, 20% to “would”, 10% to “of”, and so on. The model then selects one token and repeats this entire process to build complete sentences.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*nHILNoF8aR0OwMDX.png" /></figure><h3>What Temperature Does</h3><p>Temperature is a decimal value between 0 and 1 that directly influences these selection probabilities. It’s like adjusting the “creativity dial” on Claude’s responses.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*aRu_YJPOTyldk0d5.png" /></figure><p>At low temperatures (near 0), Claude becomes very deterministic — it almost always picks the highest probability token. At high temperatures (near 1), Claude distributes probability more evenly across options, leading to more varied and creative outputs.</p><h3>Interactive Temperature Demo</h3><p>You can see temperature in action with Claude’s interactive demo. Watch how the probability distribution changes as you adjust the temperature slider:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*VUlOfHbx6taU7a-t.png" /></figure><p>At temperature 0.0, “about” gets 100% probability — completely deterministic. At temperature 1.0, probabilities spread more evenly across all possible tokens, introducing randomness and creativity.</p><h3>Choosing the Right Temperature</h3><p>Different tasks call for different temperature ranges:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*XaHpm2rk941kqWUt.png" /></figure><h3>Low Temperature (0.0–0.3)</h3><ul><li>Factual responses</li><li>Coding assistance</li><li>Data extraction</li><li>Content moderation</li></ul><h3>Medium Temperature (0.4–0.7)</h3><ul><li>Summarization</li><li>Educational content</li><li>Problem-solving</li><li>Creative writing with constraints</li></ul><h3>High Temperature (0.8–1.0)</h3><ul><li>Brainstorming</li><li>Creative writing</li><li>Marketing content</li><li>Joke generation</li></ul><h3>Implementing Temperature in Code</h3><p>Adding temperature support to your chat function is straightforward. Here’s how to modify your existing function:</p><pre>def chat(messages, system=None, temperature=1.0):<br>    params = {<br>        &quot;model&quot;: model,<br>        &quot;max_tokens&quot;: 1000,<br>        &quot;messages&quot;: messages,<br>        &quot;temperature&quot;: temperature<br>    }<br>    <br>    if system:<br>        params[&quot;system&quot;] = system<br>    <br>    message = client.messages.create(**params)<br>    return message.content[0].text</pre><p>The key changes are adding <strong>temperature=1.0</strong> as a parameter and including <strong>&quot;temperature&quot;: temperature</strong> in the params dictionary.</p><h3>Testing Temperature Effects</h3><p>To see temperature in action, try generating movie ideas with different settings:</p><pre># Low temperature - more predictable<br>answer = chat(messages, temperature=0.0)</pre><pre># High temperature - more creative  <br>answer = chat(messages, temperature=1.0)</pre><p>At temperature 0.0, you might consistently get responses like “A time-traveling archaeologist must prevent ancient artifacts from being stolen.” At temperature 1.0, you’ll see much more variety in themes, characters, and plot elements.</p><h3>Key Takeaways</h3><p>Remember that temperature doesn’t guarantee different outputs — it just changes the probability of getting them. Even at high temperatures, Claude might occasionally produce similar responses. The key is matching your temperature choice to your specific use case:</p><ul><li>Need consistent, factual responses? Use low temperature</li><li>Want creative brainstorming? Dial up the temperature</li><li>Somewhere in between? Medium temperatures work well for most general tasks</li></ul><p>Temperature is one of the most practical parameters you can adjust to fine-tune Claude’s behavior for your specific needs.</p><pre>Response streaming</pre><p>When building chat applications with Claude, there’s a significant user experience challenge: responses can take 10–30 seconds to generate, leaving users staring at a loading spinner. The solution is response streaming, which lets users see text appear chunk by chunk as Claude generates it, creating a much more responsive feel.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*dJmfIGk2P0owi59J.png" /></figure><h3>The Problem with Standard Responses</h3><p>In a typical chat setup, your server sends a user message to Claude and waits for the complete response before sending anything back to the client. This creates an awkward delay where users have no feedback that anything is happening.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*HObbFyV9JE8_XCrL.png" /></figure><h3>How Streaming Works</h3><p>With streaming enabled, Claude immediately sends back an initial response indicating it has received your request and is starting to generate text. Then you receive a series of events, each containing a small piece of the overall response.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*gQm-SpGQrUEn7H7o.png" /></figure><p>Your server can forward these text chunks to your client application as they arrive, allowing users to see the response building up word by word. All of these events are part of a single request to Claude.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*jaI-z0IGLmHne5zN.png" /></figure><h3>Understanding Stream Events</h3><p>When you enable streaming, Claude sends back several types of events:</p><ul><li>MessageStart — A new message is being sent</li><li>ContentBlockStart — Start of a new block containing text, tool use, or other content</li><li>ContentBlockDelta — Chunks of the actual generated text</li><li>ContentBlockStop — The current content block has been completed</li><li>MessageDelta — The current message is complete</li><li>MessageStop — End of information about the current message</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*uk2CvNEe2alc3noi.png" /></figure><p>The <strong>ContentBlockDelta</strong> events contain the actual generated text that you&#39;ll want to display to users.</p><h3>Basic Streaming Implementation</h3><p>To enable streaming, add <strong>stream=True</strong> to your messages.create call:</p><pre>messages = []<br>add_user_message(messages, &quot;Write a 1 sentence description of a fake database&quot;)</pre><pre>stream = client.messages.create(<br>    model=model,<br>    max_tokens=1000,<br>    messages=messages,<br>    stream=True<br>)</pre><pre>for event in stream:<br>    print(event)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*qrrILi4ILIauqgT1.png" /></figure><h3>Simplified Text Streaming</h3><p>Rather than manually parsing events, you can use the SDK’s simplified streaming interface that extracts just the text content:</p><pre>with client.messages.stream(<br>    model=model,<br>    max_tokens=1000,<br>    messages=messages<br>) as stream:<br>    for text in stream.text_stream:<br>        print(text, end=&quot;&quot;)</pre><p>This approach automatically filters out everything except the actual text content, which is usually what you need for displaying responses to users.</p><h3>Getting the Complete Message</h3><p>While streaming individual chunks is great for user experience, you often need the complete message for storage or further processing. After streaming completes, you can get the assembled final message:</p><pre>with client.messages.stream(<br>    model=model,<br>    max_tokens=1000,<br>    messages=messages<br>) as stream:<br>    for text in stream.text_stream:<br>        # Send each chunk to your client<br>        pass<br>    <br>    # Get the complete message for database storage<br>    final_message = stream.get_final_message()</pre><p>This gives you the best of both worlds: real-time streaming for users and a complete message object for your application logic.</p><h3>Structured data</h3><p>When you need Claude to generate structured data like JSON, Python code, or bulleted lists, you’ll often run into a common problem: Claude wants to be helpful and add explanatory text around your content. While this is usually great, sometimes you need just the raw data with nothing else.</p><p>Consider building a web app that generates AWS EventBridge rules. Users enter a description, click generate, and expect to see clean JSON they can immediately copy and use. If Claude returns the JSON wrapped in markdown code blocks with explanatory text, users can’t simply copy the entire response — they have to manually select just the JSON portion.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Qu6ySO4C-5wSwUGu.png" /></figure><h3>The Problem with Default Responses</h3><p>By default, when you ask Claude to generate JSON, you might get something like this:</p><pre>```json<br>{<br>  &quot;source&quot;: [&quot;aws.ec2&quot;],<br>  &quot;detail-type&quot;: [&quot;EC2 Instance State-change Notification&quot;],<br>  &quot;detail&quot;: {<br>    &quot;state&quot;: [&quot;running&quot;]<br>  }<br>}<br>```</pre><pre>This rule captures EC2 instance state changes when instances start running.</pre><p>The JSON is correct, but it’s wrapped in markdown formatting and includes explanatory text. For a web app where users need to copy the raw JSON, this creates friction in the user experience.</p><h3>The Solution: Assistant Message Prefilling + Stop Sequences</h3><p>You can combine assistant message prefilling with stop sequences to get exactly the content you want. Here’s how it works:</p><pre>messages = []</pre><pre>add_user_message(messages, &quot;Generate a very short event bridge rule as json&quot;)<br>add_assistant_message(messages, &quot;```json&quot;)</pre><pre>text = chat(messages, stop_sequences=[&quot;```&quot;])</pre><p>This technique works by:</p><ol><li>The user message tells Claude what to generate</li><li>The prefilled assistant message makes Claude think it already started a markdown code block</li><li>Claude continues by writing just the JSON content</li><li>When Claude tries to close the code block with <strong>```</strong>, the stop sequence immediately ends generation</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*TU1tKMdLFYjejbGp.png" /></figure><p>The result is clean JSON with no extra formatting:</p><pre>{<br>  &quot;source&quot;: [&quot;aws.ec2&quot;],<br>  &quot;detail-type&quot;: [&quot;EC2 Instance State-change Notification&quot;],<br>  &quot;detail&quot;: {<br>    &quot;state&quot;: [&quot;running&quot;]<br>  }<br>}</pre><h3>Processing the Response</h3><p>You might notice some extra newline characters in the response. These are easy to handle:</p><pre>import json</pre><pre># Clean up and parse the JSON<br>clean_json = json.loads(text.strip())</pre><h3>Beyond JSON</h3><p>This technique isn’t limited to JSON generation. Use it anytime you need structured data without commentary:</p><ul><li>Python code snippets</li><li>Bulleted lists</li><li>CSV data</li><li>Any formatted content where you want just the content, not explanations</li></ul><p>The key is identifying what Claude naturally wants to wrap your content in, then using that as your prefill and stop sequence. For code, it’s usually markdown code blocks. For lists, it might be different formatting markers.</p><p>This approach gives you precise control over Claude’s output format, making it much easier to integrate AI-generated content into applications where clean, structured data is essential.</p><h3>Prompt evaluation</h3><p>When working with Claude, writing a good prompt is just the beginning. To build reliable AI applications, you need to understand two critical concepts: prompt engineering and prompt evaluation. Prompt engineering gives you techniques for writing better prompts, while prompt evaluation helps you measure how well those prompts actually work.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*7XBC51-wVgUYRXpZ.png" /></figure><h3>Prompt Engineering vs Prompt Evaluation</h3><p>Prompt engineering is your toolkit for crafting effective prompts. It includes techniques like:</p><ul><li>Multishot prompting</li><li>Structuring with XML tags</li><li>Many other best practices</li></ul><p>These techniques help Claude understand exactly what you’re asking for and how you want it to respond.</p><p>Prompt evaluation takes a different approach. Instead of focusing on how to write prompts, it’s about measuring their effectiveness through automated testing. You can:</p><ul><li>Test against expected answers</li><li>Compare different versions of the same prompt</li><li>Review outputs for errors</li></ul><h3>Three Paths After Writing a Prompt</h3><p>Once you’ve drafted a prompt, you typically face three options for what to do next:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*vooxsLHrq9F2DO5d.png" /></figure><p>Option 1: Test the prompt once and decide it’s good enough. This carries a significant risk of breaking in production when users provide unexpected inputs.</p><p>Option 2: Test the prompt a few times and tweak it to handle a corner case or two. While better than option 1, users will often provide very unexpected outputs that you haven’t considered.</p><p>Option 3: Run the prompt through an evaluation pipeline to score it, then iterate on the prompt based on objective metrics. This approach requires more work and cost, but gives you much more confidence in your prompt’s reliability.</p><h3>Why Most Engineers Fall Into Testing Traps</h3><p>Options 1 and 2 are common traps that all engineers fall into, myself included. It’s natural to write a prompt for a serious application and not test it thoroughly enough. We tend to underestimate how many edge cases real users will encounter.</p><p>The reality is that when you deploy a prompt to production, users will interact with it in ways you never anticipated. What seemed like a solid prompt during your limited testing can quickly break down when faced with the full variety of real-world inputs.</p><h3>The Evaluation-First Approach</h3><p>Option 3 represents a more systematic approach to prompt development. By running your prompt through an evaluation pipeline, you get objective metrics about its performance across a broader range of test cases. This data-driven approach lets you:</p><ul><li>Identify weaknesses before they become production issues</li><li>Compare different prompt versions objectively</li><li>Iterate with confidence based on measurable improvements</li><li>Build more reliable AI applications</li></ul><p>While this approach requires more upfront investment in time and testing infrastructure, it pays dividends in the reliability and robustness of your final application. The goal is to catch problems during development rather than after your users encounter them.</p><h3>A typical eval workflow</h3><p>A typical prompt evaluation workflow follows five key steps that help you systematically improve your prompts through objective measurement. While there are many different ways to assemble these workflows and various open source and paid tools available, understanding the core process helps you start small and scale up as needed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*j8jnr3oVzQL6HFyI.png" /></figure><h3>Step 1: Draft a Prompt</h3><p>Start by writing an initial prompt that you want to improve. For this example, we’ll use a simple prompt:</p><pre>prompt = f&quot;&quot;&quot;<br>Please answer the user&#39;s question:</pre><pre>{question}<br>&quot;&quot;&quot;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Og9OeD_NX32mVRIO.png" /></figure><p>This basic prompt will serve as our baseline for testing and improvement.</p><h3>Step 2: Create an Eval Dataset</h3><p>Your evaluation dataset contains sample inputs that represent the types of questions or requests your prompt will handle in production. The dataset should include questions that will be interpolated into your prompt template.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Jwl3SacWydlEVfjK.png" /></figure><p>For this example, our dataset includes three questions:</p><ul><li>“What’s 2+2?”</li><li>“How do I make oatmeal?”</li><li>“How far away is the Moon?”</li></ul><p>In real-world evaluations, you might have tens, hundreds, or even thousands of records. You can assemble these datasets by hand or use Claude to generate them for you.</p><h3>Step 3: Feed Through Claude</h3><p>Take each question from your dataset and merge it with your prompt template to create complete prompts. Then send each one to Claude to get responses.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*x6eToqSySBnHKEQk.png" /></figure><p>For example, the first question becomes:</p><pre>Please answer the user&#39;s question:<br>What&#39;s 2+2?</pre><p>Claude might respond with “2 + 2 = 4” for the math question, provide oatmeal cooking instructions for the second question, and give the distance to the Moon for the third.</p><h3>Step 4: Feed Through a Grader</h3><p>The grader evaluates the quality of Claude’s responses by examining both the original question and Claude’s answer. This step provides objective scoring, typically on a scale from 1 to 10, where 10 represents a perfect answer and lower scores indicate room for improvement.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*tbiGnT76rv_i1xkb.png" /></figure><p>In our example, the grader might assign:</p><ul><li>Math question: 10 (perfect answer)</li><li>Oatmeal question: 4 (needs improvement)</li><li>Moon question: 9 (very good answer)</li></ul><p>The average score across all questions gives you an objective measurement: (10 + 4 + 9) ÷ 3 = 7.66</p><h3>Step 5: Change Prompt and Repeat</h3><p>Now that you have a baseline score, you can modify your prompt and run the entire process again to see if your changes improve performance.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*IrbeeKweQA8AxoB5.png" /></figure><p>For example, you might add more guidance to your prompt:</p><pre>prompt = f&quot;&quot;&quot;<br>Please answer the user&#39;s question:</pre><pre>{question}</pre><pre>Answer the question with ample detail<br>&quot;&quot;&quot;</pre><p>After running this improved prompt through the same evaluation process, you might get a higher average score of 8.7, indicating that the additional instruction helped Claude provide better responses.</p><h3>Prompt Scoring</h3><p>The key benefit of this workflow is getting objective measurements of prompt performance. You can:</p><ul><li>Compare different prompt versions numerically</li><li>Use the version with the best score</li><li>Continue iterating to find even better approaches</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ku3-vgTgDUSXC6Hr.png" /></figure><p>This systematic approach removes guesswork from prompt engineering and gives you confidence that your changes are actually improvements rather than just different variations.</p><h3>Generating test datasets</h3><p>Building a custom prompt evaluation workflow starts with creating a solid prompt and then generating test data to see how well it performs. Let’s walk through setting up an evaluation system for a prompt that helps users write AWS-specific code.</p><h3>Setting Up the Goal</h3><p>Our prompt needs to assist users in writing three specific types of output for AWS use cases:</p><ul><li>Python code</li><li>JSON configuration files</li><li>Regular expressions</li></ul><p>The key requirement is that when a user requests help with a task, we return clean output in one of these formats without any extra explanations, headers, or footers.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*6NzTnbAh8vE4BohP.png" /></figure><p>Here’s our starting prompt (version 1):</p><pre>prompt = f&quot;&quot;&quot;<br>Please provide a solution to the following task:<br>{task}<br>&quot;&quot;&quot;</pre><h3>Creating an Evaluation Dataset</h3><p>An evaluation dataset contains inputs that we’ll feed into our prompt. For each combination of prompt and input, we’ll run the prompt and analyze the results.</p><p>Our dataset will be an array of JSON objects, where each object contains a “task” property describing what we want Claude to accomplish. We can either create this dataset by hand or generate it automatically using Claude.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*xKYgb5gVP7YqjuQ3.png" /></figure><p>Since we’re generating test data, this is a perfect opportunity to use a faster model like Haiku instead of the full Claude model.</p><h3>Generating Test Data with Code</h3><p>Let’s create a function that automatically generates our test dataset. First, we’ll need our helper functions for working with Claude:</p><pre>def add_user_message(messages, text):<br>    user_message = {&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: text}<br>    messages.append(user_message)</pre><pre>def add_assistant_message(messages, text):<br>    assistant_message = {&quot;role&quot;: &quot;assistant&quot;, &quot;content&quot;: text}<br>    messages.append(assistant_message)</pre><pre>def chat(messages, system=None, temperature=1.0, stop_sequences=[]):<br>    params = {<br>        &quot;model&quot;: model,<br>        &quot;max_tokens&quot;: 1000,<br>        &quot;messages&quot;: messages,<br>        &quot;temperature&quot;: temperature<br>    }<br>    if system:<br>        params[&quot;system&quot;] = system<br>    if stop_sequences:<br>        params[&quot;stop_sequences&quot;] = stop_sequences<br>    <br>    response = client.messages.create(**params)<br>    return response.content[0].text</pre><p>Now we’ll create our dataset generation function:</p><pre>def generate_dataset():<br>    prompt = &quot;&quot;&quot;<br>Generate an evaluation dataset for a prompt evaluation. The dataset will be used to evaluate prompts that generate Python, JSON, or Regex specifically for AWS-related tasks. Generate an array of JSON objects, each representing task that requires Python, JSON, or a Regex to complete.</pre><pre>Example output:<br>```json<br>[<br>  {<br>    &quot;task&quot;: &quot;Description of task&quot;,<br>  },<br>  ...additional<br>]<br>```</pre><pre>* Focus on tasks that can be solved by writing a single Python function, a single JSON object, or a single regex<br>* Focus on tasks that do not require writing much code</pre><pre>Please generate 3 objects.<br>&quot;&quot;&quot;</pre><p>To properly parse the JSON response, we’ll use prefilling and stop sequences:</p><pre>messages = []<br>    add_user_message(messages, prompt)<br>    add_assistant_message(messages, &quot;```json&quot;)<br>    text = chat(messages, stop_sequences=[&quot;```&quot;])<br>    return json.loads(text)</pre><h3>Testing the Dataset Generation</h3><p>Let’s run our function and see what kind of test cases we get:</p><pre>dataset = generate_dataset()<br>print(dataset)</pre><p>This should return three different test cases covering our target outputs — Python functions, JSON configurations, and regular expressions for AWS-specific tasks.</p><h3>Saving the Dataset</h3><p>Once we have our dataset, we’ll save it to a file so we can easily load it later during evaluation:</p><pre>with open(&#39;dataset.json&#39;, &#39;w&#39;) as f:<br>    json.dump(dataset, f, indent=2)</pre><p>This creates a <strong>dataset.json</strong> file in the same directory as your notebook, containing your list of tasks ready for prompt evaluation.</p><p>With this foundation in place, you now have a systematic way to generate test data for evaluating how well your prompts perform across different types of AWS-related coding tasks.</p><h4>Downloads</h4><ul><li><a href="https://cc.sj-cdn.net/instructor/4hdejjwplbrm-anthropic/assets/1762977284/001_prompt_evals.ipynb?response-content-disposition=attachment&amp;Expires=1793862941&amp;Signature=Hdy4oDw7BsHgRhl36nPg2HvHnwYL08xRFqfa2I4n4aRDFlBfSogBuCgLBExJjE42KJ~Go8WMEI7lQ23DyZK83OOt0wQITvrRO1w8n89TxFkhofIfUhBgEzej0xj7SndWJTN9hr78MBBntwNxJhJPOwU5a-MZY~-kXD18uC6XrrLYyh0NHSe4TGGMJA5IFOtE8GkPeDx3yUhvDJa5UEdOARpC6jsv-AekRoEPuaxJipQPsf0Mx9bhgHSe4pVosq67C6n8RPblo3NE5WDfc38-SFxfOrmLtMYrBdi05bm4ggZscYUPg2TIVNHAhbGQjj6LOXWaHVhdOOwhOpIjmIj88g__&amp;Key-Pair-Id=APKAI3B7HFD2VYJQK4MQ">001_prompt_evals.ipynb(opens in new tab)</a></li></ul><h3>Running the eval</h3><p>Now that we have our evaluation dataset ready, it’s time to build the core evaluation pipeline. This involves taking each test case, merging it with our prompt, feeding it to Claude, and then grading the results.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*-MRi10COiLKYRSC5.png" /></figure><p>The evaluation process follows a clear workflow: we take our dataset of test cases, combine each one with our prompt template, send it to Claude for processing, and then evaluate the output using a grader system.</p><h3>Building the Core Functions</h3><p>The evaluation pipeline consists of three main functions, each with a specific responsibility. Let’s start with the simplest one — the function that handles individual prompts.</p><h3>The run_prompt Function</h3><p>This function takes a test case and merges it with our prompt template:</p><pre>def run_prompt(test_case):<br>    &quot;&quot;&quot;Merges the prompt and test case input, then returns the result&quot;&quot;&quot;<br>    prompt = f&quot;&quot;&quot;<br>Please solve the following task:</pre><pre>{test_case[&quot;task&quot;]}<br>&quot;&quot;&quot;<br>    <br>    messages = []<br>    add_user_message(messages, prompt)<br>    output = chat(messages)<br>    return output</pre><p>Right now, we’re keeping the prompt extremely simple. We’re not including any formatting instructions, so Claude will likely return more verbose output than we need. We’ll refine this later as we iterate on our prompt design.</p><h3>The run_test_case Function</h3><p>This function orchestrates running a single test case and grading the result:</p><pre>def run_test_case(test_case):<br>    &quot;&quot;&quot;Calls run_prompt, then grades the result&quot;&quot;&quot;<br>    output = run_prompt(test_case)<br>    <br>    # TODO - Grading<br>    score = 10<br>    <br>    return {<br>        &quot;output&quot;: output,<br>        &quot;test_case&quot;: test_case,<br>        &quot;score&quot;: score<br>    }</pre><p>For now, we’re using a hardcoded score of 10. The grading logic is where we’ll spend significant time in upcoming sections, but this placeholder lets us test the overall pipeline.</p><h3>The run_eval Function</h3><p>This function coordinates the entire evaluation process:</p><pre>def run_eval(dataset):<br>    &quot;&quot;&quot;Loads the dataset and calls run_test_case with each case&quot;&quot;&quot;<br>    results = []<br>    <br>    for test_case in dataset:<br>        result = run_test_case(test_case)<br>        results.append(result)<br>    <br>    return results</pre><p>This function processes every test case in our dataset and collects all the results into a single list.</p><h3>Running the Evaluation</h3><p>To execute our evaluation pipeline, we load our dataset and run it through our functions:</p><pre>with open(&quot;dataset.json&quot;, &quot;r&quot;) as f:<br>    dataset = json.load(f)</pre><pre>results = run_eval(dataset)</pre><p>The first time you run this, expect it to take some time — even with Claude Haiku, it can take around 30 seconds to process a full dataset. We’ll cover optimization techniques later.</p><h3>Examining the Results</h3><p>The evaluation returns a structured JSON array where each object represents one test case result:</p><pre>print(json.dumps(results, indent=2))</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*k8_1Sr7WXSpz77ay.png" /></figure><p>Each result contains three key pieces of information:</p><ul><li>output: The complete response from Claude</li><li>test_case: The original test case that was processed</li><li>score: The evaluation score (currently hardcoded)</li></ul><p>As you can see in the output, Claude generates quite verbose responses since we haven’t provided specific formatting instructions yet. This is exactly the kind of issue we’ll address as we refine our prompts.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*BsXtyjTOLuwlxTEC.png" /></figure><h3>What We’ve Accomplished</h3><p>At this point, we’ve successfully built the core evaluation pipeline. We can take our dataset, process it through Claude, and collect structured results. The major missing piece is the grading system — that hardcoded score of 10 needs to be replaced with actual evaluation logic.</p><p>This pipeline represents the foundation of most AI evaluation systems. While it may seem simple, you’ve just built the majority of what an eval pipeline actually does. The complexity comes in the details — better prompts, sophisticated grading, and performance optimizations.</p><p>Next, we’ll dive into the critical topic of graders, which will transform our hardcoded scores into meaningful evaluations of Claude’s performance.</p><h3>Model based grading</h3><p>When building prompt evaluation workflows, grading systems provide objective signals about output quality. A grader takes model output and returns some kind of measurable feedback — typically a number between 1 and 10, where 10 represents high quality and 1 represents poor quality.</p><h3>Types of Graders</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*bTNcNgLJ7SgfQNAR.png" /></figure><p>There are three main approaches to grading model outputs:</p><ul><li>Code graders — Programmatically evaluate outputs using custom logic</li><li>Model graders — Use another AI model to assess the quality</li><li>Human graders — Have people manually review and score outputs</li></ul><h3>Code Graders</h3><p>Code graders let you implement any programmatic check you can imagine. Common uses include:</p><ul><li>Checking output length</li><li>Verifying output does/doesn’t have certain words</li><li>Syntax validation for JSON, Python, or regex</li><li>Readability scores</li></ul><p>The only requirement is that your code returns some usable signal — usually a number between 1 and 10.</p><h3>Model Graders</h3><p>Model graders feed your original output into another API call for evaluation. This approach offers tremendous flexibility for assessing:</p><ul><li>Response quality</li><li>Quality of instruction following</li><li>Completeness</li><li>Helpfulness</li><li>Safety</li></ul><h3>Human Graders</h3><p>Human graders provide the most flexibility but are time-consuming and tedious. They’re useful for evaluating:</p><ul><li>General response quality</li><li>Comprehensiveness</li><li>Depth</li><li>Conciseness</li><li>Relevance</li></ul><h3>Defining Evaluation Criteria</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*tNKY8E9j9Xxxnl-v.png" /></figure><p>Before implementing any grader, you need clear evaluation criteria. For a code generation prompt, you might focus on:</p><ul><li>Format — Should return only Python, JSON, or Regex without explanation</li><li>Valid Syntax — Produced code should have valid syntax</li><li>Task Following — Response should directly address the user’s task with accurate code</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*CAJJLTQpK711c4yA.png" /></figure><p>The first two criteria work well with code graders, while task following is better suited for model graders due to their flexibility.</p><h3>Implementing a Model Grader</h3><p>Here’s how to build a model grader function:</p><pre>def grade_by_model(test_case, output):<br>    # Create evaluation prompt<br>    eval_prompt = &quot;&quot;&quot;<br>    You are an expert code reviewer. Evaluate this AI-generated solution.<br>    <br>    Task: {task}<br>    Solution: {solution}<br>    <br>    Provide your evaluation as a structured JSON object with:<br>    - &quot;strengths&quot;: An array of 1-3 key strengths<br>    - &quot;weaknesses&quot;: An array of 1-3 key areas for improvement  <br>    - &quot;reasoning&quot;: A concise explanation of your assessment<br>    - &quot;score&quot;: A number between 1-10<br>    &quot;&quot;&quot;<br>    <br>    messages = []<br>    add_user_message(messages, eval_prompt)<br>    add_assistant_message(messages, &quot;```json&quot;)<br>    <br>    eval_text = chat(messages, stop_sequences=[&quot;```&quot;])<br>    return json.loads(eval_text)</pre><p><strong>The key insight is asking for strengths, weaknesses, and reasoning alongside the score. Without this context, models tend to default to middling scores around 6.</strong></p><h3>Integrating Grading into Your Workflow</h3><p><strong>Update your test case runner to call the grader:</strong></p><pre>def run_test_case(test_case):<br>    output = run_prompt(test_case)<br>    <br>    # Grade the output<br>    model_grade = grade_by_model(test_case, output)<br>    score = model_grade[&quot;score&quot;]<br>    reasoning = model_grade[&quot;reasoning&quot;]<br>    <br>    return {<br>        &quot;output&quot;: output, <br>        &quot;test_case&quot;: test_case, <br>        &quot;score&quot;: score,<br>        &quot;reasoning&quot;: reasoning<br>    }</pre><p><strong>Finally, calculate an average score across all test cases:</strong></p><pre>from statistics import mean</pre><pre>def run_eval(dataset):<br>    results = []<br>    <br>    for test_case in dataset:<br>        result = run_test_case(test_case)<br>        results.append(result)<br>    <br>    average_score = mean([result[&quot;score&quot;] for result in results])<br>    print(f&quot;Average score: {average_score}&quot;)<br>    <br>    return results</pre><p><strong>This gives you an objective metric to track as you iterate on your prompt. While model graders can be somewhat capricious, they provide a consistent baseline for measuring improvements.</strong></p><h3>Code based grading</h3><p>When evaluating AI models that generate code, you need more than just checking if the response makes sense. You also need to verify that the generated code actually has valid syntax and follows the correct format. This is where code-based grading comes in.</p><h3>How Code Grading Works</h3><p>Code grading validates two key aspects of AI-generated responses:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*A_Djwl5H1ULQNX8b.png" /></figure><ul><li>Format — The response should return only the requested code type (Python, JSON, or Regex) without explanations</li><li>Valid Syntax — The generated code should actually parse correctly as the intended language</li><li>Task Following — The response should directly address what was asked and be accurate</li></ul><p>The first two criteria are handled by the code grader, while task following is evaluated by the model grader. Together, they provide a comprehensive evaluation.</p><h3>Syntax Validation Functions</h3><p>To check if generated code has valid syntax, you can create three helper functions that attempt to parse the output:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*tssFox1Mt5kYLZcm.png" /></figure><pre>def validate_json(text):<br>    try:<br>        json.loads(text.strip())<br>        return 10<br>    except json.JSONDecodeError:<br>        return 0</pre><pre>def validate_python(text):<br>    try:<br>        ast.parse(text.strip())<br>        return 10<br>    except SyntaxError:<br>        return 0</pre><pre>def validate_regex(text):<br>    try:<br>        re.compile(text.strip())<br>        return 10<br>    except re.error:<br>        return 0</pre><p>Each function tries to parse the text as its respective format. If parsing succeeds, it returns a perfect score of 10. If it fails with an error, the syntax is invalid and returns 0.</p><h3>Dataset Format Requirements</h3><p>For the code grader to know which validator to use, your test cases need to specify the expected output format:</p><pre>{<br>    &quot;task&quot;: &quot;Create a Python function to validate an AWS IAM username&quot;,<br>    &quot;format&quot;: &quot;python&quot;<br>}</pre><p>You can update your dataset generation prompt to automatically include this format field by adding it to the example output structure.</p><h3>Improving Prompt Clarity</h3><p>To get better results from your AI model, make your prompt instructions more specific about the expected output format:</p><pre>* Respond only with Python, JSON, or a plain Regex<br>* Do not add any comments or commentary or explanation</pre><p>You can also use a pre-filled assistant message with code blocks to encourage the model to return just the raw code:</p><pre>add_assistant_message(messages, &quot;```code&quot;)</pre><p>This tells Claude to start generating code content without having to specify whether it’s Python, JSON, or Regex ahead of time.</p><h3>Combining Scores</h3><p>The final step is merging the model grader score with the code grader score. A simple approach is to take the average:</p><pre>model_grade = grade_by_model(test_case, output)<br>model_score = model_grade[&quot;score&quot;]<br>syntax_score = grade_syntax(output, test_case)</pre><pre>score = (model_score + syntax_score) / 2</pre><p>This gives equal weight to both content quality and technical correctness. You might adjust these weights based on what matters more for your specific use case.</p><h3>Testing Your Implementation</h3><p>Once you’ve implemented code grading, run your evaluation to get a baseline score. The score itself isn’t inherently good or bad — what matters is whether you can improve it by refining your prompts. This gives you a quantitative way to measure prompt engineering progress rather than relying on subjective assessment.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f6a27349af5d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[LangGraph-LLM-TypeDict,Literal]]></title>
            <link>https://medium.com/@reema.kumaritech/langgraph-llm-typedict-literal-420903326de8?source=rss-cb321e7882e1------2</link>
            <guid isPermaLink="false">https://medium.com/p/420903326de8</guid>
            <category><![CDATA[langgraph]]></category>
            <dc:creator><![CDATA[Reema Kumaritech]]></dc:creator>
            <pubDate>Wed, 04 Nov 2026 09:54:15 GMT</pubDate>
            <atom:updated>2026-11-04T09:54:15.554Z</atom:updated>
            <content:encoded><![CDATA[<pre>from typing_extensions import TypedDict<br>from typing import Literal<br>from langgraph.graph import START,END,StateGraph<br>from IPython.display import display,Image<br>import random<br><br>class playTypeDict(TypedDict):<br>    name:str<br>    game:Literal[&quot;cricket&quot;,&quot;badminton&quot;]<br><br>def play_game(state:playTypeDict):<br>    print(&quot;play game node is running&quot;)<br>    return {&quot;name&quot;:state[&quot;name&quot;]}<br><br>def cricket(state:playTypeDict):<br>    print(&quot; cricket node is running&quot;)<br>    return {&quot;name&quot;:state[&quot;name&quot;] + &quot; is playing cricket&quot;}<br><br>def badminton(state:playTypeDict):<br>    print(&quot;badminton game node is running&quot;)<br>    return {&quot;name&quot;:state[&quot;name&quot;] + &quot; is playing badminton&quot;}<br><br>def decision_node(state:playTypeDict)-&gt;Literal[&quot;cricket&quot;,&quot;badminton&quot;]:<br>    if random.random() &lt; 0.5:<br>        return &quot;cricket&quot;<br>    else:<br>        return &quot;badminton&quot;<br><br>##################graph<br>graph=StateGraph(playTypeDict)<br>graph.add_node(&quot;play_game&quot;,play_game)<br>graph.add_node(&quot;cricket&quot;,cricket)<br>graph.add_node(&quot;badminton&quot;,badminton)<br><br>graph.add_edge(START,&quot;play_game&quot;)<br>graph.add_conditional_edges(&quot;play_game&quot;, decision_node)<br>graph.add_edge(&quot;cricket&quot;,END)<br>graph.add_edge(&quot;badminton&quot;,END)<br><br>graph_builder=graph.compile()<br>display(Image(graph_builder.get_graph().draw_mermaid_png()))</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=420903326de8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[chain in Langgraph]]></title>
            <link>https://medium.com/@reema.kumaritech/chain-in-langgraph-6b5171c5fd13?source=rss-cb321e7882e1------2</link>
            <guid isPermaLink="false">https://medium.com/p/6b5171c5fd13</guid>
            <category><![CDATA[langgraph]]></category>
            <dc:creator><![CDATA[Reema Kumaritech]]></dc:creator>
            <pubDate>Wed, 04 Nov 2026 09:54:04 GMT</pubDate>
            <atom:updated>2026-11-04T09:54:04.400Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2R_KqUzyFQ0_bP6TkIEtiA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nRj7jnzQSQMRlWlKfbGhZw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lTqkRj3lfSR_-CIaONYoug.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/462/1*YnuNe7e2p8U3Lr-pfCmTRw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/925/1*wLahIrO8NqYryDO9EiNlSA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/543/1*hhHOQjk_ShA0YrnhGSI5Hw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FGbkJpRIV9c9Zh9iyT3ZXA.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6b5171c5fd13" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[LangGraph ChatBOT Tutorial]]></title>
            <link>https://medium.com/@reema.kumaritech/langgraph-chatbot-tutorial-deb3f0d0d0b6?source=rss-cb321e7882e1------2</link>
            <guid isPermaLink="false">https://medium.com/p/deb3f0d0d0b6</guid>
            <category><![CDATA[langgraph]]></category>
            <category><![CDATA[chatbots]]></category>
            <dc:creator><![CDATA[Reema Kumaritech]]></dc:creator>
            <pubDate>Wed, 04 Nov 2026 09:53:48 GMT</pubDate>
            <atom:updated>2026-11-04T09:53:48.182Z</atom:updated>
            <content:encoded><![CDATA[<pre>import os<br>from dotenv import load_dotenv<br>load_dotenv()<br>from langchain_groq import ChatGroq<br>from typing import Annotated<br>from typing_extensions import Literal, TypedDict<br>from langgraph.graph import add_messages,START,END,StateGraph<br>from IPython.display import display,Image<br><br><br># llm call<br>GROQ_API_KEY=&quot;pass your API Key here&quot;<br>llm=ChatGroq(<br>            model=&quot;qwen/qwen3.6-27b&quot;,<br>              temperature=0.0,<br>              max_retries=2,<br>              api_key=GROQ_API_KEY<br><br>)<br><br>class State(TypedDict):<br>    message: Annotated[list,add_messages]<br><br>def Chat_Bot(state: State):<br>    return {&quot;message&quot;:llm.invoke(state[&quot;message&quot;])}<br><br>graph=StateGraph(State)<br><br>#add nodes<br><br>graph.add_node(&quot;Chat_Bot&quot;,Chat_Bot)<br><br>#add edges<br>graph.add_edge(START,&quot;Chat_Bot&quot;)<br>graph.add_edge(&quot;Chat_Bot&quot;,END)<br><br>graph_builder=graph.compile()<br>display(Image(graph_builder.get_graph().draw_mermaid_png()))<br>response=graph_builder.invoke({&quot;message&quot;:&quot;what is LLM&quot;})<br>print(response[&quot;message&quot;][-1].content)<br><br></pre><h3>LangGraph Chatbot Documentation</h3><h3>Overview</h3><p>This code creates a simple <strong>LangGraph chatbot workflow</strong> using the <strong>Groq LLM (Qwen 3.6–27B model)</strong>. The workflow:</p><ol><li>Accepts a user message.</li><li>Sends the message to the LLM.</li><li>Receives the AI response.</li><li>Returns the response through the graph state.</li><li>Prints only the AI response content.</li></ol><h3>Architecture</h3><p>START</p><p>|</p><p>StartNode</p><p>+ — — — — — +</p><p>| Chat_Bot |</p><p>+ — — — — — +</p><p>|</p><p>EndNode</p><h3>Import Statements</h3><p>import os</p><p>from dotenv import load_dotenv</p><p>load_dotenv()</p><h3>Purpose</h3><p>Used for reading environment variables from a .env file.</p><h3>load_dotenv()</h3><p>Loads all variables from:</p><p>.env</p><p>Example:</p><p>GROQ_API_KEY=xxxxx</p><p>into Python runtime.</p><p>from langchain_groq import ChatGroq</p><h3>Purpose</h3><p>Provides access to Groq-hosted Large Language Models.</p><p>Used to:</p><ul><li>Send prompts</li><li>Generate responses</li><li>Integrate Groq models into LangChain/LangGraph</li></ul><p>from typing import Annotated</p><h3>Purpose</h3><p>Used to attach metadata to a type.</p><p>In LangGraph it is commonly used for reducers such as:</p><p>add_messages</p><p>from typing_extensions import TypedDict</p><h3>Purpose</h3><p>Defines the structure of the graph state.</p><p>Think of it as a schema for all data moving between nodes.</p><p>from langgraph.graph import (</p><p>add_messages,</p><p>START,</p><p>END,</p><p>StateGraph</p><p>)</p><h3>StateGraph</h3><p>Creates a LangGraph workflow.</p><h3>START</h3><p>Starting point of graph execution.</p><h3>END</h3><p>End point of graph execution.</p><h3>add_messages</h3><blockquote>Reducer used for chat history management.</blockquote><blockquote>It automatically appends new messages.</blockquote><h3><strong>from IPython.display import display, Image</strong></h3><blockquote>Purpose</blockquote><blockquote>Displays graph visualization inside Jupyter Notebook.</blockquote><h3>LLM Configuration</h3><blockquote>GROQ_API_KEY=”XXX”</blockquote><blockquote>Stores the Groq API key.</blockquote><blockquote>llm = ChatGroq(model=”qwen/qwen3.6–27b”,temperature=0.0,max_retries=2,</blockquote><blockquote>api_key=GROQ_API_KEY )</blockquote><h3>Purpose</h3><p>Creates an LLM object.</p><h3>Parameters</h3><h4>model</h4><p>model=”qwen/qwen3.6–27b”</p><p>Specifies the model to use.</p><h4>temperature</h4><p>temperature=0.0</p><p>Controls randomness.</p><p>ValueBehavior0.0Deterministic0.5Balanced1.0Creative</p><p>Current value:</p><p>0.0</p><p>Produces consistent responses.</p><h4>max_retries</h4><p>max_retries=2</p><p>Retries API call if a temporary failure occurs.</p><h4>api_key</h4><p>api_key=GROQ_API_KEY</p><p>Authenticates with Groq.</p><h3>State Definition</h3><blockquote>class State(TypedDict):</blockquote><blockquote>message: Annotated[list, add_messages]</blockquote><blockquote>Purpose</blockquote><blockquote>Defines the graph’s shared memory.</blockquote><blockquote>State Structure</blockquote><blockquote>{</blockquote><blockquote>“message”: […]</blockquote><blockquote>}</blockquote><h3>Why Annotated?</h3><p>Annotated[list, add_messages]</p><p>Tells LangGraph:</p><blockquote><em>Use add_messages reducer whenever new messages are added.</em></blockquote><p>Without it, old messages would be overwritten.</p><h3>Example</h3><p>User message:</p><p>[</p><p>HumanMessage(</p><p>content=”What is LLM?”</p><p>)</p><p>]</p><p>After response:</p><p>[</p><p>HumanMessage(</p><p>content=”What is LLM?”</p><p>),</p><p>AIMessage(</p><p>content=”LLM stands for Large Language Model.”</p><p>)</p><p>]</p><h3>Chat_Bot Node</h3><p>def Chat_Bot(state: State):</p><p>return {</p><p>“message”: llm.invoke(state[“message”])</p><p>}</p><h3>Purpose</h3><p>Sends user messages to the LLM and receives a response.</p><h3>Input</h3><p>{</p><p>“message”:[</p><p>HumanMessage(</p><p>content=”What is LLM?”</p><p>)</p><p>]</p><p>}</p><h3>Processing</h3><p>llm.invoke(state[“message”])</p><p>Calls the Groq model.</p><h3>Output</h3><p>{</p><p>“message”:</p><p>AIMessage(</p><p>content=”LLM stands for Large Language Model.”</p><p>)</p><p>}</p><h3>Creating Graph</h3><p>graph = StateGraph(State)</p><h3>Purpose</h3><p>Creates a LangGraph workflow based on the State schema.</p><p>Graph</p><p>├─ Nodes</p><p>├─ Edges</p><p>└─ State</p><h3>Adding Nodes</h3><p>graph.add_node(</p><p>“Chat_Bot”,</p><p>Chat_Bot</p><p>)</p><h3>Purpose</h3><p>Registers the chatbot function as a graph node.</p><h3>Mapping</h3><p>Chat_Bot</p><p>|</p><p>v</p><p>Chat_Bot()</p><h3>Adding Edges</h3><h3>START → Chat_Bot</h3><p>graph.add_edge(</p><p>START,</p><p>“Chat_Bot”</p><p>)</p><p>Execution begins here.</p><p>START</p><p>|</p><p>v</p><p>Chat_Bot</p><h3>Chat_Bot → END</h3><p>graph.add_edge(</p><p>“Chat_Bot”,</p><p>END</p><p>)</p><p>Terminates graph after chatbot executes.</p><p>Chat_Bot</p><p>|</p><p>v</p><p>END</p><h3>Compile Graph</h3><p>graph_builder = graph.compile()</p><h3>Purpose</h3><p>Converts the graph definition into an executable application.</p><p>Before:</p><p>Graph Definition</p><p>After:</p><p>Runnable Graph Application</p><h3>Display Graph</h3><p>display(</p><p>Image(</p><p>graph_builder.get_graph()</p><h3>Purpose</h3><p>Generates a visual diagram.</p><p>Result:</p><p>START</p><p>|</p><p>v</p><p>Chat_Bot</p><p>|</p><p>v</p><p>END</p><h3>Running the Graph</h3><p>response = graph_builder.invoke(</p><p>{</p><p>“message”:”what is LLM”</p><p>}</p><p>)</p><h3>Purpose</h3><p>Starts graph execution.</p><h3>Execution Flow</h3><p>START</p><p>|</p><p>v</p><p>Chat_Bot</p><p>|</p><p>v</p><p>END</p><h3>Recommended Format</h3><p>For chat models, use:</p><p>response = graph_builder.invoke(</p><p>{</p><p>“message”:[</p><p>(“human”,”What is LLM?”)</p><p>]</p><p>}</p><p>)</p><p>This follows LangGraph’s message-based format.</p><h3>Printing AI Response</h3><p>print(</p><p>response[“message”][-1].content</p><p>)</p><h3>Explanation</h3><h3>response[“message”]</h3><p>Contains conversation history.</p><p>Example:</p><p>[</p><p>HumanMessage(</p><p>content=”What is LLM?”</p><p>),</p><p>AIMessage(</p><p>content=”LLM stands for Large Language Models.”</p><p>)</p><p>]</p><h3>response[“message”][-1]</h3><p>Gets the last message.</p><p>AIMessage(</p><p>content=”LLM stands for Large Language Models.”</p><p>)</p><h3>.content</h3><p>Extracts only the text.</p><p>LLM stands for Large Language Models.</p><h3>End-to-End Flow</h3><p>User Question</p><p>|</p><p>v</p><p>Graph Invoke</p><p>|</p><p>v</p><p>Chat_Bot Node</p><p>|</p><p>v</p><p>Groq LLM</p><p>|</p><p>v</p><p>AI Response</p><p>|</p><p>v</p><p>State Updated</p><p>|</p><p>v</p><p>Print Content</p><h3>Key Concepts Used</h3><ul><li><strong>State</strong>: Shared data passed between nodes.</li><li><strong>TypedDict</strong>: Defines state schema.</li><li><strong>Annotated</strong>: Attaches reducer metadata.</li><li><strong>add_messages</strong>: Maintains chat history.</li><li><strong>Node</strong>: A Python function performing work.</li><li><strong>Edge</strong>: Defines execution path.</li><li><strong>START</strong>: Entry point of graph.</li><li><strong>END</strong>: Exit point of graph.</li><li><strong>compile()</strong>: Creates executable workflow.</li><li><strong>invoke()</strong>: Runs the graph.</li><li><strong>ChatGroq</strong>: Connects to Groq-hosted LLM.</li><li><strong>AIMessage.content</strong>: Returns only the generated response text.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=deb3f0d0d0b6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[LangGraph-ToolCall,toolbind]]></title>
            <link>https://medium.com/@reema.kumaritech/langgraph-toolcall-toolbind-6f5c27afb42b?source=rss-cb321e7882e1------2</link>
            <guid isPermaLink="false">https://medium.com/p/6f5c27afb42b</guid>
            <category><![CDATA[langgraph-tutorial]]></category>
            <category><![CDATA[langgraph]]></category>
            <dc:creator><![CDATA[Reema Kumaritech]]></dc:creator>
            <pubDate>Wed, 04 Nov 2026 09:53:37 GMT</pubDate>
            <atom:updated>2026-11-04T09:53:37.351Z</atom:updated>
            <content:encoded><![CDATA[<pre>import os<br>from dotenv import load_dotenv<br>from typing import Annotated<br>from typing_extensions import TypedDict<br><br>from langchain_groq import ChatGroq<br>from langchain_core.tools import tool<br><br>from langgraph.graph import StateGraph, START, END, add_messages<br>from langgraph.prebuilt import ToolNode, tools_condition<br>from IPython.display import display,Image<br><br>load_dotenv()<br><br>GROQ_API_KEY = &quot;xxxxxx&quot;<br><br>llm = ChatGroq(<br>    model=&quot;qwen/qwen3.6-27b&quot;,<br>    temperature=0.0,<br>    max_retries=2,<br>    api_key=GROQ_API_KEY<br>)<br><br>class State(TypedDict):<br>    messages: Annotated[list, add_messages]<br><br><br>@tool<br>def add(x: int, y: int) -&gt; int:<br>    &quot;&quot;&quot;Add two numbers.&quot;&quot;&quot;<br>    return x + y<br><br><br>@tool<br>def sub(x: int, y: int) -&gt; int:<br>    &quot;&quot;&quot;Subtract y from x.&quot;&quot;&quot;<br>    return x - y<br><br><br>@tool<br>def mul(x: int, y: int) -&gt; int:<br>    &quot;&quot;&quot;Multiply two numbers.&quot;&quot;&quot;<br>    return x * y<br><br><br>tools = [add, sub, mul]<br><br>llm_with_tools = llm.bind_tools(tools)<br><br><br>def chat_bot(state: State):<br>    response = llm_with_tools.invoke(state[&quot;messages&quot;])<br>    return {&quot;messages&quot;: [response]}<br><br><br>tool_node = ToolNode(tools)<br><br>graph = StateGraph(State)<br><br>graph.add_node(&quot;chat_bot&quot;, chat_bot)<br>graph.add_node(&quot;tools&quot;, tool_node)<br><br>graph.add_edge(START, &quot;chat_bot&quot;)<br><br>graph.add_conditional_edges(<br>    &quot;chat_bot&quot;,<br>    tools_condition<br>)<br><br>graph.add_edge(&quot;tools&quot;, &quot;chat_bot&quot;)<br><br>graph_builder = graph.compile()<br>display(Image(graph_builder.get_graph().draw_mermaid_png()))<br><br>result = graph_builder.invoke(<br>    {<br>        &quot;messages&quot;: [<br>            (&quot;user&quot;, &quot;what is sum of 2 and 3&quot;)<br>        ]<br>    }<br>)<br><br>print(result[&quot;messages&quot;][-1].content)</pre><p>This code creates a <strong>LangGraph AI Agent</strong> that can:</p><ol><li>Receive a user’s question.</li><li>Let the LLM (Groq Qwen model) decide whether a tool is needed.</li><li>Call a tool (add, sub, mul) if required.</li><li>Return the final answer to the user.</li></ol><p>Let’s go through it <strong>line by line</strong>.</p><h3>1. Import Libraries</h3><p>import os</p><p>from dotenv import load_dotenv</p><h3>Purpose</h3><ul><li>os is used to access environment variables.</li><li>load_dotenv() loads values stored in a .env file.</li></ul><p>Example:</p><p>GROQ_API_KEY=abc123</p><p>from typing import Annotated</p><p>from typing_extensions import TypedDict</p><h3>Purpose</h3><p>Used for defining the graph’s state structure.</p><p>from langchain_groq import ChatGroq</p><h3>Purpose</h3><p>Connects to Groq-hosted LLMs.</p><p>Example:</p><p>llm = ChatGroq(…)</p><p>Now your Python code can communicate with the model.</p><p>from langchain_core.tools import tool</p><h3>Purpose</h3><p>Converts a normal Python function into an AI tool.</p><p>Without:</p><p>def add(x,y):</p><p>it is just a Python function.</p><p>With:</p><p>@tool</p><p>def add(x,y):</p><p>the LLM can use it.</p><p>from langgraph.graph import StateGraph, START, END</p><h3>Purpose</h3><p>Used to create and control the LangGraph workflow.</p><ul><li>StateGraph → creates graph</li><li>START → starting node</li><li>END → ending node</li><li>add_messages → manages chat history</li></ul><p>from langgraph.prebuilt import ToolNode, tools_condition</p><h3>Purpose</h3><p>Provides built-in support for tool execution.</p><p>from IPython.display import display, Image</p><h3>Purpose</h3><p>Used inside Jupyter Notebook to display the graph diagram.</p><h3>2. Load Environment Variables</h3><p>load_dotenv()</p><p>Loads data from .env.</p><h3>3. API Key</h3><p>GROQ_API_KEY = “gsk_xxxxx”</p><p>Stores Groq API Key.</p><p>⚠️ Better:</p><p>GROQ_API_KEY = os.getenv(“GROQ_API_KEY”)</p><p>Never hardcode API keys.</p><h3>4. Create LLM</h3><p>llm = ChatGroq(</p><p>model=”qwen/qwen3.6–27b”,</p><p>temperature=0.0,</p><p>max_retries=2,</p><p>api_key=GROQ_API_KEY</p><p>)</p><h3>Meaning</h3><h4>Model</h4><p>model=”qwen/qwen3.6–27b”</p><p>The LLM being used.</p><h4>Temperature</h4><p>temperature=0.0</p><p>Controls randomness.</p><ul><li>0 → deterministic</li><li>1 → creative</li></ul><p>Here:</p><p>2+3 = 5</p><p>will always be consistent.</p><h4>max_retries</h4><p>max_retries=2</p><p>Retry API call up to 2 times if it fails.</p><h3>5. Graph State</h3><p>class State(TypedDict):</p><p>messages: Annotated[list, add_messages]</p><h3>Purpose</h3><p>Defines data stored inside graph.</p><p>Graph memory contains:</p><p>{</p><p>“messages”: […]</p><p>}</p><h3>Annotated</h3><p>Annotated[list, add_messages]</p><p>Means:</p><p>messages</p><p>is a list and new messages should be automatically appended.</p><p>Example:</p><p>Before:</p><p>[</p><p>HumanMessage(“Hi”)</p><p>]</p><p>After:</p><p>[</p><p>HumanMessage(“Hi”),</p><p>AIMessage(“Hello”)</p><p>]</p><h3>6. Create Tools</h3><h3>Add Tool</h3><p>@tool</p><p>def add(x: int, y: int) -&gt; int:</p><p>“””Add two numbers.”””</p><p>return</p><h3>Example</h3><p>Input:</p><p>x=2</p><p>y=3</p><p>Output:</p><p>5</p><h3>Sub Tool</h3><p>@tool</p><p>def sub(x: int, y: int) -&gt; int:</p><p>“””Subtract y from x.”””</p><p>return x — y</p><p>Example:</p><p>10–4=6</p><h3>Mul Tool</h3><p>@tool</p><p>def mul(x: int, y: int) -&gt; int:</p><p>“””Multiply two numbers.”””</p><p>return x * y</p><p>Example:</p><p>10*5=50</p><h3>7. Store Tools</h3><p>tools = [add, sub, mul]</p><p>Creates a list containing all tools.</p><p>[</p><p>add,</p><p>sub,</p><p>mul</p><p>]</p><h3>8. Bind Tools to LLM</h3><p>llm_with_tools = llm.bind_tools(tools)</p><h3>Purpose</h3><p>Gives the LLM access to tools.</p><p>Before:</p><p>LLM only chats.</p><p>After:</p><p>LLM can call add()</p><p>LLM can call sub()</p><p>LLM can call mul()</p><h3>9. Chatbot Node</h3><p>def chat_bot(state: State):</p><p>Creates chatbot node.</p><p>response = llm_with_tools.invoke(state[“messages”])</p><p>Passes conversation to LLM.</p><p>Example:</p><p>[</p><p>(“user”,”what is sum of 2 and 3&quot;)</p><p>]</p><p>``</p><p>The model may decide:</p><p>Call add(2,3)</p><p>return {“messages”:[response]}</p><p>Stores response back into graph state.</p><h3>10. Tool Node</h3><p>tool_node = ToolNode(tools)</p><p>Creates a special node that executes tools.</p><p>Example:</p><p>If model requests:</p><p>add(2,3)</p><p>ToolNode executes:</p><p>5</p><h3>11. Create Graph</h3><p>graph = StateGraph(State)</p><p>Creates workflow graph.</p><h3>12. Add Nodes</h3><p>graph.add_node(“chat_bot”, chat_bot)</p><p>Creates chatbot node.</p><p>graph.add_node(“tools”, tool_node)</p><p>Creates tool execution node.</p><p>Graph now contains:</p><p>chat_bot</p><p>tools</p><h3>13. Define Start</h3><p>graph.add_edge(START, “chat_bot”)</p><p>Workflow starts with chatbot.</p><p>START</p><p>↓</p><p>chat_bot</p><h3>14. Conditional Routing</h3><p>graph.add_conditional_edges(</p><p>“chat_bot”,</p><p>tools_condition</p><p>)</p><p>Very important.</p><p>After chatbot executes:</p><p>LangGraph checks:</p><p>Did model request a tool?</p><h3>If YES</h3><p>chat_bot</p><p>↓</p><p>tools</p><h3>If NO</h3><p>chat_bot</p><p>↓</p><p>END</p><p>Example:</p><p>Question:</p><p>What is capital of India?</p><p>No tool is required.</p><p>chat_bot → END</p><p>Question:</p><p>What is 2 + 3?</p><p>Tool needed.</p><p>chat_bot → tools</p><h3>15. Return from Tool to Chatbot</h3><p>graph.add_edge(“tools”, “chat_bot”)</p><p>After tool runs:</p><p>tools</p><p>↓</p><p>chat_bot</p><p>This lets the model see tool output and generate a final answer.</p><p>Example:</p><p>Tool returns:</p><p>5</p><p>Then chatbot says:</p><p>The sum of 2 and 3 is 5.</p><h3>Complete Flow</h3><p>START</p><p>↓</p><p>chat_bot</p><p>↓</p><p>Need Tool?</p><p>/ &lt;/span&gt;</p><p>Yes No</p><p>↓ ↓</p><p>tools END</p><p>↓</p><p>chat_bot</p><p>↓</p><p>END</p><h3>16. Compile Graph</h3><p>graph_builder = graph.compile()</p><p>Converts graph design into executable graph.</p><p>Before:</p><p>Blueprint</p><p>After:</p><p>Runnable graph</p><h3>17. Display Graph</h3><p>display(Image(graph_builder.get_graph().draw_mermaid_png()))</p><p>Creates a visual diagram.</p><p>Example:</p><p>START</p><p>|</p><p>chat_bot</p><p>|</p><p>tools</p><p>|</p><p>END</p><h3>18. Invoke Graph</h3><p>result = graph_builder.invoke(</p><p>{</p><p>“messages”: [</p><p>(“user”, “what is sum of 2 and 3”)</p><p>]</p><p>}</p><p>)</p><p>User message enters graph.</p><p>Initial state:</p><p>{</p><p>“messages”:[</p><p>(“user”,”what is sum of 2 and 3&quot;)</p><p>]</p><p>}</p><h3>19. Execution Happens</h3><h3>Step 1</h3><p>Chatbot receives:</p><p>what is sum of 2 and 3</p><h3>Step 2</h3><p>LLM decides:</p><p>I’ll use add tool.</p><h3>Step 3</h3><p>ToolNode executes:</p><p>add(2,3)</p><p>returns:</p><p>5</p><h3>Step 4</h3><p>Chatbot receives tool output:</p><p>5</p><p>and generates:</p><p>The sum of 2 and 3 is 5.</p><h3>20. Print Final Answer</h3><p>print(result[“messages”][-1].content)</p><h3>Meaning</h3><p>result[&quot;messages&quot;]</p><p>Contains all conversation messages.</p><p>Example:</p><p>[</p><p>HumanMessage,</p><p>AIMessage(tool call),</p><p>ToolMessage,</p><p>AIMessage(final answer)</p><p>]</p><p>[-1]</p><p>Gets last message.</p><p>.content</p><p>Gets actual text.</p><p>Output:</p><p>The sum of 2 and 3 is 5.</p><h3>Overall Architecture</h3><pre>User Question<br>      ↓<br>ChatGroq LLM<br>      ↓<br>Need Tool?<br>   /      \<br> Yes       No<br>  ↓         ↓<br>ToolNode   END<br>  ↓<br>ChatGroq<br>  ↓<br>Final Answer</pre><p>This is a basic <strong>LangGraph Tool Calling Agent</strong>, where the LLM acts as the decision maker and the tools perform calculations.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6f5c27afb42b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[RAG and Azure LLM Chat-BOT]]></title>
            <link>https://medium.com/@reema.kumaritech/rag-and-azure-llm-chat-bot-199f1f4c74ac?source=rss-cb321e7882e1------2</link>
            <guid isPermaLink="false">https://medium.com/p/199f1f4c74ac</guid>
            <category><![CDATA[rag-and-azure-llm]]></category>
            <dc:creator><![CDATA[Reema Kumaritech]]></dc:creator>
            <pubDate>Wed, 04 Nov 2026 09:53:13 GMT</pubDate>
            <atom:updated>2026-11-04T09:53:13.386Z</atom:updated>
            <content:encoded><![CDATA[<pre>import os<br>import tempfile<br>import streamlit as st<br><br>from dotenv import load_dotenv<br><br>from langchain_community.document_loaders import PyMuPDFLoader<br>from langchain_text_splitters import RecursiveCharacterTextSplitter<br>from langchain_chroma import Chroma<br>from langchain_core.prompts import ChatPromptTemplate<br>from langchain_classic.chains import create_retrieval_chain<br>from langchain_classic.chains.combine_documents import (<br>    create_stuff_documents_chain,<br>)<br>from langchain_openai import AzureChatOpenAI, AzureOpenAIEmbeddings<br><br><br># Load environment variables<br>load_dotenv()<br><br><br># Azure OpenAI chat model<br>llm = AzureChatOpenAI(<br>    azure_deployment=os.getenv(&quot;AZURE_OPENAI_CHAT_DEPLOYMENT&quot;),<br>    api_key=os.getenv(&quot;AZURE_OPENAI_API_KEY&quot;),<br>    azure_endpoint=os.getenv(&quot;AZURE_OPENAI_ENDPOINT&quot;),<br>    api_version=os.getenv(&quot;AZURE_OPENAI_API_VERSION&quot;),<br>    temperature=0,<br>)<br><br><br># Azure OpenAI embedding model<br>embeddings = AzureOpenAIEmbeddings(<br>    azure_deployment=os.getenv(&quot;AZURE_OPENAI_EMBEDDING_DEPLOYMENT&quot;),<br>    api_key=os.getenv(&quot;AZURE_OPENAI_API_KEY&quot;),<br>    azure_endpoint=os.getenv(&quot;AZURE_OPENAI_ENDPOINT&quot;),<br>    api_version=os.getenv(&quot;AZURE_OPENAI_API_VERSION&quot;),<br>)<br><br><br># RAG system prompt<br>system_prompt = &quot;&quot;&quot;<br>You are an AI assistant for answering questions about an uploaded PDF.<br><br>Use only the following retrieved context to answer the user&#39;s question.<br><br>Context:<br>{context}<br><br>If the answer is not available in the context, say:<br>&quot;I could not find this information in the uploaded PDF.&quot;<br><br>Keep the answer concise and limit it to approximately two lines.<br>&quot;&quot;&quot;<br><br><br>prompt = ChatPromptTemplate.from_messages(<br>    [<br>        (&quot;system&quot;, system_prompt),<br>        (&quot;human&quot;, &quot;{input}&quot;),<br>    ]<br>)<br><br><br># Streamlit page<br>st.title(&quot;🤖 AI Bot 🤖&quot;)<br><br>st.markdown(<br>    &quot;Welcome to ChatBOT, built using RAG technologies.&quot;<br>)<br><br><br># Initialize chat history<br>if &quot;messages&quot; not in st.session_state:<br>    st.session_state.messages = []<br><br><br># Initialize vector store<br>if &quot;vector_store&quot; not in st.session_state:<br>    st.session_state.vector_store = None<br><br><br># PDF uploader<br>with st.sidebar:<br><br>    st.markdown(&quot;## 🗂️ PDF Uploader Section&quot;)<br><br>    uploaded_file = st.file_uploader(<br>        &quot;Choose a PDF file&quot;,<br>        type=[&quot;pdf&quot;],<br>    )<br><br>    button_press = st.button(<br>        &quot;Process PDF&quot;,<br>        type=&quot;primary&quot;,<br>    )<br><br>    if uploaded_file is not None and button_press:<br><br>        with st.spinner(&quot;Processing PDF...&quot;):<br><br>            # Save uploaded PDF temporarily<br>            with tempfile.NamedTemporaryFile(<br>                delete=False,<br>                suffix=&quot;.pdf&quot;,<br>            ) as tmp_file:<br><br>                tmp_file.write(uploaded_file.getvalue())<br>                pdf_path = tmp_file.name<br><br>            try:<br>                # Load PDF<br>                loader = PyMuPDFLoader(pdf_path)<br>                documents = loader.load()<br><br>                st.success(&quot;✅ File loaded successfully&quot;)<br>                st.write(f&quot;📄 Pages loaded: {len(documents)}&quot;)<br><br>                # Split PDF into chunks<br>                text_splitter = RecursiveCharacterTextSplitter(<br>                    separators=[&quot;\n\n&quot;, &quot;\n&quot;, &quot; &quot;, &quot;&quot;],<br>                    chunk_size=500,<br>                    chunk_overlap=100,<br>                    length_function=len,<br>                )<br><br>                chunks = text_splitter.split_documents(documents)<br><br>                st.success(&quot;✅ Chunks created&quot;)<br>                st.write(f&quot;📄 Total chunks: {len(chunks)}&quot;)<br><br>                # Create vector store<br>                vector_store = Chroma.from_documents(<br>                    documents=chunks,<br>                    embedding=embeddings,<br>                    collection_name=&quot;PDF_collection&quot;,<br>                )<br><br>                # Save vector store in Streamlit session state<br>                st.session_state.vector_store = vector_store<br><br>                # Clear old conversation after processing a new PDF<br>                st.session_state.messages = []<br><br>                st.success(&quot;✅ Vector store created successfully&quot;)<br>                st.info(&quot;You can now ask questions about the PDF.&quot;)<br><br>            except Exception as error:<br>                st.error(f&quot;PDF processing failed: {error}&quot;)<br><br>            finally:<br>                # Delete temporary PDF<br>                if os.path.exists(pdf_path):<br>                    os.remove(pdf_path)<br><br><br># Display previous chat messages<br>for message in st.session_state.messages:<br><br>    with st.chat_message(message[&quot;role&quot;]):<br>        st.markdown(message[&quot;content&quot;])<br><br><br># Chat input<br>question = st.chat_input(<br>    &quot;Ask a question about the uploaded PDF...&quot;<br>)<br><br><br>if question:<br><br>    # Display and save user message<br>    with st.chat_message(&quot;user&quot;):<br>        st.markdown(question)<br><br>    st.session_state.messages.append(<br>        {<br>            &quot;role&quot;: &quot;user&quot;,<br>            &quot;content&quot;: question,<br>        }<br>    )<br><br>    # Check whether PDF was processed<br>    if st.session_state.vector_store is None:<br><br>        warning_message = (<br>            &quot;Please upload and process a PDF before asking a question.&quot;<br>        )<br><br>        with st.chat_message(&quot;assistant&quot;):<br>            st.warning(warning_message)<br><br>        st.session_state.messages.append(<br>            {<br>                &quot;role&quot;: &quot;assistant&quot;,<br>                &quot;content&quot;: warning_message,<br>            }<br>        )<br><br>    else:<br><br>        try:<br>            with st.chat_message(&quot;assistant&quot;):<br><br>                with st.spinner(&quot;Searching the PDF...&quot;):<br><br>                    # Get vector store from session state<br>                    vector_store = st.session_state.vector_store<br><br>                    # Create retriever<br>                    retriever = vector_store.as_retriever(<br>                        search_kwargs={&quot;k&quot;: 2}<br>                    )<br><br>                    # Create document chain<br>                    document_chain = create_stuff_documents_chain(<br>                        llm,<br>                        prompt,<br>                    )<br><br>                    # Create retrieval chain<br>                    retrieval_chain = create_retrieval_chain(<br>                        retriever,<br>                        document_chain,<br>                    )<br><br>                    # Invoke RAG chain<br>                    rag_response = retrieval_chain.invoke(<br>                        {&quot;input&quot;: question}<br>                    )<br><br>                    # IMPORTANT: Define answer before checking it<br>                    answer = rag_response[&quot;answer&quot;]<br><br>                    not_found_message = (<br>                        &quot;i could not find this information &quot;<br>                        &quot;in the uploaded pdf&quot;<br>                    )<br><br>                    # Check whether information was found in PDF<br>                    if not_found_message in answer.lower():<br><br>                        st.warning(<br>                            &quot;⚠️ Information not found in the uploaded PDF. &quot;<br>                            &quot;Searching through the general LLM.&quot;<br>                        )<br><br>                        # Invoke Azure OpenAI directly<br>                        llm_response = llm.invoke(question)<br><br>                        # Extract string content from AIMessage<br>                        answer = llm_response.content<br><br>                    else:<br>                        st.success(&quot;✅ Answer found in the PDF.&quot;)<br><br>                    # Display final answer<br>                    st.markdown(answer)<br><br>                    # Save final answer in chat history<br>                    st.session_state.messages.append(<br>                        {<br>                            &quot;role&quot;: &quot;assistant&quot;,<br>                            &quot;content&quot;: answer,<br>                        }<br>                    )<br><br>        except Exception as error:<br><br>            error_message = f&quot;Unable to generate an answer: {error}&quot;<br><br>            with st.chat_message(&quot;assistant&quot;):<br>                st.error(error_message)<br><br>            st.session_state.messages.append(<br>                {<br>                    &quot;role&quot;: &quot;assistant&quot;,<br>                    &quot;content&quot;: error_message,<br>                }<br>            )</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tWYJpzaNz7xXmhUIZ7EqKg.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=199f1f4c74ac" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[LangGraph Tutorial]]></title>
            <link>https://medium.com/@reema.kumaritech/langgraph-tutorial-132d4bb012b9?source=rss-cb321e7882e1------2</link>
            <guid isPermaLink="false">https://medium.com/p/132d4bb012b9</guid>
            <category><![CDATA[langgraph]]></category>
            <dc:creator><![CDATA[Reema Kumaritech]]></dc:creator>
            <pubDate>Wed, 19 Aug 2026 07:53:06 GMT</pubDate>
            <atom:updated>2026-08-19T07:53:06.282Z</atom:updated>
            <content:encoded><![CDATA[<p>our main focus is on code in <strong>sections: Imports → State → Nodes → Routing Function → Graph Creation → Execution Flow</strong>.</p><h3>LangGraph Conditional Routing Example</h3><h3>Purpose of the Code</h3><p>This LangGraph workflow demonstrates:</p><ol><li>Creating a graph using StateGraph.</li><li>Defining a shared state.</li><li>Creating multiple nodes.</li><li>Using a conditional edge to dynamically choose the next node.</li><li>Executing the graph and returning the final state.</li></ol><pre>from langgraph.graph import START, END, StateGraph<br>from typing_extensions import TypedDict<br>from typing import Literal<br>import random<br>from IPython.display import display,Image<br><br>class State(TypedDict):<br>    game: str<br><br>def play_game(state: State):<br>    print(&quot;I am inside play_game node&quot;)<br>    return {&quot;game&quot;: state[&quot;game&quot;] + &quot;I am playing &quot;}<br><br>def badminton(state: State):<br>    print(&quot;I am inside badminton node&quot;)<br>    return {&quot;game&quot;: state[&quot;game&quot;] + &quot;badminton&quot;}<br><br>def cricket(state: State):<br>    print(&quot;I am inside cricket node&quot;)<br>    return {&quot;game&quot;: state[&quot;game&quot;] + &quot;cricket&quot;}<br><br>def random_game(state: State) -&gt; Literal[&quot;cricket&quot;, &quot;badminton&quot;]:<br>    print(&quot;I am inside random_game node&quot;)<br><br>    if random.random() &lt; 0.5:<br>        return &quot;cricket&quot;<br>    else:<br>        return &quot;badminton&quot;<br><br>graph = StateGraph(State)<br><br>graph.add_node(&quot;play_game&quot;, play_game)<br>graph.add_node(&quot;badminton&quot;, badminton)<br>graph.add_node(&quot;cricket&quot;, cricket)<br><br>graph.add_edge(START, &quot;play_game&quot;)<br>graph.add_conditional_edges(&quot;play_game&quot;, random_game)<br>graph.add_edge(&quot;badminton&quot;, END)<br>graph.add_edge(&quot;cricket&quot;, END)<br><br>app = graph.compile()<br>display(Image(graph_builder.get_graph().draw_mermaid_png()))<br>result = app.invoke({&quot;game&quot;: &quot;&quot;})<br>print(result)</pre><p>For your documentation, the best approach is to explain the code in <strong>sections: Imports → State → Nodes → Routing Function → Graph Creation → Execution Flow</strong>.</p><h3>LangGraph Conditional Routing Example</h3><h3>Purpose of the Code</h3><p>This LangGraph workflow demonstrates:</p><ol><li>Creating a graph using StateGraph.</li><li>Defining a shared state.</li><li>Creating multiple nodes.</li><li>Using a conditional edge to dynamically choose the next node.</li><li>Executing the graph and returning the final state.</li></ol><p>The workflow randomly chooses between <strong>Cricket</strong> and <strong>Badminton</strong>.</p><h3>1. Import Statements</h3><p>from langgraph.graph import START, END, StateGraph</p><p>from typing_extensions import TypedDict</p><p>from typing import Literal</p><p>import random</p><p>from IPython.display import display, Image</p><h3>Explanation</h3><h4>StateGraph</h4><p>from langgraph.graph import StateGraph</p><p>Used to create a LangGraph workflow.</p><p>Think of it as the container that holds:</p><ul><li>Nodes</li><li>Edges</li><li>State</li></ul><h4>START</h4><p>from langgraph.graph import START</p><p>Represents the entry point of the graph.</p><p>Every graph execution begins from the START node.</p><h4>END</h4><p>from langgraph.graph import END</p><p>Represents the termination point of the graph.</p><p>When execution reaches END, the graph stops.</p><h4>TypedDict</h4><p>from typing_extensions import TypedDict</p><p>Defines the structure of the graph state.</p><p>It tells LangGraph:</p><blockquote><em>What data can flow between nodes?</em></blockquote><h4>Literal</h4><p>from typing import Literal</p><p>Used for type safety.</p><p>It specifies that a function can only return particular values.</p><p>Example:</p><p>Literal[“cricket”, “badminton”]</p><p>means only these two strings are allowed.</p><h4>random</h4><p>import random</p><p>Used to generate a random number.</p><p>This helps in choosing either Cricket or Badminton.</p><h4>display and Image</h4><p>from IPython.display import display, Image</p><p>Used to display the graph diagram inside Jupyter Notebook.</p><h3>2. Defining State</h3><p>class State(TypedDict):</p><p>game: str</p><h3>Explanation</h3><p>This defines the information that flows through the graph.</p><p>Current state contains:</p><p>{</p><p>“game”: “”</p><p>}</p><h3>Example</h3><p>Initial State:</p><p>{</p><p>“game”: “”</p><p>}</p><p>After first node:</p><p>{</p><p>“game”:”I am playing “</p><p>}</p><p>After cricket node:</p><p>{</p><p>“game”:”I am playing cricket”</p><p>}</p><h3>3. Node: play_game</h3><p>def play_game(state: State):</p><p>print(“I am inside play_game node”)</p><p>return {“game”: state[“game”] + “I am playing “}</p><h3>Purpose</h3><p>This node initializes the game message.</p><h3>Input State</h3><p>{</p><p>“game”:””</p><p>}</p><h3>Output State</h3><p>{</p><p>“game”:”I am playing “</p><p>}</p><p>``</p><h3>Console Output</h3><p>I am inside play_game node</p><h3>4. Node: badminton</h3><p>def badminton(state: State):</p><p>print(“I am inside badminton node”)</p><p>return {“game”: state[“game”] + “badminton”}</p><h3>Purpose</h3><p>Adds “badminton” to the game message.</p><h3>Input</h3><p>{</p><p>“game”:”I am playing “</p><p>}</p><h3>Output</h3><p>{</p><p>“game”:”I am playing badminton”</p><p>}</p><h3>5. Node: cricket</h3><p>def cricket(state: State):</p><p>print(“I am inside cricket node”)</p><p>return {“game”: state[“game”] + “cricket”}</p><h3>Purpose</h3><p>Adds “cricket” to the state.</p><h3>Input</h3><p>{</p><p>“game”:”I am playing “</p><p>}</p><p>``</p><h3>Output</h3><p>{</p><p>“game”:”I am playing cricket”</p><p>}</p><h3>6. Conditional Routing Function</h3><p>def random_game(state: State) -&gt; Literal[“cricket”, “badminton”]:</p><p>`</p><h3>Purpose</h3><p>Determines which node should execute next.</p><h3>Logic</h3><p>if random.random() &lt; 0.5:</p><p>return “cricket”</p><p>else:</p><p>return “badminton”</p><h3>Example</h3><p>Suppose:</p><p>random.random()</p><p>returns</p><p>0.25</p><p>Then:</p><p>return “cricket”</p><p>Suppose:</p><p>random.random()</p><p>returns</p><p>0.75</p><p>Then:</p><p>return “badminton”</p><h3>Why Literal is Used?</h3><p>-&gt; Literal[“cricket”, “badminton”]</p><p>This tells LangGraph:</p><p>The function can only route to:</p><p>“cricket”</p><p>or</p><p>“badminton”</p><p>No other node names are allowed.</p><h3>7. Creating the Graph</h3><p>graph = StateGraph(State)</p><h3>Purpose</h3><p>Creates a graph that uses the State schema.</p><p>Visually:</p><p>Graph</p><p>├── Nodes</p><p>├── Edges</p><p>└── State</p><h3>8. Adding Nodes</h3><p>graph.add_node(“play_game”, play_game)</p><p>graph.add_node(“badminton”, badminton)</p><p>graph.add_node(“cricket”, cricket)</p><h3>Syntax</h3><p>add_node(“node_name”, function)</p><h3>Mapping</h3><p>Node NameFunctionplay_gameplay_game()badmintonbadminton()cricketcricket()</p><h3>9. Adding Edges</h3><h3>START → play_game</h3><p>graph.add_edge(START, “play_game”)</p><p>Execution starts here:</p><p>START</p><p>|</p><p>v</p><p>play_game</p><h3>Conditional Edge</h3><p>graph.add_conditional_edges(</p><p>“play_game”,</p><p>random_game</p><p>)</p><p>After play_game executes:</p><p>play_game</p><p>|</p><p>v</p><p>random_game()</p><p>random_game() decides:</p><p>play_game</p><p>|</p><p>+ — — -&gt; cricket</p><p>|</p><p>+ — — -&gt; badminton</p><h3>Connecting to END</h3><p>graph.add_edge(“badminton”, END)</p><p>graph.add_edge(“cricket”, END)</p><p>``</p><p>Visually:</p><p>badminton — → END</p><p>cricket — — → END</p><h3>10. Compile Graph</h3><p>app = graph.compile()</p><h3>Purpose</h3><p>Converts the graph definition into an executable application.</p><p>Before:</p><p>Graph Definition</p><p>After:</p><p>Runnable LangGraph App</p><h3>11. Display Graph</h3><p>display(</p><p>Image(</p><p>graph_builder.get_graph().draw_mermaid_png()</p><p>)</p><p>)</p><h3>Purpose</h3><p>Generates and displays the workflow diagram.</p><p>Expected Diagram:</p><p>START</p><p>|</p><p>v</p><p>play_game</p><p>|</p><p>+ — — -&gt; cricket</p><p>| |</p><p>| v</p><p>| END</p><p>|</p><p>+ — — -&gt; badminton</p><p>|</p><p>v</p><p>END</p><h3>Note</h3><p>Your code contains a bug:</p><p>graph_builder</p><p>is not defined.</p><p>You compiled as:</p><p>app = graph.compile()</p><p>Therefore use:</p><p>display(</p><p>Image(</p><p>app.get_graph().draw_mermaid_png()</p><p>)</p><p>)</p><h3>12. Execute Graph</h3><p>result = app.invoke({“game”: “”})</p><h3>Initial State</h3><p>{</p><p>“game”:””</p><p>}</p><h3>Execution Flow</h3><p>Suppose random choice is Cricket.</p><p>Step 1:</p><p>START</p><p>↓</p><p>Step 2:</p><p>play_game</p><p>State becomes:</p><p>{</p><p>“game”:”I am playing “</p><p>}</p><p>↓</p><p>Step 3:</p><p>random_game</p><p>returns:</p><p>“cricket”</p><p>↓</p><p>Step 4:</p><p>cricket</p><p>State becomes:</p><p>{</p><p>“game”:”I am playing cricket”</p><p>}</p><p>``</p><p>↓</p><p>Step 5:</p><p>END</p><h3>13. Final Output</h3><p>print(result)</p><p>Possible output:</p><p>{‘game’: ‘I am playing cricket’}</p><p>or</p><p>{‘game’: ‘I am playing badminton’}</p><h3>Complete Workflow Diagram</h3><p>START</p><p>|</p><p>v</p><p>+ — — — — — — -+</p><p>| play_game |</p><p>+ — — — — — — -+</p><p>|</p><p>v</p><p>+ — — — — — — -+</p><p>| random_game |</p><p>+ — — — — — — -+</p><p>/ &lt;/span&gt;</p><p>/ &lt;/span&gt;</p><p>v v</p><p>+ — — — — — — + + — — — — — — +</p><p>| cricket | | badminton |</p><p>+ — — — — — — + + — — — — — — +</p><p>| |</p><p>v v</p><p>END</p><h3>Key LangGraph Concepts Used</h3><ul><li><strong>State</strong>: Shared data passed between nodes.</li><li><strong>Node</strong>: A Python function that performs a task.</li><li><strong>Edge</strong>: Defines the next node to execute.</li><li><strong>Conditional Edge</strong>: Dynamically chooses the next node.</li><li><strong>START</strong>: Entry point of the graph.</li><li><strong>END</strong>: Exit point of the graph.</li><li><strong>compile()</strong>: Converts the graph into an executable app.</li><li><strong>invoke()</strong>: Runs the graph with input state.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=132d4bb012b9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[React js tutorial End To End]]></title>
            <link>https://medium.com/@reema.kumaritech/react-js-tutorial-end-to-end-02edc315a353?source=rss-cb321e7882e1------2</link>
            <guid isPermaLink="false">https://medium.com/p/02edc315a353</guid>
            <category><![CDATA[bootstrap]]></category>
            <category><![CDATA[reactjs]]></category>
            <dc:creator><![CDATA[Reema Kumaritech]]></dc:creator>
            <pubDate>Wed, 19 Aug 2026 07:21:57 GMT</pubDate>
            <atom:updated>2026-08-19T07:21:57.028Z</atom:updated>
            <content:encoded><![CDATA[<ol><li>to check whether react and npm is install or not use below command to check the installation</li></ol><h3>Check Node.js Installation</h3><p>Before creating a React app, verify Node.js is installed:</p><ol><li>node -v</li><li>npm -v</li></ol><h3>Create my-react-app, use below command</h3><pre>npx create-react-app my-react-app</pre><blockquote>when you will use above command to create the react app it will take sometimes to download all external libraries, the <strong>node module</strong> folder contains all react libraries</blockquote><p>for running react app use below command</p><pre>npm start</pre><p>you will see below folder created</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/250/1*apxUrn16gvcdL7uGRbmI5A.png" /></figure><p>create component folder and under that you create your components and call it from app.js</p><h3><strong>app.js</strong></h3><pre><br>import &#39;./App.css&#39;;<br>import Form from &#39;./components/Form&#39;;<br>import Menubar1 from &#39;./components/Menubar1&#39;;<br><br>function App() {<br>  return (<br>    &lt;div className=&quot;App&quot;&gt;<br>      &lt;Menubar1&gt;&lt;/Menubar1&gt;<br>      &lt;Form heading=&quot;Enter your text here....&quot;&gt;&lt;/Form&gt;<br>    &lt;/div&gt;<br>  );<br>}<br><br>export default App;</pre><p>Form.js</p><pre>import React, { useState } from &#39;react&#39;;<br><br>export default function Form(props) {<br><br>    const [text, setText] = useState(&quot;set your text&quot;);<br><br>    const onChangeFunction = (event) =&gt; {<br>        setText(event.target.value);<br>    };<br><br>    const OnClickFunction = () =&gt; {<br>        let newtext = text.toUpperCase();<br>        setText(newtext);<br>    };<br>    const OnClickFunction1=()=&gt;{<br>        let newtext=text.toLowerCase();<br>        setText(newtext)<br>    }<br><br>    return (<br>        &lt;div className=&quot;mb-3&quot;&gt;<br><br>            &lt;h2&gt;{props.heading}&lt;/h2&gt;<br><br>            &lt;label<br>                htmlFor=&quot;exampleFormControlTextarea1&quot;<br>                className=&quot;form-label&quot;&gt;<br>                ChatBOT<br>            &lt;/label&gt;<br><br>            &lt;textarea<br>                className=&quot;form-control&quot;<br>                id=&quot;exampleFormControlTextarea1&quot;<br>                rows=&quot;8&quot;<br>                value={text}<br>                onChange={onChangeFunction}<br>                style={{ width: &quot;500px&quot;, marginLeft: &quot;500px&quot; }}<br>            /&gt;<br><br>            &lt;button<br>                className=&quot;btn btn-primary&quot;<br>                onClick={OnClickFunction}&gt;<br>                Set to UPPERCASE<br>            &lt;/button&gt;<br>            &lt;button<br>                className=&quot;btn btn-secondary&quot;<br>                onClick={OnClickFunction1}&gt;<br>                Set to Lowercase<br>            &lt;/button&gt;<br><br>        &lt;/div&gt;<br>    );<br>}</pre><p>Menubar1.js</p><pre>import React from &#39;react&#39;<br><br>export default function Menubar1() {<br>  return (<br>   &lt;&gt;<br>    &lt;h1&gt;Welcome to chatbot&lt;/h1&gt;<br>     &lt;div&gt;<br>            &lt;nav className=&quot;navbar navbar-expand-lg navbar-light bg-light&quot;&gt;<br>                &lt;a className=&quot;navbar-brand&quot; href=&quot;\&quot;&gt;Navbar&lt;/a&gt;<br>                &lt;button className=&quot;navbar-toggler&quot; type=&quot;button&quot; data-toggle=&quot;collapse&quot; data-target=&quot;#navbarSupportedContent&quot; aria-controls=&quot;navbarSupportedContent&quot; aria-expanded=&quot;false&quot; aria-label=&quot;Toggle navigation&quot;&gt;<br>                    &lt;span className=&quot;navbar-toggler-icon&quot;&gt;&lt;/span&gt;<br>                &lt;/button&gt;<br><br>                &lt;div className=&quot;collapse navbar-collapse&quot; id=&quot;navbarSupportedContent&quot;&gt;<br>                    &lt;ul className=&quot;navbar-nav mr-auto&quot;&gt;<br>                    &lt;li className=&quot;nav-item active&quot;&gt;<br>                        &lt;a className=&quot;nav-link&quot; href=&quot;\&quot;&gt;Home &lt;span className=&quot;sr-only&quot;&gt;(current)&lt;/span&gt;&lt;/a&gt;<br>                    &lt;/li&gt;<br>                    &lt;li className=&quot;nav-item&quot;&gt;<br>                        &lt;a className=&quot;nav-link&quot; href=&quot;\&quot;&gt;Link&lt;/a&gt;<br>                    &lt;/li&gt;<br>                    <br>                    &lt;/ul&gt;<br>                    <br>                &lt;/div&gt;<br>            &lt;/nav&gt;<br>    &lt;/div&gt;<br>   &lt;/&gt;<br>  )<br>}</pre><p>index.html</p><pre>&lt;!DOCTYPE html&gt;<br>&lt;html lang=&quot;en&quot;&gt;<br>  &lt;head&gt;<br>    &lt;meta charset=&quot;utf-8&quot; /&gt;<br>    &lt;link rel=&quot;icon&quot; href=&quot;%PUBLIC_URL%/favicon.ico&quot; /&gt;<br>    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot; /&gt;<br>    &lt;meta name=&quot;theme-color&quot; content=&quot;#000000&quot; /&gt;<br>    &lt;meta<br>      name=&quot;description&quot;<br>      content=&quot;Web site created using create-react-app&quot;<br>    /&gt;<br>    &lt;link rel=&quot;apple-touch-icon&quot; href=&quot;%PUBLIC_URL%/logo192.png&quot; /&gt;<br>   <br>    &lt;link rel=&quot;manifest&quot; href=&quot;%PUBLIC_URL%/manifest.json&quot; /&gt;<br>    <br>    &lt;title&gt;React App&lt;/title&gt;<br>    &lt;link href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot; integrity=&quot;sha384-sRIl4kxILFvY47J16cr9ZwB07vP4J8+LH7qKQnuqkuIAvNWLzeN8tE5YBujZqJLB&quot; crossorigin=&quot;anonymous&quot;&gt;<br><br>  &lt;/head&gt;<br>  &lt;body&gt;<br>    &lt;noscript&gt;You need to enable JavaScript to run this app.&lt;/noscript&gt;<br>    &lt;div id=&quot;root&quot;&gt;<br>      &lt;script src=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/js/bootstrap.bundle.min.js&quot; integrity=&quot;sha384-FKyoEForCGlyvwx9Hj09JcYn3nv7wiPVlz7YYwJrWVcXK/BmnVDxM+D2scQbITxI&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;<br>      <br>    &lt;/div&gt;<br>    <br>  &lt;/body&gt;<br>&lt;/html&gt;</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=02edc315a353" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[RAG Chat-BOT]]></title>
            <link>https://medium.com/@reema.kumaritech/rag-chat-bot-e4190866a3f2?source=rss-cb321e7882e1------2</link>
            <guid isPermaLink="false">https://medium.com/p/e4190866a3f2</guid>
            <category><![CDATA[rags]]></category>
            <category><![CDATA[conversational-ai]]></category>
            <category><![CDATA[streamlit]]></category>
            <dc:creator><![CDATA[Reema Kumaritech]]></dc:creator>
            <pubDate>Mon, 17 Aug 2026 13:59:12 GMT</pubDate>
            <atom:updated>2026-08-17T13:59:12.440Z</atom:updated>
            <content:encoded><![CDATA[<pre>import os<br>import tempfile<br>import streamlit as st<br>from dotenv import load_dotenv<br><br>from langchain_openai import AzureChatOpenAI, AzureOpenAIEmbeddings<br>from langchain_community.document_loaders import PyMuPDFLoader<br>from langchain_text_splitters import RecursiveCharacterTextSplitter<br>from langchain_chroma import Chroma<br>from langchain_core.prompts import ChatPromptTemplate<br>from langchain.chains import create_retrieval_chain<br>from langchain.chains.combine_documents import create_stuff_documents_chain<br><br># Load Environment Variables<br>load_dotenv()<br><br># Streamlit UI<br>st.title(&quot;📄 RAG ChatBot&quot;)<br>st.write(&quot;Upload a PDF and ask questions about it.&quot;)<br><br># Sidebar<br>with st.sidebar:<br>    st.header(&quot;Upload PDF&quot;)<br>    uploaded_file = st.file_uploader(<br>        &quot;Choose a PDF file&quot;,<br>        type=&quot;pdf&quot;<br>    )<br><br>if uploaded_file:<br><br>    # Save Uploaded PDF Temporarily<br>    with tempfile.NamedTemporaryFile(delete=False, suffix=&quot;.pdf&quot;) as tmp_file:<br>        tmp_file.write(uploaded_file.read())<br>        pdf_path = tmp_file.name<br><br>    # Load PDF<br>    loader = PyMuPDFLoader(pdf_path)<br>    documents = loader.load()<br><br>    with st.sidebar:<br>        st.success(f&quot;PDF Loaded: {len(documents)} pages&quot;)<br><br>    # Split Documents<br>    splitter = RecursiveCharacterTextSplitter(<br>        chunk_size=400,<br>        chunk_overlap=50,<br>        separators=[&quot;\n\n&quot;, &quot;\n&quot;, &quot; &quot;, &quot;&quot;]<br>    )<br><br>    chunks = splitter.split_documents(documents)<br><br>    with st.sidebar:<br>        st.success(f&quot;Created {len(chunks)} chunks&quot;)<br><br>    # Azure Embeddings<br>    embeddings = AzureOpenAIEmbeddings(<br>        azure_deployment=os.getenv(&quot;AZURE_OPENAI_EMBEDDING_DEPLOYMENT&quot;),<br>        azure_endpoint=os.getenv(&quot;AZURE_OPENAI_ENDPOINT&quot;),<br>        api_key=os.getenv(&quot;AZURE_OPENAI_API_KEY&quot;),<br>        api_version=os.getenv(&quot;AZURE_OPENAI_API_VERSION&quot;)<br>    )<br><br>    # Create Vector Store<br>    vectorstore = Chroma.from_documents(<br>        documents=chunks,<br>        embedding=embeddings,<br>        collection_name=&quot;pdf_collection&quot;,<br>        persist_directory=&quot;./chroma_langchain_db&quot;<br>    )<br><br>    with st.sidebar:<br>        st.success(&quot;✅ Vector Store Created&quot;)<br><br>    # Azure Chat Model<br>    llm = AzureChatOpenAI(<br>        azure_deployment=os.getenv(&quot;AZURE_OPENAI_CHAT_DEPLOYMENT&quot;),<br>        azure_endpoint=os.getenv(&quot;AZURE_OPENAI_ENDPOINT&quot;),<br>        api_key=os.getenv(&quot;AZURE_OPENAI_API_KEY&quot;),<br>        api_version=os.getenv(&quot;AZURE_OPENAI_API_VERSION&quot;),<br>        temperature=0<br>    )<br><br>    # Prompt<br>    system_prompt = &quot;&quot;&quot;<br>    You are a helpful AI assistant.<br><br>    Answer only using the provided context.<br><br>    If the answer is not available in the context,<br>    say: &quot;I could not find the answer in the document.&quot;<br><br>    Keep answers concise.<br><br>    Context:<br>    {context}<br>    &quot;&quot;&quot;<br><br>    prompt = ChatPromptTemplate.from_messages(<br>        [<br>            (&quot;system&quot;, system_prompt),<br>            (&quot;human&quot;, &quot;{input}&quot;)<br>        ]<br>    )<br><br>    # Retriever<br>    retriever = vectorstore.as_retriever(<br>        search_kwargs={&quot;k&quot;: 2}<br>    )<br><br>    # Chains<br>    document_chain = create_stuff_documents_chain(<br>        llm,<br>        prompt<br>    )<br><br>    rag_chain = create_retrieval_chain(<br>        retriever,<br>        document_chain<br>    )<br><br>    # User Question<br>    question = st.chat_input(<br>        &quot;Ask a question about the PDF...&quot;<br>    )<br><br>    if question:<br><br>        with st.chat_message(&quot;user&quot;):<br>            st.write(question)<br><br>        response = rag_chain.invoke(<br>            {<br>                &quot;input&quot;: question<br>            }<br>        )<br><br>        with st.chat_message(&quot;assistant&quot;):<br>            st.write(response[&quot;answer&quot;])<br><br>        # Optional: Show Retrieved Chunks<br>        with st.expander(&quot;Source Documents&quot;):<br>            for doc in response[&quot;context&quot;]:<br>               st.write(doc.page_content)</pre><p><strong><em>RAG Conversational chatbot</em></strong></p><pre>import os<br>from dotenv import load_dotenv<br>load_dotenv()<br>from langchain_openai import AzureChatOpenAI,AzureOpenAIEmbeddings<br>import streamlit as st<br>from langchain_community.document_loaders import PyMuPDFLoader<br>import tempfile<br>from langchain_text_splitters import RecursiveCharacterTextSplitter<br>from langchain_chroma import Chroma<br>from langchain.chains import create_retrieval_chain<br>from langchain.chains.combine_documents import create_stuff_documents_chain<br>from langchain.prompts import ChatPromptTemplate<br><br><br><br>#LLM call<br><br>llm=AzureChatOpenAI(<br>        azure_deployment=os.getenv(&quot;AZURE_OPENAI_DEPLOYMENT&quot;),<br>        api_version=&quot;2024-05-01-preview&quot;,<br>        temperature=0,<br>        max_tokens=None,<br>        timeout=None,<br>        max_retries=2 <br>)<br><br><br><br>st.title(&quot;📄 RAG ChatBot&quot;)<br>with st.sidebar:<br>    st.header(&quot;📁 Upload PDF&quot;)<br>    file_var=st.file_uploader(&quot;upload file&quot;,type=&quot;pdf&quot;)<br>    if file_var:<br>        st.success(&quot;✅ PDF Uploaded Successfully&quot;)<br>        # Save Uploaded PDF Temporarily<br>        with tempfile.NamedTemporaryFile(delete=False, suffix=&quot;.pdf&quot;) as tmp_file:<br>            tmp_file.write(file_var.read())<br>            pdf_path = tmp_file.name<br><br>        #load PDF document<br>        loader=PyMuPDFLoader(pdf_path)<br>        document=loader.load()<br>        st.success(f&quot;✅ number of PDF  {len(document)} pages&quot;)<br>        #split the PDF file<br>        recursive_splitter=RecursiveCharacterTextSplitter(<br>            chunk_size=400,<br>            chunk_overlap=50,<br>            separators=[&quot;\n\n&quot;, &quot;\n&quot;, &quot; &quot;, &quot;&quot;]<br>        )<br>        chunks=recursive_splitter.split_documents(document)<br><br>        st.success(f&quot;✅ PDF splitted and number of chunks are {len(chunks)} &quot;)<br><br>        #embeddings<br>        embedding_azure=AzureOpenAIEmbeddings(<br>            model=&quot;text-embedding-3-large&quot;,<br>            azure_endpoint=os.getenv(&quot;AZURE_OPENAI_ENDPOINT&quot;),<br>            api_key=os.getenv(&quot;AZURE_OPENAI_API_KEY&quot;),<br>            openai_api_version=os.getenv(&quot;AZURE_OPENAI_API_VERSION&quot;)<br><br>        )<br>        #store into vector store<br>        vectorstore=Chroma.from_documents(<br>            documents=chunks, <br>            embedding=embedding_azure, <br>            collection_name=&quot;RAG_Collection&quot;,<br>            persist_directory=&quot;./chroma_langchain_db&quot;<br>        )<br>        st.success(f&quot;✅ Vector store {(vectorstore)} &quot;)<br><br>        retriever= vectorstore.as_retriever(<br>            search_kwargs={&#39;k&#39;: 3}<br>        )<br><br>        #craete prompt<br>        # Prompt<br>        system_prompt = &quot;&quot;&quot;<br>        You are a helpful AI assistant.<br><br>        Answer only using the provided context.<br><br>        If the answer is not available in the context,<br>        say: &quot;I could not find the answer in the document.&quot;<br><br>        Keep answers concise.<br><br>        Context:<br>        {context}<br>        &quot;&quot;&quot;<br><br>        prompt = ChatPromptTemplate.from_messages(<br>            [<br>                (&quot;system&quot;, system_prompt),<br>                (&quot;human&quot;, &quot;{input}&quot;)<br>            ]<br>        )<br>        <br>        #create stuff document chain<br>        create_docu_chain=create_stuff_documents_chain(<br>            llm,prompt<br>        )<br>        Rag_Chain= create_retrieval_chain(<br>            retriever,create_docu_chain<br><br>        )<br><br>if &quot;messages&quot; not in st.session_state:<br>    st.session_state.messages=[]<br><br># Display previous chat history<br>for message in st.session_state.messages:<br>    with st.chat_message(message[&quot;role&quot;]):<br>        st.write(message[&quot;content&quot;])<br><br>if file_var:<br>        #take input from user<br>        question=st.chat_input(&quot;Enter your Query related to uploaded document here...&quot;)<br><br>        if question:<br>            #display mquestion<br>            with st.chat_message(&quot;user&quot;):<br>                st.write(question)<br>            #save question<br>            st.session_state.messages.append({&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: question})<br>            with st.chat_message(&quot;assistant&quot;):<br>               response= Rag_Chain.invoke({<br>                &quot;input&quot;:question<br>               })<br>               st.write(response[&quot;answer&quot;])<br>               # Optional: Show Retrieved Chunks<br>               with st.expander(&quot;Source Documents&quot;):<br>                    for doc in response[&quot;context&quot;]:<br>                        st.write(doc.page_content) <br>                <br>            #save response <br>            st.session_state.messages.append({&quot;role&quot;: &quot;assistant&quot;, &quot;content&quot;: response[&quot;answer&quot;]})</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e4190866a3f2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[RAG and RAG ChatBOT]]></title>
            <link>https://medium.com/@reema.kumaritech/rag-and-rag-chatbot-f5a6fcbaf12d?source=rss-cb321e7882e1------2</link>
            <guid isPermaLink="false">https://medium.com/p/f5a6fcbaf12d</guid>
            <category><![CDATA[chatbots]]></category>
            <category><![CDATA[rags]]></category>
            <category><![CDATA[streamlit]]></category>
            <dc:creator><![CDATA[Reema Kumaritech]]></dc:creator>
            <pubDate>Mon, 17 Aug 2026 11:16:04 GMT</pubDate>
            <atom:updated>2026-08-17T11:16:04.674Z</atom:updated>
            <content:encoded><![CDATA[<ol><li>embeddings</li></ol><pre>from langchain_huggingface import HuggingFaceEmbeddings<br>from langchain_community.document_loaders import PyMuPDFLoader<br>from langchain_text_splitters import RecursiveCharacterTextSplitter<br><br>#document loaded<br>loader=PyMuPDFLoader(&quot;Data/PDF/attention.pdf&quot;)<br>documents=loader.load()<br><br>#create chunks<br>Recursive_splitter=RecursiveCharacterTextSplitter(<br>    separators=[&quot;\n\n&quot;, &quot;\n&quot;, &quot; &quot;, &quot;&quot;],  <br>    chunk_size=100,<br>    chunk_overlap=50, <br>    length_function=len<br>)<br>chunks=Recursive_splitter.split_documents(documents)<br># Extract text from Document objects<br><br>texts = [doc.page_content for doc in chunks]<br><br>#convert chunks into vectors by using embedding models<br>embeddings = HuggingFaceEmbeddings(<br>    model_name=&quot;sentence-transformers/all-mpnet-base-v2&quot;,<br>    encode_kwargs={&quot;normalize_embeddings&quot;: True},<br>)<br><br>#query_result = embeddings.embed_query(&quot;This is a test document.&quot;)<br>doc_result = embeddings.embed_documents(texts)<br><br>print(doc_result)</pre><h3>The below code is for OpenAI Embeddings</h3><pre>import os<br>from dotenv import load_dotenv<br>load_dotenv()<br><br>from langchain_openai import AzureOpenAIEmbeddings <br>from langchain_community.document_loaders import PyMuPDFLoader<br>from langchain_text_splitters import RecursiveCharacterTextSplitter<br><br>loader=PyMuPDFLoader(&quot;Data/PDF/attention.pdf&quot;)<br>documents=loader.load()<br><br>#load pdf file<br>pdf_splitter=RecursiveCharacterTextSplitter(separators=[&quot;\n\n&quot;, &quot;\n&quot;, &quot; &quot;, &quot;&quot;],<br>chunk_size=100,<br>chunk_overlap=20, <br>length_function=len<br>)<br>#create chunks<br>chunks=pdf_splitter.split_documents(documents)<br>texts = [doc.page_content for doc in chunks]<br><br><br>embeddings = AzureOpenAIEmbeddings(<br>        model=&quot;text-embedding-3-large&quot;<br>        # dimensions: int | None = None, # Can specify dimensions with new text-embedding-3 models<br>        # azure_endpoint=&quot;https://&lt;your-endpoint&gt;.openai.azure.com/&quot;, If not provided, will read env variable AZURE_OPENAI_ENDPOINT<br>        # api_key=... # Can provide an API key directly. If missing read env variable AZURE_OPENAI_API_KEY<br>        # openai_api_version=..., # If not provided, will read env variable AZURE_OPENAI_API_VERSION<br>    )<br><br>#craete vector<br>vector=embeddings.embed_documents(texts)<br><br>print(len(vector))<br>print(vector)</pre><h3><strong>Difference between Vector store and Vector Database</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/882/1*W-49i0LzDbvR6zKiU25ZSQ.png" /></figure><h3>Most Common Choices in RAG</h3><h3>For Learning ( for POC)</h3><p>✅ FAISS<br> ✅ ChromaDB</p><h3>For Production</h3><p>✅ Pinecone<br> ✅ Qdrant<br> ✅ Milvus<br> ✅ Azure AI Search</p><pre>from langchain_chroma import Chroma<br>from langchain_community.vectorstores import FAISS</pre><pre>from langchain_chroma import Chroma<br><br>vector_store = Chroma(<br>    collection_name=&quot;example_collection&quot;,<br>    embedding_function=embeddings,<br>    persist_directory=&quot;./chroma_langchain_db&quot;,  # Where to save data locally, remove if not necessary<br>)</pre><h3>Similarity search</h3><p>Issue a semantic query using similarity_search, which returns the closest embedded documents:</p><pre>similar_docs = vector_store.similarity_search(&quot;your query here&quot;)</pre><h3>A complete code till vector store</h3><pre>import os<br>from dotenv import load_dotenv<br><br>load_dotenv()<br><br>from langchain_community.document_loaders import PyMuPDFLoader<br>from langchain_text_splitters import RecursiveCharacterTextSplitter<br>from langchain_openai import AzureOpenAIEmbeddings<br>from langchain_chroma import Chroma<br><br># Load PDF<br>print(&quot;Loading PDF...&quot;)<br>print(&quot;--&quot;*50)<br>loader = PyMuPDFLoader(&quot;Data/PDF/attention.pdf&quot;)<br>documents = loader.load()<br>print(f&quot;Loaded {len(documents)} pages&quot;)<br>print(&quot;--&quot;*50)<br># Split into chunks<br>print(&quot;Splitting documents...&quot;)<br>print(&quot;--&quot;*50)<br>splitter = RecursiveCharacterTextSplitter(<br>    chunk_size=200,<br>    chunk_overlap=50,<br>    separators=[&quot;\n\n&quot;, &quot;\n&quot;, &quot; &quot;, &quot;&quot;]<br>)<br><br>chunks = splitter.split_documents(documents)<br>print(f&quot;Created {len(chunks)} chunks&quot;)<br>print(&quot;--&quot;*50)<br># Create Azure OpenAI Embedding Model<br>print(&quot;Initializing embeddings...&quot;)<br>print(&quot;--&quot;*50)<br>azure_embeddings = AzureOpenAIEmbeddings(<br>    model=&quot;text-embedding-3-large&quot;<br>)<br><br># Create Chroma Vector Store<br>print(&quot;Creating vector store...&quot;)<br><br>&quot;&quot;&quot;Chroma.from_documents() automatically:<br>Extracts text from chunks.<br>Generates embeddings using Azure OpenAI.<br>Stores vectors in ChromaDB.&quot;&quot;&quot;<br>print(&quot;--&quot;*50)<br>vectorstore = Chroma.from_documents(<br>    documents=chunks,<br>    embedding=embeddings,<br>    collection_name=&quot;PDF_collection&quot;,<br>    persist_directory=&quot;./chroma_langchain_db&quot;<br>)<br><br>print(&quot;Data stroed in Vector Store Successfully!&quot;)<br>print(&quot;--&quot;*50)<br>print(vectorstore)</pre><h3>Final code with Chain</h3><pre>import os<br>from dotenv import load_dotenv<br><br># Load environment variables<br>load_dotenv()<br><br>from langchain_community.document_loaders import PyMuPDFLoader<br>from langchain_text_splitters import RecursiveCharacterTextSplitter<br>from langchain_openai import AzureOpenAIEmbeddings, AzureChatOpenAI<br>from langchain_chroma import Chroma<br>from langchain_core.prompts import ChatPromptTemplate<br>from langchain.chains import create_retrieval_chain<br>from langchain.chains.combine_documents import create_stuff_documents_chain<br><br># --------------------------------------------------<br># Load PDF<br># --------------------------------------------------<br>print(&quot;Loading PDF...&quot;)<br>print(&quot;--&quot; * 50)<br><br>loader = PyMuPDFLoader(&quot;Data/PDF/attention.pdf&quot;)<br>documents = loader.load()<br><br>print(f&quot;Loaded {len(documents)} pages&quot;)<br>print(&quot;--&quot; * 50)<br><br># --------------------------------------------------<br># Split Documents<br># --------------------------------------------------<br>print(&quot;Splitting documents...&quot;)<br>print(&quot;--&quot; * 50)<br><br>splitter = RecursiveCharacterTextSplitter(<br>    chunk_size=200,<br>    chunk_overlap=50,<br>    separators=[&quot;\n\n&quot;, &quot;\n&quot;, &quot; &quot;, &quot;&quot;]<br>)<br><br>chunks = splitter.split_documents(documents)<br><br>print(f&quot;Created {len(chunks)} chunks&quot;)<br>print(&quot;--&quot; * 50)<br><br># --------------------------------------------------<br># Azure OpenAI Embeddings<br># --------------------------------------------------<br>print(&quot;Initializing embeddings...&quot;)<br>print(&quot;--&quot; * 50)<br><br>&quot;&quot;&quot;embeddings = AzureOpenAIEmbeddings(<br>    azure_deployment=os.getenv(&quot;AZURE_OPENAI_EMBEDDING_DEPLOYMENT&quot;),<br>    api_key=os.getenv(&quot;AZURE_OPENAI_API_KEY&quot;),<br>    azure_endpoint=os.getenv(&quot;AZURE_OPENAI_ENDPOINT&quot;),<br>    openai_api_version=os.getenv(&quot;AZURE_OPENAI_API_VERSION&quot;)<br>)&quot;&quot;&quot;<br>embeddings = AzureOpenAIEmbeddings(<br>    azure_deployment=&quot;text-embedding-3-large&quot;<br>)<br># --------------------------------------------------<br># Create Chroma Vector Store<br># --------------------------------------------------<br>print(&quot;Creating vector store...&quot;)<br>print(&quot;--&quot; * 50)<br><br>vectorstore = Chroma.from_documents(<br>    documents=chunks,<br>    embedding=embeddings,<br>    collection_name=&quot;PDF_collection&quot;,<br>    persist_directory=&quot;./chroma_langchain_db&quot;<br>)<br><br>print(&quot;Data stored in Vector Store Successfully!&quot;)<br>print(&quot;--&quot; * 50)<br><br># --------------------------------------------------<br># Convert Vector Store to Retriever<br># --------------------------------------------------<br>print(&quot;Convert vector store as retriever&quot;)<br><br>retriever = vectorstore.as_retriever(<br>    search_kwargs={&quot;k&quot;: 3}<br>)<br><br># --------------------------------------------------<br># Prompt Template<br># --------------------------------------------------<br>system_prompt = &quot;&quot;&quot;<br>You are an assistant for question-answering tasks.<br><br>Use the following pieces of retrieved context to answer<br>the question.<br><br>If you don&#39;t know the answer, just say that you don&#39;t know.<br><br>Use three sentences maximum and keep the answer concise.<br><br>Context:<br>{context}<br>&quot;&quot;&quot;<br><br>prompt = ChatPromptTemplate.from_messages(<br>    [<br>        (&quot;system&quot;, system_prompt),<br>        (&quot;human&quot;, &quot;{input}&quot;)<br>    ]<br>)<br><br># --------------------------------------------------<br># Azure Chat OpenAI<br># --------------------------------------------------<br>&quot;&quot;&quot;llm = AzureChatOpenAI(<br>    azure_deployment=os.getenv(&quot;AZURE_OPENAI_CHAT_DEPLOYMENT&quot;),<br>    api_key=os.getenv(&quot;AZURE_OPENAI_API_KEY&quot;),<br>    azure_endpoint=os.getenv(&quot;AZURE_OPENAI_ENDPOINT&quot;),<br>    api_version=os.getenv(&quot;AZURE_OPENAI_API_VERSION&quot;),<br>    temperature=0<br>)&quot;&quot;&quot;<br>llm = AzureChatOpenAI(<br>    azure_deployment=&quot;gpt-4o&quot;,<br>    api_version=&quot;2024-02-15-preview&quot;,<br>    azure_endpoint=&quot;https://foundaryusbu.cognitiveservices.azure.com/&quot;,<br>    api_key=os.getenv(&quot;AZURE_OPENAI_API_KEY&quot;),<br>    temperature=0<br>)<br># --------------------------------------------------<br># Create Document Chain<br># --------------------------------------------------<br>document_chain = create_stuff_documents_chain(<br>    llm,<br>    prompt<br>)<br><br># --------------------------------------------------<br># Create Retrieval Chain<br># --------------------------------------------------<br>rag_chain = create_retrieval_chain(<br>    retriever,<br>    document_chain<br>)<br><br># --------------------------------------------------<br># Ask Question<br># --------------------------------------------------<br>response = rag_chain.invoke(<br>    {&quot;input&quot;: &quot;tell me about transformer model architecture&quot;}<br>)<br><br>print(&quot;\nAnswer:&quot;)<br>print(response[&quot;answer&quot;])</pre><h3><strong>RAG ChatBOT</strong></h3><pre>import os<br>import tempfile<br>from dotenv import load_dotenv<br>load_dotenv()<br>from langchain_core.prompts import ChatPromptTemplate<br>from langchain_openai import AzureChatOpenAI,AzureOpenAIEmbeddings<br>import streamlit as st<br>from langchain_community.document_loaders import PyMuPDFLoader<br>from langchain_text_splitters import RecursiveCharacterTextSplitter<br>from langchain_chroma import Chroma<br>from langchain.chains import create_retrieval_chain<br>from langchain.chains.combine_documents import create_stuff_documents_chain<br><br>print(&quot;Stremlit webpage design&quot;)<br>print(&quot;-&quot;*50)<br>st.title(&quot;Welcome to RAG ChatBOT&quot;)<br><br>with st.sidebar:<br>    st.markdown(&quot;upload your PDF file here&quot;)<br>    uploaded_file = st.file_uploader(&quot;Choose a file&quot;, type=&quot;pdf&quot;)<br><br>print(&quot;embedding part started from here...&quot;)<br>print(&quot;-&quot;*50)<br><br>print(&quot;if PDF file uploaded then only will execute below code&quot;)<br>print(&quot;-&quot;*50)<br>if uploaded_file:<br>    with tempfile.NamedTemporaryFile(delete=False, suffix=&quot;.pdf&quot;) as tmp_file:<br>        tmp_file.write(uploaded_file.read())<br>        pdf_path = tmp_file.name<br>    loader = PyMuPDFLoader(pdf_path)<br>    documents = loader.load()<br>    with st.sidebar:<br>        st.success(f&quot;PDF Loaded: {len(documents)} pages&quot;)<br>    print(&quot;convert PDF into chunks&quot;)<br>    Recursive_splitter=RecursiveCharacterTextSplitter(separators=[&quot;\n\n&quot;, &quot;\n&quot;, &quot; &quot;, &quot;&quot;], <br>        chunk_size=400,<br>        chunk_overlap=50, <br>        length_function=len<br>    )<br>    chunks=Recursive_splitter.split_documents(documents)<br>    with st.sidebar:<br>        st.success(f&quot;PDF Chunks: {len(chunks)} chunks&quot;)<br>    print(&quot;-&quot;*50)<br>    <br>    print(&quot;embeddings&quot;)<br>    print(&quot;-&quot;*50)<br><br>    embeddings = AzureOpenAIEmbeddings(<br>        model=&quot;text-embedding-3-large&quot;<br>    )<br>    print(&quot;converting chunks into vector store and storing it into chroma db&quot;)<br>    print(&quot;-&quot;*50)<br>    vectorstore = Chroma.from_documents(<br>        documents=chunks,<br>        embedding=embeddings,<br>        collection_name=&quot;PDF_collection&quot;,<br>        persist_directory=&quot;./chroma_langchain_db&quot;<br>    )<br>    with st.sidebar:<br>        st.success(f&quot; Chroma vector store: {(vectorstore)} vectors&quot;)<br>    print(&quot;llm call&quot;)<br>    print(&quot;-&quot;*50)<br><br>    llm = AzureChatOpenAI(<br>        azure_deployment= os.getenv(&quot;AZURE_OPENAI_CHAT_DEPLOYMENT&quot;),<br>        api_version=os.getenv(&quot;AZURE_OPENAI_API_VERSION&quot;),<br>        azure_endpoint=os.getenv(&quot;AZURE_OPENAI_ENDPOINT&quot;),<br>        api_key=os.getenv(&quot;AZURE_OPENAI_API_KEY&quot;),<br>        temperature=0<br>    )<br>    system_prompt = &quot;&quot;&quot;<br>        You are a helpful AI assistant.<br>        Answer only from the provided context.<br>        If the answer is not available in the context,<br>        say &#39;I could not find the answer in the document.&#39;<br>        Keep the answer within 2 lines.<br>        Context:<br>        {context}<br>        &quot;&quot;&quot;<br>    print(&quot;prompt initilazation done&quot;)<br>    print(&quot;-&quot;*50)<br>    prompt = ChatPromptTemplate.from_messages(<br>            [<br>                (&quot;system&quot;, system_prompt),       <br>                (&quot;human&quot;, &quot;{input}&quot;),<br>            ]<br>        )<br><br>    #response=llm.invoke(&quot;hi&quot;)<br>    chain= prompt | llm<br><br>    print(&quot;llm call completed&quot;)<br>    print(&quot;-&quot;*50)<br>    #print(response.content)<br>    print(&quot;app input data &quot;)<br>    print(&quot;-&quot;*50)<br>    question=st.chat_input(&quot;Enter your Question here...&quot;)<br>    if question:<br>        print(&quot;create chain for question, prompt, vector store&quot;)<br>        retriver=vectorstore.as_retriever(<br>            search_kwargs={&#39;k&#39;: 2}<br>        )<br>        print(&quot;-&quot;*50)<br><br><br>        with st.chat_message(&quot;user&quot;):<br>            st.write(question)<br><br>        document_chain=create_stuff_documents_chain(<br>            llm, prompt<br>        )<br>        rag_chain=create_retrieval_chain(retriver,document_chain<br><br>        )<br><br>        response=rag_chain.invoke({<br>            &quot;input&quot;: question<br>        })<br><br>        with st.chat_message(&quot;assistant&quot;):<br>            st.write(response[&quot;answer&quot;])<br><br>    print(&quot;robot response  &quot;)</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f5a6fcbaf12d" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>