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

1455 lines
59 KiB
HTML
Raw 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>شماره مستند: AI-ARCH-1405-022</div>
<div>نسخه: ۱.۰</div>
<div>تاریخ تهیه: شهریور ۱۴۰۵</div>
<div>طبقه‌بندی: فنی</div>
</div>
</div>
<div class="document-title">
<span class="label">ARCHITECTURE GUIDE</span>
<h1>مدیریت حافظه و کنترل کانتکس در ایجنت‌ها</h1>
<h2>از استراتژی‌های حافظه در پروژه‌های هوش مصنوعی تا غلبه بر محدودیت کانتکس در مدل‌های محلی</h2>
</div>
</header>
<div class="document-body">
<div class="intro-text">
<strong>چکیده:</strong><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>مدل‌های زبانی «حافظه» به معنای انسانی ندارند؛ آنچه ما حافظه می‌نامیم، در واقع <strong>بازسازی اطلاعات از منابع بیرونی</strong> و قرار دادن آن در پنجره کانتکس است. بنابراین مدیریت حافظه یعنی مدیریت اینکه <em>چه چیزی، چه زمانی و به چه مقدار</em> در کانتکس قرار گیرد.</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">از پنجره لغزان تا فشرده‌سازی</div>
</div>
<div class="stat-card">
<div class="stat-value">۱<br>قاعده اصلی</div>
<div class="stat-label">کانتکس گران است؛ فقط ضروری‌ها را وارد کن</div>
</div>
</div>
<div class="highlight-box">
<strong>نتیجه کلیدی:</strong> در مدل‌های محلی با کانتکس محدود (مثلاً ۴K تا ۳۲K توکن)، کیفیت ایجنت مستقیماً به توانایی شما در <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">Memory Taxonomy</div>
</div>
</div>
<div class="section-content">
<p>پیش از طراحی هر استراتژی، باید بدانیم با چه نوع حافظه‌ای سروکار داریم. دسته‌بندی زیر برگرفته از علوم شناختی و تطبیق‌یافته با معماری ایجنت است:</p>
<table class="styled-table">
<thead>
<tr>
<th>نوع حافظه</th>
<th>محتوا</th>
<th>محل نگهداری</th>
<th>مدت ماندگاری</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>کاری (Working)</strong></td>
<td>پیام‌های جاری، ابزارهای فعال، وضعیت لحظه‌ای</td>
<td>پنجره کانتکس</td>
<td>یک نوبت مکالمه</td>
</tr>
<tr>
<td><strong>اپیزودیک (Episodic)</strong></td>
<td>تاریخچه مکالمات و رویدادهای گذشته</td>
<td>پایگاه داده / فایل لاگ</td>
<td>روز تا ماه</td>
</tr>
<tr>
<td><strong>معنایی (Semantic)</strong></td>
<td>حقایق، دانش دامنه، مستندات</td>
<td>Vector DB / Knowledge Graph</td>
<td>بلندمدت</td>
</tr>
<tr>
<td><strong>رویه‌ای (Procedural)</strong></td>
<td>دستورالعمل‌ها، ابزارها، مهارت‌ها</td>
<td>پرامپت سیستم / کد ابزار</td>
<td>ثابت تا نسخه بعدی</td>
</tr>
<tr class="highlight-row">
<td><strong>بلندمدت (Long-Term)</strong></td>
<td>خلاصه‌های تجمیع‌شده و پروفایل کاربر</td>
<td>فایل / DB ساختاریافته</td>
<td>ماه تا سال</td>
</tr>
</tbody>
</table>
<div class="info-box">
<strong>💡 نکته معماری:</strong> تنها «حافظه کاری» داخل کانتکس مدل است. بقیه حافظه‌ها <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">Memory Management Strategies</div>
</div>
</div>
<div class="section-content">
<p>مدیریت حافظه در پروژه‌های هوش مصنوعی، ترکیبی از چند استراتژی است که در سه دسته کلی قرار می‌گیرند:</p>
<div class="flow-grid">
<div class="flow-card">
<div class="flow-icon">A</div>
<h5>ذخیره‌سازی</h5>
<p>اطلاعات مهم را در منابع پایدار بیرونی نگه دار.</p>
</div>
<div class="flow-card">
<div class="flow-icon">B</div>
<h5>بازیابی</h5>
<p>در هر لحظه، مرتبط‌ترین اطلاعات را پیدا و وارد کن.</p>
</div>
<div class="flow-card">
<div class="flow-icon">C</div>
<h5>تجمیع و فراموشی</h5>
<p>اطلاعات کهنه را خلاصه یا حذف کن تا فضا آزاد شود.</p>
</div>
</div>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">الف) استراتژی‌های ذخیره‌سازی</h4>
<ul style="padding-right: 20px; margin-top: 10px;">
<li style="margin-bottom: 8px;"><strong>حافظه برداری (Vector Store):</strong> ذخیره امبدینگ‌ها در پایگاه‌هایی مانند Chroma، FAISS یا Qdrant برای بازیابی معنایی.</li>
<li style="margin-bottom: 8px;"><strong>پایگاه داده ساختاریافته:</strong> ذخیره حقایق و پروفایل کاربر به‌صورت SQL یا Key-Value برای دقت بالا.</li>
<li style="margin-bottom: 8px;"><strong>گراف دانش (Knowledge Graph):</strong> مدل‌سازی روابط بین موجودیت‌ها برای استدلال چندمرحله‌ای.</li>
<li style="margin-bottom: 8px;"><strong>فایل‌های حافظه (Memory Files):</strong> ساده‌ترین و مؤثرترین روش برای ایجنت‌های محلی — ذخیره خلاصه‌ها در <span class="inline-code">memory.md</span> یا JSON.</li>
</ul>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">ب) استراتژی‌های بازیابی</h4>
<ul style="padding-right: 20px; margin-top: 10px;">
<li style="margin-bottom: 8px;"><strong>جستجوی برداری (Semantic Search):</strong> یافتن نزدیک‌ترین معنایی به پرسش جاری.</li>
<li style="margin-bottom: 8px;"><strong>بازیابی ترکیبی (Hybrid):</strong> ترکیب جستجوی کلیدواژه (BM25) و برداری برای دقت بیشتر.</li>
<li style="margin-bottom: 8px;"><strong>بازیابی زمان‌محور:</strong> اولویت‌دهی به اطلاعات جدیدتر در مکالمات طولانی.</li>
<li style="margin-bottom: 8px;"><strong>توجه به اهمیت (Importance Scoring):</strong> امتیازدهی به هر قطعه حافظه بر اساس اهمیت و تازگی.</li>
</ul>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">ج) استراتژی‌های تجمیع و فراموشی</h4>
<ul style="padding-right: 20px; margin-top: 10px;">
<li style="margin-bottom: 8px;"><strong>خلاصه‌سازی تدریجی:</strong> هر N پیام، مکالمه قدیمی را با یک خلاصه جایگزین کن.</li>
<li style="margin-bottom: 8px;"><strong>تجمیع حافظه (Consolidation):</strong> حقایق پراکنده را در یک نمای کلی ادغام کن (مثل خواب REM).</li>
<li style="margin-bottom: 8px;"><strong>منحنی فراموشی:</strong> اطلاعات کم‌استفاده به‌تدریج وزن کمتری بگیرند و آرشیو شوند.</li>
<li style="margin-bottom: 8px;"><strong>حذف مبتنی بر TTL:</strong> تعیین تاریخ انقضا برای حافظه‌های موقت.</li>
</ul>
<div class="code-block">
<span class="code-label">نمونه: ساختار ساده یک سیستم حافظه سه‌لایه</span>
<span class="kw">from</span> dataclasses <span class="kw">import</span> dataclass, field
<span class="kw">from</span> typing <span class="kw">import</span> List, Dict
<span class="kw">import</span> time
<span class="kw">@dataclass</span>
<span class="kw">class</span> <span class="fn">MemoryItem</span>:
content: <span class="fn">str</span>
importance: <span class="fn">float</span> = <span class="st">0.5</span>
timestamp: <span class="fn">float</span> = field(default_factory=time.time)
access_count: <span class="fn">int</span> = <span class="st">0</span>
<span class="kw">class</span> <span class="fn">ThreeTierMemory</span>:
<span class="kw">def</span> <span class="fn">__init__</span>(self):
self.working: List[MemoryItem] = [] <span class="cm"># در کانتکس</span>
self.episodic: List[MemoryItem] = [] <span class="cm"># تاریخچه کامل</span>
self.semantic: Dict[<span class="fn">str</span>, MemoryItem] = {} <span class="cm"># حقایق پایدار</span>
<span class="kw">def</span> <span class="fn">add</span>(self, content, importance=<span class="st">0.5</span>, tier=<span class="st">'working'</span>):
item = MemoryItem(content, importance)
<span class="kw">if</span> tier == <span class="st">'working'</span>:
self.working.append(item)
<span class="kw">elif</span> tier == <span class="st">'episodic'</span>:
self.episodic.append(item)
<span class="kw">else</span>:
self.semantic[content[:<span class="st">50</span>]] = item
<span class="kw">def</span> <span class="fn">consolidate</span>(self):
<span class="cm"># انتقال موارد مهم از کاری به اپیزودیک، و ادغام</span>
important = [m <span class="kw">for</span> m <span class="kw">in</span> self.working <span class="kw">if</span> m.importance &gt; <span class="st">0.7</span>]
self.episodic.extend(important)
self.working = [m <span class="kw">for</span> m <span class="kw">in</span> self.working <span class="kw">if</span> m.importance &lt;= <span class="st">0.7</span>]
<span class="kw">return</span> len(important)
<span class="kw">def</span> <span class="fn">decay</span>(self, threshold=<span class="st">0.2</span>):
<span class="cm"># فراموشی تدریجی بر اساس اهمیت و زمان</span>
now = time.time()
<span class="kw">for</span> m <span class="kw">in</span> self.episodic:
age_days = (now - m.timestamp) / <span class="st">86400</span>
m.importance *= <span class="st">0.99</span> ** age_days
self.episodic = [m <span class="kw">for</span> m <span class="kw">in</span> self.episodic <span class="kw">if</span> m.importance &gt; threshold]
</div>
<div class="success-box">
<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">Context Window Limits in Local Models</div>
</div>
</div>
<div class="section-content">
<p>مدل‌های محلی (Llama، Mistral، Qwen، Gemma و مشابه) به‌طور معمول پنجره کانتکس ۴K تا ۳۲K توکن دارند — بسیار کمتر از مدل‌های ابری که تا ۲۰۰K می‌رسند. این محدودیت از سه منبع ناشی می‌شود:</p>
<table class="styled-table">
<thead>
<tr>
<th>منبع محدودیت</th>
<th>توضیح</th>
<th>پیامد عملی</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>معماری مدل</strong></td>
<td>طول آموزش‌دیده و حافظه KV Cache</td>
<td>فراتر از آن، کیفیت به‌شدت افت می‌کند</td>
</tr>
<tr>
<td><strong>حافظه سخت‌افزار (VRAM)</strong></td>
<td>KV Cache با طول متن به‌صورت خطی رشد می‌کند</td>
<td>کانتکس بزرگ = مصرف حافظه زیاد = کندی</td>
</tr>
<tr>
<td><strong>سرعت استنتاج</strong></td>
<td>توجه (Attention) با طول کانتکس کندتر می‌شود</td>
<td>پاسخ‌دهی کند در کانتکس‌های بزرگ</td>
</tr>
<tr class="highlight-row">
<td><strong>نتیجه</strong></td>
<td colspan="2"><strong>کانتکس محدود یک واقعیت است؛ باید با معماری درست آن را مدیریت کرد.</strong></td>
</tr>
</tbody>
</table>
<div class="warning-box">
<strong>⚠️ پدیده «Lost in the Middle»:</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">Context Management Techniques</div>
</div>
</div>
<div class="section-content">
<p>هفت تکنیک اصلی برای کنترل کانتکس وجود دارد که در عمل با هم ترکیب می‌شوند. جدول زیر مقایسه اجمالی و سپس شرح هر یک:</p>
<table class="styled-table">
<thead>
<tr>
<th>تکنیک</th>
<th>مکانیزم</th>
<th>هزینه</th>
<th>مناسب برای</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>۱. پنجره لغزان</strong></td>
<td>نگه‌داشتن N پیام آخر</td>
<td>صفر</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>دانش دامنه بزرگ</td>
</tr>
<tr>
<td><strong>۴. کانتکس سلسله‌مراتبی</strong></td>
<td>لایه‌بندی: خلاصه کلی + جزئیات</td>
<td>متوسط</td>
<td>پروژه‌های چندفازی</td>
</tr>
<tr>
<td><strong>۵. زیرایجنت (Sub-agent)</strong></td>
<td>تقسیم کار بین ایجنت‌های تخصصی</td>
<td>بالا</td>
<td>وظایف پیچیده</td>
</tr>
<tr>
<td><strong>۶. فشرده‌سازی</strong></td>
<td>حذف کلمات پرتکرار و تکراری</td>
<td>کم</td>
<td>متن‌های طولانی</td>
</tr>
<tr class="highlight-row">
<td><strong>۷. Scratchpad</strong></td>
<td>نوشتن نتایج میانی در فایل خارجی</td>
<td>کم</td>
<td>زنجیره‌های استدلالی</td>
</tr>
</tbody>
</table>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">۱. پنجره لغزان (Sliding Window)</h4>
<p class="no-indent">ساده‌ترین روش: فقط آخرین N پیام یا M توکن را نگه می‌داریم. سریع و بدون هزینه، اما اطلاعات قدیمی را از دست می‌دهد.</p>
<div class="code-block">
<span class="code-label">پیاده‌سازی پنجره لغزان با شمارش توکن</span>
<span class="kw">def</span> <span class="fn">sliding_window</span>(messages, max_tokens=<span class="st">4000</span>, counter=<span class="kw">None</span>):
total, kept = <span class="st">0</span>, []
<span class="kw">for</span> msg <span class="kw">in</span> <span class="fn">reversed</span>(messages):
n = counter(msg)
<span class="kw">if</span> total + n &gt; max_tokens:
<span class="kw">break</span>
kept.insert(<span class="st">0</span>, msg)
total += n
<span class="kw">return</span> kept
</div>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">۲. خلاصه‌سازی تدریجی (Incremental Summarization)</h4>
<p class="no-indent">وقتی کانتکس پر شد، پیام‌های قدیمی با یک خلاصه فشرده جایگزین می‌شوند. این روش تعادل خوبی بین از دست دادن اطلاعات و صرفه‌جویی در کانتکس برقرار می‌کند.</p>
<div class="code-block">
<span class="code-label">خلاصه‌سازی هنگام نزدیک شدن به سقف کانتکس</span>
<span class="kw">def</span> <span class="fn">maybe_summarize</span>(history, llm, threshold=<span class="st">0.8</span>, max_ctx=<span class="st">8000</span>):
used = <span class="fn">count_tokens</span>(history)
<span class="kw">if</span> used &lt; max_ctx * threshold:
<span class="kw">return</span> history
<span class="cm"># نیمی از مکالمه را خلاصه کن</span>
old_part, recent = history[:<span class="fn">len</span>(history)//<span class="st">2</span>], history[<span class="fn">len</span>(history)//<span class="st">2</span>:]
summary = llm.<span class="fn">invoke</span>(
<span class="st">"این مکالمه را در حداکثر ۲۰۰ توکن خلاصه کن و حقایق کلیدی را حفظ کن:\n"</span>
+ <span class="st">"\n"</span>.<span class="fn">join</span>(m[<span class="st">'content'</span>] <span class="kw">for</span> m <span class="kw">in</span> old_part)
)
<span class="kw">return</span> [{<span class="st">'role'</span>: <span class="st">'system'</span>, <span class="st">'content'</span>: <span class="st">f'خلاصه مکالمه قبلی: {summary}'</span>}] + recent
</div>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">۳. بازیابی مبتنی بر RAG</h4>
<p class="no-indent">به جای نگه‌داشتن همه‌چیز در کانتکس، فقط مرتبط‌ترین تکه‌ها را برای پرسش جاری بازیابی می‌کنیم. مناسب زمانی که حجم دانش بیشتر از ظرفیت کانتکس است.</p>
<div class="code-block">
<span class="code-label">بازیابی پویا بر اساس پرسش کاربر</span>
<span class="kw">def</span> <span class="fn">build_context</span>(query, vector_store, k=<span class="st">5</span>, max_tokens=<span class="st">3000</span>):
docs = vector_store.<span class="fn">similarity_search</span>(query, k=k)
context, total = [], <span class="st">0</span>
<span class="kw">for</span> d <span class="kw">in</span> docs:
n = <span class="fn">count_tokens</span>(d.page_content)
<span class="kw">if</span> total + n &gt; max_tokens:
<span class="kw">break</span>
context.append(d.page_content)
total += n
<span class="kw">return</span> <span class="st">"\n---\n"</span>.<span class="fn">join</span>(context)
</div>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">۴. کانتکس سلسله‌مراتبی (Hierarchical)</h4>
<p class="no-indent">سه سطح اطلاعات: خلاصه یک‌خطی پروژه (همیشه حاضر)، خلاصه بخش جاری (پویا)، و جزئیات (فقط در صورت نیاز بازیابی). این ساختار، بازگشت‌پذیری به گذشته را بدون اشغال کانتکس فراهم می‌کند.</p>
<div class="code-block">
<span class="code-label">ساختار سه‌سطحی کانتکس</span>
CONTEXT_STRUCTURE = {
<span class="st">"L0_always"</span>: <span class="st">"هدف پروژه: ساخت API فروش. کاربر: توسعه‌دهنده ارشد."</span>,
<span class="st">"L1_current"</span>: <span class="st">"در حال رفع باگ احراز هویت در endpoint /login"</span>,
<span class="st">"L2_retrieved"</span>: [
<span class="cm"># فقط در صورت نیاز و از طریق RAG پر می‌شود</span>
]
}
</div>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">۵. معماری زیرایجنت (Sub-agent)</h4>
<p class="no-indent">به جای یک ایجنت با کانتکس بزرگ، چند ایجنت تخصصی با کانتکس کوچک: یکی برای جستجو، یکی برای کدنویسی، یکی برای بررسی. هر کدام فقط اطلاعات حوزه خود را می‌بینند و نتیجه نهایی به ایجنت هماهنگ‌کننده برمی‌گردد.</p>
<div class="quote-box">
<p>بهترین راه برای حل مسئله کانتکس بزرگ، تقسیم آن به چند کانتکس کوچک است — هر زیرایجنت یک پنجره کانتکس مستقل و تمیز دارد.</p>
<div class="source">— الگوی رایج در معماری ایجنت‌های تولیدی</div>
</div>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">۶. فشرده‌سازی کانتکس (Compression)</h4>
<p class="no-indent">حذف اطلاعات تکراری، تبدیل متن به بولت‌پوینت، و کوتاه‌سازی عبارات طولانی. یک متن ۲۰۰۰ توکنی می‌تواند به ۴۰۰ توکن با حفظ معنا فشرده شود.</p>
<div class="code-block">
<span class="code-label">فشرده‌سازی ساختاری متن ورودی</span>
<span class="kw">def</span> <span class="fn">compress_history</span>(history):
<span class="cm"># ۱. حذف پیام‌های سیستمی تکراری</span>
seen = <span class="fn">set</span>()
unique = []
<span class="kw">for</span> m <span class="kw">in</span> history:
key = m[<span class="st">'role'</span>] + m[<span class="st">'content'</span>][:<span class="st">100</span>]
<span class="kw">if</span> key <span class="kw">not</span> <span class="kw">in</span> seen:
unique.append(m)
seen.add(key)
<span class="cm"># ۲. تبدیل پیام‌های طولانی به بولت</span>
compressed = []
<span class="kw">for</span> m <span class="kw">in</span> unique:
<span class="kw">if</span> <span class="fn">len</span>(m[<span class="st">'content'</span>]) &gt; <span class="st">500</span>:
bullets = <span class="fn">extract_bullets</span>(m[<span class="st">'content'</span>])
compressed.append({<span class="st">'role'</span>: m[<span class="st">'role'</span>], <span class="st">'content'</span>: bullets})
<span class="kw">else</span>:
compressed.append(m)
<span class="kw">return</span> compressed
</div>
<h4 style="margin-top: 25px; color: var(--primary); font-size: 16px;">۷. Scratchpad خارجی</h4>
<p class="no-indent">ایجنت نتایج میانی و استدلال‌های طولانی را در یک فایل موقت ذخیره می‌کند و فقط خلاصه آن را در کانتکس نگه می‌دارد. در زمان نیاز، فایل را می‌خواند. این روش برای زنجیره‌های استدلالی طولانی حیاتی است.</p>
<div class="code-block">
<span class="code-label">Scratchpad فایل‌محور برای ایجنت محلی</span>
<span class="kw">import</span> json, pathlib
<span class="kw">class</span> <span class="fn">Scratchpad</span>:
<span class="kw">def</span> <span class="fn">__init__</span>(self, path=<span class="st">"agent_scratchpad.json"</span>):
self.path = pathlib.<span class="fn">Path</span>(path)
self.data = json.<span class="fn">loads</span>(self.path.<span class="fn">read_text</span>()) <span class="kw">if</span> self.path.<span class="fn">exists</span>() <span class="kw">else</span> {}
<span class="kw">def</span> <span class="fn">write</span>(self, key, value):
self.data[key] = value
self.path.<span class="fn">write_text</span>(json.<span class="fn">dumps</span>(self.data, ensure_ascii=<span class="kw">False</span>))
<span class="kw">def</span> <span class="fn">read</span>(self, key):
<span class="kw">return</span> self.data.<span class="fn">get</span>(key)
<span class="kw">def</span> <span class="fn">summary</span>(self):
<span class="kw">return</span> {k: <span class="fn">str</span>(v)[:<span class="st">100</span>] <span class="kw">for</span> k, v <span class="kw">in</span> self.data.<span class="fn">items</span>()}
</div>
<div class="info-box">
<strong>💡 نکته ترکیبی:</strong> در عمل، بهترین ایجنت‌ها از ترکیب این تکنیک‌ها استفاده می‌کنند: پنجره لغزان برای پیام‌های اخیر + خلاصه‌سازی برای گذشته دور + RAG برای دانش دامنه + Scratchpad برای استدلال‌های میانی.
</div>
</div>
</div>
<div class="section">
<div class="section-header">
<div class="section-number">۶</div>
<div class="section-title">
<h3>بودجه‌بندی کانتکس: تخصیص هوشمند توکن</h3>
<div class="subtitle">Context Budgeting</div>
</div>
</div>
<div class="section-content">
<p>یکی از مؤثرترین روش‌ها برای کنترل کانتکس، تعیین سهم مشخص برای هر بخش است. با یک پنجره ۸K توکنی، تخصیص پیشنهادی به‌صورت زیر است:</p>
<table class="styled-table">
<thead>
<tr>
<th>بخش کانتکس</th>
<th>سهم توکن</th>
<th>درصد</th>
<th>توضیح</th>
</tr>
</thead>
<tbody>
<tr>
<td>پرامپت سیستم و دستورالعمل‌ها</td>
<td>۸۰۰</td>
<td>۱۰٪</td>
<td>نقش، قوانین، ابزارها</td>
</tr>
<tr>
<td>خلاصه حافظه بلندمدت</td>
<td>۶۰۰</td>
<td>۷.۵٪</td>
<td>پروفایل کاربر و اهداف</td>
</tr>
<tr>
<td>مستندات بازیابی‌شده (RAG)</td>
<td>۲۴۰۰</td>
<td>۳۰٪</td>
<td>مرتبط با پرسش جاری</td>
</tr>
<tr>
<td>تاریخچه مکالمه</td>
<td>۲۴۰۰</td>
<td>۳۰٪</td>
<td>اخیر + خلاصه میانی</td>
</tr>
<tr>
<td>پاسخ مدل و فضای تنفس</td>
<td>۱۸۰۰</td>
<td>۲۲.۵٪</td>
<td>ذخیره برای خروجی</td>
</tr>
<tr class="highlight-row">
<td><strong>مجموع</strong></td>
<td><strong>۸۰۰۰</strong></td>
<td><strong>۱۰۰٪</strong></td>
<td><strong>پنجره ۸K توکن</strong></td>
</tr>
</tbody>
</table>
<div class="success-box">
<strong>✓ قاعده طلایی بودجه‌بندی:</strong> همیشه حداقل ۲۰٪ از کانتکس را برای خروجی مدل خالی بگذارید. اگر کانتکس را کاملاً پر کنید، مدل جایی برای پاسخ ندارد و یا پاسخ را قطع می‌کند.
</div>
<div class="code-block">
<span class="code-label">مدیر بودجه کانتکس</span>
<span class="kw">class</span> <span class="fn">ContextBudget</span>:
<span class="kw">def</span> <span class="fn">__init__</span>(self, total=<span class="st">8000</span>):
self.total = total
self.allocations = {
<span class="st">'system'</span>: <span class="fn">int</span>(total * <span class="st">0.10</span>),
<span class="st">'longterm'</span>: <span class="fn">int</span>(total * <span class="st">0.075</span>),
<span class="st">'rag'</span>: <span class="fn">int</span>(total * <span class="st">0.30</span>),
<span class="st">'history'</span>: <span class="fn">int</span>(total * <span class="st">0.30</span>),
<span class="st">'reserve'</span>: <span class="fn">int</span>(total * <span class="st">0.225</span>),
}
<span class="kw">def</span> <span class="fn">fit</span>(self, section, content, counter):
budget = self.allocations[section]
<span class="kw">if</span> counter(content) &lt;= budget:
<span class="kw">return</span> content
<span class="cm"># برش هوشمند تا سقف بودجه</span>
<span class="kw">return</span> <span class="fn">truncate_to_tokens</span>(content, budget, counter)
</div>
</div>
</div>
<div class="section">
<div class="section-header">
<div class="section-number">۷</div>
<div class="section-title">
<h3>مقایسه مدل‌های محلی و ابری از منظر کانتکس</h3>
<div class="subtitle">Local vs. Cloud Context Trade-offs</div>
</div>
</div>
<div class="section-content">
<p>درک تفاوت‌ها کمک می‌کند استراتژی درست را انتخاب کنید:</p>
<div class="comparison-grid">
<div class="comparison-card">
<h4>مدل ابری (API)</h4>
<ul>
<li><span>پنجره کانتکس</span> <span class="value">۱۶K تا ۲۰۰K</span></li>
<li><span>هزینه هر توکن</span> <span class="value">پرداخت به ازای مصرف</span></li>
<li><span>حریم خصوصی</span> <span class="value" style="color: var(--danger);">داده به بیرون می‌رود</span></li>
<li><span>مدیریت کانتکس</span> <span class="value">کمتر حیاتی</span></li>
</ul>
</div>
<div class="comparison-card recommended">
<span class="card-badge">توصیه معماری ✓</span>
<h4>مدل محلی (Self-hosted)</h4>
<ul>
<li><span>پنجره کانتکس</span> <span class="value">۴K تا ۳۲K</span></li>
<li><span>هزینه هر توکن</span> <span class="value">فقط برق و سخت‌افزار</span></li>
<li><span>حریم خصوصی</span> <span class="value" style="color: var(--success);">کامل و محلی</span></li>
<li><span>مدیریت کانتکس</span> <span class="value" style="color: var(--warning);">حیاتی و اجتناب‌ناپذیر</span></li>
</ul>
</div>
</div>
<div class="highlight-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>ظرفیت واقعی کانتکس مدل را بسنجید و مصرف توکن هر بخش را لاگ کنید. بدون اندازه‌گیری، بهینه‌سازی ممکن نیست.</p>
</div>
<div class="timeline-dot"></div>
</div>
<div class="timeline-item">
<div class="timeline-content">
<h5>گام ۲ — بودجه‌بندی کانتکس</h5>
<p>برای هر بخش (سیستم، RAG، تاریخچه، خروجی) سهم مشخص تعیین کنید و مدیر بودجه پیاده کنید.</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>گام ۴ — خلاصه‌سازی و پنجره لغزان</h5>
<p>مکانیزم خلاصه‌سازی تدریجی و حفظ N پیام آخر را فعال کنید تا کانتکس هرگز سرریز نشود.</p>
</div>
<div class="timeline-dot"></div>
</div>
<div class="timeline-item">
<div class="timeline-content">
<h5>گام ۵ — Scratchpad و زیرایجنت</h5>
<p>برای وظایف پیچیده، نتایج میانی را خارجی کنید و در صورت نیاز کار را بین زیرایجنت‌ها تقسیم کنید.</p>
</div>
<div class="timeline-dot"></div>
</div>
<div class="timeline-item">
<div class="timeline-content">
<h5>گام ۶ — ارزیابی و تنظیم</h5>
<p>با مجموعه‌ای از سناریوهای واقعی، دقت ایجنت را بسنجید و پارامترها (اندازه پنجره، K بازیابی، آستانه خلاصه) را تنظیم کنید.</p>
</div>
<div class="timeline-dot"></div>
</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">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>پر کردن کامل کانتکس:</strong> اگر فضای خروجی نماند، مدل پاسخ ناقص می‌دهد. همیشه ۲۰٪ رزرو کنید.</li>
<li><strong>نادیده گرفتن Lost in the Middle:</strong> اطلاعات مهم را در ابتدا یا انتهای کانتکس قرار دهید، نه وسط.</li>
<li><strong>بازیابی بی‌ربط:</strong> RAG با K زیاد، کانتکس را با نویز پر می‌کند. کیفیت بر کمیت اولویت دارد.</li>
<li><strong>خلاصه‌سازی بیش از حد:</strong> اگر خلاصه خیلی فشرده باشد، جزئیات حیاتی از بین می‌رود.</li>
<li><strong>حافظه بدون فراموشی:</strong> انبار کردن همه‌چیز بدون حذف، سیستم را کند و متناقض می‌کند.</li>
<li><strong>عدم اندازه‌گیری مصرف توکن:</strong> بدون لاگ، نمی‌دانید کدام بخش کانتکس را می‌بلعد.</li>
</ul>
</div>
<div class="quote-box">
<p>مدیریت کانتکس خوب، مثل مدیریت حافظه انسانی است: نه همه‌چیز را به یاد می‌آوریم، نه همه‌چیز را فراموش می‌کنیم — آنچه را که در لحظه اهمیت دارد، به یاد می‌آوریم.</p>
<div class="source">— اصل طراحی ایجنت‌های حافظه‌محور</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>مکالمه طولانی</td>
<td>پنجره لغزان + خلاصه‌سازی</td>
<td>Sliding Window + Summary</td>
</tr>
<tr>
<td>دانش دامنه بزرگ</td>
<td>RAG با بازیابی ترکیبی</td>
<td>Vector DB + BM25</td>
</tr>
<tr>
<td>پروژه چندفازی</td>
<td>کانتکس سلسله‌مراتبی</td>
<td>L0 + L1 + L2</td>
</tr>
<tr>
<td>وظایف پیچیده</td>
<td>زیرایجنت‌ها</td>
<td>Sub-agent Pattern</td>
</tr>
<tr>
<td>استدلال طولانی</td>
<td>Scratchpad خارجی</td>
<td>File-based Memory</td>
</tr>
<tr>
<td>کنترل همیشگی</td>
<td>بودجه‌بندی کانتکس</td>
<td>Context Budget Manager</td>
</tr>
<tr class="highlight-row">
<td><strong>حافظه بلندمدت</strong></td>
<td colspan="2"><strong>ذخیره بیرونی + تجمیع دوره‌ای + فراموشی تدریجی</strong></td>
</tr>
</tbody>
</table>
<div class="conclusion-box">
<h3>🎯 پیشنهاد نهایی</h3>
<p>محدودیت کانتکس در مدل‌های محلی، یک ضعف نیست — یک محدودیت طراحی است که شما را به ساختن معماری هوشمندتر وادار می‌کند.</p>
<p>با ترکیب بودجه‌بندی کانتکس، حافظه بیرونی، خلاصه‌سازی و RAG، می‌توان ایجنتی ساخت که با ۸K توکن، بهتر از ایجنتی با ۱۰۰K توکنِ بی‌ساختار عمل کند.</p>
<div class="highlight">
<strong>💎 فرمول موفقیت:</strong><br>
ذخیره بیرونی هوشمند + بازیابی دقیق + خلاصه‌سازی به‌موقع + بودجه‌بندی سخت‌گیرانه
</div>
</div>
<div class="info-box">
<strong>📞 گام بعدی:</strong> اگر بخواهید، می‌توان معماری فوق را برای یک سناریوی مشخص (مثلاً ایجنت پشتیبانی مشتری یا دستیار کدنویسی) به‌صورت کامل و اجرایی پیاده‌سازی کرد — شامل مدیر بودجه کانتکس، حافظه سه‌لایه، RAG و ارزیابی.
</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>