From a2acb3b055cedd765e03b1d7f213cd49bbd34c52 Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Sun, 25 Oct 2026 22:25:20 +0330 Subject: [PATCH] Update xAiApi Submodule (Chat With Documents) and Add ChangeLog / Plan Documents ... - Update xAiApi Submodule Pointer to Chat With Documents (In-Memory RAG) Implementation; - Add Documents/Docs/ChangeLogs/1405-08-03.html (ChangeLog of Pushed Commits); - Add Documents/Docs/ChangeLogs/1405-08-03-uncommitted.html (ChangeLog of this Commit); - Add Documents/Docs/ChangeLogs/CommitMessage-1405-08-03.txt; - Add Documents/Markdown/plan1.md and plan1.html (Chat With Documents Implementation Plan); - ... --- .../ChangeLogs/1405-08-03-uncommitted.html | 236 +++++++++ Documents/Docs/ChangeLogs/1405-08-03.html | 229 ++++++++ .../ChangeLogs/CommitMessage-1405-08-03.txt | 91 ++++ Documents/Markdown/plan1.html | 487 ++++++++++++++++++ Documents/Markdown/plan1.md | 68 +++ xAiApi | 2 +- 6 files changed, 1112 insertions(+), 1 deletion(-) create mode 100644 Documents/Docs/ChangeLogs/1405-08-03-uncommitted.html create mode 100644 Documents/Docs/ChangeLogs/1405-08-03.html create mode 100644 Documents/Docs/ChangeLogs/CommitMessage-1405-08-03.txt create mode 100644 Documents/Markdown/plan1.html create mode 100644 Documents/Markdown/plan1.md diff --git a/Documents/Docs/ChangeLogs/1405-08-03-uncommitted.html b/Documents/Docs/ChangeLogs/1405-08-03-uncommitted.html new file mode 100644 index 0000000..5f1e0ed --- /dev/null +++ b/Documents/Docs/ChangeLogs/1405-08-03-uncommitted.html @@ -0,0 +1,236 @@ + + + + + +گزارش تغییرات کامیت‌نشده ۱۴۰۵/۰۸/۰۳ - xSaherelmWorkspace + + + +
+ +
+

گزارش تغییرات کامیت‌نشده (Uncommitted ChangeLog)

+
یکشنبه ۳ آبان ۱۴۰۵  |  2026-10-25  |  xSaherelmWorkspace
+
+ +
+
نویسنده: Hadi Khazaee Asl
+
برنچ: master
+
وضعیت: تغییرات در Working Tree، هنوز کامیت نشده‌اند
+
پایهٔ کار: workspace 43f5cc0  /  xAiApi 47d8a19
+
+ +
+ + +
+

خلاصه

+
+
2
مخزن تغییریافته
+
5
فایل جدید
+
9
فایل تغییریافته
+
5
Endpoint جدید
+
+1,090
خط اضافه‌شده (xAiApi)
+
-170
خط حذف‌شده (xAiApi)
+
+

+ این گزارش ادامهٔ کارهای ثبت‌شده در ChangeLog ۳ آبان ۱۴۰۵ است و تکمیل قابلیت + Chat With Documents (RAG درون‌حافظه‌ای) در پروژهٔ xAiApi را پوشش می‌دهد: + تعریف مدل‌های DTO، افزودن متدهای AskWithContext به سرویس پایهٔ AI، + بازنویسی کامل ChatWithDocumentsController با Endpointهای Ingest / Ask / AskStream / Session، + سرویس پس‌زمینهٔ پاک‌سازی Sessionهای منقضی و پیکربندی RAGDescriptor. + در مخزن اصلی نیز فایل‌های مستندات (ChangeLog و طرح plan1) اضافه شده‌اند. +

+
+ + +
+

۱. مخزن اصلی: xSaherelmWorkspace

+

ریموت: git.saherelm.ir/saherelm/xSaherelmWorkspace.git

+ +
+
+ تغییرات کامیت‌نشده + بر پایهٔ 43f5cc0  |  git status +
+
4 مورد  |  3 فایل جدید + 1 ساب‌ماژول تغییریافته
+ + + + + + + + +
نوعفایلتوضیح
جدیدDocuments/Docs/ChangeLogs/1405-08-03.htmlگزارش تغییرات پوش‌شدهٔ ۳ آبان (TextChunker و In-Memory Vector Store)
جدیدDocuments/Markdown/plan1.mdطرح گام‌به‌گام تکمیل Chat With Documents (وضعیت فعلی، گام‌های ۱ تا ۸ و نکات تصمیم‌گیری)
جدیدDocuments/Markdown/plan1.htmlنسخهٔ HTML طرح فوق
ساب‌ماژولxAiApiمحتوای ساب‌ماژول تغییر کرده است (Submodule xAiApi contains modified content)؛ پس از کامیت در xAiApi باید پوینتر به‌روزرسانی شود
+
+
+ + +
+

۲. ساب‌ماژول: xAiApi

+

ریموت: git.saherelm.ir/saherelm/xSaherElmAiApi.git

