Skip to content

Talk2Scholars state

State management for the Talk2Scholars agent.

This module defines the state class Talk2Scholars, which maintains the conversation context, retrieved papers, and other relevant metadata. The state ensures consistency across agent interactions.

Talk2Scholars

Bases: AgentState

Represents the state of the Talk2Scholars agent. This class extends AgentState to maintain conversation history, retrieved papers, and interactions with the language model. Attributes: last_displayed_papers (Dict[str, Any]): Stores the most recently displayed papers. papers (Dict[str, Any]): Stores the research papers retrieved from the agent's queries. multi_papers (Dict[str, Any]): Stores multiple recommended papers from various sources. article_data (Dict[str, Any]): Stores the papers retrieved from Zotero and the pdf download agent with their metadata. zotero_write_approval_status (Dict[str, Any]): Stores the approval status and collection path for Zotero save operations. llm_model (BaseChatModel): The language model instance used for generating responses. text_embedding_model (Embeddings): The text embedding model used for similarity calculations.

Source code in aiagents4pharma/talk2scholars/state/state_talk2scholars.py
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
class Talk2Scholars(AgentState):
    """
    Represents the state of the Talk2Scholars agent.
    This class extends `AgentState` to maintain conversation history, retrieved papers,
    and interactions with the language model.
    Attributes:
        last_displayed_papers (Dict[str, Any]): Stores the most recently displayed papers.
        papers (Dict[str, Any]): Stores the research papers retrieved from the agent's queries.
        multi_papers (Dict[str, Any]): Stores multiple recommended papers from various sources.
        article_data (Dict[str, Any]): Stores the papers retrieved from Zotero and the pdf
        download agent with their metadata.
        zotero_write_approval_status (Dict[str, Any]): Stores the approval status and collection
        path for Zotero save operations.
        llm_model (BaseChatModel): The language model instance used for generating responses.
        text_embedding_model (Embeddings): The text embedding model used for
        similarity calculations.
    """

    # Agent state fields
    # Key controlling UI display: always replace to reference latest output
    # Stores the most recently displayed papers metadata
    last_displayed_papers: Annotated[Dict[str, Any], replace_dict]
    # Accumulative keys: merge new entries into existing state
    papers: Annotated[Dict[str, Any], merge_dict]
    multi_papers: Annotated[Dict[str, Any], merge_dict]
    article_data: Annotated[Dict[str, Any], merge_dict]
    # Approval status: always replace to reflect latest operation
    zotero_write_approval_status: Annotated[Dict[str, Any], replace_dict]
    llm_model: BaseChatModel
    text_embedding_model: Embeddings

merge_dict(existing, new)

Merges the existing dictionary with a new dictionary.

This function logs the state merge and ensures that the new values are appended to the existing state without overwriting other entries. Args: existing (Dict[str, Any]): The current dictionary state. new (Dict[str, Any]): The new dictionary state to merge. Returns: Dict[str, Any]: The merged dictionary state.

Source code in aiagents4pharma/talk2scholars/state/state_talk2scholars.py
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
def merge_dict(existing: Dict[str, Any], new: Dict[str, Any]) -> Dict[str, Any]:
    """
    Merges the existing dictionary with a new dictionary.

    This function logs the state merge and ensures that the new values
    are appended to the existing state without overwriting other entries.
    Args:
        existing (Dict[str, Any]): The current dictionary state.
        new (Dict[str, Any]): The new dictionary state to merge.
    Returns:
        Dict[str, Any]: The merged dictionary state.
    """
    merged = dict(existing) if existing else {}
    merged.update(new or {})
    return merged

replace_dict(existing, new)

Replaces the existing dictionary with a new dictionary.

This function logs the state update and ensures that the old state is replaced with the new one.

Parameters:

Name Type Description Default
existing Dict[str, Any]

The current dictionary state.

required
new Dict[str, Any]

The new dictionary state to replace the existing one.

required

Returns:

Type Description
Any

Dict[str, Any]: The updated dictionary state.

Example

old_state = {"papers": {"id1": "Paper 1"}} new_state = {"papers": {"id2": "Paper 2"}} updated_state = replace_dict(old_state, new_state) print(updated_state) {"papers": {"id2": "Paper 2"}}

Source code in aiagents4pharma/talk2scholars/state/state_talk2scholars.py
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
def replace_dict(existing: Dict[str, Any], new: Any) -> Any:
    """
    Replaces the existing dictionary with a new dictionary.

    This function logs the state update and ensures that the old state is replaced
    with the new one.

    Args:
        existing (Dict[str, Any]): The current dictionary state.
        new (Dict[str, Any]): The new dictionary state to replace the existing one.

    Returns:
        Dict[str, Any]: The updated dictionary state.

    Example:
        >>> old_state = {"papers": {"id1": "Paper 1"}}
        >>> new_state = {"papers": {"id2": "Paper 2"}}
        >>> updated_state = replace_dict(old_state, new_state)
        >>> print(updated_state)
        {"papers": {"id2": "Paper 2"}}
    """
    # If new is not a mapping, just replace existing value outright
    if not isinstance(new, Mapping):
        return new
    # In-place replace: clear existing mapping and update with new entries
    existing.clear()
    existing.update(new)
    return existing