aiagents4pharma 1.20.1__py3-none-any.whl → 1.22.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- aiagents4pharma/talk2knowledgegraphs/configs/__init__.py +1 -0
- aiagents4pharma/talk2knowledgegraphs/configs/config.yaml +1 -0
- aiagents4pharma/talk2knowledgegraphs/tests/test_utils_embeddings_nim_molmim.py +64 -0
- aiagents4pharma/talk2knowledgegraphs/tests/test_utils_enrichments_pubchem.py +33 -0
- aiagents4pharma/talk2knowledgegraphs/tests/test_utils_pubchem_utils.py +16 -0
- aiagents4pharma/talk2knowledgegraphs/utils/__init__.py +1 -0
- aiagents4pharma/talk2knowledgegraphs/utils/embeddings/__init__.py +1 -0
- aiagents4pharma/talk2knowledgegraphs/utils/embeddings/nim_molmim.py +54 -0
- aiagents4pharma/talk2knowledgegraphs/utils/enrichments/__init__.py +1 -0
- aiagents4pharma/talk2knowledgegraphs/utils/enrichments/pubchem_strings.py +49 -0
- aiagents4pharma/talk2knowledgegraphs/utils/pubchem_utils.py +42 -0
- aiagents4pharma/talk2scholars/agents/main_agent.py +90 -91
- aiagents4pharma/talk2scholars/agents/s2_agent.py +61 -17
- aiagents4pharma/talk2scholars/configs/agents/talk2scholars/main_agent/default.yaml +31 -10
- aiagents4pharma/talk2scholars/configs/agents/talk2scholars/s2_agent/default.yaml +8 -16
- aiagents4pharma/talk2scholars/configs/app/frontend/default.yaml +11 -9
- aiagents4pharma/talk2scholars/configs/config.yaml +1 -0
- aiagents4pharma/talk2scholars/configs/tools/multi_paper_recommendation/default.yaml +2 -0
- aiagents4pharma/talk2scholars/configs/tools/retrieve_semantic_scholar_paper_id/__init__.py +3 -0
- aiagents4pharma/talk2scholars/configs/tools/search/default.yaml +1 -0
- aiagents4pharma/talk2scholars/configs/tools/single_paper_recommendation/default.yaml +1 -0
- aiagents4pharma/talk2scholars/state/state_talk2scholars.py +36 -7
- aiagents4pharma/talk2scholars/tests/test_llm_main_integration.py +58 -0
- aiagents4pharma/talk2scholars/tests/test_main_agent.py +98 -122
- aiagents4pharma/talk2scholars/tests/test_s2_agent.py +95 -29
- aiagents4pharma/talk2scholars/tests/test_s2_tools.py +158 -22
- aiagents4pharma/talk2scholars/tools/s2/__init__.py +4 -2
- aiagents4pharma/talk2scholars/tools/s2/display_results.py +60 -21
- aiagents4pharma/talk2scholars/tools/s2/multi_paper_rec.py +35 -8
- aiagents4pharma/talk2scholars/tools/s2/query_results.py +61 -0
- aiagents4pharma/talk2scholars/tools/s2/retrieve_semantic_scholar_paper_id.py +79 -0
- aiagents4pharma/talk2scholars/tools/s2/search.py +34 -10
- aiagents4pharma/talk2scholars/tools/s2/single_paper_rec.py +39 -9
- {aiagents4pharma-1.20.1.dist-info → aiagents4pharma-1.22.0.dist-info}/METADATA +2 -1
- {aiagents4pharma-1.20.1.dist-info → aiagents4pharma-1.22.0.dist-info}/RECORD +38 -29
- aiagents4pharma/talk2scholars/tests/test_integration.py +0 -237
- {aiagents4pharma-1.20.1.dist-info → aiagents4pharma-1.22.0.dist-info}/LICENSE +0 -0
- {aiagents4pharma-1.20.1.dist-info → aiagents4pharma-1.22.0.dist-info}/WHEEL +0 -0
- {aiagents4pharma-1.20.1.dist-info → aiagents4pharma-1.22.0.dist-info}/top_level.txt +0 -0
@@ -5,37 +5,80 @@ Agent for interacting with Semantic Scholar
|
|
5
5
|
"""
|
6
6
|
|
7
7
|
import logging
|
8
|
+
from typing import Any, Dict
|
8
9
|
import hydra
|
9
10
|
from langchain_openai import ChatOpenAI
|
11
|
+
from langchain_core.language_models.chat_models import BaseChatModel
|
10
12
|
from langgraph.graph import START, StateGraph
|
11
13
|
from langgraph.prebuilt import create_react_agent, ToolNode
|
12
14
|
from langgraph.checkpoint.memory import MemorySaver
|
13
15
|
from ..state.state_talk2scholars import Talk2Scholars
|
14
16
|
from ..tools.s2.search import search_tool as s2_search
|
15
17
|
from ..tools.s2.display_results import display_results as s2_display
|
18
|
+
from ..tools.s2.query_results import query_results as s2_query_results
|
19
|
+
from ..tools.s2.retrieve_semantic_scholar_paper_id import (
|
20
|
+
retrieve_semantic_scholar_paper_id as s2_retrieve_id,
|
21
|
+
)
|
16
22
|
from ..tools.s2.single_paper_rec import (
|
17
23
|
get_single_paper_recommendations as s2_single_rec,
|
18
24
|
)
|
19
25
|
from ..tools.s2.multi_paper_rec import get_multi_paper_recommendations as s2_multi_rec
|
20
26
|
|
21
|
-
|
22
27
|
# Initialize logger
|
23
28
|
logging.basicConfig(level=logging.INFO)
|
24
29
|
logger = logging.getLogger(__name__)
|
25
30
|
|
26
31
|
|
27
|
-
def get_app(
|
32
|
+
def get_app(
|
33
|
+
uniq_id, llm_model: BaseChatModel = ChatOpenAI(model="gpt-4o-mini", temperature=0)
|
34
|
+
):
|
28
35
|
"""
|
29
|
-
|
36
|
+
Initializes and returns the LangGraph application for the Semantic Scholar (S2) agent.
|
37
|
+
|
38
|
+
This function sets up the S2 agent, which integrates various tools to search, retrieve,
|
39
|
+
and display research papers from Semantic Scholar. The agent follows the ReAct pattern
|
40
|
+
for structured interaction.
|
41
|
+
|
42
|
+
Args:
|
43
|
+
uniq_id (str): Unique identifier for the current conversation session.
|
44
|
+
llm_model (BaseChatModel, optional): The language model to be used by the agent.
|
45
|
+
Defaults to `ChatOpenAI(model="gpt-4o-mini", temperature=0)`.
|
46
|
+
|
47
|
+
Returns:
|
48
|
+
StateGraph: A compiled LangGraph application that enables the S2 agent to process
|
49
|
+
user queries and retrieve research papers.
|
50
|
+
|
51
|
+
Example:
|
52
|
+
>>> app = get_app("thread_123")
|
53
|
+
>>> result = app.invoke(initial_state)
|
30
54
|
"""
|
31
55
|
|
32
|
-
def agent_s2_node(state: Talk2Scholars):
|
56
|
+
# def agent_s2_node(state: Talk2Scholars) -> Command[Literal["supervisor"]]:
|
57
|
+
def agent_s2_node(state: Talk2Scholars) -> Dict[str, Any]:
|
33
58
|
"""
|
34
|
-
|
59
|
+
Processes the user query and retrieves relevant research papers.
|
60
|
+
|
61
|
+
This function calls the language model using the configured `ReAct` agent to analyze
|
62
|
+
the state and generate an appropriate response. The function then returns control
|
63
|
+
to the main supervisor.
|
64
|
+
|
65
|
+
Args:
|
66
|
+
state (Talk2Scholars): The current conversation state, including messages exchanged
|
67
|
+
and any previously retrieved research papers.
|
68
|
+
|
69
|
+
Returns:
|
70
|
+
Dict[str, Any]: A dictionary containing the updated conversation state.
|
71
|
+
|
72
|
+
Example:
|
73
|
+
>>> result = agent_s2_node(current_state)
|
74
|
+
>>> papers = result.get("papers", [])
|
35
75
|
"""
|
36
76
|
logger.log(logging.INFO, "Creating Agent_S2 node with thread_id %s", uniq_id)
|
37
|
-
|
38
|
-
|
77
|
+
result = model.invoke(state, {"configurable": {"thread_id": uniq_id}})
|
78
|
+
|
79
|
+
return result
|
80
|
+
|
81
|
+
logger.log(logging.INFO, "thread_id, llm_model: %s, %s", uniq_id, llm_model)
|
39
82
|
|
40
83
|
# Load hydra configuration
|
41
84
|
logger.log(logging.INFO, "Load Hydra configuration for Talk2Scholars S2 agent.")
|
@@ -46,30 +89,31 @@ def get_app(uniq_id, llm_model="gpt-4o-mini"):
|
|
46
89
|
cfg = cfg.agents.talk2scholars.s2_agent
|
47
90
|
|
48
91
|
# Define the tools
|
49
|
-
tools = ToolNode(
|
92
|
+
tools = ToolNode(
|
93
|
+
[
|
94
|
+
s2_search,
|
95
|
+
s2_display,
|
96
|
+
s2_query_results,
|
97
|
+
s2_retrieve_id,
|
98
|
+
s2_single_rec,
|
99
|
+
s2_multi_rec,
|
100
|
+
]
|
101
|
+
)
|
50
102
|
|
51
103
|
# Define the model
|
52
104
|
logger.log(logging.INFO, "Using OpenAI model %s", llm_model)
|
53
|
-
llm = ChatOpenAI(model=llm_model, temperature=cfg.temperature)
|
54
105
|
|
55
106
|
# Create the agent
|
56
107
|
model = create_react_agent(
|
57
|
-
|
108
|
+
llm_model,
|
58
109
|
tools=tools,
|
59
110
|
state_schema=Talk2Scholars,
|
60
|
-
# prompt=cfg.s2_agent,
|
61
111
|
state_modifier=cfg.s2_agent,
|
62
112
|
checkpointer=MemorySaver(),
|
63
113
|
)
|
64
114
|
|
65
|
-
# Define a new graph
|
66
115
|
workflow = StateGraph(Talk2Scholars)
|
67
|
-
|
68
|
-
# Define the two nodes we will cycle between
|
69
116
|
workflow.add_node("agent_s2", agent_s2_node)
|
70
|
-
|
71
|
-
# Set the entrypoint as `agent`
|
72
|
-
# This means that this node is the first one called
|
73
117
|
workflow.add_edge(START, "agent_s2")
|
74
118
|
|
75
119
|
# Initialize memory to persist state between graph runs
|
@@ -5,14 +5,35 @@ openai_llms:
|
|
5
5
|
- "gpt-4-turbo"
|
6
6
|
- "gpt-3.5-turbo"
|
7
7
|
temperature: 0
|
8
|
-
|
9
|
-
You are
|
8
|
+
system_prompt: >
|
9
|
+
You are the Talk2Scholars agent coordinating academic paper discovery and analysis.
|
10
10
|
|
11
|
-
|
12
|
-
1.
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
11
|
+
You have access to the following agents:
|
12
|
+
1. S2_agent: This agent can be used to search and recommend papers
|
13
|
+
from Semantic Scholar. Use this agent when the user asks for
|
14
|
+
general paper searches and recommendations. This agent can also
|
15
|
+
retrieve the Semantic Scholar ID of a paper.
|
16
|
+
router_prompt: >
|
17
|
+
You are a supervisor tasked with managing a conversation between the
|
18
|
+
following workers: {members}. Given the user request, respond with the
|
19
|
+
worker to act next. Each worker will perform a task and respond with
|
20
|
+
their results and status. When finished, respond with FINISH.
|
21
|
+
|
22
|
+
Here is a description of the workers:
|
23
|
+
1. S2_agent: This agent can be used to search and recommend papers
|
24
|
+
from Semantic Scholar. Use this agent when the user asks for
|
25
|
+
general paper searches and recommendations. This agent can also
|
26
|
+
retrieve the Semantic Scholar ID of a paper. It can also be used to
|
27
|
+
provide more information about a paper.
|
28
|
+
|
29
|
+
Here are some instructions for the workers:
|
30
|
+
1. Call the S2 agent for general paper searches and recommendations.
|
31
|
+
2. The S2 agent has access to tools for querying and displaying papers.
|
32
|
+
3. If the user wants suggestions for papers and you don’t have
|
33
|
+
a Semantic Scholar ID for it but do have the title from
|
34
|
+
the last displayed results, use the S2 agent to retrieve the
|
35
|
+
Semantic Scholar ID of the paper. Then, use the S2 agent again to display
|
36
|
+
recommendations for the paper.
|
37
|
+
4. You can call the S2 agent to get more information about a paper based
|
38
|
+
on the context of the conversation.
|
39
|
+
5. Respond with FINISH when all tasks are completed.
|
@@ -6,19 +6,11 @@ openai_llms:
|
|
6
6
|
- "gpt-3.5-turbo"
|
7
7
|
temperature: 0
|
8
8
|
s2_agent: >
|
9
|
-
You are
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
- Shows the current research papers.
|
18
|
-
- If no papers are found, it will instruct you to perform a search.
|
19
|
-
|
20
|
-
3. get_single_paper_recommendations:
|
21
|
-
- Provides recommendations based on a single selected paper.
|
22
|
-
|
23
|
-
4. get_multi_paper_recommendations:
|
24
|
-
- Provides recommendations based on multiple selected papers.
|
9
|
+
You are an academic research assistant with access to the
|
10
|
+
Semantic Scholar API for paper discovery and analysis.
|
11
|
+
You also have tools to gain more insights on the papers and
|
12
|
+
display them.
|
13
|
+
You must strictly rely on retrieved information and avoid
|
14
|
+
generating unsupported content. Do not generate hallucinations
|
15
|
+
or fabricate details of any article. Stay focused on accurate,
|
16
|
+
sourced academic insights.
|
@@ -1,14 +1,13 @@
|
|
1
|
-
#
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
1
|
+
# Page configuration
|
2
|
+
page:
|
3
|
+
title: "Talk2Scholars"
|
4
|
+
icon: "🤖"
|
5
|
+
layout: "wide"
|
6
6
|
|
7
7
|
# Available LLM models
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
- "gpt-3.5-turbo"
|
8
|
+
llms:
|
9
|
+
available_models:
|
10
|
+
- "OpenAI/gpt-4o-mini"
|
12
11
|
# # Chat UI configuration
|
13
12
|
# chat:
|
14
13
|
# assistant_avatar: "🤖"
|
@@ -16,6 +15,9 @@ llm_models:
|
|
16
15
|
# input_placeholder: "Say something ..."
|
17
16
|
# spinner_text: "Fetching response ..."
|
18
17
|
|
18
|
+
api_keys:
|
19
|
+
openai_key: "OPENAI_API_KEY"
|
20
|
+
nvidia_key: "NVIDIA_API_KEY"
|
19
21
|
# # Feedback configuration
|
20
22
|
# feedback:
|
21
23
|
# type: "thumbs"
|
@@ -1,9 +1,14 @@
|
|
1
1
|
"""
|
2
|
-
|
2
|
+
State management for the Talk2Scholars agent.
|
3
|
+
|
4
|
+
This module defines the state class `Talk2Scholars`, which maintains the conversation
|
5
|
+
context, retrieved papers, and other relevant metadata. The state ensures consistency
|
6
|
+
across agent interactions.
|
3
7
|
"""
|
4
8
|
|
5
9
|
import logging
|
6
10
|
from typing import Annotated, Any, Dict
|
11
|
+
from langchain_core.language_models import BaseChatModel
|
7
12
|
from langgraph.prebuilt.chat_agent_executor import AgentState
|
8
13
|
|
9
14
|
# Configure logging
|
@@ -12,22 +17,46 @@ logger = logging.getLogger(__name__)
|
|
12
17
|
|
13
18
|
|
14
19
|
def replace_dict(existing: Dict[str, Any], new: Dict[str, Any]) -> Dict[str, Any]:
|
15
|
-
"""
|
20
|
+
"""
|
21
|
+
Replaces the existing dictionary with a new dictionary.
|
22
|
+
|
23
|
+
This function logs the state update and ensures that the old state is replaced
|
24
|
+
with the new one.
|
25
|
+
|
26
|
+
Args:
|
27
|
+
existing (Dict[str, Any]): The current dictionary state.
|
28
|
+
new (Dict[str, Any]): The new dictionary state to replace the existing one.
|
29
|
+
|
30
|
+
Returns:
|
31
|
+
Dict[str, Any]: The updated dictionary state.
|
32
|
+
|
33
|
+
Example:
|
34
|
+
>>> old_state = {"papers": {"id1": "Paper 1"}}
|
35
|
+
>>> new_state = {"papers": {"id2": "Paper 2"}}
|
36
|
+
>>> updated_state = replace_dict(old_state, new_state)
|
37
|
+
>>> print(updated_state)
|
38
|
+
{"papers": {"id2": "Paper 2"}}
|
39
|
+
"""
|
16
40
|
logger.info("Updating existing state %s with the state dict: %s", existing, new)
|
17
41
|
return new
|
18
42
|
|
19
43
|
|
20
44
|
class Talk2Scholars(AgentState):
|
21
45
|
"""
|
22
|
-
|
46
|
+
Represents the state of the Talk2Scholars agent.
|
47
|
+
|
48
|
+
This class extends `AgentState` to maintain conversation history, retrieved papers,
|
49
|
+
and interactions with the language model.
|
23
50
|
|
24
51
|
Attributes:
|
25
|
-
|
26
|
-
|
27
|
-
|
52
|
+
last_displayed_papers (Dict[str, Any]): Stores the most recently displayed papers.
|
53
|
+
papers (Dict[str, Any]): Stores the research papers retrieved from the agent's queries.
|
54
|
+
multi_papers (Dict[str, Any]): Stores multiple recommended papers from various sources.
|
55
|
+
llm_model (BaseChatModel): The language model instance used for generating responses.
|
28
56
|
"""
|
29
57
|
|
30
58
|
# Agent state fields
|
59
|
+
last_displayed_papers: Annotated[Dict[str, Any], replace_dict]
|
31
60
|
papers: Annotated[Dict[str, Any], replace_dict]
|
32
61
|
multi_papers: Annotated[Dict[str, Any], replace_dict]
|
33
|
-
llm_model:
|
62
|
+
llm_model: BaseChatModel
|
@@ -0,0 +1,58 @@
|
|
1
|
+
"""
|
2
|
+
Integration tests for talk2scholars system with OpenAI.
|
3
|
+
"""
|
4
|
+
|
5
|
+
import os
|
6
|
+
import pytest
|
7
|
+
import hydra
|
8
|
+
from langchain_openai import ChatOpenAI
|
9
|
+
from langchain_core.messages import HumanMessage, AIMessage
|
10
|
+
from ..agents.main_agent import get_app
|
11
|
+
from ..state.state_talk2scholars import Talk2Scholars
|
12
|
+
|
13
|
+
# pylint: disable=redefined-outer-name
|
14
|
+
|
15
|
+
|
16
|
+
@pytest.mark.skipif(
|
17
|
+
not os.getenv("OPENAI_API_KEY"), reason="Requires OpenAI API key to run"
|
18
|
+
)
|
19
|
+
def test_main_agent_real_llm():
|
20
|
+
"""
|
21
|
+
Test that the main agent invokes S2 agent correctly
|
22
|
+
and updates the state with real LLM execution.
|
23
|
+
"""
|
24
|
+
|
25
|
+
# Load Hydra Configuration EXACTLY like in main_agent.py
|
26
|
+
with hydra.initialize(version_base=None, config_path="../configs"):
|
27
|
+
cfg = hydra.compose(
|
28
|
+
config_name="config", overrides=["agents/talk2scholars/main_agent=default"]
|
29
|
+
)
|
30
|
+
hydra_cfg = cfg.agents.talk2scholars.main_agent
|
31
|
+
|
32
|
+
assert hydra_cfg is not None, "Hydra config failed to load"
|
33
|
+
|
34
|
+
# Use the real OpenAI API (ensure env variable is set)
|
35
|
+
llm = ChatOpenAI(model="gpt-4o-mini", temperature=hydra_cfg.temperature)
|
36
|
+
|
37
|
+
# Initialize main agent workflow (WITH real Hydra config)
|
38
|
+
thread_id = "test_thread"
|
39
|
+
app = get_app(thread_id, llm)
|
40
|
+
|
41
|
+
# Provide an actual user query
|
42
|
+
initial_state = Talk2Scholars(
|
43
|
+
messages=[HumanMessage(content="Find AI papers on transformers")]
|
44
|
+
)
|
45
|
+
|
46
|
+
# Invoke the agent (triggers supervisor → s2_agent)
|
47
|
+
result = app.invoke(
|
48
|
+
initial_state,
|
49
|
+
{"configurable": {"config_id": thread_id, "thread_id": thread_id}},
|
50
|
+
)
|
51
|
+
|
52
|
+
# Assert that the supervisor routed correctly
|
53
|
+
assert "messages" in result, "Expected messages in response"
|
54
|
+
|
55
|
+
# Fix: Accept AIMessage as a valid response type
|
56
|
+
assert isinstance(
|
57
|
+
result["messages"][-1], (HumanMessage, AIMessage, str)
|
58
|
+
), "Last message should be a valid response"
|