|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Ragas `BaseModel`\n", |
| 8 | + "\n", |
| 9 | + "> An Extended version of Pydantics `BaseModel` for some ragas specific stuff" |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "code", |
| 14 | + "execution_count": 1, |
| 15 | + "metadata": {}, |
| 16 | + "outputs": [], |
| 17 | + "source": [ |
| 18 | + "#| default_exp model.pydantic_model" |
| 19 | + ] |
| 20 | + }, |
| 21 | + { |
| 22 | + "cell_type": "code", |
| 23 | + "execution_count": 1, |
| 24 | + "metadata": {}, |
| 25 | + "outputs": [], |
| 26 | + "source": [ |
| 27 | + "#| export\n", |
| 28 | + "import typing as t\n", |
| 29 | + "\n", |
| 30 | + "from pydantic import BaseModel, PrivateAttr\n", |
| 31 | + "\n", |
| 32 | + "from ragas_annotator.typing import FieldMeta as RagasFieldMeta" |
| 33 | + ] |
| 34 | + }, |
| 35 | + { |
| 36 | + "cell_type": "code", |
| 37 | + "execution_count": 2, |
| 38 | + "metadata": {}, |
| 39 | + "outputs": [], |
| 40 | + "source": [ |
| 41 | + "#| export\n", |
| 42 | + "class ExtendedPydanticBaseModel(BaseModel):\n", |
| 43 | + " \"\"\"Extended Pydantic BaseModel with database integration capabilities\"\"\"\n", |
| 44 | + " \n", |
| 45 | + " # Private attribute for storing the database row_id\n", |
| 46 | + " _row_id: t.Optional[int] = PrivateAttr(default=None)\n", |
| 47 | + " \n", |
| 48 | + " # Class variable for storing column mapping overrides\n", |
| 49 | + " __column_mapping__: t.ClassVar[t.Dict[str, str]] = {}\n", |
| 50 | + " \n", |
| 51 | + " def __init__(self, **data):\n", |
| 52 | + " super().__init__(**data)\n", |
| 53 | + " # Initialize column mapping if not already defined\n", |
| 54 | + " if not self.__class__.__column_mapping__:\n", |
| 55 | + " self._initialize_column_mapping()\n", |
| 56 | + " \n", |
| 57 | + " @classmethod\n", |
| 58 | + " def _initialize_column_mapping(cls):\n", |
| 59 | + " \"\"\"Initialize mapping from field names to column IDs.\"\"\"\n", |
| 60 | + " for field_name, field_info in cls.model_fields.items():\n", |
| 61 | + " # Check if field has Column metadata (for Pydantic v2)\n", |
| 62 | + " column_id = None\n", |
| 63 | + " for extra in field_info.metadata or []:\n", |
| 64 | + " if isinstance(extra, RagasFieldMeta) and extra.id:\n", |
| 65 | + " column_id = extra.id\n", |
| 66 | + " break\n", |
| 67 | + " \n", |
| 68 | + " # If no Column metadata found, use field name as column ID\n", |
| 69 | + " if not column_id:\n", |
| 70 | + " column_id = field_name\n", |
| 71 | + " \n", |
| 72 | + " cls.__column_mapping__[field_name] = column_id\n", |
| 73 | + " \n", |
| 74 | + " @classmethod\n", |
| 75 | + " def get_column_id(cls, field_name: str) -> str:\n", |
| 76 | + " \"\"\"Get the column ID for a given field name.\"\"\"\n", |
| 77 | + " if field_name not in cls.__column_mapping__:\n", |
| 78 | + " raise ValueError(f\"No column mapping found for field {field_name}\")\n", |
| 79 | + " return cls.__column_mapping__[field_name]\n", |
| 80 | + " \n", |
| 81 | + " @classmethod\n", |
| 82 | + " def set_column_id(cls, field_name: str, column_id: str):\n", |
| 83 | + " \"\"\"Set the column ID for a given field name.\"\"\"\n", |
| 84 | + " if field_name not in cls.model_fields:\n", |
| 85 | + " raise ValueError(f\"Field {field_name} not found in model\")\n", |
| 86 | + " cls.__column_mapping__[field_name] = column_id\n", |
| 87 | + " \n", |
| 88 | + " def get_db_field_mapping(self) -> t.Dict[str, str]:\n", |
| 89 | + " \"\"\"Get a mapping from field names to column IDs for this model.\"\"\"\n", |
| 90 | + " return self.__class__.__column_mapping__\n", |
| 91 | + "\n" |
| 92 | + ] |
| 93 | + }, |
| 94 | + { |
| 95 | + "cell_type": "code", |
| 96 | + "execution_count": 12, |
| 97 | + "metadata": {}, |
| 98 | + "outputs": [], |
| 99 | + "source": [ |
| 100 | + "import ragas_annotator.typing as rt" |
| 101 | + ] |
| 102 | + }, |
| 103 | + { |
| 104 | + "cell_type": "code", |
| 105 | + "execution_count": 13, |
| 106 | + "metadata": {}, |
| 107 | + "outputs": [], |
| 108 | + "source": [ |
| 109 | + "# Example usage\n", |
| 110 | + "class TestDataRow(ExtendedPydanticBaseModel):\n", |
| 111 | + " id: t.Optional[int] = None\n", |
| 112 | + " query: t.Annotated[str, rt.Text(id=\"search_query\")]\n", |
| 113 | + " persona: t.List[t.Literal[\"opt1\", \"opt2\", \"opt3\"]]" |
| 114 | + ] |
| 115 | + }, |
| 116 | + { |
| 117 | + "cell_type": "code", |
| 118 | + "execution_count": 14, |
| 119 | + "metadata": {}, |
| 120 | + "outputs": [ |
| 121 | + { |
| 122 | + "data": { |
| 123 | + "text/plain": [ |
| 124 | + "{}" |
| 125 | + ] |
| 126 | + }, |
| 127 | + "execution_count": 14, |
| 128 | + "metadata": {}, |
| 129 | + "output_type": "execute_result" |
| 130 | + } |
| 131 | + ], |
| 132 | + "source": [ |
| 133 | + "TestDataRow.__column_mapping__" |
| 134 | + ] |
| 135 | + }, |
| 136 | + { |
| 137 | + "cell_type": "code", |
| 138 | + "execution_count": 15, |
| 139 | + "metadata": {}, |
| 140 | + "outputs": [], |
| 141 | + "source": [ |
| 142 | + "t = TestDataRow(id=1, query=\"this is a test\", persona=[\"opt1\"])" |
| 143 | + ] |
| 144 | + }, |
| 145 | + { |
| 146 | + "cell_type": "code", |
| 147 | + "execution_count": 16, |
| 148 | + "metadata": {}, |
| 149 | + "outputs": [ |
| 150 | + { |
| 151 | + "data": { |
| 152 | + "text/plain": [ |
| 153 | + "{'id': 'id', 'query': 'search_query', 'persona': 'persona'}" |
| 154 | + ] |
| 155 | + }, |
| 156 | + "execution_count": 16, |
| 157 | + "metadata": {}, |
| 158 | + "output_type": "execute_result" |
| 159 | + } |
| 160 | + ], |
| 161 | + "source": [ |
| 162 | + "t.__column_mapping__" |
| 163 | + ] |
| 164 | + }, |
| 165 | + { |
| 166 | + "cell_type": "code", |
| 167 | + "execution_count": null, |
| 168 | + "metadata": {}, |
| 169 | + "outputs": [], |
| 170 | + "source": [] |
| 171 | + } |
| 172 | + ], |
| 173 | + "metadata": { |
| 174 | + "kernelspec": { |
| 175 | + "display_name": ".venv", |
| 176 | + "language": "python", |
| 177 | + "name": "python3" |
| 178 | + }, |
| 179 | + "language_info": { |
| 180 | + "codemirror_mode": { |
| 181 | + "name": "ipython", |
| 182 | + "version": 3 |
| 183 | + }, |
| 184 | + "file_extension": ".py", |
| 185 | + "mimetype": "text/x-python", |
| 186 | + "name": "python", |
| 187 | + "nbconvert_exporter": "python", |
| 188 | + "pygments_lexer": "ipython3", |
| 189 | + "version": "3.12.8" |
| 190 | + } |
| 191 | + }, |
| 192 | + "nbformat": 4, |
| 193 | + "nbformat_minor": 2 |
| 194 | +} |
0 commit comments