Ollama: Phase 1 Comprehensive Exercises

Phase 1 comprehensive exercises are the practical checkpoint that tests your learning from the first 5 lessons — from zero setup to API calls, all in one go.

💡 Tip: This practical lesson is closely tied to the first 5 lessons. We recommend completing them in order: master installation and configuration (Lesson 2), CLI basics (Lesson 3), Model management (Lesson 4), REST API (Lesson 5), then return for the comprehensive exercises. Don't skip ahead — you may encounter unfamiliar concepts and commands.

📋 Prerequisites: You need to master the following first

1. What You Will Learn


2. A Real Story from a Startup CTO

ℹ️ Info: The goal of a PoC (Proof of Concept) is to "verify feasibility" rather than "perfect execution." Proving the core pipeline works with the fastest speed earns team trust and subsequent budget. Don't fixate on Model accuracy or code quality during the PoC phase.

⚠️ Warning: When doing end-to-end PoC verification, be sure to check the "cold start" effect on API latency — the first request needs to load the Model (5-30 seconds), and only subsequent requests reflect true Inference latency. Send a warmup request first, then start timing.

(1) The Pain Point: Theory Alone Can't Convince the Team

Alice completed the first 5 lessons, but the team still had doubts about local AI: "Can it really run? Is the quality good enough?" She needs an end-to-end PoC to prove local AI is feasible before she can get budget for GPU servers.

(2) The Solution: 30-Minute End-to-End PoC

From installation to an interactive customer service prototype, Alice completed full pipeline verification in 30 minutes:

BASH
# Step 1: Install
curl -fsSL https://ollama.com/install.sh | sh

# Step 2: Pull model
ollama pull qwen2.5

# Step 3: API test
curl http://localhost:11434/api/chat -d '{
  "model": "qwen2.5",
  "messages": [{"role": "user", "content": "Hello"}],
  "stream": false
}'

3. Exercise 1: End-to-End Full Workflow

ℹ️ Note: The core goal of end-to-end verification is "proving the pipeline works" — from installation to API calls, every step succeeds. Don't pursue Model output quality at this stage; that's for later optimization. Start with the smallest Model (3B) for quick verification — it's much more efficient than debugging with a large Model.

💡 Tip: The key to an end-to-end PoC is "quickly verifying feasibility," not perfection. Use the smallest Model (3B) first to verify the pipeline works, then switch to the target Model for quality testing. This is far more efficient than pulling a large Model right away.

(1) From Zero to API Call

Complete the following steps in order, verifying each step before proceeding:

100%
flowchart LR
    A[Install Ollama] --> B[Verify Service]
    B --> C[Pull Model]
    C --> D[CLI Test]
    D --> E[REST API Test]
    E --> F[Script Integration]
Step Action Verification Command
1 Install Ollama ollama --version
2 Confirm service is running curl http://localhost:11434/api/tags
3 Pull qwen2.5 ollama pull qwen2.5
4 CLI interaction test ollama run qwen2.5 "Hello"
5 REST API test curl call to /api/chat
6 Script integration test Shell script batch call

▶ Example 1: End-to-End Verification Script

BASH
#!/bin/bash
# End-to-end verification script
set -e

echo "=== Step 1: Verify Installation ==="
ollama --version

echo "=== Step 2: Verify Service ==="
curl -s http://localhost:11434/api/tags | python3 -m json.tool > /dev/null
echo "Service is running."

echo "=== Step 3: Pull Model ==="
ollama pull qwen2.5

echo "=== Step 4: CLI Test ==="
ollama run qwen2.5 "Say hello in one sentence"

echo "=== Step 5: REST API Test ==="
curl -s http://localhost:11434/api/chat -d '{
  "model": "qwen2.5",
  "messages": [{"role": "user", "content": "Hello"}],
  "stream": false
}' | python3 -c "import sys,json; print('API works:', json.load(sys.stdin)['message']['content'][:50])"

echo "=== All Steps Passed ==="

Output:

TEXT
I'm a helpful AI assistant running locally on your machine...

4. Exercise 2: Batch Testing Model Response Quality

(1) Comparing Output Across Different Parameter-Count Models

Use the same set of questions to test different Models, evaluating speed and quality:

