Thứ Hai, 2 tháng 11, 2026

Ollama – giải pháp toàn diện cho chạy và tích hợp mô hình ngôn ngữ lớn (LLMs)

Ollama là nền tảng giúp bạn chạy và quản lý các mô hình ngôn ngữ lớn (LLMs) một cách đơn giản, hiệu quả, tương tự như HuggingFace hoặc LM Studio nhưng hướng đến sự tiện dụng và khả năng tương thích cao.


Người dùng có thể tải về và sử dụng trực tiếp các mô hình nổi tiếng như GPT-OSS, Gemma 3, DeepSeek-R1, Qwen3, Mistral, v.v.

💡 Điểm nổi bật:

  • Cài đặt dễ dàng trên macOS, Windows và Linux.
  • Hỗ trợ mô hình cục bộ và mô hình Cloud (chạy trên GPU mạnh của Ollama).
  • Có API tương thích OpenAI, dễ tích hợp với Python, JavaScript, cURL.
  • Cho phép streaming, tool calling, structured output, embeddings, vision, và web search....

🚀 Bắt đầu với Ollama (Quickstart)

1️⃣ Cài đặt Ollama

Truy cập ollama.com/download để tải bản phù hợp hệ điều hành của bạn.
Sau khi cài đặt, mở Terminal và kiểm tra bằng lệnh:

Bash
ollama --version

Nếu xuất hiện phiên bản, ví dụ ollama 0.12.2, nghĩa là bạn đã cài thành công.


2️⃣ Chạy mô hình đầu tiên

Xem danh sách các mô hình tại ollama.com/models. Ví dụ, ta dùng mô hình Gemma 3:

➤ Cách 1: Chạy trực tiếp bằng CLI

Bash
ollama run gemma3

Sau đó, có thể chat trực tiếp với mô hình gemma3:

Screenshot 2025-11-09 at 10.11.55 AM.png

➤ Cách 2: Chat qua API cURL

Bash
curl http://localhost:11434/api/chat -d '{
  "model": "gemma3",
  "messages": [{"role": "user", "content": "Hello there!"}],
  "stream": false
}'

Kết quả:

XML
{"model":"gemma3","created_at":"2025-11-09T03:13:52.111639Z","message":{"role":"assistant","content":"Hello to you too! 😊 How's your day going so far? \n\nIs there anything you'd like to talk about, or were you just saying hello? \n\nI'm here to chat, answer questions, help you brainstorm, or just listen."},"done":true,"done_reason":"stop","total_duration":1574990958,"load_duration":141030042,"prompt_eval_count":12,"prompt_eval_duration":80405416,"eval_count":56,"eval_duration":1350134792}% 

➤ Cách 3: Dùng Python

Cài đặt gói ollama:

XML
pip install ollama

Thực thi mã python, dùng hàm chat:

Python
from ollama import chat

response = chat(model='gemma3', messages=[
  {'role': 'user', 'content': 'Why is the sky blue?'}
])
print(response.message.content)

Dùng hàm chat của đối tượng Client:

Python
from ollama import Client
client = Client()
response = client.chat(
    model='gemma3',
    messages=[
        {'role': 'user', 'content': 'Why is the sky blue?'}
    ]
)
print(response['message']['content'])

Sử dụng request (không cần cài đặt gói ollama):

Python
import requests
url = "http://localhost:11434/api/chat"
payload = {
    "model": "gemma3",
    "messages": [
        {"role": "user", "content": "Why is the sky blue?"}
    ],
    "stream": False
}
response = requests.post(url, json=payload)
data = response.json()
print(data["message"]["content"])

Kiểm tra kết quả trả về, giải thích tại sao bầu trời có màu xanh.

➤ Cách 4: Dùng JavaScript

JavaScript
import ollama from 'ollama'

const response = await ollama.chat({
  model: 'gemma3',
  messages: [{ role: 'user', content: 'Why is the sky blue?' }],
})
console.log(response.message.content)

Chạy và kiểm tra kết quả trả về.


☁️ Sử dụng Ollama Cloud

Nếu máy tính của bạn không có GPU mạnh, bạn có thể dùng Cloud Models của Ollama. Chúng hoạt động giống mô hình cục bộ nhưng được xử lý trên máy chủ GPU của Ollama.

Ví dụ mô hình cloud:

  • gpt-oss:120b-cloud
  • deepseek-v3.1:671b-cloud
  • qwen3-coder:480b-cloud

Cách chạy:

Bash
ollama signin          # đăng nhập tài khoản Ollama
ollama run gpt-oss:120b-cloud

Hoặc trong Python:

