unique_toolkit 2026.48.0.dev18__py3-none-any.whl → 2026.48.0.dev20__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -28,6 +28,11 @@ class SourceSelectionMode(StrEnum):
28
28
  FROM_ORIGINAL_RESPONSE = "FROM_ORIGINAL_RESPONSE"
29
29
 
30
30
 
31
+ # Applied to the raw answer in addition to the batch citation normalisation, so
32
+ # citation formats outside the platform-resolved set can still be configured.
33
+ DEFAULT_REFERENCE_PATTERN = r"[\[<]?source[\s_]?(\d+)[>\]]?"
34
+
35
+
31
36
  class HallucinationPromptsConfig(EvaluationMetricPromptsConfig):
32
37
  system_prompt_template: PromptType = Field(default_factory=system_prompt_loader)
33
38
  user_prompt_template: PromptType = Field(default_factory=user_prompt_loader)
@@ -37,7 +42,7 @@ class HallucinationConfig(EvaluationMetricConfig):
37
42
  source_selection_mode: SkipJsonSchema[SourceSelectionMode] = Field(
38
43
  default=SourceSelectionMode.FROM_ORIGINAL_RESPONSE
39
44
  )
40
- reference_pattern: SkipJsonSchema[str] = Field(default=r"[\[<]?source(\d+)[>\]]?")
45
+ reference_pattern: SkipJsonSchema[str] = Field(default=DEFAULT_REFERENCE_PATTERN)
41
46
  enabled: SkipJsonSchema[bool] = False
42
47
  name: SkipJsonSchema[EvaluationMetricName] = EvaluationMetricName.HALLUCINATION
