Qwen Code CLI
Qwen Code CLI เป็นผู้ช่วย AI ที่พัฒนาโดย Alibaba Cloud ให้ทำงานแบบ command-line interface ช่วยให้ développement process รวดเร็วและมีประสิทธิภาพมากขึ้น
รันใน Terminal ได้ทันที
ไม่ต้องเปิด IDE หรือ Web Interface ทุกอย่างทำงานผ่าน Terminal command line เหมาะสำหรับ DevOps, Sysadmin และ Developer ที่ชอบงานด้วย CLI
เชื่อมต่อ AI Models ได้หลายแบบ
รองรับทั้ง API เฉพาะจาก Alibaba Cloud และ Local AI ผ่าน Ollama คุณสามารถเลือกใช้โมเดลที่ต้องการได้ตามความเหมาะสม
ข้อดีของ Qwen Code CLI
ใช้ Ollama เพื่อรัน AI models บนเครื่องของคุณโดยไม่ต้องส่งข้อมูลไปภายนอก
สร้าง agent ที่ปรับแต่งเองได้สำหรับงานเฉพาะทาง เช่น DevOps, Data Analysis
เชื่อมต่อกับ MCP (Model Context Protocol) servers เพื่อเข้าถึงข้อมูลภายนอก
ทำงานได้บน Linux, macOS และ Windows (ผ่าน WSL)
ติดตั้ง Qwen Code CLI
ติดตั้งผ่าน npm หรือ yarn ง่ายๆ พร้อมใช้งานในไม่กี่นาที
สิ่งที่ต้องเตรียมพร้อม
Node.js (v18+)
แนะนำให้ใช้ Node.js version 18 ขึ้นไป หรือใช้ fnm/nvm ในการจัดการเวอร์ชัน
Git
สำหรับเวอร์ชันควบคุมและ cloning repositories
Terminal
Terminal ที่ใช้งานได้ (Terminal, iTerm2, Alacritty ฯลฯ)
ติดตั้งด้วย npm
วิธีติดตั้งที่ง่ายและเร็วที่สุด
เปิด Terminal แล้วพิมพ์คำสั่งนี้:
147 packages are looking for funding
run `npm fund` for details
ตรวจสอบการติดตั้ง:
ติดตั้งด้วย Yarn
สำหรับผู้ที่ใช้ Yarn เป็น package manager
ใช้คำสั่ง yarn add หรือ yarn global add:
[2/4] Fetching packages...
[3/4] Linking dependencies...
[4/4] Building fresh packages...
success Installed @qwen/qwen-code@1.0.0 with binaries: - qwen-code
เริ่มใช้งานครั้งแรก
หลังจากติดตั้งเสร็จแล้ว ให้รันคำสั่งนี้เพื่อเริ่มต้นใช้งาน:
I'm ready to help you with your coding tasks!
Type /help to see available commands.
➜ Ready! How can I help you today?
หมายเหตุ: เข้าสู่โหมด Interactive Mode - คุณสามารถพิมพ์คำถามหรือคำสั่งได้ทันที
ผสานรวมกับ Ollama
Ollama คือเครื่องมือที่ช่วยรัน Local AI Models ได้ง่ายในเครื่องของคุณ การผสานรวม Qwen Code กับ Ollama ทำให้คุณใช้ AI ได้โดยไม่ต้องส่งข้อมูลภายนอก
ติดตั้ง Ollama
macOS (ด้วย Homebrew):
Linux (Ubuntu/Debian):
เริ่มต้นใช้งาน Ollama:
successfully pulled llama3:latest
เชื่อมต่อกับ Qwen Code
ตั้งค่า API Key ให้กับ Qwen Code ใช้ Base URL ของ Ollama:
ตั้งค่า Base URL (ถ้าใช้ Ollama บน localhost):
ค่าเริ่มต้น:
api-key: ollama- ใช้ Ollama APIbase-url: http://localhost:11434/v1- ค่า default ของ Ollama
ดาวน์โหลด AI Models สำหรับ Ollama
Ollama รองรับโมเดล AI หลายแบบ ลองดูตัวอย่างโมเดลยอดนิยมและคำสั่งในการดาวน์โหลด:
Meta Llama 3
โมเดล open-source ที่ทรงพลังจาก Meta
CodeLlama
ถูกออกแบบมา specifically สำหรับการเขียนโค้ด
Mistral
โมเดลที่รวดเร็วและมีประสิทธิภาพสูง
ตัวอย่างการใช้งาน Qwen Code กับ Ollama
คุณต้องการให้ผมช่วยอะไรครับ?
➜ เขียน Python ฟังก์ชันสำหรับ prime number check
Qwen: ครับ! นี่คือโค้ด Python สำหรับตรวจสอบว่าเป็นจำนวนเฉพาะหรือไม่:
if n < 2:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
# ตัวอย่างการใช้งาน
for num in range(1, 20):
if is_prime(num):
print(f"{num} is prime")
สร้าง Custom Agents
Custom Agents ช่วยให้คุณสร้างผู้ช่วย AI ที่ปรับแต่งเองได้สำหรับงานเฉพาะทาง สร้าง agent สำหรับ DevOps, Data Analysis หรืองานเฉพาะทางอื่นๆ
Custom Agents คืออะไร?
Custom Agents คือ AI assistant ที่คุณกำหนดค่าเองได้ คุณสามารถตั้งค่า:
-
System Prompt - กำหนดบทบาทและวิธีการตอบของ agent
-
Memory - ตั้งค่าจดจำประวัติการสนทนา
-
Tools - เพิ่มเครื่องมือที่ agent ใช้ได้
-
Example Conversations - ตัวอย่างบทสนทนาเพื่อให้ agent เข้าใจบริบท
ตัวอย่าง Custom Agent
System Prompt: "คุณคือ DevOps Expert ที่เชี่ยวชาญ Docker, Kubernetes, CI/CD pipelines"
System Prompt: "คุณคือ Data Scientist ที่เชี่ยวชาญ Python, pandas, visualization"
System Prompt: "คุณคือ Cybersecurity Expert ที่เชี่ยวชาญ penetration testing, vulnerability assessment"
สร้าง Custom Agent
Qwen Code CLI รองรับการโหลด custom agents ผ่านไฟล์ JSON หรือ environment variables
สร้างไฟล์ Agent Configuration
สร้างไฟล์ agents.json ที่ ~/.qwen/:
"agents": [
{
"name": "devops",
"description": "DevOps Expert",
"system_prompt": "You are a DevOps expert who specializes in Docker, Kubernetes, and CI/CD pipelines. Always provide practical examples and command-line instructions.",
"model": "llama3",
"temperature": 0.7
},
{
"name": "data",
"description": "Data Analysis Expert",
"system_prompt": "You are a data scientist expert in Python, pandas, NumPy, and visualization libraries.",
"model": "codellama",
"temperature": 0.5
}
]
}
เรียกใช้ Custom Agent
เรียกใช้ agent ที่สร้างไว้ผ่าน command line:
Qwen (devops): สวัสดีครับ! ผมคือ DevOps Expert
คุณต้องการให้ผมช่วยสร้าง Dockerfile หรือ Kubernetes manifest หรือเปล่าครับ?
Qwen (data): ได้ครับ! โปรดส่งข้อมูลที่ต้องการวิเคราะห์
Inside Interactive Mode:
Use Environment Variables
คุณยังสามารถตั้งค่า agent ผ่าน environment variables ได้:
export QWEN_DEFAULT_AGENT="devops"
export QWEN_AGENTS_PATH="~/.qwen/agents.json"
export QWEN_AGENT_MODEL="llama3"
Benefits:
- ไม่ต้องระบุ --agent ทุกครั้ง
- Agent เปลี่ยนแปลงได้ง่ายผ่าน env vars
- เหมาะสำหรับ shell aliases
MCP Servers Integration
Model Context Protocol (MCP) ช่วยให้ Qwen Code เข้าถึงข้อมูลภายนอก เช่น ไฟล์ในเครื่อง, API endpoints, databases และอื่นๆ
MCP (Model Context Protocol) คืออะไร?
MCP คือ protocole ที่ช่วยให้ AI models เข้าถึง context ต่างๆ ได้:
-
File System Access - อ่านและเขียนไฟล์ในเครื่อง
-
Git Operations - ดู git history, branches, ฯลฯ
-
Web Search - ค้นหาข้อมูลจากเว็บ
-
Database Queries - เชื่อมต่อฐานข้อมูล
ตัวอย่าง MCP Servers
ให้ AI เข้าถึงไฟล์และโฟลเดอร์ในเครื่องคุณ
ดู git log, branches, diffs และอื่นๆ
จัดการหน่วยความจำcontext สำหรับการสนทนา
ค้นหา scientific papers จาก ArXiv
ตั้งค่า MCP Servers
สร้างไฟล์ mcp.json ที่ ~/.qwen/ เพื่อกำหนด MCP servers:
"servers": {
"filesystem": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-filesystem", "/path/to/allowed/directory"]
},
"git": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-git"],
"env": {"GIT_DIR": "/path/to/repo/.git"}
},
"memory": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-memory"]
},
"fetch": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-fetch"]
}
}
}
Install Dependencies:
Environment Variables (Alternative):
export MCP_FILESYSTEM_PATHS="/home/user/work,/home/user/projects"
export MCP_GIT_ENABLED="true"
export MCP_MEMORY_ENABLED="true"
Security Warning:
คำเตือน! อย่าระบุ root directory (/) ใน filesystem paths เพราะอาจเปิดช่องโหว่ด้าน security เฉพาะให้สิทธิ์เข้าถึงโฟลเดอร์ที่จำเป็นเท่านั้น
ใช้งาน MCP Servers
หลังจากตั้งค่า MCP servers แล้ว Qwen Code จะใช้งานอัตโนมัติ:
import pandas as pd
df = pd.DataFrame(...)
Qwen: ไม่เห็นไฟล์ในโฟลเดอร์ของคุณครับ ผมสามารถใช้ filesystem MCP server เพื่ออ่านไฟล์ได้
[filesystem MCP server loaded]
Qwen: ตกลงครับ! ผมจะอ่านไฟล์จากโฟลเดอร์ของคุณ...
MCP Server Commands:
/mcp list
แสดง list of MCP servers
/mcp reload
รีโหลด MCP configuration
Commands และ Usage
Qwen Code CLI มี command ต่างๆ ที่ช่วยให้การใช้งานสะดวกและมีประสิทธิภาพ ดูที่นี่สำหรับ command ยอดนิยม
Command พื้นฐาน
การใช้งานพื้นฐาน
Command ภายใน Interactive Mode
/help
แสดง list of commands
/clear
ล้างประวัติการสนทนา
/edit filename.txt
เปิดไฟล์เพื่อ edit (new feature)
Tips และ Tricks
เคล็ดลับและเทคนิคการใช้งาน Qwen Code CLI ให้มีประสิทธิภาพสูงสุด
Use Shell Aliases
สร้าง alias เพื่อลดการพิมพ์:
Specify Programming Language
แจ้งภาษาที่ต้องการให้ชัดเจน:
คัดลอก command จาก history ใช้งาน:
history | grep qwen
Power User Tips
Integration แบบไม่เข้า Interactive Mode:
คุณยังสามารถใช้ Qwen Code ร่วมกับคำสั่ง shell อื่นๆ ได้ เช่น:
Quick Scripts:
สร้าง script เล็กๆ เพื่อ task ซ้ำๆ:
# ai-help.sh - Helper for quick AI queries
qwen-code --agent devops "$@"