နားလည်ထားရမယ့် အချက်
Ollama, LM Studio ရဲ့ server mode, llama.cpp ရဲ့ server အားလုံးက local HTTP API တစ်ခုကို ဖော်ပြထားတယ်ဆိုတာ နားလည်ပြီဆိုရင် HTTP request လုပ်နိုင်တဲ့ programming language ဘယ်ခုကနေမဆို local model ကို ခေါ်သုံးနိုင်ပါပြီ, chat window ကနေသာ မဟုတ်တော့ပါဘူး။ ဒီ puzzle ရဲ့ အပိုင်းတွေကို အတိအကျ နာမည်ပေးထားသင့်ပါတယ်။
- Localhost — hostname အထူးတစ်ခုဖြစ်ပြီး address 127.0.0.1 က နောက်ကွယ်မှာ ရှိနေပြီး “ဒီစက်တစ်ခုတည်း” ကို အမြဲ ဆိုလိုပါတယ်, ဒါကြောင့် localhost ဆီ ပို့တဲ့ request တစ်ခုက သင့်ကွန်ပျူတာကနေ ဘယ်တော့မှ မထွက်ဘဲ network ကိုလည်း ဘယ်တော့မှ မထိပါဘူး။
- Port — 11434 (သို့) 1234 လို ဂဏန်းတစ်ခုဖြစ်ပြီး request ကို ဘယ် program တစ်ခုက လက်ခံသင့်လဲ identify ပေးပါတယ်, ဘာဖြစ်လို့လဲဆိုတော့ program အများကြီးက machine တစ်ခုတည်းပေါ်မှာ တစ်ချိန်တည်း listen လုပ်နေနိုင်လို့ပါ။
- Endpoint — server ပေါ်က job တစ်ခုတည်းအတွက် သီးသန့် URL path တစ်ခုဖြစ်ပြီး conversation တစ်ခုအတွက် /api/chat, text ကို vector အဖြစ်ပြောင်းဖို့ /api/embeddings လိုမျိုးပါ; server တစ်ခုတည်းကပဲ task မတူညီတာတွေအတွက် endpoint အများကြီးကို ဖော်ပြနိုင်ပါတယ်။
- Request — သင့် application ပို့တဲ့ message ပါ — endpoint ဘယ်ခုလဲ, model name ဘာလဲ, ယခုအထိ conversation ဘာတွေရှိလဲ, ပုံမှန်အားဖြင့် JSON နဲ့ encode လုပ်ထားတယ်, JSON ဆိုတာ လူနဲ့ program နှစ်ခုစလုံး ဖတ်နိုင်တဲ့ structured data အတွက် plain-text format တစ်ခုပါ။
- Response — server က request ကို runtime နဲ့ model ကနေတဆင့် process လုပ်ပြီး JSON နဲ့ ပြန်ပို့ပါတယ်, generate လုပ်ထားတဲ့ text နဲ့ token count လို metadata တွေ ပါလေ့ရှိပါတယ်။
ဒီ request-response cycle over HTTP ဟာ web app တစ်ခုက weather service လို remote API တစ်ခုနဲ့ ဘယ်လို စကားပြောလဲဆိုတာနဲ့ အတိအကျ တူပါတယ်; local AI API ရဲ့ ကွာခြားချက်ကတော့ server က internet ပေါ်က တခြားနေရာမှာ မဟုတ်ဘဲ သင့်ကိုယ်ပိုင် machine ပေါ်မှာ ရှိတာပါပဲ။
- localhost
- address 127.0.0.1 ကို နောက်ကွယ်မှာ ကိုင်ထားတဲ့ hostname အထူးတစ်ခုဖြစ်ပြီး request ကို ဘယ်ကနေ လာလာ machine တစ်ခုတည်းကို အမြဲ ရည်ညွှန်းလို့ external network ပေါ်ကို ဘယ်တော့မှ မသွားပါဘူး။
- Endpoint
- server ပေါ်က job တစ်ခုတည်းအတွက် သီးသန့် URL path တစ်ခုဖြစ်ပြီး conversation တစ်ခုအတွက် /api/chat, text ကို vector ပြောင်းဖို့ /api/embeddings လိုမျိုး, server တစ်ခုတည်းကနေ endpoint အများကြီး ဖော်ပြနိုင်ပါတယ်။
- API
- server တစ်ခုက ဖော်ပြထားတဲ့ rule နဲ့ endpoint အစုအဝေးဖြစ်ပြီး software တခြားတွေက request ပို့ပြီး structured response (ပုံမှန် JSON) ရရှိနိုင်စေလို့ လူတစ်ယောက်က click နှိပ်စရာမလိုဘဲ software နဲ့ software ချင်း စကားပြောလို့ ရပါတယ်။
APP TO MODEL VIA LOCAL API
--------------------------
------------------------------------------------------------
YOUR APP
|
| HTTP request to
| http://localhost:PORT/endpoint
| (JSON body: model name + messages)
v
LOCAL RUNTIME (Ollama / llama.cpp server / LM Studio server)
|
| loads and runs
v
MODEL
|
| generates text
v
JSON RESPONSE (generated text + metadata)
|
| sent back over the same connection
v
YOUR APP (reads the response, uses the text)လက်တွေ့ scenario နဲ့ ချိတ်ကြည့်မယ်
local run နေတဲ့ model တစ်ခုကို သုံးပြီး meeting note တွေကို summarize လုပ်ပေးမယ့် Python script ငယ်လေးကို offline ဖြစ်ပြီး အပြည့်အဝ အလုပ်လုပ်စေချင်တယ်လို့ ယူဆကြည့်ပါ။
Ollama ကို စတင်ပြီး model ကို confirm လုပ်ခြင်း
Ollama ကို background မှာ စ, `llama3.2` pull လုပ်ပြီးသားဆိုတာ confirm လုပ်ပါ။
Chat endpoint ဆီ request ပို့ခြင်း
terminal chat ထဲ ရိုက်ထည့်မယ့်အစား `http://localhost:11434/api/chat` ဆီ HTTP POST request ပို့တဲ့ Python script တစ်ခု ရေးမယ်, ဒါက `ollama run` command ကိုယ်တိုင် internal မှာ သုံးတဲ့ endpoint အတူတူပါပဲ။ request body ကတော့ model ကို ခေါ်ပြီး summarize concisely လုပ်ခိုင်းတဲ့ system message နဲ့ meeting note text ပါတဲ့ user message ပါ chat message list တစ်ခုနဲ့ JSON object တစ်ခု ဖြစ်ပါတယ်။
Real API call တစ်ခုအနေနဲ့ error handle လုပ်ခြင်း
response status code ကို check လုပ်, Ollama မစသေးရင် connection error ကို handle လုပ်, local generation က ပုံမှန် web API call ထက် အချိန်ပိုကြာနိုင်လို့ timeout ကို သင့်တင့်စွာ ထားပါ။
အောက်က code example က Python ရဲ့ `requests` library ကို သုံးပြီး ဒီ pattern အတိအကျ ပြသထားပါတယ်, endpoint URL နဲ့ message content ကို Ollama-compatible local server ဘယ်ခုအတွက်မဆို ချိန်ညှိသုံးနိုင်ပါတယ်။
ကြိုတင်သတိပေးချက် — ဒါကို ဖွင့်မထားလိုက်ပါနဲ့
default အနေနဲ့ local AI server တစ်ခုက localhost ပေါ်မှာပဲ listen လုပ်လို့ သင့်စက်ရဲ့ အပြင်ဘက်ကနေ ဘာမှ မရောက်နိုင်ပါဘူး။ ဒါပေမယ့် network interface တစ်ခုပေါ်မှာ listen လုပ်အောင် (ဥပမာ 0.0.0.0 ကို bind လုပ်) configure လုပ်လို့ ရပါတယ်, authentication မထည့်ဘဲ ဒါကို လုပ်လိုက်ရင် network တူတူပေါ်က device တခြားတွေ (သို့) machine ကို expose ထားရင် internet ကနေတောင် request ပို့နိုင်ပါတယ်။ local AI server ကို မှန်ကန်စွာ လုံခြုံအောင် လုပ်တာကို နောက်ပိုင်း သင်ခန်းစာတစ်ခုက အသေးစိတ် ရှင်းပြပါလိမ့်မယ်; အခုအတွက်တော့ ‘localhost မဟုတ်တဲ့ တစ်ခုခုကို bind လုပ်တယ်’ ဆိုတာကို default အနေနဲ့ လုပ်သင့်တဲ့ အရာမဟုတ်ဘဲ security နဲ့ ဆိုင်တဲ့ တမင်ဆုံးဖြတ်ချက်အဖြစ် သဘောထားပါ။
အတူတူ စမ်းရေးကြည့်မယ်
import requests
OLLAMA_CHAT_URL = "http://localhost:11434/api/chat"
def summarize_notes(model_name, meeting_notes):
payload = {
"model": model_name,
"messages": [
{
"role": "system",
"content": (
"You are a concise assistant. Summarize the given "
"meeting notes in three bullet points."
),
},
{"role": "user", "content": meeting_notes},
],
"stream": False,
}
try:
response = requests.post(OLLAMA_CHAT_URL, json=payload, timeout=60)
response.raise_for_status()
except requests.exceptions.ConnectionError:
print("Could not reach the local model server. Is Ollama running?")
return None
except requests.exceptions.Timeout:
print("The request timed out waiting for a reply.")
return None
except requests.exceptions.HTTPError as error:
print(f"The server returned an error: {error}")
return None
data = response.json()
return data["message"]["content"]
if __name__ == "__main__":
notes = (
"Team agreed to ship the search feature next sprint. "
"Backend needs the new /api/search endpoint. "
"Design will deliver mockups by Thursday."
)
summary = summarize_notes("llama3.2", notes)
if summary is not None:
print(summary)
Run လုပ်လို့မရနိုင်ပါ
ဒီ code ဟာ ကိုယ်ပိုင် local AI server (ဥပမာ Ollama) ကို ခေါ်သုံးပါတယ် — ဒီ site ရဲ့ browser playground ကနေ visitor ရဲ့ စက်ပေါ်က local server ကို လှမ်းမခေါ်နိုင်ပါ (network access ကန့်သတ်ထားလို့ပါ)။ ကိုယ်တိုင် server run ထားတဲ့ terminal (သို့) file ထဲမှာ ဒီ code ကို run ပြီး စမ်းကြည့်ပါ။
ဒီ code ကို ဒီနေရာမှာ run မထားပါဘူး, local Ollama server run နေဖို့ လိုအပ်လို့ပါ။ Ollama run နေပြီး `llama3.2` pull လုပ်ပြီးသားဆိုရင် `summarize_notes` က note တွေကို `/api/chat` ဆီ ပို့ပြီး server က assistant ရဲ့ reply text ပါတဲ့ `message` field ပါတဲ့ JSON object နဲ့ response ပြန်ပါတယ်; function က ဒီ text ကို return ပြန်ပြီး `print` statement က model generate ထုတ်ခဲ့တဲ့ bullet သုံးချက် summary ကို ပြပါလိမ့်မယ် — အတိအကျ စာလုံးက model အပေါ်မူတည်ပြီး hardcode (သို့) predict လုပ်သင့်တာ မဟုတ်ပါဘူး။ Ollama run မနေရင် `ConnectionError` branch က crash မဖြစ်ဘဲ message တစ်ခု print ထုတ်ပါတယ်။၅ မိနစ် စမ်းကြည့်
`summarize_notes` ကို `style` parameter (ဥပမာ `'three bullet points'` သို့မဟုတ် `'one sentence'`) ထပ်ယူအောင် ပြင်ပါ, 'three bullet points' ကို hardcode မထားဘဲ system message ထဲ interpolate လုပ်ပါ။ ပြီးရင် run မလုပ်ဘဲ `style='one sentence'` နဲ့ သင်ရွေးထားတဲ့ meeting note အတို function ပို့မယ့် request payload ကို ရေးကြည့်ပါ။
သတိလေးတစ်ချက်
local server မစသေးချိန်မှာ connection error ကို handle မလုပ်ဘဲ script ကို helpful မဟုတ်တဲ့ traceback နဲ့ crash ဖြစ်စေခြင်း။
HTTP request မှာ timeout မထား (သို့) တိုတိုသတ်မှတ်ခြင်း — local generation က ပုံမှန် web API call ထက် တရားဝင် အချိန်ပိုကြာနိုင်ပါတယ်။
Ollama API documentation — Local AI / Local LLM