Files
xSaherelmWorkspace/Documents/Docs/Developer/Force LLMs to Generate Files.html
saherelm b2c4e7c767 - حذف چهار context قدیمی مربوط به 2026-11-06.
- افزودن contextهای جدید 2026-11-07 برای xAiApi، xAiMcpServer، xAiModels و xAiService.
2026-11-07 22:51:41 +03:30

1322 lines
48 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>آموزش مدل برای تولید فایل‌های خروجی | راهنمای جامع فنی</title>
<style>
@import url('https://fonts.googleapis.com/css2?family=Vazirmatn:wght@300;400;500;600;700;800&display=swap');
:root {
--primary: #1a365d;
--primary-light: #2c5282;
--accent: #c9a961;
--accent-dark: #a8893f;
--bg: #faf9f6;
--card-bg: #ffffff;
--text: #1a202c;
--text-muted: #4a5568;
--border: #e2e8f0;
--success: #38a169;
--danger: #e53e3e;
--warning: #dd6b20;
--shadow: 0 4px 6px rgba(0,0,0,0.05), 0 10px 30px rgba(26,54,93,0.08);
}
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
font-family: 'Vazirmatn', Tahoma, sans-serif;
background: var(--bg);
color: var(--text);
line-height: 1.9;
font-size: 15px;
padding: 30px 15px;
}
.document-container {
max-width: 950px;
margin: 0 auto;
background: var(--card-bg);
box-shadow: var(--shadow);
border-radius: 12px;
overflow: hidden;
}
.document-header {
background: linear-gradient(135deg, var(--primary) 0%, var(--primary-light) 100%);
color: white;
padding: 50px 40px 40px;
position: relative;
border-bottom: 4px solid var(--accent);
}
.document-header::before {
content: '';
position: absolute;
top: 0; right: 0;
width: 200px; height: 200px;
background: radial-gradient(circle, rgba(201,169,97,0.15) 0%, transparent 70%);
border-radius: 50%;
transform: translate(50%, -50%);
}
.header-top {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 30px;
position: relative;
z-index: 1;
}
.company-badge {
display: flex;
align-items: center;
gap: 12px;
}
.company-logo {
width: 60px; height: 60px;
background: var(--accent);
border-radius: 12px;
box-shadow: 0 4px 12px rgba(0,0,0,0.2);
}
.doc-meta {
text-align: left;
font-size: 12px;
opacity: 0.9;
}
.doc-meta div { margin-bottom: 3px; }
.document-title {
text-align: center;
position: relative;
z-index: 1;
padding: 20px 0;
}
.document-title .label {
display: inline-block;
background: rgba(201,169,97,0.2);
color: var(--accent);
padding: 4px 16px;
border-radius: 20px;
font-size: 12px;
font-weight: 600;
margin-bottom: 15px;
border: 1px solid rgba(201,169,97,0.4);
}
.document-title h1 {
font-size: 28px;
font-weight: 800;
margin-bottom: 10px;
letter-spacing: -0.5px;
}
.document-title h2 {
font-size: 16px;
font-weight: 400;
opacity: 0.9;
}
.document-body {
padding: 40px;
}
.intro-text {
background: linear-gradient(to left, #f7fafc, #edf2f7);
border-right: 4px solid var(--accent);
padding: 20px 25px;
border-radius: 8px;
margin-bottom: 35px;
font-size: 14px;
color: var(--text-muted);
}
.section {
margin-bottom: 40px;
page-break-inside: avoid;
}
.section-header {
display: flex;
align-items: center;
gap: 15px;
padding-bottom: 12px;
border-bottom: 2px solid var(--border);
margin-bottom: 20px;
}
.section-number {
width: 50px; height: 50px;
background: linear-gradient(135deg, var(--primary), var(--primary-light));
color: white;
border-radius: 12px;
display: flex;
align-items: center;
justify-content: center;
font-weight: 800;
font-size: 20px;
flex-shrink: 0;
box-shadow: 0 4px 10px rgba(26,54,93,0.2);
}
.section-title { flex: 1; }
.section-title h3 {
font-size: 19px;
font-weight: 700;
color: var(--primary);
margin-bottom: 2px;
}
.section-title .subtitle {
font-size: 12px;
color: var(--text-muted);
}
.section-content {
padding-right: 10px;
}
.section-content p {
margin-bottom: 12px;
text-align: justify;
text-indent: 20px;
}
.section-content p.no-indent { text-indent: 0; }
.highlight-box {
background: linear-gradient(to left, #fffaf0, #fef5e7);
border: 1px solid #f6e05e;
border-right: 4px solid var(--accent);
padding: 18px 22px;
border-radius: 8px;
margin: 20px 0;
font-size: 14px;
}
.highlight-box strong {
color: var(--accent-dark);
}
.info-box {
background: linear-gradient(to left, #ebf8ff, #e6f6ff);
border: 1px solid #90cdf4;
border-right: 4px solid var(--primary-light);
padding: 18px 22px;
border-radius: 8px;
margin: 20px 0;
font-size: 14px;
}
.success-box {
background: linear-gradient(to left, #f0fff4, #e6fffa);
border: 1px solid #9ae6b4;
border-right: 4px solid var(--success);
padding: 18px 22px;
border-radius: 8px;
margin: 20px 0;
font-size: 14px;
}
.warning-box {
background: linear-gradient(to left, #fffaf0, #fef5e7);
border: 1px solid #fbd38d;
border-right: 4px solid var(--warning);
padding: 18px 22px;
border-radius: 8px;
margin: 20px 0;
font-size: 14px;
}
.styled-table {
width: 100%;
border-collapse: collapse;
margin: 20px 0;
background: white;
border-radius: 8px;
overflow: hidden;
box-shadow: 0 2px 8px rgba(0,0,0,0.05);
}
.styled-table thead {
background: linear-gradient(135deg, var(--primary), var(--primary-light));
color: white;
}
.styled-table th {
padding: 14px 16px;
text-align: right;
font-weight: 600;
font-size: 13px;
}
.styled-table td {
padding: 13px 16px;
border-bottom: 1px solid var(--border);
font-size: 13px;
}
.styled-table tbody tr:last-child td { border-bottom: none; }
.styled-table tbody tr:nth-child(even) { background: #f8fafc; }
.styled-table .highlight-row {
background: #fffaf0 !important;
font-weight: 600;
}
.comparison-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 20px;
margin: 25px 0;
}
.comparison-card {
background: #f8fafc;
border: 2px solid var(--border);
border-radius: 10px;
padding: 20px;
position: relative;
}
.comparison-card.recommended {
border-color: var(--success);
background: linear-gradient(to left, #f0fff4, #e6fffa);
}
.comparison-card .card-badge {
position: absolute;
top: -10px;
right: 20px;
background: var(--success);
color: white;
padding: 3px 12px;
border-radius: 15px;
font-size: 11px;
font-weight: 600;
}
.comparison-card h4 {
font-size: 16px;
font-weight: 700;
color: var(--primary);
margin-bottom: 15px;
padding-bottom: 10px;
border-bottom: 2px solid var(--border);
}
.comparison-card.recommended h4 {
border-bottom-color: var(--success);
}
.comparison-card ul {
list-style: none;
padding: 0;
}
.comparison-card ul li {
padding: 8px 0;
border-bottom: 1px solid var(--border);
font-size: 13px;
display: flex;
justify-content: space-between;
}
.comparison-card ul li:last-child { border-bottom: none; }
.comparison-card ul li .value {
font-weight: 600;
color: var(--primary);
}
.quote-box {
background: linear-gradient(135deg, #f7fafc, #edf2f7);
border-right: 4px solid var(--accent);
padding: 20px 25px;
border-radius: 8px;
margin: 20px 0;
position: relative;
}
.quote-box::before {
content: '"';
position: absolute;
top: -10px;
right: 15px;
font-size: 60px;
color: var(--accent);
opacity: 0.3;
font-family: Georgia, serif;
}
.quote-box p {
font-style: italic;
color: var(--text-muted);
margin-bottom: 10px;
text-indent: 0 !important;
}
.quote-box .source {
font-size: 12px;
color: var(--text-muted);
text-align: left;
font-style: normal;
}
.stats-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 15px;
margin: 25px 0;
}
.stat-card {
background: linear-gradient(135deg, var(--primary), var(--primary-light));
color: white;
padding: 20px;
border-radius: 10px;
text-align: center;
box-shadow: 0 4px 12px rgba(26,54,93,0.2);
}
.stat-card .stat-value {
font-size: 24px;
font-weight: 800;
margin-bottom: 5px;
color: var(--accent);
line-height: 1.3;
}
.stat-card .stat-label {
font-size: 12px;
opacity: 0.9;
}
.timeline {
position: relative;
padding: 20px 0;
margin: 25px 0;
}
.timeline::before {
content: '';
position: absolute;
right: 50%;
top: 0;
bottom: 0;
width: 3px;
background: var(--border);
transform: translateX(50%);
}
.timeline-item {
display: flex;
margin-bottom: 20px;
position: relative;
}
.timeline-item:nth-child(odd) {
flex-direction: row-reverse;
}
.timeline-content {
width: 45%;
background: #f8fafc;
border: 1px solid var(--border);
border-radius: 8px;
padding: 15px;
position: relative;
}
.timeline-item:nth-child(odd) .timeline-content {
margin-left: 5%;
}
.timeline-item:nth-child(even) .timeline-content {
margin-right: 5%;
}
.timeline-dot {
position: absolute;
right: 50%;
top: 20px;
width: 16px;
height: 16px;
background: var(--accent);
border: 3px solid white;
border-radius: 50%;
transform: translateX(50%);
box-shadow: 0 2px 6px rgba(0,0,0,0.2);
z-index: 1;
}
.timeline-content h5 {
font-size: 14px;
font-weight: 700;
color: var(--primary);
margin-bottom: 8px;
}
.timeline-content p {
font-size: 12px;
color: var(--text-muted);
text-indent: 0 !important;
margin-bottom: 0 !important;
}
.reference-list {
list-style: none;
padding: 0;
margin: 20px 0;
}
.reference-list li {
background: #f8fafc;
border-right: 3px solid var(--primary-light);
padding: 15px 20px;
margin-bottom: 10px;
border-radius: 6px;
font-size: 13px;
}
.reference-list li strong {
color: var(--primary);
display: block;
margin-bottom: 5px;
}
.reference-list li .ref-source {
font-size: 11px;
color: var(--text-muted);
margin-top: 5px;
}
.conclusion-box {
background: linear-gradient(135deg, var(--primary), var(--primary-light));
color: white;
padding: 30px;
border-radius: 12px;
margin: 30px 0;
text-align: center;
box-shadow: 0 8px 20px rgba(26,54,93,0.3);
}
.conclusion-box h3 {
font-size: 22px;
font-weight: 800;
margin-bottom: 15px;
color: var(--accent);
}
.conclusion-box p {
font-size: 14px;
line-height: 1.8;
text-indent: 0 !important;
margin-bottom: 15px;
}
.conclusion-box .highlight {
background: rgba(201,169,97,0.2);
padding: 15px 20px;
border-radius: 8px;
margin-top: 20px;
border: 1px solid rgba(201,169,97,0.4);
}
.document-footer {
background: var(--primary);
color: white;
padding: 25px 40px;
text-align: center;
font-size: 12px;
margin-top: 40px;
}
.document-footer .footer-brand {
font-weight: 700;
color: var(--accent);
margin-bottom: 5px;
font-size: 14px;
}
.footer-divider {
width: 60px;
height: 2px;
background: var(--accent);
margin: 10px auto;
}
.page-info {
text-align: center;
padding: 15px;
font-size: 11px;
color: var(--text-muted);
border-top: 1px solid var(--border);
margin-top: 30px;
}
/* --- Code Blocks --- */
.code-block {
background: #1a202c;
color: #e2e8f0;
border-radius: 8px;
padding: 18px 20px;
margin: 18px 0;
direction: ltr;
text-align: left;
font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
font-size: 12.5px;
line-height: 1.7;
overflow-x: auto;
position: relative;
box-shadow: 0 4px 12px rgba(0,0,0,0.15);
border-right: 4px solid var(--accent);
}
.code-block .code-label {
display: block;
color: var(--accent);
font-size: 11px;
font-weight: 700;
margin-bottom: 10px;
letter-spacing: 0.5px;
font-family: 'Vazirmatn', Tahoma, sans-serif;
direction: rtl;
text-align: right;
}
.code-block .cm { color: #6b7280; }
.code-block .kw { color: #f6ad55; }
.code-block .st { color: #9ae6b4; }
.code-block .fn { color: #90cdf4; }
.inline-code {
background: #edf2f7;
color: var(--primary);
padding: 1px 6px;
border-radius: 4px;
font-family: 'Consolas', monospace;
font-size: 12.5px;
direction: ltr;
display: inline-block;
}
.flow-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 15px;
margin: 25px 0;
}
.flow-card {
background: #f8fafc;
border: 2px solid var(--border);
border-top: 4px solid var(--primary-light);
border-radius: 10px;
padding: 18px;
text-align: center;
}
.flow-card .flow-icon {
width: 44px; height: 44px;
margin: 0 auto 12px;
background: linear-gradient(135deg, var(--primary), var(--primary-light));
color: white;
border-radius: 10px;
display: flex;
align-items: center;
justify-content: center;
font-weight: 800;
font-size: 18px;
}
.flow-card h5 {
font-size: 14px;
font-weight: 700;
color: var(--primary);
margin-bottom: 8px;
}
.flow-card p {
font-size: 12px;
color: var(--text-muted);
text-indent: 0 !important;
margin-bottom: 0 !important;
text-align: center;
}
@media print {
body { background: white; padding: 0; }
.document-container { box-shadow: none; border-radius: 0; }
.section { page-break-inside: avoid; }
}
@media (max-width: 768px) {
.document-body { padding: 25px 20px; }
.document-header { padding: 30px 20px; }
.comparison-grid { grid-template-columns: 1fr; }
.stats-grid { grid-template-columns: 1fr; }
.flow-grid { grid-template-columns: 1fr; }
.header-top { flex-direction: column; gap: 15px; }
.doc-meta { text-align: center; }
.timeline::before { right: 18px; }
.timeline-item,
.timeline-item:nth-child(odd) { flex-direction: row-reverse; }
.timeline-content,
.timeline-item:nth-child(odd) .timeline-content,
.timeline-item:nth-child(even) .timeline-content {
width: calc(100% - 45px);
margin-right: 45px;
margin-left: 0;
}
.timeline-dot { right: 18px; }
}
</style>
</head>
<body>
<div class="document-container">
<header class="document-header">
<div class="header-top">
<div class="company-badge">
<div class="company-logo"></div>
</div>
<div class="doc-meta">
<div>شماره مستند: GUIDE-1405-014</div>
<div>نسخه: ۱.۰</div>
<div>تاریخ تهیه: شهریور ۱۴۰۵</div>
<div>طبقه‌بندی: عمومی</div>
</div>
</div>
<div class="document-title">
<span class="label">TECHNICAL GUIDE</span>
<h1>آموزش مدل برای تولید فایل‌های خروجی</h1>
<h2>ورد، PDF، اکسل و فراتر از آن — معماری، روش‌ها و نقشه راه پیاده‌سازی</h2>
</div>
</header>
<div class="document-body">
<div class="intro-text">
<strong>پرسش اصلی:</strong><br>
«چطور می‌شود یک مدل را آموزش داد تا بتواند فایل‌های مختلفی را به‌عنوان خروجی تولید کند؟ مثلاً فایل Word، PDF یا Excel.»<br><br>
این مستند، پاسخ فنی و عملی این پرسش را در قالب یک معماری سه‌لایه، سه رویکرد اصلی، روش‌های آموزش و تنظیم مدل، و نقشه راه گام‌به‌گام پیاده‌سازی ارائه می‌دهد.
</div>
<div class="section">
<div class="section-header">
<div class="section-number">۱</div>
<div class="section-title">
<h3>خلاصه اجرایی</h3>
<div class="subtitle">Executive Summary</div>
</div>
</div>
<div class="section-content">
<p>پیش از هر چیز باید یک تصور غلط رایج را اصلاح کرد: مدل زبانی «فایل» تولید نمی‌کند. فایل Word یا Excel یک ساختار دودویی/فشرده‌شده (ZIP با XML داخلی) است که مدل زبانی به‌طور مستقیم و قابل‌اتکا نمی‌تواند آن را بنویسد. کاری که مدل واقعاً انجام می‌دهد، تولید <strong>محتوای ساختاریافته</strong> یا <strong>کد</strong> است؛ سپس یک لایه اجرایی (Runtime) آن را به فایل واقعی تبدیل می‌کند.</p>
<div class="stats-grid">
<div class="stat-card">
<div class="stat-value">۳<br>لایه</div>
<div class="stat-label">مدل + ابزار + اعتبارسنجی</div>
</div>
<div class="stat-card">
<div class="stat-value">۳<br>رویکرد</div>
<div class="stat-label">تولید کد، Tool Calling، قالب‌محور</div>
</div>
<div class="stat-card">
<div class="stat-value">۵<br>سطح آموزش</div>
<div class="stat-label">از پرامپت تا Fine-tuning</div>
</div>
</div>
<div class="highlight-box">
<strong>نتیجه کلیدی:</strong> برای تولید فایل، به «آموزش دادن مدل برای نوشتن فایل» نیازی نیست. کافی است مدل را روی تولید <strong>داده ساختاریافته (JSON) یا کد کتابخانه‌ای</strong> آموزش دهید و یک لایه اجرایی امن در کنار آن قرار دهید. این جداسازی، هم دقت را بالا می‌برد و هم امنیت را تضمین می‌کند.
</div>
</div>
</div>
<div class="section">
<div class="section-header">
<div class="section-number">۲</div>
<div class="section-title">
<h3>مدل ذهنی درست: معماری سه‌لایه</h3>
<div class="subtitle">The Three-Layer Architecture</div>
</div>
</div>
<div class="section-content">
<p>هر سیستم قابل‌اتکا برای تولید فایل، از سه لایه مستقل تشکیل می‌شود. تفکیک این لایه‌ها، مهم‌ترین تصمیم معماری است:</p>
<div class="flow-grid">
<div class="flow-card">
<div class="flow-icon">۱</div>
<h5>لایه تصمیم (مدل)</h5>
<p>محتوای فایل را می‌فهمد، ساختار می‌دهد و به JSON یا کد تبدیل می‌کند.</p>
</div>
<div class="flow-card">
<div class="flow-icon">۲</div>
<h5>لایه اجرا (Runtime)</h5>
<p>با کتابخانه‌های تخصصی، داده را به فایل واقعی تبدیل و ذخیره می‌کند.</p>
</div>
<div class="flow-card">
<div class="flow-icon">۳</div>
<h5>لایه اعتبارسنجی</h5>
<p>صحت ساختار، باز شدن فایل، و کیفیت بصری خروجی را بررسی می‌کند.</p>
</div>
</div>
<table class="styled-table">
<thead>
<tr>
<th>لایه</th>
<th>وظیفه</th>
<th>ابزار نمونه</th>
<th>ریسک در صورت حذف</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>تصمیم</strong></td>
<td>تولید محتوا و ساختار</td>
<td>LLM + JSON Schema</td>
<td>محتوای بی‌ربط یا ناقص</td>
</tr>
<tr>
<td><strong>اجرا</strong></td>
<td>تبدیل داده به فایل</td>
<td>python-docx، openpyxl، WeasyPrint</td>
<td>فایل خراب یا غیرقابل‌باز شدن</td>
</tr>
<tr>
<td><strong>اعتبارسنجی</strong></td>
<td>کنترل کیفیت و صحت</td>
<td>Pydantic، بازخوانی فایل، تست بصری</td>
<td>خطای خاموش و افت اعتماد کاربر</td>
</tr>
<tr class="highlight-row">
<td><strong>نتیجه</strong></td>
<td colspan="3"><strong>سیستمی پایدار، قابل‌تست و قابل‌توسعه برای هر فرمت خروجی</strong></td>
</tr>
</tbody>
</table>
<div class="info-box">
<strong>💡 قاعده طلایی:</strong> هرگز به مدل اجازه ندهید بایت‌های یک فایل را «حدس بزند». مدل باید فقط <em>بگوید چه چیزی</em> در فایل باشد؛ <em>چگونگی ساخت فایل</em> کار کد است.
</div>
</div>
</div>
<div class="section">
<div class="section-header">
<div class="section-number">۳</div>
<div class="section-title">
<h3>سه رویکرد اصلی تولید فایل</h3>
<div class="subtitle">Code Generation · Tool Calling · Template-Based</div>
</div>
</div>
<div class="section-content">
<p>سه راهبرد متداول برای تبدیل خروجی مدل به فایل وجود دارد که هر کدام مزایا و محدودیت‌های خود را دارند:</p>
<h4 style="margin-top: 20px; color: var(--primary); font-size: 16px;">الف) تولید کد (Code Generation)</h4>
<p>مدل یک اسکریپت پایتون می‌نویسد که فایل را می‌سازد؛ سپس کد در محیط ایزوله اجرا می‌شود. انعطاف‌پذیرترین روش است، اما به سندباکس امن نیاز دارد.</p>
<div class="code-block">
<span class="code-label">نمونه: تولید فایل Word با python-docx</span>
<span class="kw">from</span> docx <span class="kw">import</span> Document
<span class="kw">from</span> docx.shared <span class="kw">import</span> Pt
doc = Document()
doc.add_heading(<span class="st">'گزارش ماهانه فروش'</span>, level=<span class="st">1</span>)
doc.add_paragraph(<span class="st">'این گزارش توسط مدل تولید شده است.'</span>)
table = doc.add_table(rows=<span class="st">1</span>, cols=<span class="st">3</span>)
table.style = <span class="st">'Light Grid Accent 1'</span>
<span class="kw">for</span> i, h <span class="kw">in</span> <span class="fn">enumerate</span>([<span class="st">'محصول'</span>, <span class="st">'تعداد'</span>, <span class="st">'مبلغ'</span>]):
table.rows[<span class="st">0</span>].cells[i].text = h
doc.save(<span class="st">'report.docx'</span>)
</div>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">ب) فراخوانی ابزار (Tool / Function Calling)</h4>
<p>بهترین روش برای سیستم‌های تولیدی. ابزار از پیش ساخته شده و تست شده است؛ مدل فقط پارامترهای ساختاریافته (JSON) را پر می‌کند. دقت بالا، امنیت کامل و رفتار قابل پیش‌بینی.</p>
<div class="code-block">
<span class="code-label">تعریف ابزار برای مدل (JSON Schema)</span>
{
<span class="st">"name"</span>: <span class="st">"create_excel_report"</span>,
<span class="st">"description"</span>: <span class="st">"ساخت فایل اکسل از جدول داده"</span>,
<span class="st">"parameters"</span>: {
<span class="st">"type"</span>: <span class="st">"object"</span>,
<span class="st">"properties"</span>: {
<span class="st">"title"</span>: { <span class="st">"type"</span>: <span class="st">"string"</span> },
<span class="st">"headers"</span>: { <span class="st">"type"</span>: <span class="st">"array"</span>, <span class="st">"items"</span>: { <span class="st">"type"</span>: <span class="st">"string"</span> } },
<span class="st">"rows"</span>: { <span class="st">"type"</span>: <span class="st">"array"</span>, <span class="st">"items"</span>: { <span class="st">"type"</span>: <span class="st">"array"</span> } },
<span class="st">"sheet_name"</span>: { <span class="st">"type"</span>: <span class="st">"string"</span>, <span class="st">"default"</span>: <span class="st">"Sheet1"</span> }
},
<span class="st">"required"</span>: [<span class="st">"title"</span>, <span class="st">"headers"</span>, <span class="st">"rows"</span>]
}
}
</div>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">ج) رویکرد قالب‌محور (Template-Based)</h4>
<p>قالب‌های آماده (docx، html، xlsx) به همراه جایگزین‌کننده متغیر. مدل فقط داده‌ها را پر می‌کند. مناسب اسناد رسمی، فاکتور، قرارداد و گزارش‌هایی با چیدمان ثابت.</p>
<div class="code-block">
<span class="code-label">نمونه: تزریق داده در قالب Word با docxtpl</span>
<span class="kw">from</span> docxtpl <span class="kw">import</span> DocxTemplate
tpl = DocxTemplate(<span class="st">'invoice_template.docx'</span>) <span class="cm"># قالب با {{ name }} و {{ items }}</span>
context = {
<span class="st">'name'</span>: <span class="st">'شرکت نمونه'</span>,
<span class="st">'items'</span>: [{<span class="st">'title'</span>: <span class="st">'خدمات'</span>, <span class="st">'price'</span>: <span class="st">'۱۲,۰۰۰,۰۰۰'</span>}],
<span class="st">'total'</span>: <span class="st">'۱۲,۰۰۰,۰۰۰'</span>
}
tpl.render(context)
tpl.save(<span class="st">'invoice_out.docx'</span>)
</div>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">مقایسه رویکردها:</h4>
<table class="styled-table">
<thead>
<tr>
<th>معیار</th>
<th>تولید کد</th>
<th>Tool Calling</th>
<th>قالب‌محور</th>
</tr>
</thead>
<tbody>
<tr>
<td>انعطاف‌پذیری</td>
<td style="color: var(--success); font-weight: 600;">بسیار بالا</td>
<td>متوسط (در محدوده ابزار)</td>
<td>پایین (چیدمان ثابت)</td>
</tr>
<tr>
<td>دقت و اتکاپذیری</td>
<td style="color: var(--danger);">متغیر</td>
<td style="color: var(--success); font-weight: 600;">بالا</td>
<td style="color: var(--success); font-weight: 600;">بسیار بالا</td>
</tr>
<tr>
<td>امنیت</td>
<td style="color: var(--danger);">نیازمند سندباکس</td>
<td style="color: var(--success); font-weight: 600;">بالا</td>
<td style="color: var(--success); font-weight: 600;">بالا</td>
</tr>
<tr>
<td>هزینه پیاده‌سازی</td>
<td>کم (اولیه) / زیاد (نگهداری)</td>
<td>متوسط</td>
<td style="color: var(--success); font-weight: 600;">کم</td>
</tr>
<tr class="highlight-row">
<td><strong>کاربرد مناسب</strong></td>
<td>خروجی‌های نامحدود و خلاقانه</td>
<td><strong>سیستم‌های تولیدی و API</strong></td>
<td>اسناد رسمی و تکرارشونده</td>
</tr>
</tbody>
</table>
<div class="success-box">
<strong>✓ پیشنهاد عملی:</strong> معماری ترکیبی بهترین گزینه است — <strong>Tool Calling</strong> به‌عنوان مسیر اصلی، <strong>قالب‌محور</strong> برای اسناد استاندارد، و <strong>تولید کد</strong> صرفاً در سندباکس برای موارد خاص.
</div>
</div>
</div>
<div class="section">
<div class="section-header">
<div class="section-number">۴</div>
<div class="section-title">
<h3>ابزارها و کتابخانه‌ها به تفکیک فرمت</h3>
<div class="subtitle">Libraries per Output Format</div>
</div>
</div>
<div class="section-content">
<p>انتخاب کتابخانه مناسب، نیمی از مسیر است. جدول زیر گزینه‌های بالغ و پرکاربرد را با توجه به نیاز پروژه فهرست می‌کند:</p>
<table class="styled-table">
<thead>
<tr>
<th>فرمت</th>
<th>کتابخانه / ابزار</th>
<th>نقطه قوت</th>
<th>ملاحظه فارسی / RTL</th>
</tr>
</thead>
<tbody>
<tr>
<td rowspan="3"><strong>Word (.docx)</strong></td>
<td><span class="inline-code">python-docx</span></td>
<td>کنترل کامل پاراگراف، جدول، استایل</td>
<td style="color: var(--warning);">تنظیم دستی RTL و فونت</td>
</tr>
<tr>
<td><span class="inline-code">docxtpl</span></td>
<td>قالب‌محور و ساده</td>
<td style="color: var(--success);">عالی — قالب را در Word بسازید</td>
</tr>
<tr>
<td><span class="inline-code">Pandoc</span></td>
<td>تبدیل Markdown به docx</td>
<td style="color: var(--success);">خوب — با قالب مرجع</td>
</tr>
<tr>
<td rowspan="3"><strong>PDF</strong></td>
<td><span class="inline-code">WeasyPrint</span></td>
<td>HTML/CSS → PDF با پشتیبانی خوب CSS</td>
<td style="color: var(--success);">بهترین گزینه برای فارسی و RTL</td>
</tr>
<tr>
<td><span class="inline-code">ReportLab</span></td>
<td>کنترل دقیق و برنامه‌نویسی‌محور</td>
<td style="color: var(--warning);">نیازمند شکل‌دهی دستی متن فارسی</td>
</tr>
<tr>
<td><span class="inline-code">LaTeX / XePersian</span></td>
<td>کیفیت تایپوگرافی بی‌نظیر</td>
<td style="color: var(--success);">عالی برای متون علمی</td>
</tr>
<tr>
<td rowspan="3"><strong>Excel (.xlsx)</strong></td>
<td><span class="inline-code">openpyxl</span></td>
<td>خواندن و نوشتن، استایل، فرمول</td>
<td>پشتیبانی از RTL در تنظیمات شیت</td>
</tr>
<tr>
<td><span class="inline-code">XlsxWriter</span></td>
<td>نمودار، قالب‌بندی پیشرفته</td>
<td>نوشتن سریع، مناسب گزارش‌سازی</td>
</tr>
<tr>
<td><span class="inline-code">pandas + openpyxl</span></td>
<td>از DataFrame به اکسل</td>
<td>سریع‌ترین راه برای جداول داده</td>
</tr>
<tr>
<td><strong>PowerPoint (.pptx)</strong></td>
<td><span class="inline-code">python-pptx</span></td>
<td>اسلاید، چارت، تصویر</td>
<td>نیازمند قالب RTL آماده</td>
</tr>
<tr class="highlight-row">
<td><strong>CSV / JSON / Markdown</strong></td>
<td>استاندارد پایتون</td>
<td>ساده‌ترین و سریع‌ترین خروجی</td>
<td style="color: var(--success); font-weight: 600;">بدون پیچیدگی</td>
</tr>
</tbody>
</table>
<div class="highlight-box">
<strong>⭐ توصیه ویژه برای فارسی:</strong> برای PDF فارسی، مسیر <strong>HTML → WeasyPrint</strong> را انتخاب کنید. با CSS می‌توانید <span class="inline-code">direction: rtl</span>، فونت وزیرمتن و شکست صحیح خطوط را به‌سادگی کنترل کنید؛ در حالی که ReportLab برای متن فارسی نیاز به پردازش پیچیده دارد.
</div>
<div class="code-block">
<span class="code-label">نمونه: PDF فارسی از HTML با WeasyPrint</span>
<span class="kw">from</span> weasyprint <span class="kw">import</span> HTML
html = <span class="st">"""
&lt;html dir="rtl"&gt;&lt;head&gt;&lt;meta charset="utf-8"&gt;
&lt;style&gt;
@font-face { font-family: 'Vazir'; src: url('Vazirmatn-Regular.ttf'); }
body { font-family: 'Vazir'; direction: rtl; }
table { width: 100%; border-collapse: collapse; }
th, td { border: 1px solid #ccc; padding: 8px; }
&lt;/style&gt;&lt;/head&gt;
&lt;body&gt;&lt;h1&gt;گزارش فروش&lt;/h1&gt;
&lt;table&gt;&lt;tr&gt;&lt;th&gt;محصول&lt;/th&gt;&lt;th&gt;مبلغ&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;کرم مرطوب‌کننده&lt;/td&gt;&lt;td&gt;۱۲۰٬۰۰۰</td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/body&gt;&lt;/html&gt;
"""</span>
HTML(<span class="fn">string</span>=html).write_pdf(<span class="st">'report.pdf'</span>)
</div>
</div>
</div>
<div class="section">
<div class="section-header">
<div class="section-number">۵</div>
<div class="section-title">
<h3>پنج سطح «آموزش» مدل</h3>
<div class="subtitle">From Prompting to Fine-Tuning</div>
</div>
</div>
<div class="section-content">
<p>عبارت «آموزش مدل» می‌تواند به پنج سطح متفاوت اشاره داشته باشد. همیشه از سطح پایین شروع کنید؛ در بیشتر موارد نیازی به آموزش واقعی (Fine-tuning) نیست.</p>
<table class="styled-table">
<thead>
<tr>
<th>سطح</th>
<th>روش</th>
<th>هزینه</th>
<th>چه زمانی؟</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>۱. پرامپت‌نویسی</strong></td>
<td>دستورالعمل + مثال (Few-shot)</td>
<td style="color: var(--success);">تقریباً صفر</td>
<td>همیشه — نقطه شروع</td>
</tr>
<tr>
<td><strong>۲. خروجی ساختاریافته</strong></td>
<td>JSON Schema / Structured Output</td>
<td style="color: var(--success);">پایین</td>
<td>وقتی قالب داده ثابت است</td>
</tr>
<tr>
<td><strong>۳. فراخوانی ابزار</strong></td>
<td>تعریف توابع و پارامترها</td>
<td>متوسط</td>
<td>سیستم‌های تولیدی</td>
</tr>
<tr>
<td><strong>۴. RAG</strong></td>
<td>بازیابی مستندات کتابخانه‌ها</td>
<td>متوسط</td>
<td>APIهای متغیر و نسخه‌های جدید</td>
</tr>
<tr class="highlight-row">
<td><strong>۵. Fine-tuning</strong></td>
<td>آموزش روی زوج‌های (درخواست → کد/JSON)</td>
<td style="color: var(--danger);">بالا</td>
<td>فقط پس از اشباع چهار سطح قبل</td>
</tr>
</tbody>
</table>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">چطور داده آموزشی برای Fine-tuning بسازیم؟</h4>
<p class="no-indent">مجموعه‌داده باید شامل زوج‌های ورودی و خروجی مطلوب باشد. بهترین منبع، لاگ واقعی سیستم خودتان است:</p>
<div class="code-block">
<span class="code-label">ساختار نمونه آموزشی (JSONL)</span>
{<span class="st">"messages"</span>: [
{<span class="st">"role"</span>: <span class="st">"system"</span>, <span class="st">"content"</span>: <span class="st">"تو یک مولد سند هستی. همیشه JSON معتبر طبق اسکیما برگردان."</span>},
{<span class="st">"role"</span>: <span class="st">"user"</span>, <span class="st">"content"</span>: <span class="st">"یک فاکتور برای ۳ قلم کالا بساز"</span>},
{<span class="st">"role"</span>: <span class="st">"assistant"</span>, <span class="st">"content"</span>:
<span class="st">"{\"type\":\"invoice\",\"items\":[{\"title\":\"A\",\"qty\":2,\"price\":100}],\"total\":200}"</span>}
]}
</div>
<div class="warning-box">
<strong>⚠️ هشدار:</strong> Fine-tuning بدون مجموعه‌داده باکیفیت و ارزیابی دقیق، معمولاً نتیجه را بدتر می‌کند. ابتدا سطح ۱ تا ۴ را کامل کنید؛ در ۸۰٪ موارد پاسخ همین‌جاست.
</div>
<div class="info-box">
<strong>💡 ترفند یادگیری از بازخورد اجرا:</strong> یک حلقه بسازید که خروجی مدل را در سندباکس اجرا کند. اگر فایل بدون خطا ساخته شد، آن نمونه به‌عنوان داده مثبت ذخیره شود؛ در غیر این صورت، خطا به مدل بازخورد داده شود. این چرخه به‌مرور کیفیت را بدون آموزش گران‌قیمت بالا می‌برد.
</div>
</div>
</div>
<div class="section">
<div class="section-header">
<div class="section-number">۶</div>
<div class="section-title">
<h3>نقشه راه پیاده‌سازی گام‌به‌گام</h3>
<div class="subtitle">Implementation Roadmap</div>
</div>
</div>
<div class="section-content">
<p>مسیر پیشنهادی از صفر تا سیستم پایدار، در شش گام:</p>
<div class="timeline">
<div class="timeline-item">
<div class="timeline-content">
<h5>گام ۱ — تعریف قرارداد داده</h5>
<p>یک JSON Schema دقیق برای هر نوع سند بنویسید: فیلدهای اجباری، انواع داده و محدودیت‌ها. این قرارداد، مرز بین مدل و کد است.</p>
</div>
<div class="timeline-dot"></div>
</div>
<div class="timeline-item">
<div class="timeline-content">
<h5>گام ۲ — ساخت ابزار تولید فایل</h5>
<p>برای هر فرمت یک تابع مستقل بنویسید که ورودی JSON بگیرد و فایل بسازد. این توابع باید کاملاً مستقل از مدل تست شوند.</p>
</div>
<div class="timeline-dot"></div>
</div>
<div class="timeline-item">
<div class="timeline-content">
<h5>گام ۳ — اتصال مدل به ابزار</h5>
<p>ابزارها را با Tool Calling یا Structured Output به مدل معرفی کنید. مدل فقط پارامترها را پر می‌کند.</p>
</div>
<div class="timeline-dot"></div>
</div>
<div class="timeline-item">
<div class="timeline-content">
<h5>گام ۴ — سندباکس و اعتبارسنجی</h5>
<p>اجرای کد در محیط ایزوله، بدون دسترسی شبکه و فایل‌سیستم آزاد. سپس بازخوانی فایل خروجی برای اطمینان از سالم بودن.</p>
</div>
<div class="timeline-dot"></div>
</div>
<div class="timeline-item">
<div class="timeline-content">
<h5>گام ۵ — مجموعه ارزیابی (Eval Set)</h5>
<p>۵۰ تا ۱۰۰ نمونه واقعی بسازید و هر تغییر را روی آن بسنجید. بدون این مجموعه، بهبود واقعی قابل تشخیص نیست.</p>
</div>
<div class="timeline-dot"></div>
</div>
<div class="timeline-item">
<div class="timeline-content">
<h5>گام ۶ — بهینه‌سازی و آموزش</h5>
<p>فقط اگر کیفیت کافی نبود، به RAG یا Fine-tuning بروید. داده آموزشی را از لاگ‌های موفق بسازید.</p>
</div>
<div class="timeline-dot"></div>
</div>
</div>
<div class="success-box">
<strong>✓ معیار موفقیت:</strong> سیستمی که در آن، هر فایل تولیدشده بدون دخالت انسان باز شود، ساختارش درست باشد و محتوایش با درخواست کاربر مطابقت داشته باشد.
</div>
</div>
</div>
<div class="section">
<div class="section-header">
<div class="section-number">۷</div>
<div class="section-title">
<h3>چالش‌ها و دام‌های رایج</h3>
<div class="subtitle">Pitfalls & Mitigations</div>
</div>
</div>
<div class="section-content">
<p>تجربه نشان می‌دهد بیشتر شکست‌ها ناشی از چند خطای مشخص است:</p>
<div class="warning-box">
<strong>⚠️ دام‌های فنی:</strong>
<ul style="padding-right: 20px; margin-top: 10px;">
<li><strong>توهم API:</strong> مدل ممکن است متدهایی بسازد که در کتابخانه وجود ندارند. راه‌حل: RAG روی مستندات و تست اجباری.</li>
<li><strong>نادیده‌گرفتن RTL:</strong> فایل ساخته می‌شود اما متن فارسی بی‌جهت و ناخوانا است. راه‌حل: تنظیم صریح جهت و فونت.</li>
<li><strong>فونت ناموجود:</strong> در سرور، فونت فارسی نصب نیست و PDF با مربع نمایش داده می‌شود. راه‌حل: embed کردن فونت.</li>
<li><strong>اعداد و ارقام:</strong> تفاوت ارقام فارسی و لاتین می‌تواند محاسبات را خراب کند.</li>
<li><strong>حجم و حافظه:</strong> فایل‌های اکسل بزرگ می‌توانند حافظه سرور را اشغال کنند. راه‌حل: نوشتن جریانی (streaming).</li>
<li><strong>اجرای کد ناامن:</strong> اجرای کد تولیدشده توسط مدل بدون سندباکس، یک آسیب‌پذیری جدی است.</li>
</ul>
</div>
<div class="comparison-grid">
<div class="comparison-card">
<h4>روش شکننده ✗</h4>
<ul>
<li><span>روش تولید</span> <span class="value" style="color: var(--danger);">بایت‌های خام فایل</span></li>
<li><span>اعتبارسنجی</span> <span class="value" style="color: var(--danger);">ندارد</span></li>
<li><span>اجرای کد</span> <span class="value" style="color: var(--danger);">بدون سندباکس</span></li>
<li><span>تست</span> <span class="value" style="color: var(--danger);">چشمی و دستی</span></li>
</ul>
</div>
<div class="comparison-card recommended">
<span class="card-badge">روش پایدار ✓</span>
<h4>روش توصیه‌شده</h4>
<ul>
<li><span>روش تولید</span> <span class="value">JSON + ابزار</span></li>
<li><span>اعتبارسنجی</span> <span class="value">Schema + بازخوانی</span></li>
<li><span>اجرای کد</span> <span class="value">سندباکس ایزوله</span></li>
<li><span>تست</span> <span class="value">Eval خودکار</span></li>
</ul>
</div>
</div>
</div>
</div>
<div class="section">
<div class="section-header">
<div class="section-number">۸</div>
<div class="section-title">
<h3>جمع‌بندی و پیشنهاد نهایی</h3>
<div class="subtitle">Final Summary & Recommendation</div>
</div>
</div>
<div class="section-content">
<p>پاسخ کوتاه به پرسش اصلی این است: برای تولید فایل، مدل را «آموزش» نمی‌دهیم تا فایل بنویسد؛ بلکه مدل را روی <strong>تولید داده ساختاریافته</strong> تنظیم می‌کنیم و <strong>لایه اجرایی</strong> را به کد می‌سپاریم.</p>
<table class="styled-table">
<thead>
<tr>
<th>فرمت هدف</th>
<th>روش پیشنهادی</th>
<th>ابزار اصلی</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>Word</strong></td>
<td>قالب‌محور یا python-docx</td>
<td><span class="inline-code">docxtpl</span> / <span class="inline-code">python-docx</span></td>
</tr>
<tr>
<td><strong>PDF</strong></td>
<td>HTML/CSS → PDF</td>
<td><span class="inline-code">WeasyPrint</span></td>
</tr>
<tr>
<td><strong>Excel</strong></td>
<td>Tool Calling + openpyxl</td>
<td><span class="inline-code">openpyxl</span> / <span class="inline-code">XlsxWriter</span></td>
</tr>
<tr>
<td><strong>PowerPoint</strong></td>
<td>قالب‌محور</td>
<td><span class="inline-code">python-pptx</span></td>
</tr>
<tr class="highlight-row">
<td><strong>سطح آموزش</strong></td>
<td colspan="2"><strong>شروع از پرامپت و Schema؛ Fine-tuning فقط در صورت نیاز</strong></td>
</tr>
</tbody>
</table>
<div class="conclusion-box">
<h3>🎯 پیشنهاد نهایی</h3>
<p>معماری سه‌لایه (مدل + ابزار + اعتبارسنجی) را بپذیرید و با رویکرد Tool Calling شروع کنید.</p>
<p>این ترکیب، هم دقیق‌ترین خروجی را می‌دهد، هم امن‌ترین و هم قابل‌توسعه‌ترین — بدون نیاز به آموزش گران‌قیمت مدل.</p>
<div class="highlight">
<strong>💎 فرمول موفقیت:</strong><br>
مدل محتوا را می‌سازد · کد فایل را می‌سازد · اعتبارسنجی کیفیت را تضمین می‌کند
</div>
</div>
<div class="info-box">
<strong>📞 گام بعدی:</strong> اگر بخواهید، می‌توان معماری فوق را برای یک فرمت مشخص (مثلاً فاکتور Word یا گزارش اکسل) به‌صورت کامل و اجرایی پیاده‌سازی کرد — شامل Schema، ابزار تولید فایل، سندباکس و مجموعه ارزیابی.
</div>
</div>
</div>
<div class="page-info">
این مستند راهنمای فنی عمومی است و قابل به‌روزرسانی بر اساس نسخه‌های جدید کتابخانه‌ها می‌باشد.
</div>
</div>
<footer class="document-footer">
<div class="footer-brand">طراحی و توسعه: هادی خزاعی اصل</div>
<div class="footer-divider"></div>
<div>تاریخ تهیه: شهریور ۱۴۰۵ &nbsp;|&nbsp; نسخه: ۱.۰ &nbsp;|&nbsp; طبقه‌بندی: عمومی</div>
</footer>
</div>
</body>
</html>