Skip to content

Query results

This tool is used to display the table of studies.

NoPapersFoundError

Bases: Exception

Exception raised when no papers are found in the state.

Source code in aiagents4pharma/talk2scholars/tools/s2/query_results.py
19
20
class NoPapersFoundError(Exception):
    """Exception raised when no papers are found in the state."""

query_results(question, state)

Query the last displayed papers from the state. If no papers are found, raises an exception.

Use this also to get the last displayed papers from the state, and then use the papers to get recommendations for a single paper or multiple papers.

Parameters:

Name Type Description Default
question str

The question to ask the agent.

required
state dict

The state of the agent containing the papers.

required

Returns:

Name Type Description
str str

A message with the last displayed papers.

Source code in aiagents4pharma/talk2scholars/tools/s2/query_results.py
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
@tool("query_results", parse_docstring=True)
def query_results(question: str, state: Annotated[dict, InjectedState]) -> str:
    """
    Query the last displayed papers from the state. If no papers are found,
    raises an exception.

    Use this also to get the last displayed papers from the state,
    and then use the papers to get recommendations for a single paper or
    multiple papers.

    Args:
        question (str): The question to ask the agent.
        state (dict): The state of the agent containing the papers.

    Returns:
        str: A message with the last displayed papers.
    """
    logger.info("Querying last displayed papers with question: %s", question)
    llm_model = state.get("llm_model")
    if not state.get("last_displayed_papers"):
        logger.info("No papers displayed so far, raising NoPapersFoundError")
        raise NoPapersFoundError(
            "No papers found. A search needs to be performed first."
        )
    context_key = state.get("last_displayed_papers")
    dic_papers = state.get(context_key)
    df_papers = pd.DataFrame.from_dict(dic_papers, orient="index")
    df_agent = create_pandas_dataframe_agent(
        llm_model,
        allow_dangerous_code=True,
        agent_type="tool-calling",
        df=df_papers,
        max_iterations=5,
        include_df_in_prompt=True,
        number_of_head_rows=df_papers.shape[0],
        verbose=True,
    )
    llm_result = df_agent.invoke(question, stream_mode=None)
    return llm_result["output"]