AI Campus • Prayogika kalike
Build a local AI chatbot with conversation memory
Connect a Python terminal chat to an installed Ollama model, preserve recent turns in memory, and handle local-server errors clearly.
Intermediate · 60–120 minutes after model setup
Before you start
- Python 3.10 or newer.
- Ollama installed with a downloaded local model that fits your RAM. Run ollama list to find its exact name.
- Disable cloud features in Ollama for an exclusively local workflow. Do not use a cloud model tag.
Cost: No paid API is required for a downloaded local model. Your hardware, storage, electricity, and initial model download still have costs.
Step-by-step walkthrough
1. Prepare a local model
Follow Ollama’s official installation and local-model instructions. Choose a local model suitable for your hardware and license. Initial model downloads need internet; this guide does not download one automatically.
2. Save the client
Save the complete program below as local_chat.py. The URL is fixed to 127.0.0.1:11434. The script checks the model metadata for a remote model before beginning chat.
3. Start chatting
Run python local_chat.py --model YOUR_LOCAL_MODEL, replacing the placeholder with the exact model name from ollama list. Ask “My study topic is vectors”, then “What is my study topic?” to test recent-turn memory.
4. Try error handling
Use an unknown model name or stop the local Ollama server. Expect an explanatory error and no automatic downloads or fallback to a cloud API. Type /quit to exit.
5. Explain the limits
Document your model, hardware, approximate response time, and failure cases. Conversation memory is in process and disappears on exit; it is not an unlimited knowledge store or a fact-checker.
Complete code: local_chat.py
Download Python fileimport argparse
import json
from urllib.request import Request, urlopen
from urllib.error import HTTPError, URLError
BASE = "http://127.0.0.1:11434/api"
def request(endpoint, payload):
req = Request(BASE + endpoint, data=json.dumps(payload).encode("utf-8"),
headers={"Content-Type": "application/json"}, method="POST")
with urlopen(req, timeout=180) as response:
return json.load(response)
def main():
parser = argparse.ArgumentParser(description="Chat with an installed local Ollama model")
parser.add_argument("--model", required=True, help="Exact installed local model name")
args = parser.parse_args()
if "cloud" in args.model.lower():
parser.exit(2, "Choose a downloaded local model, not a cloud model.\n")
messages = [{"role": "system", "content": "You are a study helper. Say when you are unsure."}]
try:
info = request("/show", {"model": args.model})
if info.get("remote_host") or info.get("remote_model"):
parser.exit(2, "Remote models are not supported in this local tutorial.\n")
print("Type /quit to leave. Recent conversation is kept only in memory.")
while True:
text = input("You: ").strip()
if text == "/quit":
break
if not text:
continue
messages = [messages[0]] + messages[1:][-12:]
messages.append({"role": "user", "content": text})
result = request("/chat", {"model": args.model, "messages": messages, "stream": False})
reply = result.get("message", {}).get("content")
if not isinstance(reply, str):
raise ValueError("The local server returned no text reply.")
print("Assistant:", reply)
messages.append({"role": "assistant", "content": reply})
except (HTTPError, URLError, TimeoutError, OSError, ValueError):
parser.exit(1, "Local model request failed. Check Ollama, the installed model name, and available memory.\n")
except (EOFError, KeyboardInterrupt):
print("\nChat closed.")
if __name__ == "__main__":
main()
Run and verify
ollama list
python local_chat.py --model YOUR_LOCAL_MODELA terminal conversation using your installed local model, with recent turns retained until exit.
Understand the limits
Requires a real local model and sufficient memory. The client can be checked offline, but response quality and hardware performance must be tested with your chosen model.