Test Dimension 3B Model 8B Model Embedding Model
Response speed Fast (30+ tok/s) Medium (15-20 tok/s) Fast (vectors only)
Chinese quality Basically usable Fluent and natural Not applicable
Reasoning ability Simple tasks Moderate complexity Not applicable
Resource usage 4GB RAM 8GB RAM 300MB RAM

▶ Example 2: Batch Model Quality Comparison

BASH
#!/bin/bash
# Batch test different models with the same questions

QUESTIONS=(
    "What is the return policy for electronics?"
    "Translate to French: Free shipping on orders over $50"
    "Write a SQL query to find top 10 customers by revenue"
)

MODELS=("llama3.2:3b" "llama3.1:8b" "qwen2.5:7b")

for model in "${MODELS[@]}"; do
    echo "=== Model: $model ==="
    for q in "${QUESTIONS[@]}"; do
        echo "Q: $q"
        start=$(date +%s%N)
        response=$(curl -s http://localhost:11434/api/chat -d "{
            \"model\": \"$model\",
            \"messages\": [{\"role\": \"user\", \"content\": \"$q\"}],
            \"stream\": false,
            \"options\": {\"temperature\": 0.3}
        }" | python3 -c "import sys,json; d=json.load(sys.stdin); print(d['message']['content'][:100])")
        end=$(date +%s%N)
        elapsed=$(( (end - start) / 1000000 ))
        echo "A: ${response}..."
        echo "Time: ${elapsed}ms"
        echo ""
    done
done

Output:

TEXT
{"status":"ok","data":{}}

5. Exercise 3: curl Chatbot Script

(1) Interactive Chat Script Design

Implement a simple chatbot using a Shell script, manually maintaining conversation history:

100%
flowchart TD
    A[Start Session] --> B[Read User Input]
    B --> C{Input == /quit?}
    C -->|Yes| D[Exit]
    C -->|No| E[Append to Messages Array]
    E --> F[Call /api/chat]
    F --> G[Print Response]
    G --> H[Append Response to Messages]
    H --> B

▶ Example 3: Interactive Chatbot

BASH
#!/bin/bash
# Simple interactive chatbot using curl

API="http://localhost:11434/api/chat"
MODEL="qwen2.5"
MESSAGES='[{"role":"system","content":"You are a helpful assistant. Be concise."}]'

echo "ChatBot (type /quit to exit)"
echo "--------------------------------"

