ခဏလေး ဒီလိုပဲ စဉ်းစားကြည့်
ဒီ lesson က Advanced chapter မှာ သင်ခဲ့တဲ့ RAG, embeddings, vector databases, နဲ့ AI agents concept တွေကို ပေါင်းစပ်ပြီး ပိုနက်ရှိုင်းစွာ ကျင့်ကြည့်ဖို့ practice set ဖြစ်ပါတယ်။ Task တွေက single-concept quiz မဟုတ်တော့ပဲ end-to-end flow တစ်ခုလုံးကို စဉ်းစားရတဲ့ level ဖြစ်ပါတယ်— document ကို chunk ခွဲတာကနေ embedding နှိုင်းယှဉ်တာ၊ RAG pipeline ဖွဲ့တာ၊ agent တစ်ခုအတွက် tool decision logic ရေးတာအထိ ဖြစ်ပါတယ်။ Lesson 1 ကနေ ပိုတိုးတက်တဲ့ difficulty ဖြစ်ပြီး၊ built-in library (e.g. LangChain) ကို အားကိုးမနေဘဲ core logic ကို ကိုယ်တိုင်နားလည်အောင် ရေးကြည့်ဖို့ ဒီဇိုင်းလုပ်ထားပါတယ်။
လေ့ကျင့်ခန်းများ
Task 1: paragraph 3 ခုပါတဲ့ document တစ်ခုကို overlap 20% ပါအောင် fixed-size chunk (character 200 လောက်) အဖြစ် ခွဲတဲ့ function တစ်ခု ရေးပါ။ Task 2: cosine similarity function တစ်ခုကို ကိုယ်တိုင် implement ပါ (built-in library မသုံးပါနဲ့)၊ ပြီးရင် vector 3 ခုပေးထားရင် query vector နဲ့ ဘယ်ဟာအနီးဆုံးလဲ ရှာတဲ့ code ရေးပါ။ Task 3: user query တစ်ခုရလာရင် vector database ကနေ top-3 relevant chunk ကို ရှာယူပြီး၊ ဒီ chunk တွေကို system prompt ထဲ context အဖြစ်ထည့်ကာ LLM ကိုခေါ်တဲ့ minimal RAG pipeline (pseudo-function form) ရေးပါ။ Task 4: weather ကိုမေးရင် get_weather tool ကိုသုံး၊ calculation ကိုမေးရင် calculator tool ကိုသုံး၊ ဘာအပိုမှမလိုရင် plain text ဖြေတဲ့ decision logic ပါတဲ့ simple agent router တစ်ခုကို design ပါ။
Code နမူနာ
# Task 2 — implement cosine similarity yourself (no numpy/sklearn shortcuts allowed)
import math
def cosine_similarity(vec_a, vec_b):
dot_product = sum(a * b for a, b in zip(vec_a, vec_b))
magnitude_a = math.sqrt(sum(a * a for a in vec_a))
magnitude_b = math.sqrt(sum(b * b for b in vec_b))
if magnitude_a == 0 or magnitude_b == 0:
return 0.0
return dot_product / (magnitude_a * magnitude_b)
def find_most_similar(query_vector, candidate_vectors):
# candidate_vectors: list of (id, vector) tuples
best_id, best_score = None, -1.0
for cand_id, cand_vector in candidate_vectors:
score = cosine_similarity(query_vector, cand_vector)
if score > best_score:
best_id, best_score = cand_id, score
return best_id, best_score
# Task 3 — minimal RAG pipeline skeleton (fill in the TODOs)
def rag_answer(user_query, vector_store, llm_client):
# TODO 1: embed the user_query
# TODO 2: retrieve top-3 chunks from vector_store using find_most_similar
# TODO 3: build a prompt that injects the retrieved chunks as context
# TODO 4: call llm_client with that prompt and return the answer
pass
# Task 4 — simple agent router
def route_to_tool(user_query):
if "weather" in user_query.lower():
return "get_weather"
if any(op in user_query for op in ["+", "-", "*", "/", "calculate"]):
return "calculator"
return "plain_text_response"Query vector တစ်ခုပေးရင် nearest chunk ID ကို မှန်ကန်စွာ ပြန်ပေးနိုင်ပြီး၊ RAG pipeline ကို document set တစ်ခုနဲ့ run လိုက်ရင် retrieved context ပါတဲ့ context-grounded answer ရရှိသင့်ပါတယ်။၅ မိနစ် စမ်းကြည့်
5 မိနစ်အတွင်း paragraph 2 ခုကို chunk ခွဲပြီး cosine_similarity function ကို sample query vector 1 ခုနဲ့ run ကြည့်ပြီး ဘယ် chunk အနီးဆုံးလဲ ကိုယ်တိုင်စစ်ကြည့်ပါ။
သတိလေးတစ်ချက်
Cosine similarity function ကို embedding dimension မတူတဲ့ vector 2 ခုနဲ့ ခေါ်ရင် error တက်နိုင်ပါတယ်၊ production မှာ dimension mismatch ကို အရင်စစ်ပါ။