|
159 | 159 | "source": [ |
160 | 160 | "# | export\n", |
161 | 161 | "@patch\n", |
162 | | - "def langfuse_experiment(\n", |
| 162 | + "def experiment(\n", |
163 | 163 | " self: Project, experiment_model: t.Type[NotionModel], name_prefix: str = \"\"\n", |
164 | 164 | "):\n", |
165 | | - " \"\"\"Decorator for creating experiment functions.\n", |
| 165 | + " \"\"\"Decorator for creating experiment functions without Langfuse integration.\n", |
166 | 166 | "\n", |
167 | 167 | " Args:\n", |
| 168 | + " experiment_model: The NotionModel type to use for experiment results\n", |
168 | 169 | " name_prefix: Optional prefix for experiment names\n", |
169 | 170 | "\n", |
170 | 171 | " Returns:\n", |
|
174 | 175 | " def decorator(func: t.Callable) -> ExperimentProtocol:\n", |
175 | 176 | " @wraps(func)\n", |
176 | 177 | " async def wrapped_experiment(*args, **kwargs):\n", |
177 | | - " # wrap the function with langfuse observation so that it can be traced\n", |
178 | | - " # and spans inside the function can be retrieved with sync_trace()\n", |
179 | | - " observed_func = observe(name=f\"{name_prefix}-{func.__name__}\")(func)\n", |
180 | | - "\n", |
181 | | - " return await observed_func(*args, **kwargs)\n", |
| 178 | + " # Simply call the function without Langfuse observation\n", |
| 179 | + " return await func(*args, **kwargs)\n", |
182 | 180 | "\n", |
183 | 181 | " # Add run method to the wrapped function\n", |
184 | 182 | " async def run_async(dataset: Dataset, name: t.Optional[str] = None):\n", |
|
196 | 194 | " for future in tqdm(asyncio.as_completed(tasks), total=len(tasks)):\n", |
197 | 195 | " result = await future\n", |
198 | 196 | " # Add each result to experiment view as it completes\n", |
199 | | - " results.append(result) if result is not None else None\n", |
| 197 | + " if result is not None:\n", |
| 198 | + " results.append(result)\n", |
200 | 199 | "\n", |
201 | 200 | " # upload results to experiment view\n", |
202 | 201 | " experiment_view = self.create_experiment(name=name, model=experiment_model)\n", |
|
208 | 207 | " wrapped_experiment.__setattr__(\"run_async\", run_async)\n", |
209 | 208 | " return t.cast(ExperimentProtocol, wrapped_experiment)\n", |
210 | 209 | "\n", |
| 210 | + " return decorator\n" |
| 211 | + ] |
| 212 | + }, |
| 213 | + { |
| 214 | + "cell_type": "code", |
| 215 | + "execution_count": null, |
| 216 | + "metadata": {}, |
| 217 | + "outputs": [], |
| 218 | + "source": [ |
| 219 | + "# | export\n", |
| 220 | + "@patch\n", |
| 221 | + "def langfuse_experiment(\n", |
| 222 | + " self: Project, experiment_model: t.Type[NotionModel], name_prefix: str = \"\"\n", |
| 223 | + "):\n", |
| 224 | + " \"\"\"Decorator for creating experiment functions with Langfuse integration.\n", |
| 225 | + "\n", |
| 226 | + " Args:\n", |
| 227 | + " experiment_model: The NotionModel type to use for experiment results\n", |
| 228 | + " name_prefix: Optional prefix for experiment names\n", |
| 229 | + "\n", |
| 230 | + " Returns:\n", |
| 231 | + " Decorator function that wraps experiment functions with Langfuse observation\n", |
| 232 | + " \"\"\"\n", |
| 233 | + "\n", |
| 234 | + " def decorator(func: t.Callable) -> ExperimentProtocol:\n", |
| 235 | + " # First, create a base experiment wrapper\n", |
| 236 | + " base_experiment = self.experiment(experiment_model, name_prefix)(func)\n", |
| 237 | + " \n", |
| 238 | + " # Override the wrapped function to add Langfuse observation\n", |
| 239 | + " @wraps(func)\n", |
| 240 | + " async def wrapped_with_langfuse(*args, **kwargs):\n", |
| 241 | + " # wrap the function with langfuse observation\n", |
| 242 | + " observed_func = observe(name=f\"{name_prefix}-{func.__name__}\")(func)\n", |
| 243 | + " return await observed_func(*args, **kwargs)\n", |
| 244 | + " \n", |
| 245 | + " # Replace the async function to use Langfuse\n", |
| 246 | + " original_run_async = base_experiment.run_async\n", |
| 247 | + " \n", |
| 248 | + " # Use the original run_async but with the Langfuse-wrapped function\n", |
| 249 | + " async def run_async_with_langfuse(dataset: Dataset, name: t.Optional[str] = None):\n", |
| 250 | + " # Override the internal wrapped_experiment with our Langfuse version\n", |
| 251 | + " base_experiment.__wrapped__ = wrapped_with_langfuse\n", |
| 252 | + " \n", |
| 253 | + " # Call the original run_async which will now use our Langfuse-wrapped function\n", |
| 254 | + " return await original_run_async(dataset, name)\n", |
| 255 | + " \n", |
| 256 | + " # Replace the run_async method\n", |
| 257 | + " base_experiment.__setattr__(\"run_async\", run_async_with_langfuse)\n", |
| 258 | + " \n", |
| 259 | + " return t.cast(ExperimentProtocol, base_experiment)\n", |
| 260 | + "\n", |
211 | 261 | " return decorator" |
212 | 262 | ] |
213 | 263 | }, |
|
0 commit comments