Artificial Intelligence (AI) refers to the simulation of human intelligence in machines that are programmed to think, learn, and make decisions. AI enables computers and systems to perform tasks that typically require human intelligence, such as problem-solving, understanding language, recognizing images, and making predictions. AI is largely categorized into 2 types: Generative AI and Agentic AI. Generative AI is a type of artificial intelligence that creates new content such as text, images, music, or code based on patterns learned from data. Agentic AI refers to AI systems that can act independently to achieve goals by making decisions, planning steps, and interacting with their environment.
In this guide, we will walk through how to build an Agentic AI system using Python, Ollama (a modern AI model for decision-making and natural language tasks), and PostgreSQL (a relational database) to create a simple book recommendation agent. A local Ollama-based Agentic AI implementation offers better data privacy and security since all processing happens on-premise without sending sensitive information to the cloud. It also reduces latency and ongoing costs by eliminating internet dependency and cloud usage fees.
What is Agentic AI?
Agentic AI represents a unique class of intelligent systems that involves real-world interaction, dynamic decision-making, and autonomous task execution. Rather than simply generating text, agentic systems act, reason, plan, and use tools to achieve specific goals. An AI Agent serves as the specialized "worker" or functional unit within a broader Agentic AI system.
Agentic AI is an AI that satisfies the below conditions.
- It can Perceive its environment by gathering and interpreting information from data sources, user inputs, sensors, or digital systems. It analyzes this information to understand context, detect changes, and identify relevant signals. This awareness enables it to make informed decisions and take goal-directed actions autonomously.
- It should be able to make decisions based on predefined goals or objectives.
- It can take actions autonomously to achieve those goals.
Think of it as: LLM + Reasoning + Tools + Autonomy = Agentic AI
For example, an autonomous robot navigating a maze, or a financial bot making trades based on market conditions, are both instances of Agentic AI.
The primary challenge with Agentic AI is the need for it to continuously evaluate its past actions, make accurate real-time predictions or decisions, and maintain a coherent memory of its experiences. Balancing adaptability, speed, and reliable long-term memory management is complex, yet essential for consistent and autonomous performance.
High-Level Architecture
-
Install Ollama: curl -fsSL https://ollama.com/install.sh | bash
- Get llama3: ollama pull llama3
- Start Ollama: ollama serve
It runs on: http://localhost:11434
- Run llama3: ollama run llama3 "some test query"
If a response is received to the test query, then llama3 is running.
Set Up Database (PostgreSQL)
- Download and install PostgreSQL from: PostgreSQL: Downloads
- Create a database table to store book related metadata. Here is an example schema query:
CREATE TABLE books (
id SERIAL PRIMARY KEY,
title TEXT,
author TEXT,
description TEXT,
source TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
- Connect this database table via python for DB Transactions.
import psycopg2
def get_connection():
return psycopg2.connect(
dbname="booksdb",
user="postgres",
password="password",
host="localhost",
port=5432
)
Connect Python to Llama 3 via Ollama
Once we have ollama "serving", we can connect and communicate with it over http. Below is an example code for it.
import requests
import json
OLLAMA_URL = "http://localhost:11434/api/generate"
def call_llama3(prompt):
payload = {
"model": "llama3",
"prompt": prompt,
"stream": False
}
response = requests.post(OLLAMA_URL, json=payload)
response.raise_for_status()
return response.json()["response"]
Define the Agent Prompt
The agent prompt is a set of instructions that is given to the agent to make it behave accordingly. Below is an example (as a python string).
AGENT_PROMPT = """
You are a book discovery agent.
Decide whether you need to:
1. Search the internet for books
2. Use existing database knowledge
3. Summarize results for the user
Respond in JSON:
{
"action": "search | respond",
"query": "...",
"final_answer": "..."
}
"""
Fetching Book Data from the Internet
Here is an example code that we can write as a "tool" for the agent to fetch book related data.
import requests
def search_books(query):
url = f"https://openlibrary.org/search.json?q={query}"
response = requests.get(url).json()
books = []
for doc in response.get("docs", [])[:5]:
books.append({
"title": doc.get("title"),
"author": doc.get("author_name", ["Unknown"])[0],
"description": doc.get("first_sentence", "")
})
return books
Storing Book related metadata in Database
When we fetch the book related metadata from OpenLibrary, we can use the below code to store them in our PostGres DB.
def store_books(books):
conn = get_connection()
cur = conn.cursor()
for book in books:
cur.execute(
"""
INSERT INTO books (title, author, description, source)
VALUES (%s, %s, %s, %s)
""",
(book["title"], book["author"], book["description"], "OpenLibrary")
)
conn.commit()
cur.close()
conn.close()
Build the Agent
Now, we can connect all the steps mentioned above and build and the agent.
import json
def run_agent(user_query):
prompt = f"{AGENT_PROMPT}\nUser query: {user_query}"
response = call_llama(prompt)
decision = json.loads(response)
if decision["action"] == "search":
books = search_books(decision["query"])
store_books(books)
return f"I found these books:\n" + \
"\n".join([b["title"] for b in books])
return decision["final_answer"]
Query the Agent
Now to interact with the agent we can write a simple command line tool in python.
while True:
query = input("Ask about books: ")
if query.lower() == "exit":
break
print(run_agent(query))
Tool Calling
We can also use the Tool Calling feature to call external functions from the LLM. Note that the LLM needs to support this feature. The llama3.1 is a good LLM for that.
For tool calling we need to first define the external function. And then pass the function as a “tool” to the LLM via ollama library calls. Suppose we want to call a function to add two numbers from LLM. Example code for that is given below.
from ollama import Client, chat, ChatResponse
def add_num(x:int, y:int):
return x+y
messages.append({
"role": "user",
"content": "add 9 and 5"
})
response: ChatResponse = chat(
model="llama3.1:70b",
messages=messages,
tools=[add_num]
)
if response and response.message:
message = response.message
if message.tool_calls:
print(eval(tool.function.name)(**tool.function.arguments))
Conclusion
With Ollama, Llama 3, Python, and PostgreSQL, you can build a powerful agentic AI system that autonomously fetches, stores, and serves book knowledge. This architecture can be used to build agents for other purposes as well, like, research assistants, recommendation engines, and knowledge bots. As LLMs continue to improve, the real differentiator will not be the model alone, but how intelligently it is embedded into agentic workflows.