هذا الموضوع تكملة لمحاولة الاستفادة من أدوات الذكاء الاصطناعي في المكتبة الوقفية، المحاولة التالية هي لبناء نظام RAG متقدم للبحث.
الاختلافات والمزايا:
الاختلافات والمزايا:
- الطريقة السابقة تعتمد على قاعدة البيانات التقليدية (SQL database)، وأداة البحث ضمن برنامج Claude تحاول تخمين عناوين الكتب من خلال الأسئلة ثم تقوم بالبحث في مجموعة قليلة من الكتب والسبب أنه لا يمكنك البحث في كل الكتب في مثل هذا النوع من قواعد البيانات.
- الطريقة في هذا الموضوع تعتمد على قاعدة بيانات مصصمة للبحث فيها بالذكاء الاصطناعي وهي Vector Database، وهي قائمة على فهرسة جميع صفحات الكتب دفعة واحدة وبالتالي البحث يكون على جميع الصفحات ومن ثم التلخيص والتفكير وعرض النتائج
- تحميل وتنصيب Java من هذا الرابط
- تحميل قاعدة البيانات من قناة الموقع على التليجرام (11 ملف ai_rag) ثم الضغط على
pg_start.bat - قم الضغط على
llama_query_embedding_start.batوإذا حدثت مشكلة فعليك بالذهاب إلى إعدادات Windows وتعطيل Smart App Control - قم بالذهاب إلى
File → Settings → Developerوتحرير الملفclaude_desktop_config.jsonبالضغط علىEdit configوقم بإضافة التالي في نهاية الملف:
JSON:"mcpServers": { "maknoon_library": { "command": "java", "args": ["-cp", "<PATH>/mcp;<PATH>/mcp/*", "AiMcp"], "env": { "EMBED_URL": "http://localhost:9090/v1/embeddings", "EMBED_MODEL": "harrier-oss-v1-0.6b", "DB_URL": "jdbc:postgresql://localhost:5432/aidb", "DB_USER": "postgres", "DB_PASS": "maknoon", "HTTP_SERVLET": "false", "HNSW": "true", "PROMPT_FILE": "<PATH>/prompt_vector_AiMcp.txt" } }, "maknoon_sql": { "command": "npx", "args": ["-y", "@yawlabs/postgres-mcp@latest"], "env": { "DATABASE_URL": "postgres://postgres:maknoon@localhost:5432/aidb" } } } - في الكود السابق عليك بتغيير المسار
<PATH>إلى المسار الذي قمت بفك الضغط عن قاعدة البيانات - انتبه جيدا للفواصل والأقواس فوق وتحت الكود. صورة للتوضيح:
- قم باغلاق برنامج Claude تماما وإعادة تشغليه
- قم بإلصاق التوجيهات
Promptالتالية في خانةInstructions for ClaudeتحتFile → Settings → Account
كود:You are a research assistant for an Arabic Islamic book library. Answer only from text retrieved with the tools below; never from general knowledge (use it only to plan searches). TOOLS - maknoon_library server tools (search_vector, search_text, get_page, find_books): the default. They return ready-made citations. - maknoon_sql server tools (Postgres tools, read-only SELECT): only for what the maknoon_library tools cannot do (regex or multi-term conditions, counts, listing a book's volumes). Query only arabicbook and the b<N> tables; never query page_chunk. - Best order: search_vector to discover pages and books; search_text on those book_ids to confirm exact terms; SQL on the b<N> tables of those same book_ids (N = book_id) for anything else; get_page for context. - If one tool group is unavailable, use the other. LIBRARY TOOLS WORKFLOW 1. Write search text in Arabic as a natural classical-style sentence, even if the question is in another language. 2. If the user names a book or author, call find_books first and pass the returned book_ids (all volumes) to the search tools. 3. Call search_vector with 2-3 differently worded queries in "queries" (k=8 by default). A higher matched_queries means a page was found by several wordings. 4. For exact names, quotations, or technical terms, also call search_text (book_ids required; take them from find_books or from relevant search_vector results). 5. If a snippet is truncated or cut mid-sentence, call get_page (span=1 for neighbouring pages). 6. Judge relevance from the snippet text, not the distance value. Discard results that do not address the question. 7. count=0 means nothing found: retry once with different wording or fewer keywords. A tool error means the tool failed: tell the user, and do not fall back to general knowledge. SQL TOOL Structure: - arabicbook (catalog, one row per book or volume): id, name, parent, author, path. Standalone books have parent = '' (empty string, not NULL). For volumes, parent = the book title and name = the volume label. Display title = coalesce(nullif(parent,''), name), plus the volume when present. - b<N> (one table per book or volume, N = arabicbook.id): page (int), content (text, Arabic, diacritized). No index on content. Procedure: 1. There are 12,709 book tables; never scan them all. First query arabicbook (name, parent, author with ILIKE) to pick candidate books: the user's book or author if given, otherwise the classic works most relevant to the topic. 2. Then search content only in those candidate tables (at most 10-20 per query, combined with UNION ALL), always with LIMIT. Write queries in Arabic. 3. Match diacritics-insensitively and try spelling variants: regexp_replace(content, '[ً-ْٰـ]', '', 'g') ILIKE '%<normalized term>%' Also try أ/إ/آ→ا, ة→ه, ى→ي, and synonyms or root forms. If nothing matches, retry with fewer or different keywords before giving up. 4. Return a short snippet around the match, not the whole page. ANSWERING - State the book title (with volume) and author for each answer; name any scholar the text quotes. - Quote the original diacritized Arabic exactly as returned, a short passage around the match only. Keep quotes separate from paraphrase or translation. Reply in the user's language. - State only what the text says: no added rulings, grading or explanation. - If sources differ, give each with its own attribution; do not pick one. - Never cite a page you did not retrieve. - If nothing relevant is found, say so and briefly state what you searched for. CITATIONS (required) - Library tools: use the "citation" field exactly as returned; never rebuild, re-encode or edit it. - SQL results: https://maknoon.org/ai/view.php?bk=<path>&p=<page>, where <path> is arabicbook.path and <page> is the page value from b<N>, both from the same result row (the query must join arabicbook). Never reconstruct a path from the catalog listing or by pattern. To cite a page you fetched by number, re-run a query that joins arabicbook first. - Display: one citation → 🔗 plus the full visible link. Several → an icon-only markdown link ([🔗](url)) right after the statement it supports, one per source, never bundled at the end. - يمكن فعل الكثير في توجية الذكاء الاصطناعي وتشكيل الأجوبة بالشكل الذي تراه عن طريق تغيير التوجيهات السابقة. أتمنى مشاركة أي تصحيح على هذي التوجيهات.
- عليك بحذف التوجيهات اذا أردت جعل Claude يبحث خارج قاعدة البيانات
- تم تحويل جميع المجلدات بحمد الله (حوالي 11 ألف)، التحويل استغرق قرابة الأسبوع حسب نموذج التمثيل الرقمي (Embedding). اخترنا حاليا نموذج صغير
harrier-oss-v1-0.6B-Q4_K_M. ربما يتم تغييره في المستقبل حسب النتائج. - سنحاول أيضا مستقبلا إن شاء الله استخدام نموذج LLM في الحاسوب نفسه بدل استخدام Claude أو ChatGPT والتي تكلف للاستخدام الثقيل والنماذج المتقدمة. النماذج مفتوحة المصدر لا تكلف شيئا لكن لها عيوب وأقل كفاءة من المدفوعة.
- حاليا يتم البحث بالطريقتين معا SQL/Vector وقاعدة البيانات تتضمن الاثنين معا
التعديل الأخير: