ခဏလေး ဒီလိုပဲ စဉ်းစားကြည့်
Advanced chapter မှာ သင်ခဲ့တဲ့ Agents နှင့် Tools concept ကို ဒီနေရာမှာ အသုံးချပါမယ်။ Part 2 ရဲ့ conversational RAG chain ကို "document_search" ဆိုတဲ့ Tool တစ်ခုအဖြစ် wrap လုပ်ပြီး၊ document ထဲမှာ မပါတဲ့ မေးခွန်းများ (ဥပမာ - ရိုးရိုး calculation) အတွက် tool နောက်တစ်ခုကိုပါ ထည့်ကာ Agent တစ်ခု ဆောက်ပါမယ်။ Agent ဟာ user ရဲ့ မေးခွန်းအလိုက် ဘယ် tool ကို ဘယ်အချိန် သုံးရမလဲဆိုတာ ကိုယ်တိုင် ဆုံးဖြတ်နိုင်ပါတယ်။ နောက်ဆုံးအနေနဲ့ production-observability-evaluation chapter က lesson ကို ကိုးကားပြီး try/except error handling နှင့် simple CLI loop ဖြင့် project ကို usable app တစ်ခု အဖြစ် ပြီးမြောက်အောင် polish လုပ်ပါမယ်။ ဒီအဆင့်ပြီးရင် document data ကို search နိုင်ပြီး, follow-up context ကို memory ဖြင့် မှတ်နိုင်ပြီး, tool တွေကို agent ဖြင့် ရွေးချယ်အသုံးပြုနိုင်တဲ့ chatbot တစ်ခု ပြီးစီးသွားမည်။
လက်တွေ့ ဆောက်ကြည့်မယ်
Part 2 ရဲ့ conv_chain ကို function တစ်ခုအဖြစ် wrap ပြီး Tool(name="document_search", func=..., description="user document ထဲက data နှင့် ပတ်သက်တဲ့ မေးခွန်းများကို ဖြေရန် အသုံးပြုပါ") ဖြင့် tool တစ်ခု ဖန်တီးပါ။ Word count/calculator ကဲ့သို့ simple tool နောက်တစ်ခု ထပ်ထည့်ပါ။ initialize_agent (or create_react_agent) ဖြင့် tool 2 ခုစလုံးကို agent ထဲ ပေါင်းထည့်ပြီး verbose=True နှင့် max_iterations သတ်မှတ်ပါ။ try/except ဖြင့် error handling ထည့်ပြီး, while True loop ဖြင့် user input ကို ဆက်တိုက်ဖတ်ကာ "exit" ရိုက်ရင် ရပ်တဲ့ simple CLI အဖြစ် ပြီးမြောက်အောင် ရေးပါ။
Code နမူနာ
from langchain.agents import Tool, initialize_agent, AgentType
def document_qa_func(question: str) -> str:
result = conv_chain.invoke({"question": question})
return result["answer"]
def word_count_func(text: str) -> str:
return f"Word count: {len(text.split())}"
tools = [
Tool(
name="document_search",
func=document_qa_func,
description="User ရဲ့ ကိုယ်ပိုင် document ထဲက data နှင့် ပတ်သက်တဲ့ မေးခွန်းများကို ဖြေရန် အသုံးပြုပါ",
),
Tool(
name="word_counter",
func=word_count_func,
description="ပေးထားသော text ရဲ့ word count ကို ရေတွက်ရန် အသုံးပြုပါ",
),
]
agent = initialize_agent(
tools=tools,
llm=llm,
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
verbose=True,
max_iterations=4,
)
def main():
print("Document Chatbot (ပြီးဆုံးရန် 'exit' ရိုက်ပါ)")
while True:
user_input = input("You: ")
if user_input.strip().lower() == "exit":
break
try:
response = agent.invoke({"input": user_input})
print("Bot:", response["output"])
except Exception as e:
print("Error တစ်ခု ဖြစ်ပွားခဲ့သည်:", e)
if __name__ == "__main__":
main()
Document data နှင့်ပတ်သက်တဲ့ မေးခွန်းများကို document_search tool ဖြင့်လည်းကောင်း, word count ကဲ့သို့ မေးခွန်းများကို word_counter tool ဖြင့်လည်းကောင်း agent ကိုယ်တိုင် ရွေးချယ်ပြီး ဖြေပေးနိုင်သော CLI chatbot တစ်ခု ရရှိမည်။၅ မိနစ် စမ်းကြည့်
5 မိနစ်အတွင်း သင့်ဟာ tool တတိယခု (ဥပမာ - လက်ရှိရက်စွဲကို ပြန်ပေးတဲ့ current_date tool) ကို ရေးပြီး agent ရဲ့ tools list ထဲ ထည့်ကြည့်ပါ။
သတိလေးတစ်ချက်
Production app တစ်ခုမှာ agent ရဲ့ output ကို တိုက်ရိုက် user ဆီ trust မလုပ်ဘဲ error handling, max_iterations, နှင့် LangSmith ကဲ့သို့ observability tool ဖြင့် trace မှတ်ခြင်းတို့ကို အမြဲထည့်သွင်းစဉ်းစားသင့်သည်။