File size: 1,489 Bytes
b2931f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
"""lookup_citation(chunk_id) β€” re-fetch one full chunk from Qdrant by id.

Why the agent needs this: retrieval hands the model a working set, but during
reasoning it may want to pull a specific chunk back in full β€” to quote an exact
figure, or to re-read a chunk it cited earlier in a longer tool loop. This is
the read-only "dereference a citation" primitive.

Thin wrapper over retrieval.vector.retrieve_by_chunk_ids β€” no new Qdrant
plumbing, just the single-id ergonomics and a not-found path that returns data
rather than raising (so the agent can recover).
"""

from __future__ import annotations

from typing import Any

from finrag.retrieval.vector import payload_to_chunk, retrieve_by_chunk_ids


def lookup_citation(chunk_id: str) -> dict[str, Any]:
    """Return the full chunk for `chunk_id`, or an error dict if absent.

    The score is 1.0 β€” it's an exact id fetch, not a similarity match; the
    field exists only to reuse the RetrievedChunk shape the rest of the
    system already speaks.
    """
    payloads = retrieve_by_chunk_ids([chunk_id])
    payload = payloads.get(chunk_id)
    if payload is None:
        return {"error": f"No chunk found with id {chunk_id!r}"}
    return {"chunk": payload_to_chunk(payload, score=1.0).model_dump()}


if __name__ == "__main__":
    # A real id from the AAPL FY2023 net-sales table chunk (seen in /answer).
    print(lookup_citation("d899f2e938bec647"))
    print(lookup_citation("deadbeefdeadbeef"))  # not-found path