<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Jay Emerson Navares</title>
    <description>The latest articles on Forem by Jay Emerson Navares (@jpnavares).</description>
    <link>https://forem.com/jpnavares</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3083372%2Faed9e4fc-9fae-43a9-a861-cb224b1d60ae.png</url>
      <title>Forem: Jay Emerson Navares</title>
      <link>https://forem.com/jpnavares</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/jpnavares"/>
    <language>en</language>
    <item>
      <title>Run Your Own AI: Python Chatbots with Ollama and Langchain</title>
      <dc:creator>Jay Emerson Navares</dc:creator>
      <pubDate>Thu, 24 Apr 2025 15:14:34 +0000</pubDate>
      <link>https://forem.com/up_min_sparcs/how-to-make-a-chatbot-in-python-using-a-local-llm-7h8</link>
      <guid>https://forem.com/up_min_sparcs/how-to-make-a-chatbot-in-python-using-a-local-llm-7h8</guid>
      <description>&lt;p&gt;Co-authored with &lt;a class="mentioned-user" href="https://dev.to/nazakun021"&gt;@nazakun021&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Are you tired of not being able to use ChatGPT because of internet issues, API limits, or concerns about your privacy? Are you looking for a way to run a smart assistant without relying on the cloud and for free? If so, then you’re on the right track by clicking on this article! &lt;/p&gt;

