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.
📋 Prerequisites: You need to master the following first
- Lesson 1: Local AI Concepts and Environment Overview
- Lesson 2: Ollama Installation and Environment Configuration
- Lesson 3: CLI Basic Interaction
- Lesson 4: Model Management
- Lesson 5: REST API Fundamentals
1. What You Will Learn
- End-to-end Deployment full workflow hands-on
- Shell script batch testing of Model response quality
- Building a simple chatbot with curl
- Alice's local PoC feasibility verification method
- Self-assessment and Phase 2 preparation direction
2. A Real Story from a Startup CTO
(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:
# 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.
(1) From Zero to API Call
Complete the following steps in order, verifying each step before proceeding:
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
#!/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:
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
#!/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:
{"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:
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
#!/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:
{"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
#!/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:
{"status":"ok","data":{}}
▶ Example 5: Self-Assessment Checklist Verification
#!/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:
I'm a helpful AI assistant running locally on your machine...
7. Comprehensive Example: Complete End-to-End Verification Workflow
#!/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
ollama --version, then curl localhost:11434/api/tags, troubleshooting layer by layer. 90% of issues are service not started or Model not pulled.OLLAMA_KEEP_ALIVE=30m to keep the Model in memory, and subsequent requests respond in seconds.📖 Summary
- End-to-end workflow: Install → Verify service → Pull Model → CLI test → API call → Script integration
- Batch testing can compare response quality and speed across different Models
- A curl chatbot is the best practical way to understand the REST API
- PoC verification focuses on five dimensions: functionality, latency, throughput, cost, privacy
- Self-assessment checklist ensures each skill is truly mastered
- Phase 2 will cover Python SDK, Modelfile, GPU acceleration, and other core features
📝 Exercises
- Basic (Difficulty ⭐): Complete each step of the end-to-end verification script, ensuring all pass, and save screenshots of the results.
- 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.
- 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.