Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions translations/ar/.co-op-translator.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"language_code": "ar"
},
"README.md": {
"original_hash": "a49252a2706935f21c8abf8471481553",
"translation_date": "2026-04-21T01:33:30+00:00",
"original_hash": "e1043c69855a4bf9bb560a087f3f0bff",
"translation_date": "2026-08-26T17:46:48+00:00",
"source_file": "README.md",
"language_code": "ar"
},
Expand Down Expand Up @@ -53,6 +53,12 @@
"source_file": "code/06.E2E/E2E_Phi-4-RAG-Azure-AI-Search.ipynb",
"language_code": "ar"
},
"code/06.E2E/E2E_Phi-4-mini_Local_Hybrid_RAG_SQLite_FTS5.ipynb": {
"original_hash": "71f9303a811084cf162acf977625cda9",
"translation_date": "2026-08-26T17:47:41+00:00",
"source_file": "code/06.E2E/E2E_Phi-4-mini_Local_Hybrid_RAG_SQLite_FTS5.ipynb",
"language_code": "ar"
},
"code/07.Lab/01/AIPC/extensions/phi3ext/CHANGELOG.md": {
"original_hash": "dbb0b6218ce5f9cf0ede8f4201f6ad58",
"translation_date": "2025-07-16T16:29:36+00:00",
Expand Down
301 changes: 153 additions & 148 deletions translations/ar/README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 🚀 استرجاع هجين مع Microsoft phi-4-mini و SQLite FTS5 (نماذج لغة صغيرة بدون سحابة)\n",
"\n",
"> **المؤلف:** Çağrı Giray Keşan ([@Cagrik34](https://github.com/Cagrik34)) \n",
"> **التركيز:** نماذج اللغة الصغيرة (SLMs)، SQLite FTS5 BM25، التضمينات الكثيفة، دمج الترتيب العكسي (RRF)\n",
"\n",
"---\n",
"\n",
"## 📌 1. الدافع: معضلة استرجاع الكلمات المفتاحية في نماذج اللغة الصغيرة المحلية\n",
"غالبًا ما تفشل هندسات RAG القياسية التي تعتمد فقط على التضمينات المتجهية الكثيفة في استرجاع الرموز العددية الدقيقة (مثلاً، `2,340,000 TL`، رموز العقود، أرقام الحسابات). \n",
"بالمقابل، تفوت عمليات البحث اللفظي المتناثر (BM25) المترادفات الدلالية والأسئلة المعاد صياغتها.\n",
"\n",
"توضح هذه الوصفة كيفية تنفيذ **محرك استرجاع هجين عالي السرعة داخل الذاكرة** يجمع بين:\n",
"1. **المتجهات الكثيفة** (تشابه جيب التمام)\n",
"2. **البحث اللفظي المتناثر** (SQLite FTS5 BM25)\n",
"3. **دمج الترتيب العكسي (RRF, $k=60$)**\n",
"4. **توليد الاقتباسات المبنية (`[1]`, `[2]`)** مع Microsoft `phi-4-mini`.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import sqlite3\n",
"import numpy as np\n",
"from typing import List, Tuple, Dict, Any\n",
"\n",
"RRF_K = 60\n",
"TOP_K = 2\n",
"print(\"✅ Core dependencies loaded successfully.\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 🏗️ 2. مخطط SQLite مزدوج (متجهات كثيفة + جدول FTS5 افتراضي بتصنيف BM25)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class LocalHybridRAGStore:\n",
" def __init__(self, db_path: str = \":memory:\"):\n",
" self.conn = sqlite3.connect(db_path)\n",
" self._init_schema()\n",
"\n",
" def _init_schema(self) -> None:\n",
" with self.conn:\n",
" self.conn.execute(\"\"\"\n",
" CREATE TABLE IF NOT EXISTS document_chunks (\n",
" id INTEGER PRIMARY KEY AUTOINCREMENT,\n",
" source_file TEXT NOT NULL,\n",
" chunk_index INTEGER NOT NULL,\n",
" content TEXT NOT NULL,\n",
" embedding BLOB NOT NULL\n",
" )\n",
" \"\"\")\n",
" self.conn.execute(\"\"\"\n",
" CREATE VIRTUAL TABLE IF NOT EXISTS document_chunks_fts USING fts5(\n",
" content,\n",
" source_file UNINDEXED,\n",
" chunk_index UNINDEXED,\n",
" tokenize='unicode61'\n",
" )\n",
" \"\"\")\n",
"\n",
" def insert_chunk(self, source_file: str, chunk_index: int, content: str, embedding: List[float]) -> None:\n",
" vec = np.array(embedding, dtype=np.float32)\n",
" norm = np.linalg.norm(vec)\n",
" if norm > 0:\n",
" vec = vec / norm\n",
"\n",
" with self.conn:\n",
" self.conn.execute(\n",
" \"INSERT INTO document_chunks (source_file, chunk_index, content, embedding) VALUES (?, ?, ?, ?)\",\n",
" (source_file, chunk_index, content, vec.tobytes())\n",
" )\n",
" self.conn.execute(\n",
" \"INSERT INTO document_chunks_fts (content, source_file, chunk_index) VALUES (?, ?, ?)\",\n",
" (content, source_file, str(chunk_index))\n",
" )\n",
"\n",
" def search_dense(self, query_embedding: List[float], top_k: int = 5) -> List[Tuple[int, str, str, float]]:\n",
" q_vec = np.array(query_embedding, dtype=np.float32)\n",
" q_norm = np.linalg.norm(q_vec)\n",
" if q_norm > 0:\n",
" q_vec = q_vec / q_norm\n",
"\n",
" cursor = self.conn.execute(\"SELECT id, source_file, content, embedding FROM document_chunks\")\n",
" results = []\n",
" for doc_id, src, content, blob in cursor.fetchall():\n",
" doc_vec = np.frombuffer(blob, dtype=np.float32)\n",
" similarity = float(np.dot(q_vec, doc_vec))\n",
" results.append((doc_id, src, content, similarity))\n",
" results.sort(key=lambda x: x[3], reverse=True)\n",
" return results[:top_k]\n",
"\n",
" def search_sparse_bm25(self, query_text: str, top_k: int = 5) -> List[Tuple[int, str, str, float]]:\n",
" clean_tokens = [t for t in query_text.replace(\"'\", \"\").replace('\"', '').split() if len(t) > 1]\n",
" if not clean_tokens:\n",
" return []\n",
" fts_query = \" OR \".join(f'\"{t}\"' for t in clean_tokens)\n",
" cursor = self.conn.execute(\n",
" \"SELECT rowid, source_file, content, rank FROM document_chunks_fts WHERE document_chunks_fts MATCH ? ORDER BY rank LIMIT ?\",\n",
" (fts_query, top_k)\n",
" )\n",
" results = []\n",
" for doc_id, src, content, bm25_rank in cursor.fetchall():\n",
" bm25_score = 1.0 / (1.0 + abs(float(bm25_rank)))\n",
" results.append((doc_id, src, content, bm25_score))\n",
" return results\n",
"\n",
" def hybrid_search(self, query_text: str, query_embedding: List[float], top_k: int = TOP_K) -> List[Dict[str, Any]]:\n",
" dense_hits = self.search_dense(query_embedding, top_k=10)\n",
" sparse_hits = self.search_sparse_bm25(query_text, top_k=10)\n",
" fused_scores = {}\n",
" chunk_map = {}\n",
"\n",
" for rank, (doc_id, src, content, sim) in enumerate(dense_hits, start=1):\n",
" key = f\"{src}::{content[:50]}\"\n",
" chunk_map[key] = (src, content, \"vector\")\n",
" fused_scores[key] = fused_scores.get(key, 0.0) + (1.0 / (RRF_K + rank))\n",
"\n",
" for rank, (doc_id, src, content, bm25) in enumerate(sparse_hits, start=1):\n",
" key = f\"{src}::{content[:50]}\"\n",
" if key not in chunk_map:\n",
" chunk_map[key] = (src, content, \"bm25\")\n",
" else:\n",
" chunk_map[key] = (src, content, \"hybrid\")\n",
" fused_scores[key] = fused_scores.get(key, 0.0) + (1.0 / (RRF_K + rank))\n",
"\n",
" sorted_keys = sorted(fused_scores.keys(), key=lambda k: fused_scores[k], reverse=True)[:top_k]\n",
" output = []\n",
" for citation_idx, key in enumerate(sorted_keys, start=1):\n",
" src, content, match_type = chunk_map[key]\n",
" output.append({\n",
" \"citation_index\": citation_idx,\n",
" \"source_file\": src,\n",
" \"content\": content,\n",
" \"rrf_score\": fused_scores[key],\n",
" \"match_type\": match_type\n",
" })\n",
" return output\n",
"\n",
"print(\"✅ LocalHybridRAGStore class compiled successfully.\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 📊 3. قياس أداء استيعاب وتنفيذ العينة\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"store = LocalHybridRAGStore()\n",
"\n",
"sample_docs = [\n",
" (\"q3_financial_report.pdf\", 0, \"CodePulse engineering project total Q3 budget was allocated at 2,340,000 TL with 15 active developers.\", [0.8, 0.1, 0.2] + [0.0] * 1021),\n",
" (\"architecture_specs.md\", 0, \"Zenith AI leverages Microsoft phi-4-mini (3.8B parameters) for local zero-cloud inference.\", [0.2, 0.9, 0.1] + [0.0] * 1021),\n",
" (\"hr_policy_2026.docx\", 0, \"Remote work expense allowance is capped at 15,000 TL per employee quarterly.\", [0.1, 0.1, 0.8] + [0.0] * 1021)\n",
"]\n",
"\n",
"for src, idx, content, emb in sample_docs:\n",
" store.insert_chunk(src, idx, content, emb)\n",
"\n",
"query = \"What is the total allocated budget for the CodePulse project?\"\n",
"query_vec = [0.75, 0.15, 0.25] + [0.0] * 1021\n",
"\n",
"results = store.hybrid_search(query, query_vec, top_k=2)\n",
"for res in results:\n",
" print(f\"[{res['citation_index']}] {res['source_file']} ({res['match_type'].upper()}) -> Score: {res['rrf_score']:.4f}\")\n",
" print(f\" Content: {res['content']}\\n\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 📝 4. صياغة مطالبات مرتكزة لنموذج Microsoft phi-4-mini\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def construct_grounded_prompt(query: str, retrieved_chunks: List[Dict[str, Any]]) -> str:\n",
" context_blocks = []\n",
" for chunk in retrieved_chunks:\n",
" context_blocks.append(f\"[{chunk['citation_index']}] (Source: {chunk['source_file']})\\n{chunk['content']}\")\n",
" context_str = \"\\n\\n\".join(context_blocks)\n",
"\n",
" return f\"\"\"You are Zenith AI, an enterprise-grade local assistant.\n",
"Answer the user query strictly based on the provided context below.\n",
"Every factual claim must cite its source index like [1] or [2].\n",
"If the context does not contain the answer, respond: 'This information is not present in the indexed documents.'\n",
"\n",
"--- CONTEXT ---\n",
"{context_str}\n",
"--- END CONTEXT ---\n",
"\n",
"User Query: {query}\n",
"Answer:\"\"\"\n",
"\n",
"prompt = construct_grounded_prompt(query, results)\n",
"print(prompt)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n\n<!-- CO-OP TRANSLATOR DISCLAIMER START -->\n**تنويه**:\nتمت ترجمة هذا المستند باستخدام خدمة الترجمة بالذكاء الاصطناعي [Co-op Translator](https://github.com/Azure/co-op-translator). بينما نسعى للدقة، يرجى العلم أن الترجمات الآلية قد تحتوي على أخطاء أو عدم دقة. يجب اعتبار المستند الأصلي بلغته الأصلية المصدر الرسمي والمعتمد. للمعلومات الهامة، يُنصح بالاستعانة بترجمة بشرية محترفة. نحن غير مسؤولين عن أي سوء فهم أو تفسير ناتج عن استخدام هذه الترجمة.\n<!-- CO-OP TRANSLATOR DISCLAIMER END -->\n"
]
}
],
"metadata": {
"language_info": {
"name": "python",
"version": "3.11.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
10 changes: 8 additions & 2 deletions translations/bg/.co-op-translator.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"language_code": "bg"
},
"README.md": {
"original_hash": "a49252a2706935f21c8abf8471481553",
"translation_date": "2026-04-20T23:51:01+00:00",
"original_hash": "e1043c69855a4bf9bb560a087f3f0bff",
"translation_date": "2026-08-26T19:51:42+00:00",
"source_file": "README.md",
"language_code": "bg"
},
Expand Down Expand Up @@ -53,6 +53,12 @@
"source_file": "code/06.E2E/E2E_Phi-4-RAG-Azure-AI-Search.ipynb",
"language_code": "bg"
},
"code/06.E2E/E2E_Phi-4-mini_Local_Hybrid_RAG_SQLite_FTS5.ipynb": {
"original_hash": "71f9303a811084cf162acf977625cda9",
"translation_date": "2026-08-26T19:52:44+00:00",
"source_file": "code/06.E2E/E2E_Phi-4-mini_Local_Hybrid_RAG_SQLite_FTS5.ipynb",
"language_code": "bg"
},
"code/07.Lab/01/AIPC/extensions/phi3ext/CHANGELOG.md": {
"original_hash": "dbb0b6218ce5f9cf0ede8f4201f6ad58",
"translation_date": "2025-07-16T16:32:11+00:00",
Expand Down
Loading