while true; do
    read -p "You: " input
    if [ "$input" = "/quit" ]; then
        echo "Goodbye!"
        break
    fi

    # Append user message
    MESSAGES=$(echo "$MESSAGES" | python3 -c "
import sys, json
msgs = json.load(sys.stdin)
msgs.append({'role': 'user', 'content': '$input'})
print(json.dumps(msgs))
")

    # Call API
    response=$(curl -s "$API" -d "{
        \"model\": \"$MODEL\",
        \"messages\": $MESSAGES,
        \"stream\": false,
        \"options\": {\"temperature\": 0.5}
    }")

    # Extract and print response
    content=$(echo "$response" | python3 -c "import sys,json; print(json.load(sys.stdin)['message']['content'])")
    echo "Bot: $content"

    # Append assistant response
    MESSAGES=$(echo "$MESSAGES" | python3 -c "
import sys, json
msgs = json.load(sys.stdin)
msgs.append({'role': 'assistant', 'content': '''$content'''})
print(json.dumps(msgs))
")
    echo ""
done

Output:

TEXT
{"status":"ok","data":{}}

6. Alice's Local PoC Feasibility Verification

(1) PoC Evaluation Dimensions

Dimension Check Item Target
Functionality Can correctly reply to common questions Accuracy > 80%
Latency First-token response time < 500ms
Throughput Requests processed per minute > 10 req/min
Cost Hardware investment vs cloud savings Break even within 6 months
Privacy Does data leave local 0 leakage

▶ Example 4: PoC Performance Benchmark Script

BASH
#!/bin/bash
# Quick PoC benchmark for local AI

API="http://localhost:11434/api/chat"
MODEL="qwen2.5"
REQUESTS=10

echo "=== Local AI PoC Benchmark ==="
echo "Model: $MODEL"
echo "Requests: $REQUESTS"
echo ""

total_time=0
total_tokens=0

for i in $(seq 1 $REQUESTS); do
    start=$(date +%s%N)
    response=$(curl -s "$API" -d "{
        \"model\": \"$MODEL\",
        \"messages\": [{\"role\": \"user\", \"content\": \"Explain the return policy briefly\"}],
        \"stream\": false,
        \"options\": {\"temperature\": 0.3}
    }")
    end=$(date +%s%N)

    elapsed_ms=$(( (end - start) / 1000000 ))
    tokens=$(echo "$response" | python3 -c "import sys,json; print(json.load(sys.stdin).get('eval_count', 0))")

    total_time=$((total_time + elapsed_ms))
    total_tokens=$((total_tokens + tokens))
    echo "Request $i: ${elapsed_ms}ms, ${tokens} tokens"
done

avg_time=$((total_time / REQUESTS))
avg_tokens=$((total_tokens / REQUESTS))
tput=$((total_tokens * 1000 / total_time))

echo ""
echo "=== Results ==="
echo "Avg latency: ${avg_time}ms"
echo "Avg tokens:  ${avg_tokens}"
echo "Throughput:  ${tput} tokens/s"
echo "Target met:  $([ $avg_time -lt 5000 ] && echo 'YES' || echo 'NO')"

Output:

TEXT
{"status":"ok","data":{}}

▶ Example 5: Self-Assessment Checklist Verification

BASH
#!/bin/bash
# Phase 1 self-assessment checklist

echo "=== Phase 1 Self-Assessment ==="
echo ""

checks=0
total=6

# Check 1: Ollama installed
if command -v ollama &> /dev/null; then
    echo "[PASS] Ollama is installed"
    ((checks++))
else
    echo "[FAIL] Ollama is not installed"
fi

# Check 2: Service running
if curl -s http://localhost:11434/api/tags > /dev/null 2>&1; then
    echo "[PASS] Ollama service is running"
    ((checks++))
else
    echo "[FAIL] Ollama service is not running"
fi

# Check 3: At least one model available
model_count=$(ollama list 2>/dev/null | tail -n +2 | wc -l)
if [ "$model_count" -gt 0 ]; then
    echo "[PASS] $model_count model(s) available"
    ((checks++))
else
    echo "[FAIL] No models pulled"
fi

# Check 4: CLI interaction works
if ollama run llama3.2 "test" > /dev/null 2>&1; then
    echo "[PASS] CLI interaction works"
    ((checks++))
else
    echo "[FAIL] CLI interaction failed"
fi

# Check 5: REST API responds
if curl -s http://localhost:11434/api/generate -d '{"model":"llama3.2","prompt":"hi","stream":false}' > /dev/null 2>&1; then
    echo "[PASS] REST API responds"
    ((checks++))
else
    echo "[FAIL] REST API not responding"
fi

# Check 6: Streaming works
if curl -s http://localhost:11434/api/chat -d '{"model":"llama3.2","messages":[{"role":"user","content":"hi"}]}' | grep -q '"done":true'; then
    echo "[PASS] Streaming API works"
    ((checks++))
else
    echo "[FAIL] Streaming API failed"
fi

echo ""
echo "Score: $checks / $total"
if [ "$checks" -eq "$total" ]; then
    echo "✅ Ready for Phase 2!"
else
    echo "⚠️ Review failed items before proceeding."
fi

Output:

TEXT
I'm a helpful AI assistant running locally on your machine...

7. Comprehensive Example: Complete End-to-End Verification Workflow

BASH
#!/bin/bash
# ============================================
# Comprehensive: Full Phase 1 E2E workflow
# Install → Configure → Pull → Test → Report
# ============================================

set -e
REPORT="phase1_report_$(date +%Y%m%d_%H%M%S).txt"

echo "Phase 1 E2E Workflow" | tee "$REPORT"
echo "====================" | tee -a "$REPORT"
echo "" | tee -a "$REPORT"

# Step 1: Install verification
echo "## Installation" | tee -a "$REPORT"
echo "Ollama: $(ollama --version 2>&1)" | tee -a "$REPORT"
echo "OS: $(uname -s) $(uname -m)" | tee -a "$REPORT"
echo "RAM: $(free -g | awk '/^Mem:/{print $2}')GB" | tee -a "$REPORT"
if command -v nvidia-smi &> /dev/null; then
    echo "GPU: $(nvidia-smi --query-gpu=name --format=csv,noheader)" | tee -a "$REPORT"
else
    echo "GPU: CPU-only mode" | tee -a "$REPORT"
fi
echo "" | tee -a "$REPORT"

# Step 2: Pull SupportBot model stack
echo "## Model Stack" | tee -a "$REPORT"
ollama pull qwen2.5 2>&1 | tail -1 | tee -a "$REPORT"
ollama pull llama3.2:3b 2>&1 | tail -1 | tee -a "$REPORT"
ollama pull nomic-embed-text 2>&1 | tail -1 | tee -a "$REPORT"
echo "" | tee -a "$REPORT"

# Step 3: Quality test with 3 questions
echo "## Quality Test" | tee -a "$REPORT"
questions=(
    "What is your return policy?"
    "How do I track my order?"
    "Do you ship internationally?"
)
for q in "${questions[@]}"; do
    echo "Q: $q" | tee -a "$REPORT"
    answer=$(curl -s http://localhost:11434/api/chat -d "{
        \"model\": \"qwen2.5\",
        \"messages\": [{\"role\": \"system\", \"content\": \"You are an e-commerce support agent. Be concise.\"},
                       {\"role\": \"user\", \"content\": \"$q\"}],
        \"stream\": false,
        \"options\": {\"temperature\": 0.3}
    }" | python3 -c "import sys,json; print(json.load(sys.stdin)['message']['content'][:120])")
    echo "A: $answer" | tee -a "$REPORT"
    echo "" | tee -a "$REPORT"
done

# Step 4: Performance benchmark
echo "## Benchmark" | tee -a "$REPORT"
start=$(date +%s%N)
curl -s http://localhost:11434/api/chat -d '{
    "model": "qwen2.5",
    "messages": [{"role": "user", "content": "Hello"}],
    "stream": false
}' > /dev/null
end=$(date +%s%N)
latency=$(( (end - start) / 1000000 ))
echo "Single request latency: ${latency}ms" | tee -a "$REPORT"
echo "Target < 5000ms: $([ $latency -lt 5000 ] && echo PASS || echo FAIL)" | tee -a "$REPORT"
echo "" | tee -a "$REPORT"

echo "Report saved: $REPORT"

❓ FAQ

Q What should I do if the end-to-end script fails at a step?
A Verify each step individually. First ollama --version, then curl localhost:11434/api/tags, troubleshooting layer by layer. 90% of issues are service not started or Model not pulled.
Q Model loading is slow during batch testing, what can I do?
A The first call needs to load the Model into memory (cold start). Set OLLAMA_KEEP_ALIVE=30m to keep the Model in memory, and subsequent requests respond in seconds.
Q What if the chatbot script's conversation history gets too long?
A A growing messages array consumes the context window. We recommend implementing a sliding window — keeping only the last 10 turns and discarding earlier ones. Or periodically summarize and compress.
Q PoC benchmark results are inconsistent, what should I do?
A The first request includes Model loading time (cold start bias). We recommend discarding the first 2 requests and averaging the next 10. Ensure no other load during testing.
Q What prerequisite knowledge is needed for Phase 2?
A Python basics (functions, classes, async/await), HTTP API calling experience, basic Docker concepts. Lesson 7 begins Python SDK; we recommend getting familiar with Python beforehand.
Q How do I know if I'm ready for Phase 2?
A Complete the self-assessment checklist 6/6 items and be able to independently build a multi-turn dialogue script with curl — then you're ready for Phase 2.

📖 Summary


📝 Exercises

  1. Basic (Difficulty ⭐): Complete each step of the end-to-end verification script, ensuring all pass, and save screenshots of the results.
  2. Intermediate (Difficulty ⭐⭐): Run the batch Model quality comparison script, select 3 different Models, record response times and quality scores, and write a brief analysis report.
  3. Advanced (Difficulty ⭐⭐⭐): Complete a full PoC verification for Alice's SaaS company — write benchmark scripts, record latency and throughput data, calculate 12-month cost comparisons, and output a feasibility report document.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