รันจริง และสิ่งที่ได้เรียนรู้

จาก 0 ถึง AI Agent — บันทึกการเรียนรู้ผ่านโปรเจกต์จริง

ตอนที่ 6: รันจริง + สิ่งที่ได้เรียนรู้


มาถึงตอนสุดท้ายแล้ว ตอนนี้เราจะรันโปรแกรมจริง ลองถามคำถาม และสรุปสิ่งที่ได้เรียนรู้จากโปรเจกต์นี้ทั้งหมด


Clone และเตรียม

git clone https://github.com/nipitpongpan/ask-my-docs
cd ask-my-docs
python -m venv .venv
source .venv/bin/activate   # Mac/Linux
# หรือ .venv\Scripts\activate  # Windows
pip install anthropic pydantic python-dotenv

สร้างไฟล์ .env:

ANTHROPIC_API_KEY=sk-ant-api03-ใส่-key-ของคุณ

ใส่เอกสารที่อยากให้ AI อ่าน

สร้างไฟล์ .txt ใดๆ ใส่ไว้ในโฟลเดอร์ docs/ เช่น สร้างไฟล์ python.txt:

Python is a high-level programming language created by Guido van Rossum in 1991.
It is known for its clean syntax and readability.
Python is widely used in web development, data science, and AI.

หรือจะเป็นเอกสารอะไรก็ได้ — นโยบายบริษัท, บันทึกการประชุม, คู่มือการใช้งาน


รันโปรแกรม

python ask.py

จะเห็น:

Docs folder: /home/user/ask-my-docs/docs
Type 'quit' to exit

You:

ลองถาม:

You: What is Python?
Thinking...
  → list_files({})
  → read_file({'filename': 'python.txt'})

Claude: Python is a high-level programming language created by Guido van Rossum in 1991.
Sources:     ['python.txt']
Confidence:  high
In docs:     True

เห็นไหมครับ — มันบอกด้วยว่าเจอจากไฟล์ไหน และมั่นใจแค่ไหน

ลองถามเรื่องที่ไม่มีในเอกสาร:

You: What is the weather today?
Thinking...
  → list_files({})
  → return_answer({'answer': 'I could not find this in the available documents', ...})

Claude: I could not find this in the available documents
Sources:     []
Confidence:  low
In docs:     False

มันไม่แต่งเรื่อง — บอกตรงๆ ว่าไม่มีข้อมูล


สิ่งที่ได้เรียนรู้จริงๆ จากโปรเจกต์นี้

1. Tool Calling ทำงานผ่าน “การสนทนา” ไม่ใช่การรันโค้ดโดยตรง

Claude ไม่ได้รันโค้ด Python ของเรา — มันแค่ส่ง JSON มาบอกว่า “อยากเรียก tool ชื่อนี้ ด้วย input นี้” แล้วเราเป็นคนรัน ผลลัพธ์ก็ส่งกลับไปเป็น text ธรรมดา ความเข้าใจตรงนี้ทำให้ debug ง่ายขึ้นมาก

2. Description ของ tool สำคัญพอๆ กับโค้ด

ครั้งแรกที่ทำ ผมเขียน description สั้นมากแบบ “read a file” — ปรากฏว่า Claude ไม่ค่อยเรียก tool นี้ตามที่ต้องการ พอเพิ่ม description ให้ชัดขึ้น พฤติกรรมเปลี่ยนทันที

3. return_answer เป็น pattern ที่ elegant กว่าการบอกให้ตอบเป็น JSON

เคยลองบอกใน system prompt ว่า “ตอบเป็น JSON format นี้เสมอ” — Claude บางครั้งก็ทำ บางครั้งก็ไม่ทำ แต่พอใช้ tool schema บังคับ format Claude ทำตามได้เกือบ 100%

4. Agent Loop ไม่ได้ซับซ้อนอย่างที่คิด

ก่อนทำโปรเจกต์นี้คิดว่า agent loop จะซับซ้อนมาก แต่จริงๆ มันแค่ “ส่งไป รอ ดู stop_reason ถ้าต้องรัน tool ก็รันแล้วส่งกลับ วนซ้ำ” ทำความเข้าใจครั้งเดียวแล้วจำได้เลย

5. ชื่อไฟล์มีผลต่อการตัดสินใจของ Claude

Claude เลือกไฟล์ที่จะอ่านจากชื่อไฟล์ + preview บรรทัดแรก ถ้าตั้งชื่อไฟล์ว่า doc1.txt, doc2.txt Claude จะงงกว่าตั้งชื่อว่า python-overview.txt, project-guidelines.txt


ขั้นถัดไปที่อยากลอง

ถ้าอยากต่อยอดจากโปรเจกต์นี้:

  • เพิ่ม tool search_files ที่ค้นหาเนื้อหาใน docs แทนที่จะต้องเปิดทีละไฟล์
  • รองรับไฟล์ .pdf หรือ .docx นอกจาก .txt
  • เพิ่ม conversation history ให้ถามต่อเนื่องกันได้

  • จบ Series นี้

    ตั้งแต่ตอนที่ 1 ถึงตอนนี้ เราได้เรียนรู้:

  • ✅ วิธีเชื่อมต่อ Anthropic API
  • ✅ Tool Calling คืออะไรและทำงานยังไง
  • ✅ วิธีนิยาม tool ด้วย schema และ description
  • ✅ Agent Loop ที่วนจนได้คำตอบ
  • ✅ Structured Output ด้วย Pydantic
  • โปรเจกต์นี้เล็กมาก ไม่ถึง 100 บรรทัด แต่ครอบคลุม concept หลักที่ AI Agent ทุกตัวในโลกใช้

    หวังว่าจะเป็นประโยชน์ครับ 🙏


    📚 Series: จาก 0 ถึง AI Agent — บันทึกการเรียนรู้ผ่านโปรเจกต์จริง

    Source code ทั้งหมดอยู่ที่ github.com/nipitpongpan/ask-my-docs


    Posted

    in

    ,

    by

    Tags:

    Comments

    Leave a Reply

    Your email address will not be published. Required fields are marked *