เมื่อถูกเรนเดอร์ บล็อกนี้จะปรากฏเป็นตัวอย่างโค้ดมาตรฐาน อย่างไรก็ตาม ในระหว่างการทดสอบ มันจะถูกรันเป็นโค้ด Python ปกติ ลักษณะคู่ขนานนี้ทำให้มั่นใจได้ว่าเอกสารยังคงสะอาดสำหรับผู้อ่าน ในขณะที่ให้ตัวอย่างที่แข็งแกร่งและสามารถทดสอบได้สำหรับนักพัฒนา วิธีการนี้มีผลกระทบอย่างยิ่งต่อโดเมนที่ซับซ้อน เช่น AI ซึ่งตัวอย่างมักเกี่ยวข้องกับการโหลดโมเดลที่ซับซ้อนและขั้นตอนการอนุมาน
## การผสานรวมอย่างราบรื่นกับ `pytest` และคุณสมบัติขั้นสูง
ความแตกต่างที่สำคัญของแนวทางของ Hugging Face คือการผสานรวมอย่างราบรื่นกับเฟรมเวิร์กการทดสอบที่ทันสมัย โดยเฉพาะ `pytest` เมื่อติดตั้ง `hf-doc-builder` แล้ว `pytest` สามารถค้นหาและรันบล็อกที่สามารถรันได้ภายในไฟล์ Markdown โดยอัตโนมัติ โดยถือว่าแต่ละบล็อกเป็นรายการทดสอบมาตรฐาน ซึ่งหมายความว่าตัวอย่างเอกสารสามารถเข้าร่วมในโครงสร้างพื้นฐานการทดสอบที่มีอยู่ของโปรเจกต์ได้อย่างเต็มที่ โดยใช้ประโยชน์จากคุณสมบัติอันทรงพลังของ `pytest` เช่น การยืนยัน (assertions), ฟิกซ์เจอร์ (fixtures), เครื่องมือดีบัก และการรายงานที่ครอบคลุม
### วิวัฒนาการของเอกสารที่สามารถรันได้: `doctest` vs. `doc-builder`
| คุณสมบัติ | `doctest` (แบบดั้งเดิม) | `doc-builder` (Modern Runnable Markdown) |
| :--------------------------- | :---------------------------------------------------- | :----------------------------------------------------- |
| **แนวทางการทดสอบ** | ฝังการทดสอบเป็นเซสชันล่ามในเอกสาร | ถือว่าส่วนย่อยของเอกสารเป็นโค้ด Python ปกติสำหรับการทดสอบ |
| **การผสานรวม** | โมดูลไลบรารีมาตรฐาน | ปลั๊กอิน `pytest` สำหรับการผสานรวมที่ราบรื่น |
| **ไวยากรณ์การทดสอบ** | พรอมต์ `>>>` การจับคู่เอาต์พุตที่คาดหวัง | โค้ด Python มาตรฐาน การยืนยัน `pytest` |
| **ความยืดหยุ่น** | จำกัด การจับคู่เอาต์พุตที่เปราะบาง | สูง รองรับการทดสอบที่ซับซ้อน, ดีคอร์เรเตอร์, การดีบัก |
| **ความสะอาดของเอกสาร** | สามารถทำให้เอกสารรกด้วยกลไกการทดสอบ | รักษากระดาษที่สะอาดด้วยคำสั่งที่ซ่อนอยู่ |
| **การดีบัก** | การเปรียบเทียบสตริง การตรวจสอบโดยตรงน้อยลง | การดีบัก Python มาตรฐาน tracebacks เต็มรูปแบบ |
| **การตั้งค่า/การยกเลิก** | สามารถเพิ่ม 'เสียงรบกวน' ให้กับตัวอย่าง | จัดการบริบทได้อย่างมีประสิทธิภาพด้วยบล็อกต่อเนื่อง |
| **แหล่งที่มาของความจริง** | รูปแบบเอกสารและการทดสอบที่ฝังอยู่ | แหล่งที่มาของ Markdown ทดสอบผ่านการรัน Python มาตรฐาน |
`doc-builder` ยังแนะนำ **บล็อกต่อเนื่อง** (continuation blocks) ซึ่งเป็นคุณสมบัติที่สำคัญสำหรับบทช่วยสอนหลายขั้นตอน สิ่งเหล่านี้ช่วยให้ผู้เขียนสามารถแบ่งตัวอย่างออกเป็นส่วนย่อยที่มองเห็นได้หลายส่วน เช่น `runnable:test_basic` ตามด้วย `runnable:test_basic:2` ที่สำคัญ บล็อกเหล่านี้ใช้บริบทการทำงานเดียวกันในระหว่างการทดสอบ ทำให้การไหลของคำแนะนำเป็นไปอย่างเป็นธรรมชาติโดยไม่บังคับให้โค้ดทั้งหมดอยู่ในบล็อกเดียวยาวๆ ความยืดหยุ่นนี้มีความสำคัญอย่างยิ่งในการแนะนำผู้ใช้ผ่านการใช้งานโมเดล AI ที่ซับซ้อนหรือไปป์ไลน์การประมวลผลข้อมูล
ตัวอย่างเช่น เวิร์กโฟลว์การพัฒนา AI agent อาจเกี่ยวข้องกับหลายขั้นตอน: การกำหนดเครื่องมือของ agent, การเริ่มต้น agent, และการรันคิวรี บล็อกต่อเนื่องช่วยให้แต่ละขั้นตอนเหล่านี้สามารถนำเสนอได้อย่างชัดเจนในส่วนเอกสารที่แยกจากกัน ในขณะที่ถูกรันเป็นลำดับการทดสอบที่รวมกันเป็นหนึ่งเดียว คล้ายกับวิธีที่เวิร์กโฟลว์ของ agent ขั้นสูงคือ [การนำ AI ที่มีความสามารถเชิงตัวแทนไปใช้งานจริง: ส่วนที่ 1](/th/operationalizing-agentic-ai-part-1-a-stakeholders-guide)
## การรักษาเอกสารให้สะอาดในขณะที่รับประกันการทดสอบที่แข็งแกร่ง
หนึ่งในโซลูชันที่สง่างามที่สุดของ `doc-builder` คือความสามารถในการรักษาเอกสารที่เรนเดอร์ให้สะอาด แม้ว่าแหล่งที่มาของ Markdown จะมีคำสั่งเฉพาะสำหรับการทดสอบ นักพัฒนาสามารถฝังความคิดเห็นเช่น `# doc-builder: hide` สำหรับบรรทัดที่สามารถรันได้แต่ไม่ควรปรากฏในเอกสาร หรือ `# doc-builder: ignore-bare-assert` สำหรับการยืนยันที่เป็นส่วนหนึ่งของการทดสอบแต่ไม่ควรแสดงความคิดเห็นนั้น ในทำนองเดียวกัน ดีคอร์เรเตอร์ `pytest` (`# pytest-decorator: ...`) จะถูกตัดออกในระหว่างการเรนเดอร์
สิ่งนี้ทำให้มั่นใจได้ว่าเอกสารยังคงมุ่งเน้นไปที่การสอนและความชัดเจน โดยไม่ถูกรบกวนด้วยส่วนประกอบการทดสอบที่ซ้ำซ้อน ผู้ใช้จะเห็นเฉพาะโค้ดที่เกี่ยวข้อง ในขณะที่ระบบพื้นฐานรับประกันการทำงานของมัน ความสมดุลนี้มีความสำคัญอย่างยิ่งสำหรับเอกสารเครื่องมือสำหรับนักพัฒนา ซึ่งทั้งความสวยงามและความถูกต้องสมบูรณ์เป็นสิ่งสำคัญที่สุด
## ผลกระทบต่อโครงการ AI ขนาดใหญ่และอื่น ๆ
สำหรับที่เก็บโค้ดขนาดใหญ่เช่น Hugging Face's Transformers ที่มีหน้าเอกสารหลายร้อยหน้าและตัวอย่างนับพัน คุณสมบัตินี้เป็นการเปลี่ยนแปลงครั้งใหญ่ ช่วยให้การป้องกันความคลาดเคลื่อนของเอกสารเป็นไปโดยอัตโนมัติ ซึ่งเป็นปัญหาที่จะต้องใช้ความพยายามด้วยตนเองอย่างมหาศาล หรือนำไปสู่กระแสของตัวอย่างที่เสียอยู่ตลอดเวลา เอกสารที่สามารถรันได้ช่วยให้เอกสารและโค้ดเบสสอดคล้องกัน รักษาความน่าเชื่อถือในขนาดที่การตรวจสอบด้วยตนเองไม่สามารถทำได้จริง สิ่งนี้สอดคล้องกับความพยายามที่กว้างขึ้นในชุมชน AI เพื่อ [การประเมิน AI Agent สำหรับการใช้งานจริง](/th/evaluating-ai-agents-for-production-a-practical-guide-to-strands-evals) อย่างเข้มงวดและรับประกันความน่าเชื่อถือ
ด้วยการนำเอกสารที่สามารถรันได้เข้าสู่ยุคสมัยใหม่ของ `pytest` และไปป์ไลน์ CI/CD ที่ซับซ้อน Hugging Face ได้แสดงให้เห็นถึงความมุ่งมั่นอันแข็งแกร่งต่อประสบการณ์นักพัฒนาและคุณภาพโค้ด เป้าหมายยังคงเหมือนเดิมเมื่อกว่าสองทศวรรษที่แล้ว: ตัวอย่างเอกสารควรทำงาน แต่ตอนนี้ พวกมันไม่เพียงแค่แสดงให้เห็นว่าโค้ด *ควร* ทำงานอย่างไร แต่ยัง *พิสูจน์* อย่างต่อเนื่องว่ามันทำงานได้จริง ซึ่งส่งเสริมระบบนิเวศที่น่าเชื่อถือและไว้วางใจได้มากขึ้นสำหรับการพัฒนา AI
คำถามที่พบบ่อย
What is the core problem Hugging Face's runnable Markdown addresses?
Hugging Face's runnable Markdown addresses the pervasive problem of 'documentation drift,' where code examples in documentation become outdated and silently break as libraries and APIs evolve. This leads to user frustration and diminishes the credibility of the documentation. By making documentation examples runnable and testable, the doc-builder ensures that these snippets are continuously validated against the codebase, guaranteeing that they always work as advertised. This proactive approach prevents broken examples, enhances user trust, and improves the overall developer experience by providing reliable resources.
How does runnable Markdown differ from Python's traditional `doctest` module?
While both `doctest` and runnable Markdown aim for executable documentation, they differ significantly in their approach. `doctest` embeds tests directly into documentation syntax, requiring examples to mirror interactive interpreter sessions with expected output. This often leads to documentation being cluttered with test mechanics. Hugging Face's runnable Markdown, in contrast, treats documentation snippets as normal Python code living within Markdown files. It integrates seamlessly with modern testing frameworks like `pytest`, allowing for complex assertions, debugging, and standard test infrastructure. This separation of concerns ensures documentation remains clean and readable, while testing remains powerful and flexible, avoiding the limitations of `doctest`'s brittle output matching and verbose setup/teardown.
What are 'continuation blocks' in Hugging Face's `doc-builder`?
Continuation blocks are a powerful feature in Hugging Face's `doc-builder` that allow authors to split complex code examples or tutorials across multiple visible Markdown snippets while maintaining a shared execution context during testing. This means that a setup defined in one runnable block can be reused and built upon in a subsequent block, without forcing the documentation to present everything as one long, monolithic code fence. For example, `runnable:test_basic` can define initial variables, and `runnable:test_basic:2` can then use those variables. This enhances readability and instructional flow in documentation, making it easier to present multi-step processes without sacrificing the integrity of the underlying testable code.
How does `doc-builder` integrate with existing testing frameworks like `pytest`?
Hugging Face's `doc-builder` integrates natively with `pytest`, transforming runnable Markdown blocks into standard `pytest` test items. With `hf-doc-builder` installed, `pytest` automatically discovers and executes these blocks within Markdown files. This integration means that documentation examples can leverage the full power of `pytest`, including its assertion mechanisms, fixtures, decorators, and debugging tools. Failures appear as normal test failures with comprehensive tracebacks, allowing developers to debug effectively. This approach avoids the need for a special-purpose testing mini-language, embedding documentation tests directly into the project's existing, robust test infrastructure.
How does `doc-builder` ensure documentation remains clean despite embedded test logic?
A key design principle of `doc-builder` is to prevent test mechanics from polluting the user-facing documentation. Authors can embed test-only directives, such as `# pytest-decorator: transformers.testing_utils.slow` or `# doc-builder: hide` for lines that should be executable but not displayed, directly within the Markdown source. When the documentation is rendered, `doc-builder` intelligently strips these directives and comments, presenting a clean, readable code snippet to the user. This allows developers to write comprehensive tests alongside their examples without compromising the clarity and brevity expected of good documentation, maintaining a clear separation between the source code for testing and the rendered content for users.
What are the benefits of runnable documentation for large AI projects like Hugging Face Transformers?
For large AI projects such as Hugging Face Transformers, which involve extensive documentation and thousands of code examples, runnable documentation offers immense benefits. It drastically reduces 'documentation drift' by continuously validating examples against the evolving codebase, ensuring they remain accurate and functional. This prevents user frustration caused by broken examples and builds trust in the documentation's reliability. By integrating with `pytest`, it allows these projects to manage documentation tests within their existing CI/CD pipelines, making manual review of examples unnecessary at scale. This automated validation is crucial for maintaining the quality and usability of documentation in rapidly developing and complex software ecosystems.
Can runnable Markdown be adopted by other projects outside of Hugging Face?
Yes, the principles and mechanisms behind Hugging Face's runnable Markdown, particularly its integration with standard Python testing tools like `pytest` and its focus on separating testing logic from displayed documentation, are highly applicable and beneficial for any software project. While the `doc-builder` itself is specific to Hugging Face, the underlying ideas represent a best practice in developer tools. Other projects can implement similar systems using existing tools or adapt `doc-builder`'s concepts to ensure their documentation examples are continuously tested and reliable. This approach is a general solution to a common problem across the software development landscape, making documentation more robust and trustworthy.
อัปเดตข่าวสาร
รับข่าว AI ล่าสุดในกล่องจดหมายของคุณ