43
48
  language_model: LMI = LanguageModelInfo.from_name(
@@ -5,6 +5,7 @@ from unique_toolkit._common.utils.jinja.render import render_template
5
5
  from unique_toolkit.agentic.evaluation.config import EvaluationMetricConfig
6
6
  from unique_toolkit.agentic.evaluation.exception import EvaluatorException
7
7
  from unique_toolkit.agentic.evaluation.hallucination.constants import (
8
+ DEFAULT_REFERENCE_PATTERN,
8
9
  SourceSelectionMode,
9
10
  hallucination_required_input_fields,
10
11
  )
@@ -16,6 +17,9 @@ from unique_toolkit.agentic.evaluation.schemas import (
16
17
  )
17
18
  from unique_toolkit.content import ContentReference
18
19
  from unique_toolkit.content.schemas import ContentChunk
20
+ from unique_toolkit.experimental._internal.streaming.pattern_replacer import (
21
+ normalize_citations_to_brackets,
22
+ )
19
23
  from unique_toolkit.language_model.builder import MessagesBuilder
20
24
  from unique_toolkit.language_model.infos import ModelCapabilities
21
25
  from unique_toolkit.language_model.invocation_stats import (
@@ -31,6 +35,8 @@ from unique_toolkit.language_model.service import LanguageModelService
31
35
 
32
36
  _LOGGER = getLogger(__name__)
33
37
 
38
+ _NORMALIZED_CITATION_PATTERN = re.compile(r"\[(\d+)\]")
39
+
34
40
 
35
41
  async def check_hallucination(
36
42
  company_id: str,
@@ -255,7 +261,7 @@ def context_text_from_stream_response(
255
261
  response: LanguageModelStreamResponse,
256
262
  selected_chunks: list[ContentChunk],
257
263
  source_selection_mode: SourceSelectionMode = SourceSelectionMode.FROM_ORIGINAL_RESPONSE,
258
- reference_pattern: str = r"[\[<]?source(\d+)[>\]]?",
264
+ reference_pattern: str = DEFAULT_REFERENCE_PATTERN,
259
265
  ) -> list[str]:
260
266
  """Extract context text from stream response based on selected chunks.
261
267
 
@@ -361,12 +367,12 @@ def _from_original_response_source_selection_mode(
361
367
  selected_chunks: list[ContentChunk],
362
368
  reference_pattern: str,
363
369
  ) -> list[ContentChunk]:
364
- """Extract referenced chunks from original text using regex pattern.
370
+ """Extract chunks cited in the original text, as normalised citations or via ``reference_pattern``.
365
371
 
366
372
  Args:
367
373
  original_text: The original response text containing source references.
368
374
  selected_chunks: List of content chunks to select from.
369
- ref_pattern: Regex pattern for extracting source numbers.
375
+ reference_pattern: Regex pattern for extracting source numbers.
370
376
 
371
377
  Returns:
372
378
  List of referenced content chunks.
@@ -374,11 +380,22 @@ def _from_original_response_source_selection_mode(
374
380
  if original_text is None:
375
381
  raise ValueError("original_text is required for FROM_ORIGINAL_RESPONSE mode")
376
382
  _LOGGER.debug("Processing original text for source extraction")
377
- source_number_matches = re.findall(reference_pattern, original_text)
383
+ normalized_text = normalize_citations_to_brackets(original_text)
384
+ source_number_matches = [
385
+ *_NORMALIZED_CITATION_PATTERN.findall(normalized_text),
386
+ *re.findall(reference_pattern, original_text),
387
+ ]
378
388
 
379
389
  # Remove duplicates and preserve order
380
390
  source_numbers = list(dict.fromkeys(int(num) for num in source_number_matches))
381
391
 
392
+ if not source_numbers and selected_chunks:
393
+ _LOGGER.warning(
394
+ f"No source citations found (pattern {reference_pattern!r}) although "
395
+ f"{len(selected_chunks)} chunks are available; the hallucination "
396
+ "check will run without references."
397
+ )
398
+
382
399
  # Add bounds checking
383
400
  max_index = len(selected_chunks) - 1
384
401
  valid_source_numbers = [idx for idx in source_numbers if 0 <= idx <= max_index]
@@ -205,7 +205,7 @@ def test_hallucination_config__has_default_regex_pattern__for_source_references(
205
205
  config: HallucinationConfig = HallucinationConfig()
206
206
 
207
207
  # Assert
208
- assert config.reference_pattern == r"[\[<]?source(\d+)[>\]]?"
208
+ assert config.reference_pattern == r"[\[<]?source[\s_]?(\d+)[>\]]?"
209
209
 
210
210
 
211
211
  @pytest.mark.ai
@@ -481,7 +481,7 @@ def test_hallucination_metric_default_config__has_expected_default_settings__con
481
481
  # Act & Assert
482
482
  assert config.name == EvaluationMetricName.HALLUCINATION
483
483
  assert config.source_selection_mode == SourceSelectionMode.FROM_ORIGINAL_RESPONSE
484
- assert config.reference_pattern == r"[\[<]?source(\d+)[>\]]?"
484
+ assert config.reference_pattern == r"[\[<]?source[\s_]?(\d+)[>\]]?"
485
485
 
486
486
 
487
487
  @pytest.mark.ai
@@ -571,6 +571,124 @@ def test_from_original_response_source_selection_mode__returns_empty_list__when_
571
571
  assert len(result) == 0
572
572
 
573
573
 
574
+ @pytest.mark.ai
575
+ @pytest.mark.parametrize(
576
+ "original_text",
577
+ [
578
+ "Claim one [source 0] and claim two [source 2].",
579
+ "Claim one [source_0] and claim two [source_2].",
580
+ "Claim one [source0] and claim two <source 2>.",
581
+ ],
582
+ )
583
+ def test_from_original_response_source_selection_mode__matches_spaced_and_underscored_citations__with_default_pattern(
584
+ sample_chunks: List[ContentChunk],
585
+ original_text: str,
586
+ ) -> None:
587
+ """
588
+ Purpose: Verify the default pattern extracts citations written as [source N] or [source_N].
589
+ Why this matters: The chat backend resolves these forms into references, so the judge
590
+ must see the same chunks or it flags a sourced answer as high hallucination (UN-28753).
591
+ Setup summary: Default config pattern, text citing sources 0 and 2 in each form,
592
+ assert both chunks are returned in order.
593
+ """
594
+ # Arrange
595
+ pattern: str = HallucinationConfig().reference_pattern
596
+
597
+ # Act
598
+ result: List[ContentChunk] = _from_original_response_source_selection_mode(
599
+ original_text, sample_chunks, pattern
600
+ )
601
+
602
+ # Assert
603
+ assert result == [sample_chunks[0], sample_chunks[2]]
604
+
605
+
606
+ @pytest.mark.ai
607
+ def test_from_original_response_source_selection_mode__logs_warning__when_chunks_exist_but_none_cited(
608
+ sample_chunks: List[ContentChunk],
609
+ caplog: pytest.LogCaptureFixture,
610
+ ) -> None:
611
+ """
612
+ Purpose: Verify a warning is logged when chunks are available but no citation matches.
613
+ Why this matters: An unmatched citation format silently sends the judge no references;
614
+ the warning makes that failure visible in logs.
615
+ Setup summary: Text with an unsupported citation form, assert empty result and warning.
616
+ """
617
+ # Arrange
618
+ original_text: str = "Claim [ref 1]."
619
+ pattern: str = HallucinationConfig().reference_pattern
620
+
621
+ # Act
622
+ with caplog.at_level("WARNING"):
623
+ result: List[ContentChunk] = _from_original_response_source_selection_mode(
624
+ original_text, sample_chunks, pattern
625
+ )
626
+
627
+ # Assert
628
+ assert result == []
629
+ assert "No source citations found" in caplog.text
630
+
631
+
632
+ @pytest.mark.ai
633
+ @pytest.mark.parametrize(
634
+ "original_text",
635
+ [
636
+ "Claim one [Source 0] and claim two [SOURCE 2].",
637
+ "Claim one [<source 0>] and claim two [<source2>].",
638
+ "Claim one [0] and claim two [2].",
639
+ "Claims [source: 0, 2].",
640
+ "Claims [0, 2].",
641
+ "Claim one [**0**] and claim two [**2**].",
642
+ 'Claim one source_number="0" and claim two source_number="2".',
643
+ "Claim one source n°0 and claim two source n°2.",
644
+ ],
645
+ )
646
+ def test_from_original_response_source_selection_mode__matches_all_platform_normalized_citations(
647
+ sample_chunks: List[ContentChunk],
648
+ original_text: str,
649
+ ) -> None:
650
+ """
651
+ Purpose: Verify every citation format the chat backend turns into a reference is extracted.
652
+ Why this matters: Running the answer through BATCH_NORMALIZATION_PATTERNS keeps the judge's
653
+ context in line with the references shown in the UI (UN-28753).
654
+ Setup summary: Default config pattern, text citing sources 0 and 2 in each normalised form,
655
+ assert both chunks are returned in order.
656
+ """
657
+ # Arrange
658
+ pattern: str = HallucinationConfig().reference_pattern
659
+
660
+ # Act
661
+ result: List[ContentChunk] = _from_original_response_source_selection_mode(
662
+ original_text, sample_chunks, pattern
663
+ )
664
+
665
+ # Assert
666
+ assert result == [sample_chunks[0], sample_chunks[2]]
667
+
668
+
669
+ @pytest.mark.ai
670
+ def test_from_original_response_source_selection_mode__matches_legacy_pattern__on_spaced_citations(
671
+ sample_chunks: List[ContentChunk],
672
+ ) -> None:
673
+ """
674
+ Purpose: Verify configs that still carry the pre-UN-28753 pattern also see [source N] citations.
675
+ Why this matters: reference_pattern can be persisted in space configs; normalisation must fix
676
+ the mismatch without requiring a config migration.
677
+ Setup summary: Legacy pattern without separator support, spaced citations, assert both found.
678
+ """
679
+ # Arrange
680
+ original_text: str = "Claim one [source 0] and claim two [source 2]."
681
+ legacy_pattern: str = r"[\[<]?source(\d+)[>\]]?"
682
+
683
+ # Act
684
+ result: List[ContentChunk] = _from_original_response_source_selection_mode(
685
+ original_text, sample_chunks, legacy_pattern
686
+ )
687
+
688
+ # Assert
689
+ assert result == [sample_chunks[0], sample_chunks[2]]
690
+
691
+
574
692
  @pytest.mark.ai
575
693
  def test_from_original_response_source_selection_mode__works_with_custom_regex_pattern(
576
694
  sample_chunks: List[ContentChunk],
@@ -423,7 +423,7 @@ class HistoryManager:
423
423
 
424
424
  cited: set[int] = {
425
425
  int(m)
426
- for m in re.findall(r"\[source(\d+)\]", assistant_text, re.IGNORECASE)
426
+ for m in re.findall(r"\[source[\s_]?(\d+)\]", assistant_text, re.IGNORECASE)
427
427
  }
428
428
 
429
429
  return [
@@ -95,6 +95,14 @@ BATCH_NORMALIZATION_PATTERNS: list[NormalizationPattern] = [
95
95
  """Normalisation rules that convert model-emitted citation formats
96
96
  to the canonical ``[N]`` bracket notation (for batch processing)."""
97
97
 
98
+
99
+ def normalize_citations_to_brackets(text: str) -> str:
100
+ """Rewrite every citation format the platform resolves into canonical ``[N]``."""
101
+ for pattern, replacement in BATCH_NORMALIZATION_PATTERNS:
102
+ text = re.sub(pattern, replacement, text)
103
+ return text
104
+
105
+
98
106
  NORMALIZATION_MAX_MATCH_LENGTH = 80
99
107
  """Upper bound on characters any single pattern can match. Sized for
100
108
  multi-source patterns like ``[source: 1, 2, ..., 20]``."""
@@ -19,7 +19,9 @@ the interface may change.
19
19
  uv add 'unique-toolkit[activity-records]'
20
20
  ```
21
21
 
22
- The extra installs asyncpg and prometheus_client. Also install `opentelemetry-api`
22
+ The extra installs asyncpg and prometheus_client. `AmqpPublisher` needs the
23
+ `activity-records-amqp` extra on top (`uv add 'unique-toolkit[activity-records-amqp]'`),
24
+ which adds aio-pika. Also install `opentelemetry-api`
23
25
  (or the `otel` extra) so events get their correlationId from the active trace.
24
26
 
25
27
  ## Migration
@@ -59,7 +61,22 @@ used for both, so it must not share a cache that other callers keep warm.
59
61
  Delivery is done by a sender the caller supplies (`PublisherLike`):
60
62
  `async publish(events: list[dict]) -> None`. Raise a `TimeoutError` subclass on
61
63
  timeout. Any other exception means rejected and the row backs off. The caller
62
- owns the sender's connection. The GraphQL sender lands with UN-29210.
64
+ owns the sender's connection. `publishers.amqp_publisher.AmqpPublisher` (RabbitMQ) is the
65
+ production sender until UN-29210 lands the GraphQL one. It reads `AMQP_URL`, or
66
+ `AMQP_HOST` with `AMQP_USERNAME` and `AMQP_PASSWORD` (plus optional `AMQP_PORT`,
67
+ `AMQP_VHOST`, `AMQP_HEARTBEAT_INTERVAL_IN_SECONDS`), and fails at `connect()` if none is
68
+ set. Call `connect()` before `start()` and `close()` after `stop()`.
69
+ `publishers.logging_publisher.LoggingPublisher` only logs events and deletes the rows
70
+ without delivering them: local testing, never production.
71
+
72
+ `AmqpPublisher` declares the ingest queue and its dead letter queue before the first
73
+ publish, with the same arguments as the Node producer and consumer (durable; the ingest
74
+ queue dead letters to the default exchange with the DLQ name as routing key). If a queue
75
+ already exists with other arguments, RabbitMQ refuses and `publish` raises
76
+ `ChannelPreconditionFailed`; the relay then keeps backing off every row until the queue
77
+ is fixed. The queues are not bound to a custom `exchange`, so that case still needs its
78
+ own bindings. A wrong broker host or password shows up as publish timeouts, not as a
79
+ startup error.
63
80
 
64
81
  ## FastAPI
65
82
  Create one runtime and start and stop it in the FastAPI lifespan:
@@ -1,10 +1,11 @@
1
- """Activity Records settings: size limits."""
1
+ """Activity Records settings: size limits and the broker connection."""
2
2
 
3
3
  from __future__ import annotations
4
4
 
5
5
  from typing import Final, Self
6
+ from urllib.parse import quote
6
7
 
7
- from pydantic import Field, model_validator
8
+ from pydantic import Field, SecretStr, model_validator
8
9
  from pydantic_settings import BaseSettings, SettingsConfigDict
9
10
 
10
11
  from unique_toolkit.experimental.resources.activity_records.json_size import (
@@ -94,3 +95,46 @@ class ActivityRecordsConfig(BaseSettings):
94
95
  "maximally-sized event always fits a batch by itself"
95
96
  )
96
97
  return self
98
+
99
+
100
+ class AmqpSettings(BaseSettings):
101
+ """``AMQP_URL`` when set; otherwise ``AMQP_HOST``, ``AMQP_USERNAME`` and ``AMQP_PASSWORD``
102
+ (all required) with optional port and vhost."""
103
+
104
+ model_config = SettingsConfigDict(
105
+ env_prefix="AMQP_", extra="ignore", hide_input_in_errors=True
106
+ )
107
+
108
+ url: SecretStr | None = None
109
+ host: str | None = None
110
+ port: int = 5672
111
+ username: str = ""
112
+ password: SecretStr = Field(default_factory=lambda: SecretStr(""))
113
+ vhost: str = "/"
114
+ heartbeat_interval_in_seconds: int = 30
115
+
116
+ @model_validator(mode="after")
117
+ def _require_a_broker(self) -> Self:
118
+ if self._configured_url():
119
+ return self
120
+ if not (self.host and self.username and self.password.get_secret_value()):
121
+ raise ValueError(
122
+ "AMQP_URL, or AMQP_HOST with AMQP_USERNAME and AMQP_PASSWORD, is required"
123
+ )
124
+ return self
125
+
126
+ def _configured_url(self) -> str | None:
127
+ if self.url is None:
128
+ return None
129
+ return self.url.get_secret_value() or None
130
+
131
+ @property
132
+ def connection_url(self) -> str:
133
+ configured = self._configured_url()
134
+ if configured is not None:
135
+ return configured
136
+ assert self.host is not None
137
+ username = quote(self.username, safe="")
138
+ password = quote(self.password.get_secret_value(), safe="")
139
+ vhost = quote(self.vhost, safe="")
140
+ return f"amqp://{username}:{password}@{self.host}:{self.port}/{vhost}"
@@ -0,0 +1,155 @@
1
+ """AMQP publisher for activity records. This module is the only one that imports aio-pika."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import asyncio
6
+ import logging
7
+ import socket
8
+ from collections.abc import Sequence
9
+ from typing import Final
10
+ from urllib.parse import parse_qsl, urlencode, urlsplit, urlunsplit
11
+ from uuid import uuid4
12
+
13
+ from unique_toolkit.experimental.resources.activity_records.config import AmqpSettings
14
+ from unique_toolkit.experimental.resources.activity_records.json_size import json_bytes
15
+ from unique_toolkit.experimental.resources.activity_records.prepare import WireEvent
16
+
17
+ try:
18
+ import aio_pika # pyright: ignore[reportMissingImports]
19
+ from aio_pika import DeliveryMode, Message # pyright: ignore[reportMissingImports]
20
+ from aio_pika.abc import ( # pyright: ignore[reportMissingImports]
21
+ AbstractExchange,
22
+ AbstractRobustConnection,
23
+ )
24
+ from aio_pika.connection import make_url # pyright: ignore[reportMissingImports]
25
+ from aio_pika.robust_channel import ( # pyright: ignore[reportMissingImports]
26
+ RobustChannel,
27
+ )
28
+ except ImportError as error: # pragma: no cover
29
+ raise ImportError(
30
+ "The AMQP publisher needs aio-pika. "
31
+ "Install it with: uv add 'unique-toolkit[activity-records-amqp]'"
32
+ ) from error
33
+
34
+ ROUTING_KEY: Final = "unique.activity-records.ingest"
35
+ DEAD_LETTER_QUEUE: Final = "unique.activity-records.ingest.dlq"
36
+ CONTENT_TYPE: Final = "application/cloudevents-batch+json"
37
+ logger = logging.getLogger(__name__)
38
+
39
+
40
+ def _connection_url(settings: AmqpSettings) -> str:
41
+ parts = urlsplit(settings.connection_url)
42
+ query = {
43
+ "heartbeat": str(settings.heartbeat_interval_in_seconds),
44
+ "name": f"{socket.gethostname()}-activity-records:publisher",
45
+ **dict(parse_qsl(parts.query)),
46
+ }
47
+ return urlunsplit(parts._replace(query=urlencode(query, safe=":")))
48
+
49
+
50
+ async def _declare_ingest_queues(channel: RobustChannel) -> None:
51
+ """Same arguments as Node's ``assertActivityIngestQueues``; a mismatch makes RabbitMQ
52
+ refuse the declaration."""
53
+ queue_options = {"durable": True, "exclusive": False, "auto_delete": False}
54
+ await channel.declare_queue(DEAD_LETTER_QUEUE, **queue_options)
55
+ await channel.declare_queue(
56
+ ROUTING_KEY,
57
+ **queue_options,
58
+ arguments={
59
+ "x-dead-letter-exchange": "",
60
+ "x-dead-letter-routing-key": DEAD_LETTER_QUEUE,
61
+ },
62
+ )
63
+
64
+
65
+ class AmqpPublisher:
66
+ """Publishes each batch to the ingest queue as one persistent message. It declares the
67
+ ingest queue and its dead letter queue before the first publish. The caller owns it:
68
+ ``connect()`` before the outbox starts, ``close()`` after it stops."""
69
+
70
+ def __init__(self, exchange: str = "") -> None:
71
+ self._exchange_name = exchange
72
+ self._connection: AbstractRobustConnection | None = None
73
+ self._channel: RobustChannel | None = None
74
+ self._connect_task: asyncio.Task[None] | None = None
75
+
76
+ async def connect(self) -> None:
77
+ """Start connecting in the background and return at once. Raises ``ValidationError``
78
+ when no broker is configured."""
79
+ if self._connection is not None:
80
+ return
81
+ settings = AmqpSettings()
82
+ connection = aio_pika.RobustConnection(
83
+ make_url(_connection_url(settings)),
84
+ fail_fast=False,
85
+ )
86
+ self._connection = connection
87
+
88
+ async def connect_in_background() -> None:
89
+ try:
90
+ await connection.connect()
91
+ except Exception as error:
92
+ logger.warning(
93
+ "Activity Records broker connection failed (%s)",
94
+ type(error).__name__,
95
+ )
96
+
97
+ self._connect_task = asyncio.create_task(connect_in_background())
98
+
99
+ async def close(self) -> None:
100
+ """Cancel the connect task and close the connection. Safe to call twice."""
101
+ task = self._connect_task
102
+ self._connect_task = None
103
+ if task is not None and not task.done():
104
+ task.cancel()
105
+ await asyncio.gather(task, return_exceptions=True)
106
+ connection = self._connection
107
+ self._connection = None
108
+ self._channel = None
109
+ if connection is not None:
110
+ await connection.close()
111
+
112
+ async def publish(self, events: Sequence[WireEvent]) -> None:
113
+ """Raises ``RuntimeError`` before ``connect()``, aio-pika's ``PublishError`` for an
114
+ unroutable message and ``DeliveryError`` for a nack. A queue declared elsewhere with
115
+ other arguments raises ``ChannelPreconditionFailed``."""
116
+ exchange = await self._exchange()
117
+ await exchange.publish(
118
+ Message(
119
+ json_bytes(events),
120
+ content_type=CONTENT_TYPE,
121
+ delivery_mode=DeliveryMode.PERSISTENT,
122
+ message_id=str(uuid4()),
123
+ ),
124
+ routing_key=ROUTING_KEY,
125
+ mandatory=True,
126
+ )
127
+
128
+ async def _exchange(self) -> AbstractExchange:
129
+ channel = await self._open_channel()
130
+ if self._exchange_name == "":
131
+ return channel.default_exchange
132
+ # ensure=False does not declare and does not passive-declare.
133
+ return await channel.get_exchange(self._exchange_name, ensure=False)
134
+
135
+ async def _open_channel(self) -> RobustChannel:
136
+ connection = self._connection
137
+ if connection is None:
138
+ raise RuntimeError("Activity records publisher is not connected")
139
+ await connection.ready()
140
+ channel = self._channel
141
+ if channel is None:
142
+ # A robust channel reopens itself after a reconnect, so it is opened once.
143
+ # aio-pika 9: on_return_raises maps a mandatory return to PublishError and a nack to DeliveryError.
144
+ opened = await connection.channel(
145
+ publisher_confirms=True, on_return_raises=True
146
+ )
147
+ assert isinstance(opened, RobustChannel)
148
+ try:
149
+ await _declare_ingest_queues(opened)
150
+ except BaseException:
151
+ await opened.close()
152
+ raise
153
+ channel = self._channel = opened
154
+ await channel.ready()
155
+ return channel
@@ -0,0 +1,30 @@
1
+ """A sender that only logs, for testing the outbox end to end."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import logging
6
+ from collections.abc import Sequence
7
+
8
+ from unique_toolkit.experimental.resources.activity_records.json_size import json_bytes
9
+ from unique_toolkit.experimental.resources.activity_records.prepare import WireEvent
10
+
11
+ logger = logging.getLogger(__name__)
12
+
13
+
14
+ class LoggingPublisher:
15
+ """Local testing sender: logs each event and reports success without delivering it, so
16
+ the outbox rows are deleted. ``include_payload=True`` logs client data: never use it in
17
+ production."""
18
+
19
+ def __init__(self, *, include_payload: bool = False) -> None:
20
+ self.include_payload = include_payload
21
+
22
+ async def publish(self, events: Sequence[WireEvent]) -> None:
23
+ logger.info("Activity events batch: count=%d", len(events))
24
+ for event in events:
25
+ if self.include_payload:
26
+ logger.info("Activity event: %s", json_bytes(event).decode("utf-8"))
27
+ else:
28
+ logger.info(
29
+ "Activity event: id=%s type=%s", event.get("id"), event.get("type")
30
+ )
@@ -3,7 +3,7 @@ import re
3
3
  from unique_toolkit.chat.schemas import ChatMessage
4
4
  from unique_toolkit.content.schemas import ContentChunk, ContentReference
5
5
  from unique_toolkit.experimental._internal.streaming.pattern_replacer import (
6
- BATCH_NORMALIZATION_PATTERNS,
6
+ normalize_citations_to_brackets,
7
7
  )
8
8
 
9
9
 
@@ -84,9 +84,7 @@ def _preprocess_message(text: str) -> str:
84
84
  to ``<sup>N</sup>``. The streaming path uses ``NORMALIZATION_PATTERNS`` which
85
85
  outputs ``<sup>N</sup>`` directly.
86
86
  """
87
- for pattern, replacement in BATCH_NORMALIZATION_PATTERNS:
88
- text = re.sub(pattern, replacement, text)
89
- return text
87
+ return normalize_citations_to_brackets(text)
90
88
 
91
89
 
92
90
  def _limit_consecutive_source_references(text: str) -> str:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: unique-toolkit
3
- Version: 2026.48.0.dev18
3
+ Version: 2026.48.0.dev20
4
4
  Summary:
5
5
  Author: Cedric Klinkert, Martin Fadler, Sadique Sheik, Fabian Schläpfer, Pascal Hauri, Ejiroghene Oki-Peter, Rami Azouz, Ricardo Visini, Sanzio Monti
6
6
  Author-email: Cedric Klinkert <cedric.klinkert@unique.ch>, Martin Fadler <martin.fadler@unique.ch>, Sadique Sheik <sadique@unique.ch>, Fabian Schläpfer <fabian@unique.ch>, Pascal Hauri <pascal@unique.ch>, Ejiroghene Oki-Peter <oki-peter.ejiroghene@unique.ai>, Rami Azouz <rami.ext@unique.ch>, Ricardo Visini <ricardo.visini@unique.ch>, Sanzio Monti <sanzio.monti@unique.ai>
@@ -16,7 +16,7 @@ Requires-Dist: sseclient>=0.0.27,<0.0.28
16
16
  Requires-Dist: openai>=1.105.0,<3
17
17
  Requires-Dist: platformdirs>=4.0.0,<5
18
18
  Requires-Dist: pillow>=13.0.0
19
- Requires-Dist: unique-sdk>=2026.48.0.dev17,<2026.48.0rc0
19
+ Requires-Dist: unique-sdk>=2026.48.0.dev18,<2026.48.0rc0
20
20
  Requires-Dist: jambo>=0.1.2,<0.2
21
21
  Requires-Dist: jinja2>=3.1.6,<4
22
22
  Requires-Dist: docxtpl>=0.20.1,<0.21
@@ -35,7 +35,9 @@ Requires-Dist: cachetools>=5.0,<6
35
35
  Requires-Dist: rapidfuzz>=3.0,<4
36
36
  Requires-Dist: pyyaml>=6.0.3
37
37
  Requires-Dist: asyncpg>=0.30,<1 ; extra == 'activity-records'
38
- Requires-Dist: unique-toolkit[monitoring]>=2026.48.0.dev18,<2026.48.0rc0 ; extra == 'activity-records'
38
+ Requires-Dist: unique-toolkit[monitoring]>=2026.48.0.dev20,<2026.48.0rc0 ; extra == 'activity-records'
39
+ Requires-Dist: aio-pika>=9.5.5,<10 ; extra == 'activity-records-amqp'
40
+ Requires-Dist: unique-toolkit[activity-records]>=2026.48.0.dev20,<2026.48.0rc0 ; extra == 'activity-records-amqp'
39
41
  Requires-Dist: fastapi>=0.133.0,<1.0.0 ; extra == 'fastapi'
40
42
  Requires-Dist: starlette>=1.3.1,<2.0.0 ; extra == 'fastapi'
41
43
  Requires-Dist: uvicorn>=0.37.0,<0.38 ; extra == 'fastapi'
@@ -50,6 +52,7 @@ Requires-Dist: opentelemetry-instrumentation-fastapi>=0.65b0,<1 ; extra == 'otel
50
52
  Requires-Dist: opentelemetry-instrumentation-requests>=0.65b0,<1 ; extra == 'otel'
51
53
  Requires-Python: >=3.12
52
54
  Provides-Extra: activity-records
55
+ Provides-Extra: activity-records-amqp
53
56
  Provides-Extra: fastapi
54
57
  Provides-Extra: langchain
55
58
  Provides-Extra: monitoring
@@ -97,21 +97,21 @@ unique_toolkit/agentic/evaluation/context_relevancy/schema.py,sha256=lZd0TPzH43i
97
97
  unique_toolkit/agentic/evaluation/context_relevancy/service.py,sha256=RAmV21RXiyUd_kLQtBG7ZqaSa3iEJeQZQVcG95N6UOg,11866
98
98
  unique_toolkit/agentic/evaluation/evaluation_manager.py,sha256=OIh1LhkKQFIpori06Ikaidc1HtIFItVxUsYp73XLCeg,8801
99
99
  unique_toolkit/agentic/evaluation/exception.py,sha256=6DtYhsOdJ7WnbpjY45pb9m7VN4ZbRACsQ-VRLeHZXgs,911
100
- unique_toolkit/agentic/evaluation/hallucination/constants.py,sha256=uEQR-CNKmeXizDaLOLHhwTZ0iYUmFwoKeMLHPm4M0sY,3208
100
+ unique_toolkit/agentic/evaluation/hallucination/constants.py,sha256=rQnXb70eEFRnjk_1xUZmgBZOHbc_RKF9mS9Ml49IW5w,3428
101
101
  unique_toolkit/agentic/evaluation/hallucination/hallucination_evaluation.py,sha256=R_GKauBDj-u-taDGG1k6JULgEyFcOceaOqjTTO6fwZs,4730
102
102
  unique_toolkit/agentic/evaluation/hallucination/prompts/__init__.py,sha256=4KFYMZsB3fJUKzoiUJE1npZ0gueWgvceB32EUrN-v7A,343
103
103
  unique_toolkit/agentic/evaluation/hallucination/prompts/system_prompt.j2,sha256=sDUX6G645Ba40D_qKu4cUI8g-sJOfG8JpZreTNFgf7M,2616
104
104
  unique_toolkit/agentic/evaluation/hallucination/prompts/user_prompt.j2,sha256=mD_qE9fOkyc1XXrebFt097ddx8bTlA6lbY04hKSQmWs,273
105
105
  unique_toolkit/agentic/evaluation/hallucination/service.py,sha256=QSPsRxx2H-YpSR2Jn7zJKtwZfZ7elTPAFQiYvM3gYfs,2618
106
- unique_toolkit/agentic/evaluation/hallucination/utils.py,sha256=maiMxlUjT9_U0GAH9HvemMIUE-47MCGtwm2XMwDAShQ,15664
106
+ unique_toolkit/agentic/evaluation/hallucination/utils.py,sha256=pU40fkyMB4O9eO4ii_XjqEmXxA9E2Ni1sGKKxxW1A_U,16351
107
107
  unique_toolkit/agentic/evaluation/output_parser.py,sha256=0FDo8YY_Dc4qlTNeYyQkznzIFj9aX9wMrLOTbhhTl6g,1418
108
108
  unique_toolkit/agentic/evaluation/schemas.py,sha256=RT0O-AXSOf1zV3fDbUfn7V6bOIA01pkzui02Uad9E3Y,3815
109
109
  unique_toolkit/agentic/evaluation/tests/fixtures.py,sha256=Q-ughTfDiAdsMKbBVGzFiBucFdAx-FXgJ9iqp5xMyPs,2801
110
110
  unique_toolkit/agentic/evaluation/tests/test_config.py,sha256=p7xFQ7KE_yU8jGpqYA7ntAYe5Vln33wd6nwv3FM9XfI,8327
111
111
  unique_toolkit/agentic/evaluation/tests/test_context_relevancy_service.py,sha256=-GRxH70vzvrZG6Hz0igH6i2CqM7Z9jjKt3396xWDh84,12011
112
- unique_toolkit/agentic/evaluation/tests/test_hallucination_constants.py,sha256=A77nRcniI7hUf640f1sTvCAibQbJE30ikbz1RX4fltw,20478
112
+ unique_toolkit/agentic/evaluation/tests/test_hallucination_constants.py,sha256=dD30f06SlCZdD-PvI89CVat_tCC2qHzAN9Ry3yGxKig,20490
113
113
  unique_toolkit/agentic/evaluation/tests/test_hallucination_external_context.py,sha256=3JR9WfBWazX7a1h3uq77Q6kSKm8Mt-aAW8KUYxJUw14,4303
114
- unique_toolkit/agentic/evaluation/tests/test_hallucination_utils.py,sha256=TJcJf9SZcH1dsSq38NkvSBM0VZfBRkan49Una3UPQB0,41546
114
+ unique_toolkit/agentic/evaluation/tests/test_hallucination_utils.py,sha256=YPpNc95UszRMquhmj7ZYwyiJRrcHeNEyIvnnXY9MYHE,45943
115
115
  unique_toolkit/agentic/evaluation/tests/test_output_parser.py,sha256=KfltytmvqnPWLhmZpBXqcRmnlYorw_USwM5rkLVv8so,5179
116
116
  unique_toolkit/agentic/evaluation/tests/test_prompt_loaders.py,sha256=zBREdlKf5tdDyB8XSaNgpQv3-tuZJoYteeJrp6WMWDM,11897
117
117
  unique_toolkit/agentic/evaluation/utils.py,sha256=HmyPaDV8wdW-_gOjjW-wDaMKgdrsP5-SHP7OqTmGI_A,264
@@ -119,7 +119,7 @@ unique_toolkit/agentic/feature_flags/__init__.py,sha256=fBR9FaLrc9e8sdYDe1LwP9aN
119
119
  unique_toolkit/agentic/feature_flags/feature_flags.py,sha256=3E24xKSlpmZmIgIyPrXT_P_6yG1zeODFVJdEgcY0uJA,7250
120
120
  unique_toolkit/agentic/history_manager/context_overflow.py,sha256=N5g8bHUHcRz7gey3cCX-xwK7DJkp_O-3IthMnGsIvcE,9472
121
121
  unique_toolkit/agentic/history_manager/history_construction_with_contents.py,sha256=cw7YpLMk3NKlNJ4k0Nv2bhMH15NEDYUUwvrloJrV38s,26096
122
- unique_toolkit/agentic/history_manager/history_manager.py,sha256=JM57S-Cou_vzDR3eM52gYRjgu-lhVUDoYyVGE7pgIYY,19078
122
+ unique_toolkit/agentic/history_manager/history_manager.py,sha256=SUa6OnPD1LCHBYDzbapUbDfE-8KJCBwO9go7oYNuABU,19084
123
123
  unique_toolkit/agentic/history_manager/loop_token_reducer.py,sha256=yLxBPF6gkzpBTL5aOzPgQ61bsZd2kOOxeNrGBEdWTEg,31350
124
124
  unique_toolkit/agentic/history_manager/settings.py,sha256=Lrx3a8yXeU0MXrWlV6RZSHsgYWF9Zrs8LyR96OpqQ_k,1216
125
125
  unique_toolkit/agentic/history_manager/utils.py,sha256=DiBip8xtah3I9-G-_Ul3OjlYKunrXR-5RJtzREHWV_0,6579
@@ -309,7 +309,7 @@ unique_toolkit/experimental/__init__.py,sha256=fhSv-QhH0yxnnGqSwbFhX7Rp3TCG_zBph
309
309
  unique_toolkit/experimental/_internal/__init__.py,sha256=wClblbvHTnOCjymmXVoAde1is3AF18iQ46lK6wKObr4,995
310
310
  unique_toolkit/experimental/_internal/streaming/__init__.py,sha256=YExuxaOHB96xT1k0FsWiZEo6Ngiyv54GumhCAKW_3aA,942
311
311
  unique_toolkit/experimental/_internal/streaming/common.py,sha256=AOvgFqywJcm2qBhz78Zyt4LZh8uI-X2dj0lsjaKCyZ8,8551
312
- unique_toolkit/experimental/_internal/streaming/pattern_replacer.py,sha256=RLLQKC12SvTah-1V-yyVBOIW00m47yJIPCzZd0Zkor4,8922
312
+ unique_toolkit/experimental/_internal/streaming/pattern_replacer.py,sha256=RthtvrkYcuL3Fq4pXanzNJJRshnmxYneIq3yo80ZqfM,9194
313
313
  unique_toolkit/experimental/components/__init__.py,sha256=rl-s8AnHZoLEOB8ei9jQgiRIndfMWrzWJm8gEMzNcS4,2029
314
314
  unique_toolkit/experimental/components/_base.py,sha256=XZHKffTIy331ZQnsywl0m1XkJ4DCjHV2on5EBkaG-ik,4153
315
315
  unique_toolkit/experimental/components/content_tree/__init__.py,sha256=zAMzctv__M5e4OppVD_neO471V4zKBeHcbVrixZciaM,1946
@@ -361,15 +361,18 @@ unique_toolkit/experimental/integrations/openai/streaming/event_routing/subscrib
361
361
  unique_toolkit/experimental/integrations/openai/streaming/event_routing/subscribers/message_persister.py,sha256=JVA0lcF3_WCYEUgWqX53gO5Lj7zR1iFCCuNXuWDPGbo,8930
362
362
  unique_toolkit/experimental/integrations/openai/streaming/event_routing/subscribers/progress_log_persister.py,sha256=VCYGZY3WSpI7mvVs2gO89-M0JenakW01BGfBfFkPDrw,3499
363
363
  unique_toolkit/experimental/resources/__init__.py,sha256=KdMPaaneY0twZ-JBfwImRjXZaWT68NVuOIeqdNXFh_Y,3377
364
- unique_toolkit/experimental/resources/activity_records/README.md,sha256=L04gy3sxzSSAA-LlsveSJ27hwAO1kM4KySfvfB1XS6w,15337
364
+ unique_toolkit/experimental/resources/activity_records/README.md,sha256=iyPFhxZRs6fd-coVAURu3qQpX5D9nbpA5bFkYiH2JFs,16570
365
365
  unique_toolkit/experimental/resources/activity_records/__init__.py,sha256=hhuMC8_HVKO38l9A9J8MFayZ_LAs7YUEmd7pWbnZtsY,548
366
- unique_toolkit/experimental/resources/activity_records/config.py,sha256=8jV6gT89zKBnHymTz_SSQbN6gfGPgK7xtKsdcZ4GA6s,3261
366
+ unique_toolkit/experimental/resources/activity_records/config.py,sha256=iggq3hnKt_c3z8bZ45MD6YFTvoimuUyU_-cCBCM6KMk,4831
367
367
  unique_toolkit/experimental/resources/activity_records/enqueue.py,sha256=uONXVBdpwtDWnoc2a6HewjA0POh4jUfoZgBnsTHr8q4,12429
368
368
  unique_toolkit/experimental/resources/activity_records/json_size.py,sha256=gfIRmI08txf0Y_wEyPTqtYrUk9FHwSouYC4R7qtdgdY,9761
369
369
  unique_toolkit/experimental/resources/activity_records/metrics.py,sha256=HuAbgEfJk658pPiswiqMAzlAL5CNYzzwGlQN73si9kA,4501
370
370
  unique_toolkit/experimental/resources/activity_records/migrate.py,sha256=LNksBdPfwRG6Xm8krShI55qA7urK19932pcuAx1xGjM,2800
371
371
  unique_toolkit/experimental/resources/activity_records/prepare.py,sha256=Q2VOqQ9S0hO1yTBGzMyCnk8AQ-UDwkIpQQlq4DQSIXo,10419
372
372
  unique_toolkit/experimental/resources/activity_records/protocols.py,sha256=pOrxKhUbj3eQx-NfnPMb6fvsHWGXwdgaqxIPRfDwN2A,611
373
+ unique_toolkit/experimental/resources/activity_records/publishers/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
374
+ unique_toolkit/experimental/resources/activity_records/publishers/amqp_publisher.py,sha256=Sapea2x8nloOiOAksHWHO5rQuA01lIJGT7S2WxLU9GI,6232
375
+ unique_toolkit/experimental/resources/activity_records/publishers/logging_publisher.py,sha256=wBGjfvVfnV-lViOX8l79AGEecxActZnrnZhU49NeON4,1148
373
376
  unique_toolkit/experimental/resources/activity_records/relay.py,sha256=7md0U9HglBMjWdAvOmvsHozKDGEAI_7I-peM1IFOG74,19607
374
377
  unique_toolkit/experimental/resources/activity_records/schemas.py,sha256=6-jbRHJrqOjbPp9ij1etpbZjKZ6ol-TiE60vdiDkqJA,6864
375
378
  unique_toolkit/experimental/resources/activity_records/store.py,sha256=is-tESmoo-ox7nY6WrIOXdqe8UH1QsLTydoIXxtB8WA,13342
@@ -421,7 +424,7 @@ unique_toolkit/language_model/invocation_stats.py,sha256=CQbpUz394IsVd8eNTB1gPSM
421
424
  unique_toolkit/language_model/model_costs.py,sha256=AVHAuMTzsglzusWsVF_xwBVEndST-TVCVi0H49OhdvE,7419
422
425
  unique_toolkit/language_model/prompt.py,sha256=JSawaLjQg3VR-E2fK8engFyJnNdk21zaO8pPIodzN4Q,3991
423
426
  unique_toolkit/language_model/provider_token_count.py,sha256=mS597Mvrc7EsAc9iJSmASIEwvETSPLtIEsWxusPrQVc,5337
424
- unique_toolkit/language_model/reference.py,sha256=T3wr2jP_uRg5wKVIbKqLbxJlcgxfbZQGq3XdjbQ2kSo,6984
427
+ unique_toolkit/language_model/reference.py,sha256=YcADLGMhgV5Hc9gKANjdICkfIMnQDssuN52OHlgNkuE,6908
425
428
  unique_toolkit/language_model/schemas.py,sha256=o8zGkfoYayO1gL85G-ulV3Akp_BwjgEWGGUb89j1OgU,35440
426
429
  unique_toolkit/language_model/service.py,sha256=lgpzWhqxLBOeFacCafQi4e7BqAxJbOlr8GnCYcKeTdM,13274
427
430
  unique_toolkit/language_model/settings.py,sha256=Yj9zcIctL3WhxDR4v6VnliYmn3txq519MK8DoWFHuiQ,1542
@@ -447,6 +450,6 @@ unique_toolkit/short_term_memory/service.py,sha256=2JMBEBn6GoyK-a0xvdcWpPft-3C74
447
450
  unique_toolkit/smart_rules/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
448
451
  unique_toolkit/smart_rules/compile.py,sha256=vRe0aMJWuVIK-VsqomgxQImzke7MDmwgSxxJfYCa7zw,1386
449
452
  unique_toolkit/test_utilities/events.py,sha256=FLrhMh0A_mQutAUjoD0FdMcVQ4Gq31r35OVGzRjvLro,6760
450
- unique_toolkit-2026.48.0.dev18.dist-info/WHEEL,sha256=eh7sammvW2TypMMMGKgsM83HyA_3qQ5Lgg3ynoecH3M,79
451
- unique_toolkit-2026.48.0.dev18.dist-info/METADATA,sha256=7WwS2s1VmOeKhWhEbIZYhcw1NgsLwBpZuXeLJAr7eOs,8021
452
- unique_toolkit-2026.48.0.dev18.dist-info/RECORD,,
453
+ unique_toolkit-2026.48.0.dev20.dist-info/WHEEL,sha256=eh7sammvW2TypMMMGKgsM83HyA_3qQ5Lgg3ynoecH3M,79
454
+ unique_toolkit-2026.48.0.dev20.dist-info/METADATA,sha256=0XqYeMGX1hsSccvS3XM0UpX_H8b9nBwXXxqS0TnCszI,8243
455
+ unique_toolkit-2026.48.0.dev20.dist-info/RECORD,,