+ +
+
+ Implement Chat With Documents (In-Memory RAG) ... + کامیت‌نشده  |  بر پایهٔ 47d8a19 +
+
11 فایل (۲ جدید، ۹ تغییریافته)  |  +1,090 / -170
+ + + + + + + + + + + + + + + +
نوعفایلتغییرتوضیح
جدیدModels/XAiDocumentModels.cs+181مدل‌های DTO: XAiDocumentIngestResult, XAiDocumentAskRequest, XAiDocumentSource, XAiDocumentAnswer, XAiDocumentAnswerChunk (SSE), XAiDocumentSessionInfo, XAiDocumentRAGDescriptor
جدیدProviders/XInMemoryVectorStoreCleanupService.cs+77BackgroundService که هر ۵ دقیقه RemoveExpired() را روی Vector Store اجرا و نتیجه را لاگ می‌کند
تغییرControllers/ChatWithDocumentsController.cs+672بازنویسی کامل: اصلاح using به Microsoft.AspNetCore.Mvc، حذف فیلد محلی و IDisposable، تزریق IXTextChunker/IXInMemoryVectorStore/IXDefaultEmbeddingService/IXFileContentExtractor/XAiApiConfiguration، پیاده‌سازی ۵ Endpoint و متدهای کمکی ExtractText, EmbedChunks, Retrieve, ToSources, GetMetadata, WriteChunk
تغییرProviders/XAIServiceBase.cs+183 / -5region جدید «Context Actions»: AskWithContextAsync, AskWithContextAsEnumerable, GetContextHistory (Introduction + پرامپت RAG + بلوک Context + سؤال کاربر) و BuildContextBlock؛ بدون ذخیره در دیتابیس؛ حذف usingهای بلااستفاده
تغییرInterfaces/IXAiServiceBase.cs+34 / -1افزودن امضای AskWithContextAsync و AskWithContextAsEnumerable در region «Context Actions»
تغییرProviders/XInMemoryVectorStore.cs+62 / -26بهینه‌سازی Search: تبدیل queryEmbedding.ToArray() یک‌بار قبل از حلقه؛ فرمت‌دهی امضاها و انتقال متدهای خصوصی به region «Private»
تغییرExtensions/XModelsExtensions.cs+21 / -1افزودن IsValid(this XAiDocumentAskRequest) (بررسی Prompt، SessionId، بازهٔ TopK و MinScore)
تغییرDI/XDIHelperExtension.cs+11ثبت XInMemoryVectorStoreCleanupService به‌عنوان HostedService و افزودن AddXAiChatWithDocuments()
تغییرConfigurations/XAiApiConfiguration.cs+5افزودن پراپرتی RAGDescriptor از نوع XAiDocumentRAGDescriptor
تغییرStartup.cs+3 / -3جایگزینی AddXAiInMemoryVectorStore() با AddXAiChatWithDocuments()
تغییرappsettings.Development.json+18 (محتوایی)افزودن پرامپت ChatWithDocuments به Prompts و بخش RAGDescriptor؛ دیف خام (+142/-124) ناشی از تغییر فاصله/Line Ending است
+ +

Endpointهای کنترلر ChatWithDocuments

+ + + + + + + + + +
متدمسیرورودیخروجی / توضیح
POST/ChatWithDocuments/Ingestmultipart/form-data (files, sessionId?, ttlMinutes?)XAiDocumentIngestResult — Extract → Chunk → Embed (Batch) → Store؛ فایل‌های ناسازگار/خالی در SkippedFiles
POST/ChatWithDocuments/AskXAiDocumentAskRequestXAiDocumentAnswer — Embedding سؤال → Search → AskWithContextAsync؛ در نبود Context: NoContextFound = true
POST/ChatWithDocuments/AskStreamXAiDocumentAskRequestSSE (text/event-stream) — رویداد اول Sources، سپس Delta‌های متن، رویداد آخر IsFinal = true
GET/ChatWithDocuments/Session/{id}GuidXAiDocumentSessionInfo (بدون Embedding) یا 404
DELETE/ChatWithDocuments/Session/{id}Guidحذف Session؛ 200 / 404
+ +

پیکربندی جدید در appsettings.Development.json

+
"RAGDescriptor": {
+    "MaxTopK": 20,
+    "DefaultTopK": 5,
+    "DefaultTtlMinutes": 30,
+    "EmbeddingBatchSize": 32,
+    "SourceSnippetLength": 200,
+    "DefaultMinScore": "0.35f",
+    "MetadataFileName": "fileName",
+    "MetadataMimeType": "mimeType",
+    "PromptName": "ChatWithDocuments",
+    "MetadataChunkIndex": "chunkIndex"
+}
+
{
+    "Name": "ChatWithDocuments",
+    "Template": "You are a document question-answering assistant. Answer the user's question ONLY based on the provided Context chunks below. ...",
+    "Description": "System prompt for RAG based Chat With Documents",
+    "Params": []
+}
+
+ +

نکات فنی

