|
24 | 24 | "metadata": {}, |
25 | 25 | "outputs": [], |
26 | 26 | "source": [ |
27 | | - "# | export\n", |
| 27 | + "#| export\n", |
28 | 28 | "import typing as t\n", |
29 | | - "import os\n", |
30 | 29 | "\n", |
31 | | - "from notion_client import Client as NotionClient\n", |
32 | | - "from ragas_annotator.backends.mock_notion import MockNotionClient\n", |
33 | | - "from ragas_annotator.backends.notion_backend import NotionBackend" |
| 30 | + "from ragas_experimental.backends.ragas_api_client import RagasApiClient" |
34 | 31 | ] |
35 | 32 | }, |
36 | 33 | { |
|
39 | 36 | "metadata": {}, |
40 | 37 | "outputs": [], |
41 | 38 | "source": [ |
42 | | - "# | export\n", |
43 | | - "class NotionClientFactory:\n", |
44 | | - " \"\"\"Factory for creating Notion client instances.\"\"\"\n", |
| 39 | + "#| export\n", |
| 40 | + "class RagasApiClientFactory:\n", |
| 41 | + " \"\"\"Factory for creating Ragas API client instances.\"\"\"\n", |
45 | 42 | "\n", |
46 | 43 | " @staticmethod\n", |
47 | 44 | " def create(\n", |
48 | | - " use_mock: bool = False,\n", |
49 | | - " api_key: t.Optional[str] = None,\n", |
50 | | - " initialize_project: bool = False,\n", |
51 | | - " root_page_id: t.Optional[str] = None,\n", |
52 | | - " ) -> t.Union[NotionClient, MockNotionClient]:\n", |
53 | | - " \"\"\"Create a Notion client.\n", |
| 45 | + " app_token: t.Optional[str] = None,\n", |
| 46 | + " base_url: t.Optional[str] = None,\n", |
| 47 | + " ) -> RagasApiClient:\n", |
| 48 | + " \"\"\"Create a Ragas API client.\n", |
54 | 49 | "\n", |
55 | 50 | " Args:\n", |
56 | | - " use_mock: If True, create a mock client\n", |
57 | | - " api_key: Notion API key (only used for real client)\n", |
58 | | - " initialize_project: If True and using mock, initialize project structure\n", |
59 | | - " root_page_id: Required if initialize_project is True\n", |
| 51 | + " api_key: The API key for the Ragas API\n", |
| 52 | + " base_url: The base URL for the Ragas API\n", |
60 | 53 | "\n", |
61 | 54 | " Returns:\n", |
62 | | - " Union[NotionClient, MockNotionClient]: A real or mock client\n", |
| 55 | + " RagasApiClient: A Ragas API client instance\n", |
63 | 56 | " \"\"\"\n", |
64 | | - " if use_mock:\n", |
65 | | - " client = MockNotionClient()\n", |
| 57 | + " if app_token is None:\n", |
| 58 | + " app_token = os.getenv(\"RAGAS_APP_TOKEN\")\n", |
66 | 59 | "\n", |
67 | | - " # Optionally initialize project structure\n", |
68 | | - " if initialize_project and root_page_id:\n", |
69 | | - " # Create root page if it doesn't exist in the mock client\n", |
70 | | - " if root_page_id not in client._pages:\n", |
71 | | - " # Create root page\n", |
72 | | - " root_page = {\n", |
73 | | - " \"id\": root_page_id,\n", |
74 | | - " \"object\": \"page\",\n", |
75 | | - " \"created_time\": client._get_timestamp(),\n", |
76 | | - " \"last_edited_time\": client._get_timestamp(),\n", |
77 | | - " \"archived\": False,\n", |
78 | | - " \"properties\": {\n", |
79 | | - " \"title\": {\n", |
80 | | - " \"type\": \"title\",\n", |
81 | | - " \"title\": [\n", |
82 | | - " {\n", |
83 | | - " \"plain_text\": \"Root Page\",\n", |
84 | | - " \"type\": \"text\",\n", |
85 | | - " \"text\": {\"content\": \"Root Page\"},\n", |
86 | | - " }\n", |
87 | | - " ],\n", |
88 | | - " }\n", |
89 | | - " },\n", |
90 | | - " }\n", |
91 | | - " client.add_page(root_page)\n", |
| 60 | + " if app_token is None:\n", |
| 61 | + " raise ValueError(\"RAGAS_API_KEY environment variable is not set\")\n", |
92 | 62 | "\n", |
93 | | - " # Create required sub-pages\n", |
94 | | - " for page_name in [\"Datasets\", \"Experiments\", \"Comparisons\"]:\n", |
95 | | - " # Create page ID\n", |
96 | | - " page_id = client._create_id()\n", |
| 63 | + " if base_url is None:\n", |
| 64 | + " base_url = os.getenv(\"RAGAS_API_BASE_URL\")\n", |
97 | 65 | "\n", |
98 | | - " # Create page\n", |
99 | | - " page = {\n", |
100 | | - " \"id\": page_id,\n", |
101 | | - " \"object\": \"page\",\n", |
102 | | - " \"created_time\": client._get_timestamp(),\n", |
103 | | - " \"last_edited_time\": client._get_timestamp(),\n", |
104 | | - " \"archived\": False,\n", |
105 | | - " \"properties\": {\n", |
106 | | - " \"title\": {\n", |
107 | | - " \"type\": \"title\",\n", |
108 | | - " \"title\": [\n", |
109 | | - " {\n", |
110 | | - " \"plain_text\": page_name,\n", |
111 | | - " \"type\": \"text\",\n", |
112 | | - " \"text\": {\"content\": page_name},\n", |
113 | | - " }\n", |
114 | | - " ],\n", |
115 | | - " }\n", |
116 | | - " },\n", |
117 | | - " \"parent\": {\"type\": \"page_id\", \"page_id\": root_page_id},\n", |
118 | | - " }\n", |
119 | | - " client.add_page(page)\n", |
| 66 | + " if base_url is None:\n", |
| 67 | + " base_url = \"https://api.dev.app.ragas.io\"\n", |
120 | 68 | "\n", |
121 | | - " # Add child block to root\n", |
122 | | - " child_block = {\n", |
123 | | - " \"id\": client._create_id(),\n", |
124 | | - " \"object\": \"block\",\n", |
125 | | - " \"type\": \"child_page\",\n", |
126 | | - " \"created_time\": client._get_timestamp(),\n", |
127 | | - " \"last_edited_time\": client._get_timestamp(),\n", |
128 | | - " \"child_page\": {\"title\": page_name},\n", |
129 | | - " }\n", |
130 | | - "\n", |
131 | | - " client.add_children(root_page_id, [child_block])\n", |
132 | | - "\n", |
133 | | - " return client\n", |
134 | | - " else:\n", |
135 | | - " # For real client, use provided API key or environment variable\n", |
136 | | - " if api_key is None:\n", |
137 | | - " api_key = os.getenv(\"NOTION_API_KEY\")\n", |
138 | | - "\n", |
139 | | - " if api_key is None:\n", |
140 | | - " raise ValueError(\n", |
141 | | - " \"api_key must be provided or set as NOTION_API_KEY environment variable\"\n", |
142 | | - " )\n", |
143 | | - "\n", |
144 | | - " return NotionClient(auth=api_key)" |
145 | | - ] |
146 | | - }, |
147 | | - { |
148 | | - "cell_type": "code", |
149 | | - "execution_count": null, |
150 | | - "metadata": {}, |
151 | | - "outputs": [ |
152 | | - { |
153 | | - "data": { |
154 | | - "text/plain": [ |
155 | | - "MockNotionClient(num_pages=0, num_databases=0, num_blocks=0)" |
156 | | - ] |
157 | | - }, |
158 | | - "execution_count": null, |
159 | | - "metadata": {}, |
160 | | - "output_type": "execute_result" |
161 | | - } |
162 | | - ], |
163 | | - "source": [ |
164 | | - "# create the mock notion client\n", |
165 | | - "mock_notion_client = NotionClientFactory.create(use_mock=True)\n", |
166 | | - "mock_notion_client" |
167 | | - ] |
168 | | - }, |
169 | | - { |
170 | | - "cell_type": "markdown", |
171 | | - "metadata": {}, |
172 | | - "source": [ |
173 | | - "the `initialize_project` adds the project pages too for you." |
174 | | - ] |
175 | | - }, |
176 | | - { |
177 | | - "cell_type": "code", |
178 | | - "execution_count": null, |
179 | | - "metadata": {}, |
180 | | - "outputs": [ |
181 | | - { |
182 | | - "data": { |
183 | | - "text/plain": [ |
184 | | - "MockNotionClient(num_pages=4, num_databases=0, num_blocks=0)" |
185 | | - ] |
186 | | - }, |
187 | | - "execution_count": null, |
188 | | - "metadata": {}, |
189 | | - "output_type": "execute_result" |
190 | | - } |
191 | | - ], |
192 | | - "source": [ |
193 | | - "mock_notion_client = NotionClientFactory.create(\n", |
194 | | - " use_mock=True, initialize_project=True, root_page_id=\"your_root_page_id\"\n", |
195 | | - ")\n", |
196 | | - "mock_notion_client" |
197 | | - ] |
198 | | - }, |
199 | | - { |
200 | | - "cell_type": "code", |
201 | | - "execution_count": null, |
202 | | - "metadata": {}, |
203 | | - "outputs": [], |
204 | | - "source": [ |
205 | | - "# | export\n", |
206 | | - "class NotionBackendFactory:\n", |
207 | | - " \"\"\"Factory for creating NotionBackend instances.\"\"\"\n", |
208 | | - "\n", |
209 | | - " @staticmethod\n", |
210 | | - " def create(\n", |
211 | | - " root_page_id: str,\n", |
212 | | - " use_mock: bool = False,\n", |
213 | | - " api_key: t.Optional[str] = None,\n", |
214 | | - " initialize_project: bool = False,\n", |
215 | | - " notion_client: t.Optional[t.Union[NotionClient, MockNotionClient]] = None,\n", |
216 | | - " ) -> NotionBackend:\n", |
217 | | - " \"\"\"Create a NotionBackend instance.\n", |
218 | | - "\n", |
219 | | - " Args:\n", |
220 | | - " root_page_id: The ID of the root page\n", |
221 | | - " use_mock: If True, create a backend with a mock client\n", |
222 | | - " api_key: Notion API key (only used for real client)\n", |
223 | | - " initialize_project: If True and using mock, initialize project structure\n", |
224 | | - " notion_client: Optional pre-configured Notion client\n", |
225 | | - "\n", |
226 | | - " Returns:\n", |
227 | | - " NotionBackend: A backend instance with either real or mock client\n", |
228 | | - " \"\"\"\n", |
229 | | - " # Use provided client or create one\n", |
230 | | - " if notion_client is None:\n", |
231 | | - " notion_client = NotionClientFactory.create(\n", |
232 | | - " use_mock=use_mock,\n", |
233 | | - " api_key=api_key,\n", |
234 | | - " initialize_project=initialize_project,\n", |
235 | | - " root_page_id=root_page_id,\n", |
236 | | - " )\n", |
237 | | - "\n", |
238 | | - " # Create and return the backend\n", |
239 | | - " return NotionBackend(root_page_id=root_page_id, notion_client=notion_client)" |
| 69 | + " return RagasApiClient(app_token=app_token, base_url=base_url)\n" |
240 | 70 | ] |
241 | 71 | } |
242 | 72 | ], |
|
0 commit comments