Skip to content

Display results

Tool for displaying search or recommendation results.

This module defines a tool that retrieves and displays a table of research papers found during searches or recommendations. If no papers are found, an exception is raised to signal the need for a new search.

NoPapersFoundError

Bases: Exception

Exception raised when no research papers are found in the agent's state.

This exception helps the language model determine whether a new search or recommendation should be initiated.

Example

if not papers: raise NoPapersFoundError("No papers found. A search is needed.")

Source code in aiagents4pharma/talk2scholars/tools/s2/display_results.py
28
29
30
31
32
33
34
35
36
37
38
class NoPapersFoundError(Exception):
    """
    Exception raised when no research papers are found in the agent's state.

    This exception helps the language model determine whether a new search
    or recommendation should be initiated.

    Example:
        >>> if not papers:
        >>>     raise NoPapersFoundError("No papers found. A search is needed.")
    """

display_results(tool_call_id, state)

Displays retrieved research papers after a search or recommendation.

This function retrieves the last displayed research papers from the state and returns them as an artifact for further processing. If no papers are found, it raises a NoPapersFoundError to indicate that a new search is needed.

Parameters:

Name Type Description Default
tool_call_id Annotated[str, InjectedToolCallId]

The tool call ID for tracking.

required
state dict

The agent's state containing retrieved papers.

required

Returns:

Name Type Description
Command Command

A command containing a message with the number of displayed papers and an attached artifact for further reference.

Raises:

Type Description
NoPapersFoundError

If no research papers are found in the agent's state.

Example

state = {"last_displayed_papers": {"paper1": "Title 1", "paper2": "Title 2"}} result = display_results(tool_call_id="123", state=state) print(result.update["messages"][0].content) "2 papers found. Papers are attached as an artifact."

Source code in aiagents4pharma/talk2scholars/tools/s2/display_results.py
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
@tool("display_results", parse_docstring=True)
def display_results(
    tool_call_id: Annotated[str, InjectedToolCallId],
    state: Annotated[dict, InjectedState],
) -> Command:
    """
    Displays retrieved research papers after a search or recommendation.

    This function retrieves the last displayed research papers from the state and
    returns them as an artifact for further processing. If no papers are found,
    it raises a `NoPapersFoundError` to indicate that a new search is needed.

    Args:
        tool_call_id (Annotated[str, InjectedToolCallId]): The tool call ID for tracking.
        state (dict): The agent's state containing retrieved papers.

    Returns:
        Command: A command containing a message with the number of displayed papers
                 and an attached artifact for further reference.

    Raises:
        NoPapersFoundError: If no research papers are found in the agent's state.

    Example:
        >>> state = {"last_displayed_papers": {"paper1": "Title 1", "paper2": "Title 2"}}
        >>> result = display_results(tool_call_id="123", state=state)
        >>> print(result.update["messages"][0].content)
        "2 papers found. Papers are attached as an artifact."
    """
    logger.info("Displaying papers")
    context_key = state.get("last_displayed_papers")
    artifact = state.get(context_key)
    if not artifact:
        logger.info("No papers found in state, raising NoPapersFoundError")
        raise NoPapersFoundError(
            "No papers found. A search/rec needs to be performed first."
        )
    content = f"{len(artifact)} papers found. Papers are attached as an artifact."
    return Command(
        update={
            "messages": [
                ToolMessage(
                    content=content,
                    tool_call_id=tool_call_id,
                    artifact=artifact,
                )
            ],
        }
    )