+
    +
  • تمام دسترسی‌ها به Session با ownerId = (await GetUserInfo()).UserId محدود می‌شود و همهٔ Endpointها از الگوی try/catch + GetExceptionActionResult(ex) پیروی می‌کنند.
  • +
  • Context به‌صورت پیام System جداگانه (پرامپت RAG + بلوک Chunkها با Identifier) به مدل ارسال می‌شود؛ هیچ‌چیز در دیتابیس ذخیره نمی‌شود.
  • +
  • TopK با Math.Clamp(1, MaxTopK) محدود می‌شود و مقادیر پیش‌فرض از RAGDescriptor خوانده می‌شوند.
  • +
  • Embedding تکه‌ها به‌صورت Batch (EmbeddingBatchSize = 32) انجام می‌شود و Metadata هر Vector شامل fileName, mimeType, chunkIndex است.
  • +
  • سرویس پاک‌سازی به‌صورت HostedService داخل AddXAiInMemoryVectorStore() ثبت می‌شود.
  • +
+
+ هشدار پیکربندی: مقدار "DefaultMinScore": "0.35f" در appsettings.Development.json به‌صورت رشته با پسوند f نوشته شده است. + بایندر پیکربندی .NET نمی‌تواند آن را به float تبدیل کند و هنگام Bind خطا می‌دهد؛ باید قبل از کامیت به 0.35 (عدد) اصلاح شود. +
+
+ کارهای باقی‌مانده طبق طرح (plan1.md): افزودن RAGDescriptor و پرامپت ChatWithDocuments به appsettings.json (Production)، + تست دستی جریان Ingest → Ask → AskStream → Delete با یک PDF، تصمیم دربارهٔ پروژهٔ یونیت‌تست (xAiApi.Tests) و پاک‌سازی فایل‌های موقت tmp.md / tmp.html. +
+
+ + +
+

۳. مخازن بدون تغییر

+

در ۳۲ ساب‌ماژول زیر تغییر کامیت‌نشده‌ای وجود ندارد:

+
    +
  • Modules (۲۸ مورد): xAiModels, xAiService, xApiSDKHolder, xCategoryService, xCommons, xContentService, xDataService, xEventService, xExceptions, xFileService, xFrameworkComponentsHolder, xFrameworkCoreHolder, xFrameworkIdentitySDKHolder, xFrameworkPushServiceHolder, xFrameworkServicesHolder, xHttpService, xIdentityHelper, xIdentityModels, xIdentityService, xMessageService, xModels, xPushService, xServices, xStorageService, xStringService, xTagService, xTermsAndConditionsService, xWebinarService
  • +
  • Applications (۴ مورد): xApi, xClient, xIds, xFrameworkTest
  • +
+
+ + +
+

۴. دستورات استفاده‌شده برای استخراج تغییرات

+
git status --porcelain=v1 -uall
+git diff --submodule=log xAiApi
+git -C xAiApi status --porcelain=v1 -uall
+git -C xAiApi diff --stat
+git -C xAiApi diff --ignore-all-space --stat -- appsettings.Development.json
+

پیام‌های کامیت پیشنهادی در فایل CommitMessage-1405-08-03.txt کنار همین گزارش قرار دارد.

+
+ +
+ + +
+ + + diff --git a/Documents/Docs/ChangeLogs/1405-08-03.html b/Documents/Docs/ChangeLogs/1405-08-03.html new file mode 100644 index 0000000..345511b --- /dev/null +++ b/Documents/Docs/ChangeLogs/1405-08-03.html @@ -0,0 +1,229 @@ + + + + + +گزارش تغییرات ۱۴۰۵/۰۸/۰۳ - xSaherelmWorkspace + + + +
+ +
+

گزارش تغییرات (ChangeLog)

+
یکشنبه ۳ آبان ۱۴۰۵  |  2026-10-25  |  xSaherelmWorkspace
+
+ +
+
نویسنده: Hadi Khazaee Asl
+
برنچ: master
+
وضعیت: همه کامیت‌ها به origin/master پوش شده‌اند
+
بازه زمانی: 17:23 تا 17:58 (+03:30)
+
+ +
+ + +
+

خلاصه

+
+
2
مخزن تغییریافته
+
4
کامیت پوش‌شده
+
7
فایل جدید
+
16
فایل تغییریافته (کل)
+
+1,454
خط اضافه‌شده
+
-9
خط حذف‌شده
+
+

+ محور اصلی کارهای امروز، پیاده‌سازی زیرساخت Chat With Documents در پروژه + xAiApi بود: افزودن سرویس تکه‌بندی متن (TextChunker) و + مخزن برداری درون‌حافظه‌ای (In-Memory Vector Store) به‌همراه ریفکتور ثبت سرویس‌ها در DI. + در مخزن اصلی ورک‌اسپیس نیز یادداشت طرح (tmp.md / tmp.html) و پوینتر ساب‌ماژول xAiApi به‌روزرسانی شد. +

+
+ + +
+

۱. مخزن اصلی: xSaherelmWorkspace

+

ریموت: git.saherelm.ir/saherelm/xSaherelmWorkspace.git

+ +
+
+ last works ... + 2c991e7  |  2026-10-25 17:23:25 +
+
3 فایل  |  +419 / -1
+ + + + + + + +
نوعفایلتغییرتوضیح
جدیدDocuments/Markdown/tmp.md+24طرح نهایی «Chat With Documents» (SessionId به‌صورت Guid، جریان Ingest → Ask، TTL، TopK/MinScore)
جدیدDocuments/Markdown/tmp.html+394نسخه HTML همان طرح
ساب‌ماژولxAiApi+1 / -1پوینتر از 4b857de به 429b97c
+
+ +
+
+ last ... + 43f5cc0  |  2026-10-25 17:58:14 +
+
1 فایل  |  +1 / -1
+ + + + + +
نوعفایلتغییرتوضیح
ساب‌ماژولxAiApi+1 / -1پوینتر از 429b97c به 47d8a19
+
+
+ + +
+

۲. ساب‌ماژول: xAiApi

+

ریموت: git.saherelm.ir/saherelm/xSaherElmAiApi.git

+ +
+
+ add TextChunker Service for AI Project ... + 429b97c  |  2026-10-25 17:23:03 +
+
9 فایل  |  +760 / -6
+ + + + + + + + + + + + + +
نوعفایلتغییرتوضیح
جدیدInterfaces/IXTextChunker.cs+32قرارداد سرویس تکه‌بندی: ChunkText(text, source) و ChunkTexts(text)
جدیدProviders/XTextChunker.cs+588پیاده‌سازی تکه‌بندی متن با ChunkSize/Overlap، نرمال‌سازی فاصله‌ها و ادغام تکه‌های کوچک
جدیدModels/XTextChunk.cs+38مدل خروجی تکه: Index, Text, Source, Identifier (مثل file.pdf#3), StartOffset, Length
جدیدModels/XTextChunkerDescriptor.cs+33تنظیمات: ChunkSize=800, ChunkOverlap=150, MinChunkSize=50, ChunkIdentifierSeparator="#", NormalizeWhitespace=true
تغییرConfigurations/XAiApiConfiguration.cs+5افزودن پراپرتی TextChunker از نوع XTextChunkerDescriptor
تغییرDI/XDIHelperExtension.cs+29افزودن AddXAiServices() (انتقال ثبت سرویس‌های AI از Startup) و AddXAiTextChunker() (Singleton)
تغییرExtensions/XModelsExtensions.cs+21 / -1افزودن IsValid(this XTextChunkerDescriptor) برای اعتبارسنجی تنظیمات
تغییرStartup.cs+6 / -4جایگزینی ثبت مستقیم سرویس‌ها با AddXAiTextChunker() و AddXAiServices()
تغییرappsettings.Development.json+8 / -1افزودن بخش TextChunker به پیکربندی
+
"TextChunker": {
+    "ChunkSize": 800,
+    "ChunkOverlap": 150,
+    "MinChunkSize": 50,
+    "NormalizeWhitespace": true,
+    "ChunkIdentifierSeparator": "#"
+}
+
+ +
+
+ last ... + 47d8a19  |  2026-10-25 17:58:05 +
+
5 فایل  |  +275 / -2
+ + + + + + + + + +
نوعفایلتغییرتوضیح
جدیدInterfaces/IXInMemoryVectorStore.cs+48قرارداد مخزن برداری: CreateSession, Replace, Add, Search(sessionId, queryEmbedding, topK, minScore), TryGet, Remove, RemoveExpired, Count
جدیدProviders/XInMemoryVectorStore.cs+197پیاده‌سازی Singleton درون‌حافظه‌ای مبتنی بر Session (Guid)، پشتیبانی از TTL و بررسی OwnerId
جدیدModels/XAiDocumentSession.cs+18مدل Session: Id, Ttl, OwnerId, CreatedAtUtc, LastAccessAtUtc, Vectors, IsExpired()
تغییرDI/XDIHelperExtension.cs+8افزودن AddXAiInMemoryVectorStore()
تغییرStartup.cs+4 / -2فراخوانی AddXAiInMemoryVectorStore() و حذف usingهای بلااستفاده
+
+ +

نکات فنی

+
    +
  • هر دو سرویس XTextChunker و XInMemoryVectorStore به‌صورت Singleton ثبت شده‌اند (Stateless / حافظه مشترک).
  • +
  • داده‌های Vector Store فقط در حافظه نگه‌داری می‌شوند و با انقضای TTL یا حذف Session پاک می‌شوند؛ چیزی در دیتابیس یا دیسک نوشته نمی‌شود.
  • +
  • ثبت سرویس‌های IXDefaultAiService, IXDefaultEmbeddingService, IXDefaultThinkingAiService از Startup.cs به متد AddXAiServices() منتقل شد.
  • +
+
+ کارهای باقی‌مانده طبق طرح (tmp.md): افزودن AskWithContextAsync به IXAiServiceBase، + بازنویسی ChatWithDocumentsController (Endpointهای Ingest / Ask / AskStream / Session)، + ثوابت پرامپت در XAiApiConstants، و یونیت‌تست‌های XTextChunker و XInMemoryVectorStore.Search در xFrameworkTest. +
+
+ + +
+

۳. مخازن بدون تغییر

+

در ۳۲ ساب‌ماژول زیر امروز هیچ کامیتی ثبت نشده است:

+
    +
  • Modules (۲۸ مورد): xAiModels, xAiService, xApiSDKHolder, xCategoryService, xCommons, xContentService, xDataService, xEventService, xExceptions, xFileService, xFrameworkComponentsHolder, xFrameworkCoreHolder, xFrameworkIdentitySDKHolder, xFrameworkPushServiceHolder, xFrameworkServicesHolder, xHttpService, xIdentityHelper, xIdentityModels, xIdentityService, xMessageService, xModels, xPushService, xServices, xStorageService, xStringService, xTagService, xTermsAndConditionsService, xWebinarService
  • +
  • Applications (۴ مورد): xApi, xClient, xIds, xFrameworkTest
  • +
+
+ + +
+

۴. دستورات استفاده‌شده برای استخراج لاگ

+
git log --since="2026-10-25 00:00" --until="2026-10-26 00:00" --stat --date=iso
+git -C xAiApi log --since="2026-10-25 00:00" --stat --date=iso
+git status -sb            # master...origin/master (in sync)
+
+ +
+ + +
+ + + diff --git a/Documents/Docs/ChangeLogs/CommitMessage-1405-08-03.txt b/Documents/Docs/ChangeLogs/CommitMessage-1405-08-03.txt new file mode 100644 index 0000000..a07d488 --- /dev/null +++ b/Documents/Docs/ChangeLogs/CommitMessage-1405-08-03.txt @@ -0,0 +1,91 @@ +=============================================================================== + Commit Messages - xSaherelmWorkspace + تاریخ: یکشنبه ۱۴۰۵/۰۸/۰۳ (2026-10-25) + نویسنده: Hadi Khazaee Asl + گزارش کامل: Documents/Docs/ChangeLogs/1405-08-03-uncommitted.html +=============================================================================== + +ترتیب کامیت: ابتدا ساب‌ماژول xAiApi، سپس مخزن اصلی (برای به‌روزرسانی پوینتر ساب‌ماژول). +توجه: پیش از کامیت، مقدار "DefaultMinScore": "0.35f" در xAiApi/appsettings.Development.json + باید به 0.35 (عدد) اصلاح شود. + + +------------------------------------------------------------------------------- + [1] xAiApi (https://git.saherelm.ir/saherelm/xSaherElmAiApi.git) - 1405/08/03 +------------------------------------------------------------------------------- +Implement Chat With Documents (In-Memory RAG) ... + +- Add XAiDocumentModels (IngestResult, AskRequest, Answer, AnswerChunk, Source, SessionInfo, RAGDescriptor DTOs); +- Add AskWithContextAsync and AskWithContextAsEnumerable (Context Actions) to IXAiServiceBase and XAIServiceBase; +- Add GetContextHistory and BuildContextBlock Helpers for Building RAG Chat History without Persisting; +- Rewrite ChatWithDocumentsController with Ingest, Ask, AskStream (SSE), Get Session and Delete Session Endpoints; +- Add XInMemoryVectorStoreCleanupService (BackgroundService) for Removing Expired Sessions Periodically; +- Add AddXAiChatWithDocuments DI Extension, Register Cleanup HostedService and use it in Startup; +- Add RAGDescriptor Property to XAiApiConfiguration and its Section to appsettings.Development.json; +- Add ChatWithDocuments System Prompt to Prompts in appsettings.Development.json; +- Add IsValid Extension Method for XAiDocumentAskRequest; +- Optimize XInMemoryVectorStore.Search (Query Embedding Converted once) and Refactor Formatting; +- Remove Unused Usings in XAIServiceBase and IXAiServiceBase; + +Files: + A Models/XAiDocumentModels.cs + A Providers/XInMemoryVectorStoreCleanupService.cs + M Controllers/ChatWithDocumentsController.cs + M Providers/XAIServiceBase.cs + M Interfaces/IXAiServiceBase.cs + M Providers/XInMemoryVectorStore.cs + M Extensions/XModelsExtensions.cs + M DI/XDIHelperExtension.cs + M Configurations/XAiApiConfiguration.cs + M Startup.cs + M appsettings.Development.json + + +------------------------------------------------------------------------------- + [2] xSaherelmWorkspace (https://git.saherelm.ir/saherelm/xSaherelmWorkspace.git) - 1405/08/03 +------------------------------------------------------------------------------- +Update xAiApi Submodule (Chat With Documents) and Add ChangeLog / Plan Documents ... + +- Update xAiApi Submodule Pointer to Chat With Documents (In-Memory RAG) Implementation; +- Add Documents/Docs/ChangeLogs/1405-08-03.html (ChangeLog of Pushed Commits); +- Add Documents/Docs/ChangeLogs/1405-08-03-uncommitted.html (ChangeLog of this Commit); +- Add Documents/Docs/ChangeLogs/CommitMessage-1405-08-03.txt; +- Add Documents/Markdown/plan1.md and plan1.html (Chat With Documents Implementation Plan); + +Files: + M xAiApi + A Documents/Docs/ChangeLogs/1405-08-03.html + A Documents/Docs/ChangeLogs/1405-08-03-uncommitted.html + A Documents/Docs/ChangeLogs/CommitMessage-1405-08-03.txt + A Documents/Markdown/plan1.md + A Documents/Markdown/plan1.html + + +------------------------------------------------------------------------------- + دستورات پیشنهادی +------------------------------------------------------------------------------- +# 1) xAiApi +cd xAiApi +git add -A +git commit -m "Implement Chat With Documents (In-Memory RAG) ..." -m "- Add XAiDocumentModels (IngestResult, AskRequest, Answer, AnswerChunk, Source, SessionInfo, RAGDescriptor DTOs); +- Add AskWithContextAsync and AskWithContextAsEnumerable (Context Actions) to IXAiServiceBase and XAIServiceBase; +- Add GetContextHistory and BuildContextBlock Helpers for Building RAG Chat History without Persisting; +- Rewrite ChatWithDocumentsController with Ingest, Ask, AskStream (SSE), Get Session and Delete Session Endpoints; +- Add XInMemoryVectorStoreCleanupService (BackgroundService) for Removing Expired Sessions Periodically; +- Add AddXAiChatWithDocuments DI Extension, Register Cleanup HostedService and use it in Startup; +- Add RAGDescriptor Property to XAiApiConfiguration and its Section to appsettings.Development.json; +- Add ChatWithDocuments System Prompt to Prompts in appsettings.Development.json; +- Add IsValid Extension Method for XAiDocumentAskRequest; +- Optimize XInMemoryVectorStore.Search (Query Embedding Converted once) and Refactor Formatting; +- Remove Unused Usings in XAIServiceBase and IXAiServiceBase;" +git push origin master +cd .. + +# 2) xSaherelmWorkspace +git add xAiApi Documents/Docs/ChangeLogs Documents/Markdown/plan1.md Documents/Markdown/plan1.html +git commit -m "Update xAiApi Submodule (Chat With Documents) and Add ChangeLog / Plan Documents ..." -m "- Update xAiApi Submodule Pointer to Chat With Documents (In-Memory RAG) Implementation; +- Add Documents/Docs/ChangeLogs/1405-08-03.html (ChangeLog of Pushed Commits); +- Add Documents/Docs/ChangeLogs/1405-08-03-uncommitted.html (ChangeLog of this Commit); +- Add Documents/Docs/ChangeLogs/CommitMessage-1405-08-03.txt; +- Add Documents/Markdown/plan1.md and plan1.html (Chat With Documents Implementation Plan);" +git push origin master diff --git a/Documents/Markdown/plan1.html b/Documents/Markdown/plan1.html new file mode 100644 index 0000000..0cc4261 --- /dev/null +++ b/Documents/Markdown/plan1.html @@ -0,0 +1,487 @@ + + + +plan1.md + + + + + + + + + + + + +

‌xFrameworkTest یک پروژه Cordova/JS است، نه پروژه تست .NET — بنابراین یونیت‌تست C# جایی برای قرارگیری ندارد (در برنامه لحاظ می‌کنم).

+

وضعیت فعلی (آنچه انجام شده)

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
مؤلفهوضعیتمحل
IXTextChunker / XTextChunkerانجام شده، در DI ثبت شدهxAiApi\Providers\XTextChunker.cs
XTextChunk, XTextChunkerDescriptor + IsValid() + کانفیگ TextChunkerانجام شدهModels\, Extensions\XModelsExtensions.cs:51, appsettings.Development.json
IXInMemoryVectorStore / XInMemoryVectorStore / XAiDocumentSessionانجام شده، در DI ثبت شده (TTL پیش‌فرض ۳۰ دقیقه، OwnerId، CosineSimilarity)xAiApi\Providers\XInMemoryVectorStore.cs
ChatWithDocumentsControllerاسکلت خالی — هنوز IList<XAiVectorDto> vectorStore محلی دارد، using Microsoft.AspNetCore.Components اشتباه (باید Mvc باشد)، هیچ Endpoint ندارد، IXInMemoryVectorStore/IXTextChunker تزریق نشدهxAiApi\Controllers\ChatWithDocumentsController.cs:6,30
AskWithContextAsync در IXAiServiceBaseانجام نشده—
پرامپت ChatWithDocuments در appsettings + ثابت‌هاانجام نشده (فقط Introduction/ContentExtraction/Embedding وجود دارد)Constants\XAiApiConstants.cs:14-22
مدل‌های Request/Responseانجام نشده—
ثبت RemoveExpired دوره‌ایانجام نشده (فقط lazy داخل Count/TryGet)—
+

گام‌های باقی‌مانده

+

گام ۱ — ثوابت و پرامپت

+ +

گام ۲ — مدل‌های DTO (پوشه xAiApi\Models\)

+ +

گام ۳ — سرویس پایه: AskWithContextAsync / AskWithContextAsEnumerable

+ +

گام ۴ — بازنویسی ChatWithDocumentsController

+ +

گام ۵ — DI و Startup

+ +

گام ۶ — بهبودهای کوچک Vector Store (اختیاری ولی توصیه‌شده)

+ +

گام ۷ — اعتبارسنجی

+ +

گام ۸ — مستندسازی

+ +

نکات تصمیم‌گیری

+
    +
  1. Context در پرامپت یا پیام جدا؟ پیشنهاد: پیام System جداگانه (بدون نیاز به placeholder).
  2. +
  3. Ingest روی Session موجود (Add) پشتیبانی شود یا هر Ingest، Session جدید بسازد؟ پیشنهاد: پارامتر اختیاری sessionId.
  4. +
  5. یونیت‌تست: ساخت پروژه xAiApi.Tests یا صرف‌نظر؟
  6. +
+

اگر طرح مورد تأیید است، گزینه‌های بالا را مشخص کنید و به Act mode سوئیچ کنید (toggle to Act mode) تا پیاده‌سازی را از گام ۱ شروع کنم.

+ + + diff --git a/Documents/Markdown/plan1.md b/Documents/Markdown/plan1.md new file mode 100644 index 0000000..814ec9a --- /dev/null +++ b/Documents/Markdown/plan1.md @@ -0,0 +1,68 @@ +‌`xFrameworkTest` یک پروژه Cordova/JS است، نه پروژه تست .NET — بنابراین یونیت‌تست C# جایی برای قرارگیری ندارد (در برنامه لحاظ می‌کنم). + +## وضعیت فعلی (آنچه انجام شده) + +| مؤلفه | وضعیت | محل | +|---|---|---| +| `IXTextChunker` / `XTextChunker` | انجام شده، در DI ثبت شده | `xAiApi\Providers\XTextChunker.cs` | +| `XTextChunk`, `XTextChunkerDescriptor` + `IsValid()` + کانفیگ `TextChunker` | انجام شده | `Models\`, `Extensions\XModelsExtensions.cs:51`, `appsettings.Development.json` | +| `IXInMemoryVectorStore` / `XInMemoryVectorStore` / `XAiDocumentSession` | انجام شده، در DI ثبت شده (TTL پیش‌فرض ۳۰ دقیقه، OwnerId، CosineSimilarity) | `xAiApi\Providers\XInMemoryVectorStore.cs` | +| `ChatWithDocumentsController` | **اسکلت خالی** — هنوز `IList vectorStore` محلی دارد، `using Microsoft.AspNetCore.Components` اشتباه (باید `Mvc` باشد)، هیچ Endpoint ندارد، `IXInMemoryVectorStore`/`IXTextChunker` تزریق نشده | `xAiApi\Controllers\ChatWithDocumentsController.cs:6,30` | +| `AskWithContextAsync` در `IXAiServiceBase` | **انجام نشده** | — | +| پرامپت `ChatWithDocuments` در appsettings + ثابت‌ها | **انجام نشده** (فقط Introduction/ContentExtraction/Embedding وجود دارد) | `Constants\XAiApiConstants.cs:14-22` | +| مدل‌های Request/Response | **انجام نشده** | — | +| ثبت `RemoveExpired` دوره‌ای | **انجام نشده** (فقط lazy داخل `Count`/`TryGet`) | — | + +## گام‌های باقی‌مانده + +### گام ۱ — ثوابت و پرامپت +- `xAiApi\Constants\XAiApiConstants.cs`: افزودن + `XAiApiChatWithDocumentsPromptName = "ChatWithDocuments"`, `XAiDocumentDefaultTopK = 5`, `XAiDocumentDefaultMinScore = 0.35f`, `XAiDocumentDefaultTtlMinutes = 30`, کلیدهای Metadata (`fileName`, `mimeType`, `chunkIndex`). +- `appsettings.Development.json` و `appsettings.json`: افزودن پرامپت `ChatWithDocuments` به آرایه `Prompts` (دستور پاسخ‌دهی فقط بر اساس Context، ذکر منبع، و اعلام «در اسناد یافت نشد»). پرامپت باید placeholder برای Context داشته باشد یا Context به‌صورت پیام جداگانه System ارسال شود (پیشنهاد: پیام جدا، ساده‌تر). + +### گام ۲ — مدل‌های DTO (پوشه `xAiApi\Models\`) +- `XAiDocumentIngestResult` — `SessionId`, `FilesCount`, `ChunksCount`, `SkippedFiles[]`, `ExpiresAtUtc` +- `XAiDocumentAskRequest` — `SessionId`, `Prompt`, `TopK?`, `MinScore?` + متد `IsValid()` در `XModelsExtensions` +- `XAiDocumentAnswer` — `Answer`, `NoContextFound`, `Sources: IList` (`Identifier`, `FileName`, `ChunkIndex`, `Score`, `Snippet`) +- `XAiDocumentSessionInfo` — خروجی `GET Session/{id}` بدون Embeddingها (`Id`, `CreatedAtUtc`, `LastAccessAtUtc`, `Ttl`, `VectorsCount`, فهرست فایل‌ها) + +### گام ۳ — سرویس پایه: `AskWithContextAsync` / `AskWithContextAsEnumerable` +- `Interfaces\IXAiServiceBase.cs`: افزودن region جدید «Context Actions» با دو امضا: + `Task AskWithContextAsync(string prompt, IEnumerable context, CancellationToken)` و نسخه `IAsyncEnumerable`. +- `Providers\XAIServiceBase.cs`: پیاده‌سازی مشابه `AskTextAsync` (:97) ولی History = + `[System: Introduction] + [System: ChatWithDocuments-Prompt + Context متنی (هر chunk با شماره و Identifier)] + [User: prompt]`. + **بدون** هیچ تماسی با `dataProvider` (ذخیره در DB نمی‌شود). متد کمکی private `BuildContextBlock(IEnumerable)`. + +### گام ۴ — بازنویسی `ChatWithDocumentsController` +- اصلاح `using Microsoft.AspNetCore.Components` → `Microsoft.AspNetCore.Mvc`؛ حذف فیلد `vectorStore` و `IDisposable`. +- تزریق: `IXDefaultAiService`, `IXDefaultEmbeddingService`, `IXFileContentExtractor`, `IXTextChunker`, `IXInMemoryVectorStore`, `XAiApiConfiguration`. +- Endpointها (الگو از `XAiServiceControllerBase.cs:718-760` و SSE از `:655-710`): + - `POST Ingest` (`multipart/form-data`, `IFormFileCollection`, اختیاری `sessionId` برای افزودن به Session موجود): هر فایل → `CanExtract` → `ExtractRichAsync` → متن = `Text ?? AudioTranscript` → `textChunker.Chunk(text, fileName)` → `embeddingService.Embeddings(batch)` → ساخت `XAiVectorDto` با `Identifier = chunk.Identifier`, `Metadata{fileName, mimeType, chunkIndex}` → `vectorStore.CreateSession(vectors, ownerId)` یا `Add(...)` → بازگشت `XAiDocumentIngestResult`. + - `POST Ask` (`[FromBody] XAiDocumentAskRequest`): `embeddingService.Embedding(prompt)` → `vectorStore.Search(sessionId, emb, topK, minScore, ownerId)` → اگر خالی: `{ NoContextFound = true }` → وگرنه `aiService.AskWithContextAsync` → `XAiDocumentAnswer` با Sources. + - `POST AskStream`: همان جریان با `AskWithContextAsEnumerable` و SSE (کپی هدرها و `DisableBuffering` از کنترلر پایه). + - `GET Session/{id}`: `TryGet` → `XAiDocumentSessionInfo` یا 404. + - `DELETE Session/{id}`: `Remove` → 200/404. +- همه با `try/catch` + `GetExceptionActionResult(ex)` و `ownerId = (await GetUserInfo()).UserId` طبق الگوی موجود. + +### گام ۵ — DI و Startup +- `DI\XDIHelperExtension.cs`: متد `AddXAiChatWithDocuments()` که `AddXAiTextChunker()` + `AddXAiInMemoryVectorStore()` را صدا می‌زند؛ `Startup.cs:191-196` به یک فراخوانی ساده‌تر تبدیل شود. +- (اختیاری) یک `BackgroundService`/`Timer` ساده برای `RemoveExpired()` هر ۵ دقیقه تا حافظه نشت نکند. + +### گام ۶ — بهبودهای کوچک Vector Store (اختیاری ولی توصیه‌شده) +- `XInMemoryVectorStore.cs:138-139`: `queryEmbedding.ToArray()` در هر iteration حلقه تکرار می‌شود → یک‌بار قبل از حلقه. +- محدودیت حداکثر تعداد Session / Vector برای جلوگیری از OOM (ثابت در Constants). + +### گام ۷ — اعتبارسنجی +- `dotnet build xAiApi\xAiApi.csproj` +- تست دستی: `POST ChatWithDocuments/Ingest` با یک PDF → دریافت `SessionId` → `POST Ask` با سؤال مرتبط و نامرتبط (بررسی `NoContextFound`) → `AskStream` → `DELETE Session`. +- **یونیت‌تست:** `xFrameworkTest` پروژه Cordova/JS است، نه .NET؛ پس یا تست‌ها حذف شوند یا یک پروژه `xAiApi.Tests` (xUnit) جدید ساخته شود — نیاز به تصمیم شما. + +### گام ۸ — مستندسازی +- به‌روزرسانی `Documents\Docs\ChangeLogs\1405-08-03.html` (یا ChangeLog روز بعد) و حذف فایل‌های موقت `Documents\Markdown\tmp.md/tmp.html` یا انتقال به `Documents\Docs\Developer\17_ChatWithDocuments.html`. + +## نکات تصمیم‌گیری +1. **Context در پرامپت یا پیام جدا؟** پیشنهاد: پیام System جداگانه (بدون نیاز به placeholder). +2. **Ingest روی Session موجود** (`Add`) پشتیبانی شود یا هر Ingest، Session جدید بسازد؟ پیشنهاد: پارامتر اختیاری `sessionId`. +3. **یونیت‌تست**: ساخت پروژه `xAiApi.Tests` یا صرف‌نظر؟ + +اگر طرح مورد تأیید است، گزینه‌های بالا را مشخص کنید و به **Act mode** سوئیچ کنید (toggle to Act mode) تا پیاده‌سازی را از گام ۱ شروع کنم. \ No newline at end of file diff --git a/xAiApi b/xAiApi index 47d8a19..47da04a 160000 --- a/xAiApi +++ b/xAiApi @@ -1 +1 @@ -Subproject commit 47d8a1942330502599b8f9db919e32cda3fca27a +Subproject commit 47da04a5946292c9906da860ebd495ec13d0537e