Ollama: CLI Basic Interaction
The CLI is the direct channel to converse with local Large Language Models — type a question, press Enter for an answer, no code required.
ollama run for the first time with a specific Model, Ollama will automatically download the Model file (2GB-40GB+) from the remote repository before starting Inference. Subsequent runs of the same Model load from local files directly, with no re-download needed. We recommend pulling the Model in advance: ollama pull <model>.
📋 Prerequisites: You need to master the following first
1. What You Will Learn
- Single-shot and interactive dialogue modes
- Multi-turn conversations and slash commands
- System Prompt role configuration
- Pipeline and redirection integration with Shell scripts
- Alice's SQL generation practical scenario
2. A Real Story from a SaaS Founder
(1) The Pain Point: Writing SQL by Hand Is Too Slow
Alice founded the GlobalShop e-commerce platform and writes dozens of complex SQL queries daily to analyze business data. Going from product requirements to SQL statements takes an average of 15 minutes per query, with frequent mistakes in JOIN conditions. She needs a tool to quickly convert natural language to SQL.
(2) The Solution: CLI Generates SQL in Seconds
Using the Ollama CLI with a System Prompt, Alice inputs "query last month's active user count" and gets complete SQL in 3 seconds:
ollama run llama3.2 "Write a SQL query to count active users last month"
3. ollama run Command Deep Dive
ℹ️ Note: The examples in this course focus on actual functionality demonstrations. Please concentrate on CLI command syntax, parameter meanings, and interactive mode usage.
(1) Two Execution Modes
| Mode | Command | Behavior |
|---|---|---|
| Single-shot mode | ollama run model "prompt" |
Exits after output |
| Interactive mode | ollama run model |
Enters continuous dialogue |
sequenceDiagram
participant U as User
participant O as Ollama CLI
participant S as Ollama Server
U->>O: ollama run llama3.2
O->>S: Check if model loaded
S-->>O: Model ready
O-->>U: >>> prompt
U->>O: What is machine learning?
O->>S: POST /api/generate
S-->>O: Streaming response
O-->>U: Machine learning is...
▶ Example 1: Single-shot and Interactive Modes
# Single-shot: ask and exit
ollama run llama3.2 "Explain recursion in one sentence"
# Recursion is a function that calls itself to solve smaller instances of the same problem.
# Interactive: stay in conversation
ollama run llama3.2
>>> What is Python?
Python is a high-level programming language...
>>> Can you give an example? # Context preserved
Sure! Here's a simple Python example:
print("Hello, World!")
Output:
I'm a helpful AI assistant running locally on your machine...
(2) Key Run Parameters
| Parameter | Purpose | Example |
|---|---|---|
--verbose |
Show Inference time and speed | ollama run --verbose llama3.2 "Hello" |
--system |
Set System Prompt | ollama run --system "Reply in JSON" llama3.2 "List colors" |
--prompt-template |
Custom prompt template | ollama run --prompt-template "Q: {{.Prompt}}\nA:" llama3.2 "What is AI?" |
▶ Example 2: Run Command with Parameters
# Show performance metrics
ollama run --verbose llama3.2 "Hello"
# ...response...
# total duration: 1.2s
# load duration: 800ms
# prompt eval count: 12 token(s)
# prompt eval speed: 15.0 tokens/s
# eval count: 45 token(s)
# eval speed: 30.0 tokens/s
# Set system prompt inline
ollama run --system "Reply in JSON format only" llama3.2 "List 3 colors"
# {"colors": ["red", "blue", "green"]}
Output:
I'm a helpful AI assistant running locally on your machine...
4. ollama chat Multi-turn Dialogue
(1) Differences Between chat and run
| Feature | ollama run |
ollama chat |
|---|---|---|
| Context | Retains previous turn by default | Explicit multi-turn messages |
| Message format | Plain text | Supports role distinction (user/assistant/system) |
| API mapping | /api/generate |
/api/chat |
| Use case | Quick questions | Structured conversations |
(2) Slash Command Reference
| Command | Function |
|---|---|
/clear |
Clear conversation context |
/help |
Show all commands |
/set parameter |
Set Inference parameters |
/set system |
Set System Prompt |
/info |
Show Model information |
/exit or Ctrl+D |
Exit conversation |
▶ Example 3: Chat Multi-turn Dialogue and Parameter Adjustment
# Start chat session
ollama chat llama3.2
>>> /set system You are a helpful data analyst
System prompt set.
>>> Analyze this: sales dropped 20% last quarter
The 20% sales drop could indicate several issues...
>>> What visualization would you recommend?
I'd suggest a line chart showing quarterly trends...
>>> /set parameter temperature 0.3
Set parameter 'temperature' to 0.3
>>> Give me the SQL for that analysis
SELECT quarter, revenue FROM sales_data...
>>> /clear
Cleared session context.
>>> /exit
Output:
# Command executed successfully
5. System Prompt and Prompt Engineering Basics
(1) The Role of System Prompt
System Prompt sets the role, rules, and output format for the Model before the conversation begins:
graph TB
SP[System Prompt<br/>Role + Rules + Format] --> C[Conversation Context]
U[User Message] --> C
C --> M[Model Response]
M --> C
| Role | System Prompt Example | Effect |
|---|---|---|
| SQL Expert | "You are a SQL expert. Only output valid SQL." | Outputs only SQL statements |
| Translator | "You are a translator. Translate to French." | Auto-translates |
| Customer Service | "You are a customer service agent. Be polite and concise." | Polite, concise replies |
| Code Reviewer | "You are a code reviewer. Point out bugs and improvements." | Focuses on code issues |
▶ Example 4: Alice's SQL Generation Scenario
# Alice's SQL assistant with System Prompt
ollama run --system "You are a PostgreSQL expert. Given a natural language question, output ONLY the SQL query. No explanations, no markdown, just the SQL." llama3.2
>>> Count users who registered in the last 30 days and made at least one purchase
SELECT COUNT(DISTINCT u.id)
FROM users u
JOIN orders o ON u.id = o.user_id
WHERE u.created_at >= NOW() - INTERVAL '30 days'
AND o.id IS NOT NULL;
>>> Find the top 5 products by revenue this month
SELECT p.name, SUM(oi.quantity * oi.price) AS revenue
FROM products p
JOIN order_items oi ON p.id = oi.product_id
JOIN orders o ON oi.order_id = o.id
WHERE o.created_at >= DATE_TRUNC('month', CURRENT_DATE)
GROUP BY p.id, p.name
ORDER BY revenue DESC
LIMIT 5;
Output:
I'm a helpful AI assistant running locally on your machine...
6. Pipeline and Shell Integration
ollama run reads input from stdin and waits for the complete response before outputting to stdout. This means pipeline mode is non-streaming, suitable for batch processing rather than real-time interaction.
(1) CLI Pipeline Mode
The Ollama CLI integrates seamlessly with Unix pipelines for automated workflows:
| Mode | Command | Use Case |
|---|---|---|
| stdin input | echo "prompt" | ollama run model |
Scripted invocation |
| Output redirect | ollama run model "prompt" > output.txt |
Save results |
| Chained pipeline | cat data.txt | ollama run model | jq . |
Post-processing |
▶ Example 5: Shell Script Integration
# Batch translate product descriptions
echo "Translate to French: Premium wireless headphones" | ollama run llama3.2
# Generate commit messages from git diff
git diff --staged | ollama run llama3.2 "Write a concise git commit message for these changes"
# Summarize a log file
cat /var/log/app.log | tail -100 | ollama run llama3.2 "Summarize the key errors in this log"
Output:
I'm a helpful AI assistant running locally on your machine...
7. Comprehensive Example: Alice's SQL Assistant Script
LIMIT and EXPLAIN safety checks before running.
#!/bin/bash
# ============================================
# Comprehensive: Alice's SQL Assistant
# Natural language to SQL with context awareness
# ============================================
MODEL="llama3.2"
SYSTEM="You are a PostgreSQL expert. Output ONLY valid SQL. No explanations, no markdown fences."
# Interactive mode with preset system prompt
sql_chat() {
echo "SQL Assistant ready. Type your query in natural language (or 'exit'):"
ollama run --system "$SYSTEM" "$MODEL"
}
# Single-shot: pipe question, get SQL
sql_ask() {
local question="$1"
echo "$question" | ollama run --system "$SYSTEM" "$MODEL"
}
# Batch mode: process questions from file
sql_batch() {
local input_file="$1"
local output_file="sql_output_$(date +%Y%m%d_%H%M%S).sql"
while IFS= read -r question; do
echo "-- Question: $question" >> "$output_file"
sql_ask "$question" >> "$output_file"
echo "" >> "$output_file"
done < "$input_file"
echo "Batch complete: $output_file"
}
# Main menu
case "${1:-interactive}" in
ask) sql_ask "$2" ;;
batch) sql_batch "$2" ;;
*) sql_chat ;;
esac
-- Question: Count users registered last month
SELECT COUNT(*) FROM users
WHERE created_at >= DATE_TRUNC('month', CURRENT_DATE - INTERVAL '1 month')
AND created_at < DATE_TRUNC('month', CURRENT_DATE);
-- Question: Top 10 products by sales
SELECT p.name, SUM(oi.quantity) AS total_sold
FROM products p
JOIN order_items oi ON p.id = oi.product_id
GROUP BY p.id, p.name
ORDER BY total_sold DESC
LIMIT 10;
❓ FAQ
ollama run for quick single-shot questions; use ollama chat for continuous conversations with context. Both work for daily development — run is lighter, chat supports role distinction./set parameter temperature 0.2 to adjust. Higher temperature (0.7-1.0) yields more random, creative output.--verbose shows the Inference process, but the output is still the full result.ollama run qwen2.📖 Summary
ollama runsupports both single-shot and interactive modes — the most commonly used commandollama chatprovides structured multi-turn dialogue with role distinction and slash commands- System Prompt sets the role and rules for the Model, controlling output format
- CLI integrates with Unix pipelines for scripted AI workflows
- The temperature parameter controls output randomness; lower values are more stable
- Alice reduced SQL writing time from 15 minutes to 3 seconds using CLI + System Prompt
📝 Exercises
- Basic (Difficulty ⭐): Use
ollama runfor an interactive dialogue of at least 3 turns, trying the/set systemand/clearcommands. - Intermediate (Difficulty ⭐⭐): Create a "code review assistant" with a System Prompt, submit a code snippet for review, and observe how the role setting affects the output.
- Advanced (Difficulty ⭐⭐⭐): Write a Shell script that reads a list of requirements from a file, batch-generates SQL, and outputs to a file, including error handling and timeout mechanisms.