&lt;p&gt;In this guide, we’ll show you how to build a chatbot from scratch using Python and a locally running LLM — no cloud dependencies, and you wouldn’t even need the internet to run this chatbot!&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Why Use A Local LLM?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Local LLMs&lt;/strong&gt; (Large Language Models) offer an alternative to cloud-based AI services. With a local LLM, all data stays on your machine — nothing is sent to external servers. This is crucial if you want to work with sensitive data such as healthcare, finance, etc. Local models also give you complete control. You can tweak your prompts and the system’s behavior. You can even regulate the model itself on domain-specific data to improve performance!&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Why Use Python?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Python&lt;/strong&gt; is the ideal choice for building a chatbot with a local LLM because it's easy to learn, has a clean and readable syntax, and boasts a massive ecosystem of AI and machine learning libraries. Tools like &lt;strong&gt;llama-cpp-python&lt;/strong&gt;, and &lt;strong&gt;langchain&lt;/strong&gt; are all Python-friendly, making integration with LLMs seamless.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Why use Langchain?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Langchain&lt;/strong&gt; is used because it simplifies building powerful applications with large language models by providing a framework that supports chaining together multiple steps, integrating with external data sources (like files, APIs, or databases), managing memory for conversations, and creating tool-using agents—all while keeping the code modular and scalable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prerequisites&lt;/strong&gt;&lt;br&gt;
Before we get started, make sure you have:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Python 3.12&lt;/li&gt;
&lt;li&gt;A system with at least 8GB RAM (the more RAM the better)&lt;/li&gt;
&lt;li&gt;Basic familiarity with Python&lt;/li&gt;
&lt;li&gt;[Optional] A GPU if you want faster speed.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;Setting up A Local LLM&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Downloading Ollama&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First of all, install &lt;strong&gt;Ollama&lt;/strong&gt; from &lt;a href="https://ollama.com/" rel="noopener noreferrer"&gt;https://ollama.com/&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flkytn0ew9708znwjww3g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flkytn0ew9708znwjww3g.png" alt="Code" width="800" height="431"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Then select download. Choose depending on your operating system. We’ll select Windows for this installation.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvluc9ot6blzhda355p3t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvluc9ot6blzhda355p3t.png" alt="Code" width="800" height="427"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;After downloading, open the .exe file.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fda09wx9mk18jja8m4wzi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fda09wx9mk18jja8m4wzi.png" alt="Code" width="800" height="122"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;After setting up everything, go to your Command Prompt and type "&lt;strong&gt;ollama&lt;/strong&gt;".&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnx7qec0iogs8euif5u4w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnx7qec0iogs8euif5u4w.png" alt="Code" width="800" height="406"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;We still haven’t downloaded any local LLMs. To browse one, go to &lt;a href="https://ollama.com/search" rel="noopener noreferrer"&gt;https://ollama.com/search&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwi9v1wfx36jegaqfy6dc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwi9v1wfx36jegaqfy6dc.png" alt="Code" width="800" height="434"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You can find almost all models here. Select a model depending on your system's RAM and CPU. For uniformity’s sake, we’ll use the “&lt;strong&gt;Llama 3.2 3B&lt;/strong&gt;” version in this tutorial.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs3mtlfqn3td5nc1frvj3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs3mtlfqn3td5nc1frvj3.png" alt="Code" width="800" height="173"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Select it, then copy the command prompt line to download it to your device.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ollama run llama3.2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgevke4s0tfjhxai58rb1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgevke4s0tfjhxai58rb1.png" alt="Code" width="800" height="368"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Then paste it into your command prompt.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkolo7j0g2v83u5wxjc3x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkolo7j0g2v83u5wxjc3x.png" alt="Code" width="800" height="208"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;After downloading, try to do a sample test on your model to see if everything works as expected          &lt;em&gt;(Optional, exit your Command Prompt, then reenter)&lt;/em&gt;
.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;Ollama                     // Opening Ollama 
&amp;gt;Ollama list                    // Listing all local LLMs in your device
&amp;gt;Ollama run &amp;lt;your model name here&amp;gt;  // Running your model, in this case “Llama3.2”

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Let’s try prompting it, “&lt;strong&gt;Introduce yourself.&lt;/strong&gt;”&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffv0f5jah6z8p3ilqekuy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffv0f5jah6z8p3ilqekuy.png" alt="Code" width="800" height="286"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The ollama installation worked well! Now, let’s work on our simple Chatbot.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2. Coding The Chatbot&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Let’s open our Code Editor. In this tutorial, we’ll be using &lt;strong&gt;VS Code&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The next step will be creating a virtual environment that will install the dependencies that we need for this project. Firstly, let’s make a folder that will contain our new chatbot. Let’s name the folder “&lt;strong&gt;local-llm-chatbot&lt;/strong&gt;”.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpru52s9vth4bfwrsh285.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpru52s9vth4bfwrsh285.png" alt="Code" width="800" height="477"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Now we are inside the folder. Let’s start preparing our environment.&lt;/li&gt;
&lt;li&gt;Select  “&lt;strong&gt;Ctrl + Backtick&lt;/strong&gt;” to open the terminal window. Then type in “&lt;strong&gt;python3 -m venv chatbot&lt;/strong&gt;” for Windows. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7s9s5y8fcamdhloy9rr9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7s9s5y8fcamdhloy9rr9.png" alt="Code" width="800" height="472"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, type in:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.\chatbot\Scripts\activate.bat // if you're running on the command prompt.
.\chatbot\Scripts\Activate.ps1 // if you're on PowerShell.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Reminder: Take note of the spelling and the capitalization.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7hl9tz5lzelnbesyirq6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7hl9tz5lzelnbesyirq6.png" alt="Code" width="800" height="207"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If we see that prefix, we’re good to go! Now, let’s download all the packages we need.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Type in:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install langchain langchain-ollama ollama

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvq37hjhjr76ox7mwogqv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvq37hjhjr76ox7mwogqv.png" alt="Code" width="800" height="194"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now it should start downloading…&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz5i2ymfppl60f8kbv8br.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz5i2ymfppl60f8kbv8br.png" alt="Code" width="800" height="159"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After downloading. We’ll create a Python file. Let’s call it “&lt;strong&gt;main.py&lt;/strong&gt;” right now.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Focrzff11k4r0w1mkaqkk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Focrzff11k4r0w1mkaqkk.png" alt="Code" width="535" height="317"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In your main.py, type in:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from langchain_ollama import OllamaLLM
model = OllamaLLM(model=”llama3.2”)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcskokmsk0gh13rc23rmt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcskokmsk0gh13rc23rmt.png" alt="Code" width="800" height="258"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;After all that set-up, now let’s try generating a sample response from the LLM.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;To do that, type in:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;result = model.invoke(input=’Enter Question Here’)
print (result)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fln7mhqdtlarjy2rgy2a6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fln7mhqdtlarjy2rgy2a6.png" alt="Code" width="800" height="219"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then run.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn3nayytubhksa96d94os.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn3nayytubhksa96d94os.png" alt="Code" width="800" height="320"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Now, the setup works, but it’s not &lt;strong&gt;user-friendly&lt;/strong&gt;, and there's still some room for improvement.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;To do that, let’s &lt;strong&gt;import&lt;/strong&gt; a prompt template.&lt;br&gt;
Type in:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; from langchain_core.prompts import ChatPromptTemplate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9y66turmwp92ecfo48kh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9y66turmwp92ecfo48kh.png" alt="Code" width="800" height="165"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then we want to make the actual chat template to instruct the model on how to respond. To do that, make a &lt;strong&gt;multiline string&lt;/strong&gt;, then pass the conversation &lt;strong&gt;{context} and the {question}&lt;/strong&gt;. And how it should answer.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyt8j9zpnf0lf2ialehd2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyt8j9zpnf0lf2ialehd2.png" alt="Code" width="800" height="356"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is how it look likes after giving the model a template!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmilza0xpik3sx49lya7v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmilza0xpik3sx49lya7v.png" alt="Code" width="800" height="133"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But now we want to continue talking to the chatbot. Here is a &lt;strong&gt;function to handle conversation&lt;/strong&gt;!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def handle_conversation():
    context = ""
    print("Welcome to the AI Chatbot Powered by Llama3.2, Type 'exit' to quit.")
    while True:
        user_input = input("You: ")
        if user_input.lower() == "exit":
            break
        result = chain.invoke({"context": context, "question": user_input})
        print("Bot: ", result)
        context += f"\nUser: {user_input}\nAI: {result}"


