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.

💡 Tip: When using 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


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:

BASH
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
100%
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

BASH
# 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:

TEXT
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

BASH
# 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:

TEXT
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

BASH
# 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:

TEXT
# Command executed successfully

5. System Prompt and Prompt Engineering Basics

💡 Tip: System Prompt is the most effective way to control Model behavior. A clear System Prompt transforms a "general assistant" into a "specialized tool" — the more specific, the better. "You are a PostgreSQL expert, output only SQL" is 10x more effective than "You are an assistant."

(1) The Role of System Prompt

System Prompt sets the role, rules, and output format for the Model before the conversation begins:

100%
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

BASH
# 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:

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

6. Pipeline and Shell Integration

ℹ️ Info: In pipeline mode, 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

BASH
# 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:

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

7. Comprehensive Example: Alice's SQL Assistant Script

⚠️ Warning: LLM-generated SQL may contain syntax errors or logic flaws — never execute it directly on a production database. Always verify in a test environment first, or add LIMIT and EXPLAIN safety checks before running.

BASH
#!/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
💻 Output:

TEXT
-- 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

Q Should I use ollama run or ollama chat?
A Use 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.
Q Can CLI conversations save history?
A Context is preserved within an interactive session but cleared on exit. For persistence, use pipeline redirection to a file or manage conversation history through the REST API yourself.
Q How can I make the output more consistent and stable?
A Set a low temperature (0.1-0.3) for more deterministic output. In the CLI, use /set parameter temperature 0.2 to adjust. Higher temperature (0.7-1.0) yields more random, creative output.
Q Why is there no streaming output in pipeline mode?
A Pipeline mode (stdin input) is non-streaming by default, waiting for the complete response before output. Adding --verbose shows the Inference process, but the output is still the full result.
Q Does a long System Prompt affect performance?
A Yes. System Prompt counts toward the context window (default 2048 tokens); an excessively long one reduces available conversation space. We recommend keeping it under 200 tokens.
Q How do I ask questions in other languages in the CLI?
A Just type in your language directly. Models like llama3.2 and qwen2 support multiple languages. For the best Chinese experience, we recommend ollama run qwen2.

📖 Summary


📝 Exercises

  1. Basic (Difficulty ⭐): Use ollama run for an interactive dialogue of at least 3 turns, trying the /set system and /clear commands.
  2. 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.
  3. 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.
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%

🙏 帮我们做得更好

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

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