Python
from ollama import Client

client = Client()
for part in client.chat('gpt-oss:120b-cloud', messages=[{'role':'user','content':'Explain quantum computing'}], stream=True):
  print(part['message']['content'], end='')

🧠 Chế độ “Thinking” – Mô hình biết suy luận

Một số mô hình (như Qwen 3, DeepSeek-R1, GPT-OSS) có khả năng “tư duy trung gian” – hiển thị quá trình suy luận trước khi trả lời.

Ví dụ (Python):

Python
from ollama import chat

response = chat(
  model='qwen3',
  messages=[{'role':'user','content':'How many letters r are in strawberry?'}],
  think=True
)
print('Thinking:', response.message.thinking)
print('Answer:', response.message.content)

👉 Khi bật think=True, bạn có thể thấy phần reasoning trace (dòng suy nghĩ) tách biệt với câu trả lời cuối cùng – rất hữu ích trong việc huấn luyện AI.


📦 Structured Outputs – Trích xuất dữ liệu dạng JSON

Ollama có thể buộc mô hình trả lời theo cấu trúc JSON cố định, giúp lập trình viên dễ xử lý kết quả.

Ví dụ:

Python
from ollama import chat
from pydantic import BaseModel

class Country(BaseModel):
  name: str
  capital: str
  languages: list[str]

response = chat(
  model='qwen3',
  messages=[{'role': 'user', 'content': 'Tell me about Canada.'}],
  format=Country.model_json_schema(),
)

country = Country.model_validate_json(response.message.content)
print(country)

✅ Kết quả:

JSON
{"name": "Canada", "capital": "Ottawa", "languages": ["English", "French"]}

🖼️ Vision – Phân tích hình ảnh bằng AI

Một số mô hình (như Gemma3, Llama4-vision) hỗ trợ đầu vào là ảnh.

Ví dụ:

Python
from ollama import chat

response = chat(
  model='gemma3',
  messages=[{
    'role': 'user',
    'content': 'What is in this image?',
    'images': ['cat.jpg']
  }]
)
print(response.message.content)

🔍 Web Search – Tra cứu thông tin thời gian thực

Ollama hỗ trợ API tìm kiếm web, giúp mô hình có thể cập nhật dữ liệu mới nhất. Chú ý cần đăng ký API_KEY tại https://ollama.com/settings/keys

Ví dụ (Python):

Python
import os
os.environ["OLLAMA_API_KEY"] = "Cho key vào đây" #Trước khi import Ollama
import ollama
response = ollama.web_search("Vietnam's food")
print(response)

Hoặc kết hợp với tool calling để tạo chatbot tự động tìm kiếm:

Python
from ollama import chat, web_search, web_fetch

messages = [{'role':'user','content':"What's Ollama's new engine?"}]
response = chat(model='qwen3', messages=messages, tools=[web_search, web_fetch], think=True)
print(response)

⚙️ Embeddings – Ứng dụng trong tìm kiếm ngữ nghĩa

Tạo vector embedding để dùng trong các hệ thống tìm kiếm hoặc gợi ý thông minh:

Python
import ollama
vec = ollama.embed(model='embeddinggemma', input='The quick brown fox jumps over the lazy dog.')
print(len(vec['embeddings'][0]))  # độ dài vector

🔧 Tool Calling – Khi mô hình tự gọi hàm

Ollama cho phép mô hình tự động gọi các hàm trong mã Python hoặc JS.
Ví dụ mô hình có thể yêu cầu “get_temperature(city='New York')” rồi sử dụng kết quả.

Ví dụ:

Python
from ollama import chat

def get_temperature(city: str) -> str:
  return {'New York': '22°C', 'London': '15°C'}.get(city, 'Unknown')

messages = [{'role':'user','content':"What's the temperature in New York?"}]
response = chat(model='qwen3', messages=messages, tools=[get_temperature], think=True)
print(response.message.content)

📚 Kết luận

Ollama là công cụ mạnh mẽ, giúp các nhà nghiên cứu, giảng viên và sinh viên trải nghiệm, huấn luyện và triển khai mô hình AI tại chỗ mà không cần hạ tầng đám mây phức tạp. Nhờ khả năng tương thích API OpenAI, chạy được mô hình Cloud, và hỗ trợ đầy đủ Vision, Tools, Structured Output, Ollama xứng đáng là nền tảng trung tâm trong các dự án AI học thuật và giáo dục. Truy cập https://ollama.com/docs để khám phá API, Cloud Model và ví dụ tích hợp chi tiết.