if __name__ == "__main__":
    handle_conversation()

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And with that, you've successfully created a fully functional chatbot! If you want the interface to look &lt;strong&gt;nicer&lt;/strong&gt;, you can copy-paste this block of code!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from langchain_ollama import OllamaLLM
from langchain_core.prompts import ChatPromptTemplate


template = """
Answer the question below based on our conversation history


Conversation History:
{context}


Question:
{question}


Answer:
"""


# Initialize the model (ensure model is running)
try:
    model = OllamaLLM(model="llama3.2")
except Exception as e:
    print(f"Error Initializing the LLM.")
    print(f"Details: {e}")
    exit() # Exit if model can't be loaded


prompt = ChatPromptTemplate.from_template(template)
chain = prompt | model


def handle_conversation():
    context = "The conversation has just begun." # Start with a neutral context
    print("\n--- AI Chatbot (Powered by Llama3.2) ---")
    print("Ask me anything! Type 'exit' when you're done.")
    print("----------------------------------------")


    while True:
        user_input = input("You: ")
        if user_input.lower() == "exit":
            print("\n----------------------------------------")
            print("Llama3.2: Goodbye! Thanks for chatting.")
            print("----------------------------------------")
 break


        print("Llama3.2: Thinking...")


        try:
            result = chain.invoke({"context": context, "question": user_input})
            print(f"Llama3.2: {result.strip()}")


            context += f"\nUser: {user_input}\nAI: {result.strip()}"

        except Exception as e:
            print(f"Llama3.2: Sorry, I encountered an error trying to respond. ({e})")

        print("----------------------------------------")


if __name__ == "__main__":
    handle_conversation()

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;What can you  do next?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;After building a local chatbot with &lt;strong&gt;Python&lt;/strong&gt; and &lt;strong&gt;LLaMA 3&lt;/strong&gt;, the next step is to create &lt;strong&gt;agentic workflows&lt;/strong&gt;—which let your chatbot do more than just respond. With tools like LangChain, you can build** AI agents** that make decisions, use functions, access files or &lt;strong&gt;APIs&lt;/strong&gt;, and remember past interactions. This turns your chatbot into a smart assistant that can handle multi-step tasks like searching documents, doing calculations, or summarizing info—all powered locally without needing the internet.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Conclusion&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Congratulations!&lt;/strong&gt; You’ve successfully created your very own Python chatbot powered entirely by a large language model running locally with &lt;strong&gt;Ollama&lt;/strong&gt; and &lt;strong&gt;LLaMA 3&lt;/strong&gt;. The process we followed—setting up &lt;strong&gt;Ollama&lt;/strong&gt;, pulling the &lt;strong&gt;LLaMA 3 model&lt;/strong&gt;, and coding the &lt;strong&gt;Python&lt;/strong&gt; interaction—serves as a practical and empowering starting point. &lt;/p&gt;

&lt;p&gt;By building this &lt;strong&gt;chatbot&lt;/strong&gt;, you’ve gained hands-on experience in integrating cutting-edge AI models into real-world software. If you want, you can enhance your chatbot by adding features like &lt;strong&gt;memory, voice recognition&lt;/strong&gt;, a &lt;strong&gt;graphical user interface&lt;/strong&gt;, or even connecting it to &lt;strong&gt;APIs&lt;/strong&gt; and &lt;strong&gt;databases&lt;/strong&gt;. The skills and tools you’ve learned here are transferable to a wide range of AI and &lt;strong&gt;machine learning projects&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“&lt;em&gt;The best way to predict the future is to invent it&lt;/em&gt;.” — &lt;strong&gt;Alan Kay&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;With this mindset, your chatbot isn’t just a project—it’s your &lt;strong&gt;chicken jockey&lt;/strong&gt; into the future of &lt;strong&gt;AI innovation&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;If you want to see the actual code, feel free to check out our &lt;a href="https://github.com/Nazakun021/local-llm-chatbot" rel="noopener noreferrer"&gt;GitHub Repository.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;References:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://www.youtube.com/watch?v=d0o89z134CQ&amp;amp;t=336s&amp;amp;ab_channel=TechWithTim" rel="noopener noreferrer"&gt;Creating Local LLM's in Python!&lt;/a&gt;&lt;br&gt;
&lt;a href="https://github.com/ollama/ollama" rel="noopener noreferrer"&gt;Ollama's Model Library&lt;/a&gt;&lt;br&gt;
&lt;a href="https://python.langchain.com/docs/integrations/llms/ollama/" rel="noopener noreferrer"&gt;How Langchain interact with Olama Models&lt;/a&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>ai</category>
      <category>tutorial</category>
      <category>python</category>
    </item>
  </channel>
</rss>
