sentence_attribution
zocrates.agents.library_search.sentence_attribution ¶
Sentence-level attribution for claims using Option B (Pre-Segmented Numbered Spans).
Segments source passages into numbered atomic spans (preserving headings, table rows, display math blocks) and evaluates which spans support an extracted claim using nvidia/nemotron-3.5-lightning:free via OpenRouter.
ATTRIBUTION_SYSTEM_PROMPT
module-attribute
¶
ATTRIBUTION_SYSTEM_PROMPT = 'You are an expert academic fact-checker. Your task is to identify which numbered sentence(s), table row(s), or equation(s) in a source passage directly support an extracted scientific claim.\n\nAnalyze the claim against the numbered sentences in the source passage.\n\nOutput MUST be a single valid JSON object strictly matching this schema:\n{\n "supporting_indices": [<1-based integer indices of directly supporting sentences, or [] if none>],\n "unsupported_aspects": "<concise description of assertions from the claim not found in the text, or empty string if all facts are substantiated>"\n}\n\nStrict Rules:\n1. Direct Factual Support Only: A sentence supports a claim ONLY if it explicitly asserts a specific fact, finding, parameter, or mechanism in the claim. Topical similarity, introductory remarks, or general thematic relevance is NOT support.\n2. Strictly Minimal Attribution: Select ONLY the specific 1 to 3 key sentences that directly substantiate factual assertions. Do NOT select surrounding context, background, or preamble. NEVER select all or most sentences in a passage.\n3. Quantitative & Parameter Rigor: If the claim asserts specific quantities, concentrations, volumes, reaction numbers, or model names (e.g., 20 μM, 0.0156 µm³, Pepke et al., ODE solver) and the text lacks those exact values or names, note those specific missing elements in "unsupported_aspects".\n4. Natural Scholarly Explanation: In "unsupported_aspects", describe in clear, natural prose which specific factual assertions, mechanisms, or numbers from the claim are not found in the text. Refer naturally to "the text" or "the passage" without citing sentence numbers. If all facts are substantiated, set to "".\n5. When No Sentences Support: If the passage discusses the same general topic but contains NO direct evidence for the specific assertions in the claim, return "supporting_indices": [] and explain what is missing in "unsupported_aspects".\n6. Output ONLY the raw JSON object.'
SENTENCE_ATTRIBUTION_LOCAL_TIMEOUT_SEC
module-attribute
¶
SENTENCE_ATTRIBUTION_MODEL
module-attribute
¶
_ABBREVIATION_RE
module-attribute
¶
_ABBREVIATION_RE = re.compile('\\b(?:et al|e\\.g|i\\.e|etc|vs|cf|fig|eq|no|sec|approx|ca|dr|prof|mr|mrs|jr|sr|[a-z])\\.?$', re.IGNORECASE)
_SENTENCE_BOUNDARY_RE
module-attribute
¶
compute_sentence_attribution
async
¶
compute_sentence_attribution(claim, source_text, api_key=None, timeout=SENTENCE_ATTRIBUTION_TIMEOUT_SEC, model=None, base_url=None, zotero_data_dir=None)
Compute sentence-level attribution for a claim against source text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
claim
|
str
|
Target claim statement to verify. |
required |
source_text
|
str
|
Grounding source excerpt or chunk. |
required |
api_key
|
str | None
|
API key for remote providers. |
None
|
timeout
|
float
|
Timeout in seconds for model completion. |
SENTENCE_ATTRIBUTION_TIMEOUT_SEC
|
model
|
str | None
|
Optional model identifier override. |
None
|
base_url
|
str | None
|
Optional API base URL for local providers. |
None
|
zotero_data_dir
|
Path | str | None
|
Optional Zotero data directory for catalog discovery. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Attribution result dictionary. |
Source code in zocrates/agents/library_search/sentence_attribution.py
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 | |
format_spans_for_prompt ¶
Format numbered spans for model consumption.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spans
|
list[str]
|
List of segmented text spans. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Formatted string with 1-based indices [1]..[N]. |
Source code in zocrates/agents/library_search/sentence_attribution.py
parse_attribution_response ¶
Parse and validate JSON attribution response from the LLM.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw_text
|
str
|
Raw response string from LLM. |
required |
total_spans
|
int
|
Total count of spans sent to LLM for index bounding. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dict with keys supporting_indices, unsupported_aspects. |
Source code in zocrates/agents/library_search/sentence_attribution.py
persist_sentence_attribution_to_session
async
¶
persist_sentence_attribution_to_session(session_id, invocation_id, chunk_index, occurrence, claim, attribution_data, session_service, app_name='zocrates')
Locate matching assessment in session state and persist sentence attribution.
Updates both in-memory session.state['retrieved_evidence'] and the SQLite sessions.db database when session_service is backed by DatabaseSessionService.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session_id
|
str
|
Target session identifier. |
required |
invocation_id
|
str | None
|
Optional invocation ID scoping the turn. |
required |
chunk_index
|
int | None
|
1-based index of the chunk. |
required |
occurrence
|
int | None
|
1-based occurrence number of citation. |
required |
claim
|
str
|
Claim text to match. |
required |
attribution_data
|
dict[str, Any]
|
Sentence attribution result dictionary. |
required |
session_service
|
Any
|
Active ADK session service. |
required |
app_name
|
str
|
Application name for session service. |
'zocrates'
|
Returns:
| Type | Description |
|---|---|
bool
|
True if an assessment was found and updated, False otherwise. |
Source code in zocrates/agents/library_search/sentence_attribution.py
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 | |
segment_source_text ¶
Segment source_text into numbered atomic spans.
Preserves Markdown headings, table rows (skipping divider rows), and display math blocks while splitting prose into sentences with academic abbreviation guards.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Raw source text or Markdown excerpt. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
List of atomic string spans. |
Source code in zocrates/agents/library_search/sentence_attribution.py
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | |