Saturday, April 25, 2026

Dynamic Prompt Engineering for LLM(Llama3.2) In my Local Machine

Dynamic prompting tailors the input prompt to adaptively include only the most relevant information from prior interactions. This approach ensures that essential context is retained while keeping the token count within limits.

Dynamic Prompt Engineering
  • Definition: The process of creating or modifying the prompt itself at runtime (dynamically).
  • It focuses on how you structure and word the instructions, templates, roles, examples, reasoning steps, output format, etc.
  • The prompt (especially the system message or user message) changes based on the situation — e.g., different roles, tones, few-shot examples, or instructions depending on the task.

Example in the Python Code
system_message = f"""You are a {role}.

CONTEXT:
{context}

USER PREFERENCES:
{prefs}

TONE:
{tone}
"""
This is dynamic prompt engineering — you're building a customized prompt by filling in variables (role, context, prefs, tone).


References

Shekhar Agrawal; Srinivasa Sunil Chippada; Rathish Mohan. Ultimate Agentic AI with AutoGen for Enterprise Automation: Design, Build, And Deploy Enterprise-Grade AI Agents Using LLMs and AutoGen To Power Intelligent, ... Enterprise Automation (English Edition) (p. 177). Orange Education Pvt Ltd, AVA™. Kindle Edition. 



Python code for this 

# === Fix for Jupyter Notebook ===

import nest_asyncio

nest_asyncio.apply()          # ← This line solves the "event loop is already running" error


import asyncio

from autogen_ext.models.ollama import OllamaChatCompletionClient

from autogen_agentchat.agents import AssistantAgent

from autogen_agentchat.ui import Console

def create_dynamic_llama_agent(

    name: str,

    role: str,

    context: str,

    prefs: str = "",

    tone: str = "helpful"

):

    system_message = f"""You are a {role}.

CONTEXT:

{context}

USER PREFERENCES:

{prefs}

TONE:

{tone}

Think step by step. Be clear, accurate, and follow the user's preferences.

"""

    model_client = OllamaChatCompletionClient(

        model="llama3.2:latest",      # Use "llama3.2:3b" if you downloaded the small version

        temperature=0.7,

        num_ctx=8192,

    )

    agent = AssistantAgent(

        name=name,

        model_client=model_client,

        system_message=system_message,

    )

    print(f"✅ Created dynamic agent '{name}' with role: {role}")

    print(f"   Context length: {len(context)} characters\n")

    return agent


# ====================== Main Function ======================

async def main():

    analyst = create_dynamic_llama_agent(

        name="DataAnalyst",

        role="data analyst",

        context="The latest quarterly sales data shows a 22% drop in Europe, while Asia grew by 15%. Main product 'WidgetX' underperformed.",

        prefs="Always include chart suggestions and 2-3 actionable recommendations",

        tone="professional and data-driven"

    )

    task = "Analyze the sales situation and suggest recovery strategies."

    print("🚀 Running task with dynamic prompt...\n")

    # Streaming output with nice formatting

    stream = analyst.run_stream(task=task)

    await Console(stream)


# ====================== Run in Jupyter ======================

# Just run this cell

await main()







Thursday, April 23, 2026

Implement Calculator Tool using Autogen and Llama3.2(LLM in local machine)

LLMs are pre-trained on large datasets but do not have access to information beyond their training cut-off date.

They cannot retrieve real-time information

They lack enterprise-specific knowledge unless explicitly trained on it.


Example:

AI Assistant for Financial Services Problem: 

A banking chatbot is designed to assist customers with loan eligibility, interest rates, and payment schedules, but: It cannot perform real-time financial calculations. It lacks access to the latest interest rates. Users demand personalized financial advice. 

Solution with Tools: 

Calculator Tool: Computes EMI payments, interest accumulation, and loan eligibility. 

Finance API Tool: Fetches live interest rates and account balances. 

Memory Tool: Stores previous queries to offer personalized financial planning.



Autogen Output Implementing Calculator Tool

References

Shekhar Agrawal; Srinivasa Sunil Chippada; Rathish Mohan. Ultimate Agentic AI with AutoGen for Enterprise Automation: Design, Build, And Deploy Enterprise-Grade AI Agents Using LLMs and AutoGen To Power Intelligent, ... Enterprise Automation (English Edition). Orange Education Pvt Ltd, AVA™. Kindle Edition. 


Python Code

import autogen

import math


# =========================

# 1. Define LLM config

# =========================

config_list = [

    {

        "model": "llama3.2",

        "base_url": "http://localhost:11434/v1",  # Ollama endpoint

        "api_key": "ollama"  # dummy value

    }

]


llm_config = {

    "config_list": config_list,

    "temperature": 0,

}


# =========================

# 2. Create calculator tool

# =========================

def calculator(expression: str) -> str:

    """

    Evaluate a math expression safely.

    """

    try:

        # Allowed names (safe eval)

        allowed_names = {

            "sqrt": math.sqrt,

            "pow": pow,

            "sin": math.sin,

            "cos": math.cos,

            "tan": math.tan,

            "pi": math.pi,

            "e": math.e,

        }


        result = eval(expression, {"__builtins__": {}}, allowed_names)

        return str(result)

    except Exception as e:

        return f"Error: {str(e)}"


# =========================

# 3. Create Assistant Agent

# =========================

assistant = autogen.AssistantAgent(

    name="assistant",

    llm_config=llm_config,

)


# =========================

# 4. Create User Proxy Agent

# =========================

user_proxy = autogen.UserProxyAgent(

    name="user_proxy",

    human_input_mode="NEVER",

    code_execution_config=False,

)


# =========================

# 5. Register tool

# =========================

assistant.register_for_llm(

    name="calculator",

    description="Evaluate mathematical expressions"

)(calculator)


user_proxy.register_for_execution(

    name="calculator"

)(calculator)


# =========================

# 6. Start conversation

# =========================

user_proxy.initiate_chat(

    assistant,

    message="What is (5 + 3) * sqrt(16)?"

)

Tuesday, April 21, 2026

AI Agent Get Tomorrow Weather Info from Website(open-meteo) and Summarize It

The agent is created using Microsoft Autogen and Ollama. The agent pull the weather data through the website(open-meteo) API and return the data in JSON format. The AI agent then summarize the data and produce a short summary on tomorrow's weather. 







References

Shekhar Agrawal; Srinivasa Sunil Chippada; Rathish Mohan. Ultimate Agentic AI with AutoGen for Enterprise Automation: Design, Build, And Deploy Enterprise-Grade AI Agents Using LLMs and AutoGen To Power Intelligent, ... Enterprise Automation (English Edition). Orange Education Pvt Ltd, AVA™. Kindle Edition. 




Python Code generated by Grok as below:

import autogen

import requests

from datetime import datetime, timedelta

import json

# ====================== LLM CONFIG (your Ollama setup) ======================

config_list = [

    {

        "model": "llama3.2",          # Change to your actual pulled model (e.g. qwen2.5, phi4, etc.)

        "base_url": "http://localhost:11434/v1",

        "api_key": "ollama",

    }

]

llm_config = {

    "config_list": config_list,

    "seed": 42,

    "temperature": 0.7,

}

# ====================== Create Agents ======================

assistant = autogen.AssistantAgent(

    name="Weather_Assistant",

    llm_config=llm_config,

    system_message="""You are a helpful weather analyst.

    When given weather data (in JSON), extract and clearly summarize tomorrow's weather.

    Include: date, max/min temperature, weather condition, precipitation chance, and wind if available.

    Keep the response natural and easy to read."""

)

user_proxy = autogen.UserProxyAgent(

    name="User",

    human_input_mode="NEVER",                    # Fully automated

    max_consecutive_auto_reply=2,

    code_execution_config={

        "work_dir": "coding",

        "use_docker": False

    }

)

# ====================== Function to get tomorrow's weather ======================

def get_tomorrow_weather(city: str = "Kuala Lumpur"):

    """

    Fetch tomorrow's weather using Open-Meteo API (free, no key needed).

    Returns nicely formatted data or error message.

    """

    try:

        # Step 1: Get coordinates for the city

        geo_url = f"https://geocoding-api.open-meteo.com/v1/search?name={city}&count=1&language=en&format=json"

        geo_response = requests.get(geo_url, timeout=10)

        geo_data = geo_response.json()


        if not geo_data.get("results"):

            return f"❌ Could not find location: {city}"


        lat = geo_data["results"][0]["latitude"]

        lon = geo_data["results"][0]["longitude"]


        # Step 2: Get weather forecast (daily for tomorrow)

        tomorrow = (datetime.now() + timedelta(days=1)).strftime("%Y-%m-%d")

        weather_url = (

            f"https://api.open-meteo.com/v1/forecast?"

            f"latitude={lat}&longitude={lon}"

            f"&daily=weather_code,temperature_2m_max,temperature_2m_min,"

            f"precipitation_probability_max,wind_speed_10m_max"

            f"&timezone=auto&forecast_days=2"

        )

        weather_response = requests.get(weather_url, timeout=10)

        weather_data = weather_response.json()

        # Extract tomorrow's data (index 1 = tomorrow)

        daily = weather_data["daily"]

        idx = 1  # tomorrow

        weather_code = daily["weather_code"][idx]

        temp_max = daily["temperature_2m_max"][idx]

        temp_min = daily["temperature_2m_min"][idx]

        precip_prob = daily["precipitation_probability_max"][idx]

        wind_max = daily["wind_speed_10m_max"][idx]


        # Simple weather code description

        code_desc = {

            0: "Clear sky", 1: "Mainly clear", 2: "Partly cloudy", 3: "Overcast",

            45: "Fog", 48: "Depositing rime fog",

            51: "Light drizzle", 53: "Moderate drizzle", 55: "Dense drizzle",

            61: "Slight rain", 63: "Moderate rain", 65: "Heavy rain",

            71: "Slight snow", 73: "Moderate snow", 75: "Heavy snow",

            80: "Slight rain showers", 81: "Moderate rain showers", 82: "Violent rain showers",

            95: "Thunderstorm", 96: "Thunderstorm with slight hail", 99: "Thunderstorm with heavy hail"

        }.get(weather_code, "Unknown")


        result = {

            "city": city,

            "date": daily["time"][idx],

            "condition": code_desc,

            "temperature_max": temp_max,

            "temperature_min": temp_min,

            "precipitation_probability": precip_prob,

            "wind_speed_max": wind_max,

            "units": {"temp": "°C", "wind": "km/h", "precip": "%"}

        }

        return json.dumps(result, indent=2)

    except Exception as e:

        return f"❌ Error fetching weather: {str(e)}"


# ====================== Start the conversation ======================

print("🌤️ Fetching tomorrow's weather using AutoGen + Ollama...\n")

# First, get raw weather data using the function

raw_data = get_tomorrow_weather("Kuala Lumpur")   # Change city here if you want

# Let the assistant summarize it nicely

user_proxy.initiate_chat(

    assistant,

    message=f"""Here is the raw weather data for tomorrow in JSON format:

{raw_data}

Please summarize tomorrow's weather in a friendly, natural way."""

)

# Optional: You can also run it for your current location by changing the city

# Example: get_tomorrow_weather("Singapore") or "London"

Sunday, April 19, 2026

Create simple AI Agent using Microsoft Autogen and Ollama(OpenSource) to plot a Stock Chart.

Picture 1

Just share share what I found out today. Both Microsoft Autogen and Ollama are open source, hence they are free to try out in own laptop. 

Firstly, need to install Autogen and Ollama(in laptop also) in python.

The code to create the agent is simple as below.

from autogen import AssistantAgent, UserProxyAgent

config_list = [

{

    "model":"kimi-k2.5:cloud",

    "base_url": "http://localhost:11434/v1",

            "api_key": "ollama",

}

]

assistant = AssistantAgent("assistant", llm_config={"config_list": config_list})

user_proxy = UserProxyAgent("user_proxy", code_execution_config={"work_dir":"coding","use_docker": False})

user_proxy.initiate_chat(assistant, message="Plot a chart of NVDA and TESLA stock price change YTD. Get information using yfinance. Save the graph as Stock1.png")


The output is as below when the code is run.


A graph as shown in Picture 1 is drawn and save as .png file.


Reference

https://www.youtube.com/watch?v=DMYCJe1vBVA

