{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# RAG Tutorial: Architecture, Retrieval & Evaluation\n",
        "Xenors practice notebook. Run each cell and compare the output with the tutorial page.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Measure retrieval hit rate\n",
        "Evaluate retrieval separately from answer generation so you know where failures originate.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "tests=[('q1','docA',['docA','docC']),('q2','docB',['docD','docB'])]\n",
        "hits=sum(expected in retrieved for _,expected,retrieved in tests)\n",
        "print('hit_rate@2 =',hits/len(tests))\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "**Expected/checkpoint**\n",
        "```text\nhit_rate@2 = 1.0\n```\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Add citations to the assembled prompt\n",
        "Preserving source IDs makes citations possible downstream.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "chunks=[('policy.pdf','Refunds are allowed within 30 days.'),('faq.md','Digital goods are final sale.')]\n",
        "ctx='\\n'.join(f'[{s}] {t}' for s,t in chunks)\n",
        "print(ctx)\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "**Expected/checkpoint**\n",
        "```text\n[policy.pdf] Refunds are allowed within 30 days.\n[faq.md] Digital goods are final sale.\n```\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "name": "python",
      "version": "3.x"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}