kubectlconfigured to a Kubernetes cluster where the AK‑F services (router, generator, vector store, Neo4j) are already running.## 1️⃣ Prep the environment (≈ 2 min)
```bash
# 1️⃣ Clone the demo repo (contains the Scrapy project & helper scripts)
git clone https://github.com/yourorg/akf‑blog‑demo.git
cd akf‑blog‑demo
# 2️⃣ Create a Python venv and install deps
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
```
> **Tip:** The `requirements.txt` bundles Scrapy, `tiktoken`, `sentence‑transformers`, `neo4j`, and `requests`.
---
## 2️⃣ Crawl the two archive months (≈ 3 min)
```bash
# Run the spider – it writes JSONL chunks to data/blog_chunks.jsonl
./run_ingest.sh
```
You should see log lines like:
```
2025-10-12 08:00:00 INFO Scraped article: LoRA adapters in transformers – step‑by‑step
2025-10-12 08:00:00 INFO Produced 7 chunks for article a1b2c3…
```
After the script finishes, verify the file size (≈ 1–2 MB for two months):
```bash
ls -lh data/blog_chunks.jsonl
```
---
## 3️⃣ Enrich, embed & store vectors (≈ 4 min)
```bash
# 3️⃣ Run the enrichment script
python enrich_and_store.py \
--input data/blog_chunks.jsonl \
--vector-store faiss \
--embedding-model sentence-transformers/all-mpnet-base-v2 \
--output data/faiss_index
```
What the script does:
| Step | Action |
|------|--------|
| **Entity extraction** | spaCy `en_core_web_sm` → adds a list of entities to each JSON line. |
| **Embedding** | `model.encode(text, normalize_embeddings=True)` → 768‑dim vector. |
| **FAISS index** | `IndexIVFFlat` with `nlist=100`, `nprobe=10`. |
| **Persistence** | Writes the index and a `metadata.pkl` (mapping UUID → metadata). |
When it finishes you’ll see something like:
```
✅ Processed 1 842 chunks
✅ FAISS index written to data/faiss_index/
```
---
## 4️⃣ Load the chunks into Neo4j (≈ 3 min)
```bash
# 4️⃣ Push the JSONL into the graph
python load_into_neo4j.py \
--uri bolt://neo4j.my‑cluster.svc.cluster.local:7687 \
--user neo4j \
--password $NEO4J_PASS \
--jsonl data/blog_chunks.jsonl
```
The script:
1. Opens a Bolt session.
2. Batches rows (5 k per batch) and runs the three MERGE statements from the previous answer (Article, Chunk, HAS_CHUNK).
3. Creates an index on `Article.id` and `Chunk.id` for fast look‑ups.
Sample output:
```
✔️ Imported 5 000 rows (batch 1)
✔️ Imported 5 000 rows (batch 2)
…
✅ Neo4j import complete
```
---
## 5️⃣ Register the new **BlogExpert** in the MoE router (≈ 1 min)
```bash
# 5️⃣ Patch the router config (assumes the router reads a ConfigMap named moe-router-config)
kubectl -n akf-engine patch configmap moe-router-config \
--type=json \
-p='[{"op":"add","path":"/data/experts","value":"- name: BlogExpert\n type: retrieval\n weight: 0.05"}]'
```
Then trigger a rolling restart so the router picks up the change:
```bash
kubectl -n akf-engine rollout restart deployment/moe-router
```
---
## 6️⃣ Live query demo (≈ 2 min)
```bash
# 6️⃣ Send a request to the API gateway (replace <gateway‑svc> with your actual host)
curl -X POST http://<gateway-svc>/v1/chat \
-H "Content-Type: application/json" \
-d '{"messages":[{"role":"user","content":"What does Swervin Curvin say about LoRA adapters?"}]}'
```
**Expected response (truncated):**
```json
{
"role":"assistant",
"content":"Swervin Curvin’s October‑2025 post “LoRA adapters in transformers – step‑by‑step” explains that …\n\n**References**\n- LoRA adapters in transformers – step‑by‑step – https://swervincurvin.blogspot.com/2025/10/lora-adapters.html"
}
```
Notice the **reference block** at the bottom – that is the output of the newly added `BlogExpert`.
You can also test a non‑blog query to confirm the engine still uses its core knowledge:
```bash
curl -X POST http://<gateway-svc>/v1/chat \
-d '{"messages":[{"role":"user","content":"What are the latest NVIDIA H100 specs?"}]}'
```
The answer should come from the original domain‑specific index, **without** a blog reference.
---
## 7️⃣ Quick performance check (optional, < 1 min)
```bash
# Query Prometheus for the new metric that the RAM service emits
curl -s http://prometheus.monitoring.svc:9090/api/v1/query \
-G --data-urlencode 'query=rate(ram_vector_search_seconds_sum[1m])' | jq .
```
You should see a modest latency (≈ 0.12 s) indicating the extra FAISS lookup is well within the existing autoscaling headroom.
---
## 8️⃣ Clean‑up (optional)
```bash
# Remove the demo data from Neo4j
python cleanup_neo4j.py \
--uri bolt://neo4j.my‑cluster.svc.cluster.local:7687 \
--user neo4j \
--password $NEO4J_PASS \
--label Article
```
```bash
# Delete the FAISS index files
rm -rf data/faiss_index
```
---
### TL;DR of the demo flow
1. **Crawl** → `data/blog_chunks.jsonl` (Scrapy).
2. **Enrich + embed** → FAISS index (`data/faiss_index`).
3. **Load** → Neo4j Article/Chunk graph.
4. **Register** → `BlogExpert` in MoE router.
5. **Query** → see blog‑derived references in the answer.
This demo is a live end‑to‑end illustration of how new external content becomes part of the Adaptive Knowledge‑Fusion Engine’s knowledge graph, retrieval‑augmented memory, and MoE routing.
MIT License
Copyright (c) 2025 Cory Miller
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
No comments:
Post a Comment