File size: 2,440 Bytes
26d4279
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""Entry point: run one agent turn and shape the result for the API."""
from __future__ import annotations

import logging
import uuid

from ..config import agent as agent_cfg
from .graph import get_graph
from .state import AgentAnswer

logger = logging.getLogger("inkference.agent")

# Hard stop on graph supersteps. max_steps bounds the tool loop; this bounds the
# graph itself, so a routing mistake can't spin forever.
_RECURSION_HEADROOM = 4


def run_agent(
    doc_id: int,
    question: str,
    store,
    index,
    thread_id: str | None = None,
    persona: str | None = None,
    max_steps: int | None = None,
) -> AgentAnswer:
    thread_id = thread_id or uuid.uuid4().hex
    steps_cap = max(1, min(int(max_steps or agent_cfg.max_steps), 8))
    graph = get_graph(store, index)

    config = {
        # Namespaced by document: a stale thread id from another corpus must not
        # resurrect the wrong conversation.
        "configurable": {"thread_id": f"{doc_id}:{thread_id}"},
        "recursion_limit": steps_cap * 2 + _RECURSION_HEADROOM,
    }
    state = {
        "doc_id": doc_id,
        "question": question,
        "persona": persona,
        "max_steps": steps_cap,
        # Reset per turn; history and evidence-free state carry over via the checkpointer.
        "steps": 0,
        "next_action": {},
        "stop_reason": "",
    }

    logger.info("agent: doc=%s thread=%s persona=%s q=%r",
                doc_id, thread_id, persona, question[:100])
    final = graph.invoke(state, config)
    logger.info("agent: doc=%s steps=%s stop=%s sources=%s",
                doc_id, final.get("steps"), final.get("stop_reason"),
                final.get("source_pages"))

    evidence = final.get("evidence") or []
    return AgentAnswer(
        question=question,
        answer=final.get("answer", ""),
        source_pages=final.get("source_pages", []),
        persona=persona,
        contexts=[
            {"page_number": e["page_number"], "score": e.get("score", 0.0),
             "text": e["text"], "via": e.get("via"), "cite": e.get("cite")}
            for e in evidence
        ],
        thread_id=thread_id,
        standalone_question=final.get("standalone_question"),
        steps=final.get("steps", 0),
        grounded=final.get("grounded", True),
        stop_reason=final.get("stop_reason") or "answer",
        trace=final.get("trace", []),
        books=final.get("books", {}),
    )