Friday, April 17, 2026

A Tour on Vector Database - PineCone

The previous article I share mentioned that unstructured data are usually store in vector database. I quickly google to search for opensource database, to have a look at how vector database look like. And I found PineCone.





In vector database, data are store in index, which is different from normal database where data is store in table.

From the example given, I manage to create 1 index, and load some sentences into the index.




As we can see, the search results list the sentences I load into the index. We can see there is score at every sentences. According to Chatgpt, the sentences(data) are converted into vectors (embeddings). 

  • When you search, your query is also turned into a vector
  • The system compares your query vector with stored vectors

👉 The score is the result of that comparison.

The score depends on the similarity metric used:

Example: 

1. Cosine Similarity (most common)

2. Dot Product

A vector database score is simply:

A numerical measure of how close your query is to stored data in vector space





Wednesday, April 15, 2026

Simple Generative AI (HuggingFace) From Book with Python

Text Generation (LLMs) 

The core idea here is to model the probability of the next token (word, subword, or character) given all the preceding tokens. This is essentially a sophisticated next-word predictor. For example, “The cat sat on the [mat].” Here, the model calculates the probability of every possible next word and selects one. By doing this millions of times, it generates coherent prose.

We’ll use the Hugging Face ecosystem, which is the industry standard for deploying and experimenting with GenAI models. Specifically, we’ll use the transformers library and a small but capable LLM to get our feet wet with text generation.

OUTPUT PRINTSCREEN


Python Code

from transformers import pipeline

generator = pipeline('text-generation', model="distilgpt2")

prompt = 'The most popular cloth brand will'

output = generator(

    prompt,

    max_length=100,

    num_return_sequences=1,

    do_sample=True,

    top_k=50,

    top_p=0.95,

    temperature=0.75,

)

print("Prompt")

print(prompt)

print("\nGenerated output")

print(output[0]['generated_text'])





References

Kharwal, Aman. Hands-On GenAI, LLMs and AI Agents (p. 16). BlueRose Publishers. Kindle Edition. 

Saturday, December 20, 2025

Proportional (P) Controller: Step Response and the Effect of Gain (Kp)


The Proportional (P) controller is the simplest form of feedback control. It calculates the control output as:u = Kp × error
(where error = setpoint − actual value)
When we apply a step input (sudden change in setpoint from 0 to 1), the system's response depends heavily on the gain Kp.Key Effects of Increasing Kp
  • Faster response: The output rises toward the setpoint more quickly.
  • Reduced steady-state error: The final offset gets smaller, but never zero.
For a typical first-order system and unit step, the steady-state error is:ess = 1 / (1 + Kp)
Final output = Kp / (1 + Kp)
Step Response Comparison![Step Response Graph]
(Embed the graph here: setpoint jumps to 1.0; outputs shown for Kp = 0.5, 1.0, 2.0, 5.0, 10.0)
  • Kp = 0.5 → final ≈ 0.33 (error 0.67) – slow, large offset
  • Kp = 1.0 → final ≈ 0.50 (error 0.50)
  • Kp = 2.0 → final ≈ 0.67 (error 0.33)
  • Kp = 5.0 → final ≈ 0.83 (error 0.17)
  • Kp = 10.0 → final ≈ 0.91 (error 0.09) – fast, small offset
In this first-order example, there's no overshoot. Higher-order systems can oscillate at high Kp.LimitationPure P control always leaves some steady-state error for step changes. To eliminate it, add an integral term (PI/PID controller).When to Use P-OnlyGreat for simple applications where a small offset is acceptable and fast response matters (e.g., basic fan speed or level control).Higher Kp gives you speed and accuracy—but push it too far and stability suffers.

Results



Theoretical steady-state values for unit step input:
(Formula: final output = Kp / (1 + Kp), error = 1 / (1 + Kp))
Kp =  0.5 → final output = 0.3333, steady-state error = 0.6667
Kp =  1.0 → final output = 0.5000, steady-state error = 0.5000
Kp =  2.0 → final output = 0.6667, steady-state error = 0.3333
Kp =  5.0 → final output = 0.8333, steady-state error = 0.1667
Kp = 10.0 → final output = 0.9091, steady-state error = 0.0909