Skip to content
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,4 @@ cython_debug/
.python-version
uv.lock
_proc
experiments
File renamed without changes.
212 changes: 21 additions & 191 deletions nbs/backends/factory.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,10 @@
"metadata": {},
"outputs": [],
"source": [
"# | export\n",
"#| export\n",
"import typing as t\n",
"import os\n",
"\n",
"from notion_client import Client as NotionClient\n",
"from ragas_annotator.backends.mock_notion import MockNotionClient\n",
"from ragas_annotator.backends.notion_backend import NotionBackend"
"from ragas_experimental.backends.ragas_api_client import RagasApiClient"
]
},
{
Expand All @@ -39,204 +36,37 @@
"metadata": {},
"outputs": [],
"source": [
"# | export\n",
"class NotionClientFactory:\n",
" \"\"\"Factory for creating Notion client instances.\"\"\"\n",
"#| export\n",
"class RagasApiClientFactory:\n",
" \"\"\"Factory for creating Ragas API client instances.\"\"\"\n",
"\n",
" @staticmethod\n",
" def create(\n",
" use_mock: bool = False,\n",
" api_key: t.Optional[str] = None,\n",
" initialize_project: bool = False,\n",
" root_page_id: t.Optional[str] = None,\n",
" ) -> t.Union[NotionClient, MockNotionClient]:\n",
" \"\"\"Create a Notion client.\n",
" app_token: t.Optional[str] = None,\n",
" base_url: t.Optional[str] = None,\n",
" ) -> RagasApiClient:\n",
" \"\"\"Create a Ragas API client.\n",
"\n",
" Args:\n",
" use_mock: If True, create a mock client\n",
" api_key: Notion API key (only used for real client)\n",
" initialize_project: If True and using mock, initialize project structure\n",
" root_page_id: Required if initialize_project is True\n",
" api_key: The API key for the Ragas API\n",
" base_url: The base URL for the Ragas API\n",
"\n",
" Returns:\n",
" Union[NotionClient, MockNotionClient]: A real or mock client\n",
" RagasApiClient: A Ragas API client instance\n",
" \"\"\"\n",
" if use_mock:\n",
" client = MockNotionClient()\n",
" if app_token is None:\n",
" app_token = os.getenv(\"RAGAS_APP_TOKEN\")\n",
"\n",
" # Optionally initialize project structure\n",
" if initialize_project and root_page_id:\n",
" # Create root page if it doesn't exist in the mock client\n",
" if root_page_id not in client._pages:\n",
" # Create root page\n",
" root_page = {\n",
" \"id\": root_page_id,\n",
" \"object\": \"page\",\n",
" \"created_time\": client._get_timestamp(),\n",
" \"last_edited_time\": client._get_timestamp(),\n",
" \"archived\": False,\n",
" \"properties\": {\n",
" \"title\": {\n",
" \"type\": \"title\",\n",
" \"title\": [\n",
" {\n",
" \"plain_text\": \"Root Page\",\n",
" \"type\": \"text\",\n",
" \"text\": {\"content\": \"Root Page\"},\n",
" }\n",
" ],\n",
" }\n",
" },\n",
" }\n",
" client.add_page(root_page)\n",
" if app_token is None:\n",
" raise ValueError(\"RAGAS_API_KEY environment variable is not set\")\n",
"\n",
" # Create required sub-pages\n",
" for page_name in [\"Datasets\", \"Experiments\", \"Comparisons\"]:\n",
" # Create page ID\n",
" page_id = client._create_id()\n",
" if base_url is None:\n",
" base_url = os.getenv(\"RAGAS_API_BASE_URL\")\n",
"\n",
" # Create page\n",
" page = {\n",
" \"id\": page_id,\n",
" \"object\": \"page\",\n",
" \"created_time\": client._get_timestamp(),\n",
" \"last_edited_time\": client._get_timestamp(),\n",
" \"archived\": False,\n",
" \"properties\": {\n",
" \"title\": {\n",
" \"type\": \"title\",\n",
" \"title\": [\n",
" {\n",
" \"plain_text\": page_name,\n",
" \"type\": \"text\",\n",
" \"text\": {\"content\": page_name},\n",
" }\n",
" ],\n",
" }\n",
" },\n",
" \"parent\": {\"type\": \"page_id\", \"page_id\": root_page_id},\n",
" }\n",
" client.add_page(page)\n",
" if base_url is None:\n",
" base_url = \"https://api.dev.app.ragas.io\"\n",
"\n",
" # Add child block to root\n",
" child_block = {\n",
" \"id\": client._create_id(),\n",
" \"object\": \"block\",\n",
" \"type\": \"child_page\",\n",
" \"created_time\": client._get_timestamp(),\n",
" \"last_edited_time\": client._get_timestamp(),\n",
" \"child_page\": {\"title\": page_name},\n",
" }\n",
"\n",
" client.add_children(root_page_id, [child_block])\n",
"\n",
" return client\n",
" else:\n",
" # For real client, use provided API key or environment variable\n",
" if api_key is None:\n",
" api_key = os.getenv(\"NOTION_API_KEY\")\n",
"\n",
" if api_key is None:\n",
" raise ValueError(\n",
" \"api_key must be provided or set as NOTION_API_KEY environment variable\"\n",
" )\n",
"\n",
" return NotionClient(auth=api_key)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"MockNotionClient(num_pages=0, num_databases=0, num_blocks=0)"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# create the mock notion client\n",
"mock_notion_client = NotionClientFactory.create(use_mock=True)\n",
"mock_notion_client"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"the `initialize_project` adds the project pages too for you."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"MockNotionClient(num_pages=4, num_databases=0, num_blocks=0)"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mock_notion_client = NotionClientFactory.create(\n",
" use_mock=True, initialize_project=True, root_page_id=\"your_root_page_id\"\n",
")\n",
"mock_notion_client"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# | export\n",
"class NotionBackendFactory:\n",
" \"\"\"Factory for creating NotionBackend instances.\"\"\"\n",
"\n",
" @staticmethod\n",
" def create(\n",
" root_page_id: str,\n",
" use_mock: bool = False,\n",
" api_key: t.Optional[str] = None,\n",
" initialize_project: bool = False,\n",
" notion_client: t.Optional[t.Union[NotionClient, MockNotionClient]] = None,\n",
" ) -> NotionBackend:\n",
" \"\"\"Create a NotionBackend instance.\n",
"\n",
" Args:\n",
" root_page_id: The ID of the root page\n",
" use_mock: If True, create a backend with a mock client\n",
" api_key: Notion API key (only used for real client)\n",
" initialize_project: If True and using mock, initialize project structure\n",
" notion_client: Optional pre-configured Notion client\n",
"\n",
" Returns:\n",
" NotionBackend: A backend instance with either real or mock client\n",
" \"\"\"\n",
" # Use provided client or create one\n",
" if notion_client is None:\n",
" notion_client = NotionClientFactory.create(\n",
" use_mock=use_mock,\n",
" api_key=api_key,\n",
" initialize_project=initialize_project,\n",
" root_page_id=root_page_id,\n",
" )\n",
"\n",
" # Create and return the backend\n",
" return NotionBackend(root_page_id=root_page_id, notion_client=notion_client)"
" return RagasApiClient(app_token=app_token, base_url=base_url)\n"
]
}
],
Expand Down
Loading