|
55 | 55 | instrument_generate_from_raw, |
56 | 56 | start_generate_span, |
57 | 57 | ) |
| 58 | +from ..telemetry.context import generate_request_id, with_context |
58 | 59 | from .adapters import ( |
59 | 60 | AdapterMixin, |
60 | 61 | AdapterType, |
@@ -389,71 +390,80 @@ async def _generate_from_context( |
389 | 390 | span = start_generate_span( |
390 | 391 | backend=self, action=action, ctx=ctx, format=format, tool_calls=tool_calls |
391 | 392 | ) |
392 | | - await self.do_generate_walk(action) |
393 | 393 |
|
394 | | - # Upsert model options. |
395 | | - model_opts = self._simplify_and_merge(model_options) |
| 394 | + with with_context( |
| 395 | + request_id=generate_request_id(), |
| 396 | + model_id=str(getattr(self, "model_id", "unknown")), |
| 397 | + ): |
| 398 | + await self.do_generate_walk(action) |
396 | 399 |
|
397 | | - # Requirements can be automatically rerouted to a requirement adapter. |
398 | | - if isinstance(action, Requirement): |
399 | | - # See docs/dev/requirement_aLoRA_rerouting.md |
400 | | - reroute_to_alora = self.default_to_constraint_checking_alora |
401 | | - adapter_name = "requirement_check" |
| 400 | + # Upsert model options. |
| 401 | + model_opts = self._simplify_and_merge(model_options) |
402 | 402 |
|
403 | | - if isinstance(action, ALoraRequirement): |
404 | | - reroute_to_alora = True |
405 | | - adapter_name = action.intrinsic_name |
406 | | - alora_action = action |
407 | | - else: |
408 | | - assert action.description is not None, ( |
409 | | - "must have a description when generating from a requirement" |
410 | | - ) |
411 | | - alora_action = ALoraRequirement(action.description, adapter_name) |
| 403 | + # Requirements can be automatically rerouted to a requirement adapter. |
| 404 | + if isinstance(action, Requirement): |
| 405 | + # See docs/dev/requirement_aLoRA_rerouting.md |
| 406 | + reroute_to_alora = self.default_to_constraint_checking_alora |
| 407 | + adapter_name = "requirement_check" |
412 | 408 |
|
413 | | - # Check if a requirement_check (or AloraRequirement specified) adapter |
414 | | - # exists. |
415 | | - alora_req_adapter = get_adapter_for_intrinsic( |
416 | | - adapter_name, [AdapterType.ALORA], self._added_adapters |
417 | | - ) |
418 | | - if alora_req_adapter is None: |
419 | | - # Log a warning if using an AloraRequirement but no adapter fit. |
420 | | - if reroute_to_alora and isinstance(action, ALoraRequirement): |
421 | | - MelleaLogger.get_logger().warning( |
422 | | - f"attempted to use an AloraRequirement but backend {self} doesn't have the specified adapter added {adapter_name}; defaulting to regular generation" |
| 409 | + if isinstance(action, ALoraRequirement): |
| 410 | + reroute_to_alora = True |
| 411 | + adapter_name = action.intrinsic_name |
| 412 | + alora_action = action |
| 413 | + else: |
| 414 | + assert action.description is not None, ( |
| 415 | + "must have a description when generating from a requirement" |
423 | 416 | ) |
424 | | - reroute_to_alora = False |
| 417 | + alora_action = ALoraRequirement(action.description, adapter_name) |
| 418 | + |
| 419 | + # Check if a requirement_check (or AloraRequirement specified) adapter |
| 420 | + # exists. |
| 421 | + alora_req_adapter = get_adapter_for_intrinsic( |
| 422 | + adapter_name, [AdapterType.ALORA], self._added_adapters |
| 423 | + ) |
| 424 | + if alora_req_adapter is None: |
| 425 | + # Log a warning if using an AloraRequirement but no adapter fit. |
| 426 | + if reroute_to_alora and isinstance(action, ALoraRequirement): |
| 427 | + MelleaLogger.get_logger().warning( |
| 428 | + f"attempted to use an AloraRequirement but backend {self} doesn't have the specified adapter added {adapter_name}; defaulting to regular generation" |
| 429 | + ) |
| 430 | + reroute_to_alora = False |
425 | 431 |
|
426 | | - if issubclass(type(action), LLMaJRequirement): |
427 | | - reroute_to_alora = False |
| 432 | + if issubclass(type(action), LLMaJRequirement): |
| 433 | + reroute_to_alora = False |
428 | 434 |
|
429 | | - if reroute_to_alora: |
430 | | - # Keep the alora requirement handling separate for now. |
| 435 | + if reroute_to_alora: |
| 436 | + # Keep the alora requirement handling separate for now. |
| 437 | + mot = await self._generate_from_intrinsic( |
| 438 | + alora_action, ctx, model_options=model_opts |
| 439 | + ) |
| 440 | + # Store span for telemetry |
| 441 | + if span is not None: |
| 442 | + mot._meta["_telemetry_span"] = span |
| 443 | + return mot, ctx.add(alora_action).add(mot) |
| 444 | + |
| 445 | + elif isinstance(action, Intrinsic): |
431 | 446 | mot = await self._generate_from_intrinsic( |
432 | | - alora_action, ctx, model_options=model_opts |
| 447 | + action, ctx, model_options=model_opts |
433 | 448 | ) |
434 | 449 | # Store span for telemetry |
435 | 450 | if span is not None: |
436 | 451 | mot._meta["_telemetry_span"] = span |
437 | | - return mot, ctx.add(alora_action).add(mot) |
| 452 | + return mot, ctx.add(action).add(mot) |
438 | 453 |
|
439 | | - elif isinstance(action, Intrinsic): |
440 | | - mot = await self._generate_from_intrinsic( |
441 | | - action, ctx, model_options=model_opts |
| 454 | + mot = await self._generate_from_context_standard( |
| 455 | + action, |
| 456 | + ctx, |
| 457 | + _format=format, |
| 458 | + model_options=model_opts, |
| 459 | + tool_calls=tool_calls, |
442 | 460 | ) |
443 | | - # Store span for telemetry |
| 461 | + |
| 462 | + # Store span in metadata for post_processing to record telemetry |
444 | 463 | if span is not None: |
445 | 464 | mot._meta["_telemetry_span"] = span |
446 | | - return mot, ctx.add(action).add(mot) |
447 | 465 |
|
448 | | - mot = await self._generate_from_context_standard( |
449 | | - action, ctx, _format=format, model_options=model_opts, tool_calls=tool_calls |
450 | | - ) |
451 | | - |
452 | | - # Store span in metadata for post_processing to record telemetry |
453 | | - if span is not None: |
454 | | - mot._meta["_telemetry_span"] = span |
455 | | - |
456 | | - return mot, ctx.add(action).add(mot) |
| 466 | + return mot, ctx.add(action).add(mot) |
457 | 467 |
|
458 | 468 | def _generate_with_adapter_lock( |
459 | 469 | self, adapter_name: str, generate_func: Callable, *args, **kwargs |
|
0 commit comments