=============================
TƯ VẤN: Click Here hoặc Hotline/Zalo 090.29.12.888
=============================
Website không chứa bất kỳ quảng cáo nào, mọi đóng góp để duy trì phát triển cho website (donation) xin vui lòng gửi về STK 90.2142.8888 - Ngân hàng Vietcombank Thăng Long - TRAN VAN BINH
=============================
Nếu bạn không muốn bị AI thay thế và tiết kiệm 3-5 NĂM trên con đường trở thành DBA chuyên nghiệp hay làm chủ Database thì hãy đăng ký ngay KHOÁ HỌC ORACLE DATABASE A-Z ENTERPRISE, được Coaching trực tiếp từ tôi với toàn bộ bí kíp thực chiến, thủ tục, quy trình của gần 20 năm kinh nghiệm (mà bạn sẽ KHÔNG THỂ tìm kiếm trên Internet/Google) từ đó giúp bạn dễ dàng quản trị mọi hệ thống Core tại Việt Nam và trên thế giới, đỗ OCP.
- CÁCH ĐĂNG KÝ: Gõ (.) hoặc để lại số điện thoại hoặc inbox https://m.me/tranvanbinh.vn hoặc Hotline/Zalo 090.29.12.888
- Chi tiết tham khảo:
https://bit.ly/oaz_w
=============================
2 khóa học online qua video giúp bạn nhanh chóng có những kiến thức nền tảng về Linux, Oracle, học mọi nơi, chỉ cần có Internet/4G:
- Oracle cơ bản: https://bit.ly/admin_1200
- Linux: https://bit.ly/linux_1200
=============================
KẾT NỐI VỚI CHUYÊN GIA TRẦN VĂN BÌNH:
📧 Mail: binhoracle@gmail.com
☎️ Mobile/Zalo: 0902912888
👨 Facebook: https://www.facebook.com/BinhOracleMaster
👨 Inbox Messenger: https://m.me/101036604657441 (profile)
👨 Fanpage: https://www.facebook.com/tranvanbinh.vn
👨 Inbox Fanpage: https://m.me/tranvanbinh.vn
👨👩 Group FB: https://www.facebook.com/groups/DBAVietNam
👨 Website: https://www.tranvanbinh.vn
👨 Blogger: https://tranvanbinhmaster.blogspot.com
🎬 Youtube: https://www.youtube.com/@binhguru
👨 Tiktok: https://www.tiktok.com/@binhguru
👨 Linkin: https://www.linkedin.com/in/binhoracle
👨 Twitter: https://twitter.com/binhguru
👨 Podcast: https://www.podbean.com/pu/pbblog-eskre-5f82d6
👨 Địa chỉ: Tòa nhà Sun Square - 21 Lê Đức Thọ - Phường Mỹ Đình 1 - Quận Nam Từ Liêm - TP.Hà Nội

=============================
cơ sở dữ liệu, cơ sở dữ liệu quốc gia, database, AI, trí tuệ nhân tạo, artificial intelligence, machine learning, deep learning, LLM, ChatGPT, DeepSeek, Grok, oracle tutorial, học oracle database, Tự học Oracle, Tài liệu Oracle 12c tiếng Việt, Hướng dẫn sử dụng Oracle Database, Oracle SQL cơ bản, Oracle SQL là gì, Khóa học Oracle Hà Nội, Học chứng chỉ Oracle ở đầu, Khóa học Oracle online,sql tutorial, khóa học pl/sql tutorial, học dba, học dba ở việt nam, khóa học dba, khóa học dba sql, tài liệu học dba oracle, Khóa học Oracle online, học oracle sql, học oracle ở đâu tphcm, học oracle bắt đầu từ đâu, học oracle ở hà nội, oracle database tutorial, oracle database 12c, oracle database là gì, oracle database 11g, oracle download, oracle database 19c/21c/23c/23ai, oracle dba tutorial, oracle tunning, sql tunning , oracle 12c, oracle multitenant, Container Databases (CDB), Pluggable Databases (PDB), oracle cloud, oracle security, oracle fga, audit_trail,oracle RAC, ASM, oracle dataguard, oracle goldengate, mview, oracle exadata, oracle oca, oracle ocp, oracle ocm , oracle weblogic, postgresql tutorial, mysql tutorial, mariadb tutorial, ms sql server tutorial, nosql, mongodb tutorial, oci, cloud, middleware tutorial, docker, k8s, micro service, hoc solaris tutorial, hoc linux tutorial, hoc aix tutorial, unix tutorial, securecrt, xshell, mobaxterm, putty

ĐỌC NHIỀU

Trần Văn Bình - Oracle Database Master