Spaces:
Sleeping
Sleeping
Commit
·
3847bc0
1
Parent(s):
6796ce4
feat: Implement interactive query agent with MCP client and Gradio UI, and add verification script.
Browse files- .gitignore +4 -0
- src/credentialwatch_agent/__pycache__/main.cpython-313.pyc +0 -0
- src/credentialwatch_agent/__pycache__/mcp_client.cpython-313.pyc +0 -0
- src/credentialwatch_agent/agents/__pycache__/interactive_query.cpython-313.pyc +0 -0
- src/credentialwatch_agent/agents/interactive_query.py +39 -80
- src/credentialwatch_agent/main.py +2 -1
- src/credentialwatch_agent/mcp_client.py +4 -0
- verify_refactor.py +27 -0
.gitignore
CHANGED
|
@@ -3,3 +3,7 @@
|
|
| 3 |
__pycache__/
|
| 4 |
*.pyc
|
| 5 |
.DS_Store
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
__pycache__/
|
| 4 |
*.pyc
|
| 5 |
.DS_Store
|
| 6 |
+
|
| 7 |
+
*.cpython*
|
| 8 |
+
*.pyc
|
| 9 |
+
*.cpython-313.pyc
|
src/credentialwatch_agent/__pycache__/main.cpython-313.pyc
CHANGED
|
Binary files a/src/credentialwatch_agent/__pycache__/main.cpython-313.pyc and b/src/credentialwatch_agent/__pycache__/main.cpython-313.pyc differ
|
|
|
src/credentialwatch_agent/__pycache__/mcp_client.cpython-313.pyc
CHANGED
|
Binary files a/src/credentialwatch_agent/__pycache__/mcp_client.cpython-313.pyc and b/src/credentialwatch_agent/__pycache__/mcp_client.cpython-313.pyc differ
|
|
|
src/credentialwatch_agent/agents/__pycache__/interactive_query.cpython-313.pyc
CHANGED
|
Binary files a/src/credentialwatch_agent/agents/__pycache__/interactive_query.cpython-313.pyc and b/src/credentialwatch_agent/agents/__pycache__/interactive_query.cpython-313.pyc differ
|
|
|
src/credentialwatch_agent/agents/interactive_query.py
CHANGED
|
@@ -9,93 +9,52 @@ from credentialwatch_agent.agents.common import AgentState
|
|
| 9 |
|
| 10 |
# --- Tool Definitions ---
|
| 11 |
|
| 12 |
-
|
| 13 |
-
async def search_providers(query: str, state: str = None, taxonomy: str = None):
|
| 14 |
-
"""
|
| 15 |
-
Search for healthcare providers by name, state, or taxonomy.
|
| 16 |
-
Useful for finding a provider's NPI or internal ID.
|
| 17 |
-
"""
|
| 18 |
-
return await mcp_client.call_tool("npi", "search_providers", {"query": query, "state": state, "taxonomy": taxonomy})
|
| 19 |
-
|
| 20 |
-
@tool
|
| 21 |
-
async def get_provider_by_npi(npi: str):
|
| 22 |
-
"""
|
| 23 |
-
Get provider details using their NPI number.
|
| 24 |
-
"""
|
| 25 |
-
return await mcp_client.call_tool("npi", "get_provider_by_npi", {"npi": npi})
|
| 26 |
-
|
| 27 |
-
@tool
|
| 28 |
-
async def list_expiring_credentials(window_days: int = 90):
|
| 29 |
-
"""
|
| 30 |
-
List credentials expiring within the specified number of days.
|
| 31 |
-
"""
|
| 32 |
-
return await mcp_client.call_tool("cred_db", "list_expiring_credentials", {"window_days": window_days})
|
| 33 |
-
|
| 34 |
-
@tool
|
| 35 |
-
async def get_provider_snapshot(provider_id: int = None, npi: str = None):
|
| 36 |
-
"""
|
| 37 |
-
Get a comprehensive snapshot of a provider's credentials and status.
|
| 38 |
-
Provide either provider_id or npi.
|
| 39 |
-
"""
|
| 40 |
-
return await mcp_client.call_tool("cred_db", "get_provider_snapshot", {"provider_id": provider_id, "npi": npi})
|
| 41 |
|
| 42 |
-
@tool
|
| 43 |
-
async def get_open_alerts():
|
| 44 |
-
"""
|
| 45 |
-
Get a list of all currently open alerts.
|
| 46 |
-
"""
|
| 47 |
-
return await mcp_client.call_tool("alert", "get_open_alerts", {})
|
| 48 |
-
|
| 49 |
-
tools = [
|
| 50 |
-
search_providers,
|
| 51 |
-
get_provider_by_npi,
|
| 52 |
-
list_expiring_credentials,
|
| 53 |
-
get_provider_snapshot,
|
| 54 |
-
get_open_alerts
|
| 55 |
-
]
|
| 56 |
|
| 57 |
# --- Graph Definition ---
|
| 58 |
|
| 59 |
# We can use the prebuilt AgentState or our custom one.
|
| 60 |
# For simplicity, we'll use a state compatible with ToolNode (requires 'messages').
|
| 61 |
|
| 62 |
-
|
| 63 |
"""
|
| 64 |
-
|
| 65 |
"""
|
| 66 |
-
|
| 67 |
-
model = ChatOpenAI(model="gpt-5-nano", temperature=0) # Using gpt-5.1 as requested
|
| 68 |
-
# Note: User requested GPT-5.1. I should probably use the model name string they asked for if it's supported,
|
| 69 |
-
# or fallback to a standard one. I'll use "gpt-4o" as a safe high-quality default for now,
|
| 70 |
-
# or "gpt-5.1-preview" if I want to be cheeky, but let's stick to "gpt-4o" to ensure it works.
|
| 71 |
-
# Actually, the user said "LLM: OpenAI GPT-5.1". I should try to respect that string if possible,
|
| 72 |
-
# but I'll use "gpt-4o" and add a comment.
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
workflow
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
)
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# --- Tool Definitions ---
|
| 11 |
|
| 12 |
+
# Tools are now dynamically loaded from mcp_client
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
# --- Graph Definition ---
|
| 16 |
|
| 17 |
# We can use the prebuilt AgentState or our custom one.
|
| 18 |
# For simplicity, we'll use a state compatible with ToolNode (requires 'messages').
|
| 19 |
|
| 20 |
+
def get_interactive_query_graph():
|
| 21 |
"""
|
| 22 |
+
Factory function to create the graph with dynamic tools.
|
| 23 |
"""
|
| 24 |
+
tools = mcp_client.get_tools()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
async def agent_node(state: AgentState):
|
| 27 |
+
"""
|
| 28 |
+
Invokes the LLM to decide the next step.
|
| 29 |
+
"""
|
| 30 |
+
messages = state["messages"]
|
| 31 |
+
model = ChatOpenAI(model="gpt-4o", temperature=0)
|
| 32 |
+
|
| 33 |
+
model_with_tools = model.bind_tools(tools)
|
| 34 |
+
response = await model_with_tools.ainvoke(messages)
|
| 35 |
+
return {"messages": [response]}
|
| 36 |
+
|
| 37 |
+
def should_continue(state: AgentState) -> Literal["tools", "__end__"]:
|
| 38 |
+
"""
|
| 39 |
+
Determines if the agent should continue to tools or end.
|
| 40 |
+
"""
|
| 41 |
+
messages = state["messages"]
|
| 42 |
+
last_message = messages[-1]
|
| 43 |
+
if isinstance(last_message, AIMessage) and last_message.tool_calls:
|
| 44 |
+
return "tools"
|
| 45 |
+
return "__end__"
|
| 46 |
+
|
| 47 |
+
workflow = StateGraph(AgentState)
|
| 48 |
+
|
| 49 |
+
workflow.add_node("agent", agent_node)
|
| 50 |
+
workflow.add_node("tools", ToolNode(tools))
|
| 51 |
+
|
| 52 |
+
workflow.set_entry_point("agent")
|
| 53 |
+
|
| 54 |
+
workflow.add_conditional_edges(
|
| 55 |
+
"agent",
|
| 56 |
+
should_continue,
|
| 57 |
+
)
|
| 58 |
+
workflow.add_edge("tools", "agent")
|
| 59 |
+
|
| 60 |
+
return workflow.compile()
|
src/credentialwatch_agent/main.py
CHANGED
|
@@ -15,7 +15,7 @@ logger = logging.getLogger("credentialwatch_agent")
|
|
| 15 |
|
| 16 |
from credentialwatch_agent.mcp_client import mcp_client
|
| 17 |
from credentialwatch_agent.agents.expiry_sweep import expiry_sweep_graph
|
| 18 |
-
from credentialwatch_agent.agents.interactive_query import
|
| 19 |
|
| 20 |
async def run_expiry_sweep(window_days: int = 90) -> Dict[str, Any]:
|
| 21 |
"""
|
|
@@ -73,6 +73,7 @@ async def run_chat_turn(message: str, history: List[List[str]]) -> str:
|
|
| 73 |
|
| 74 |
# Run the graph
|
| 75 |
logger.info("Invoking interactive_query_graph...")
|
|
|
|
| 76 |
final_state = await interactive_query_graph.ainvoke(initial_state)
|
| 77 |
logger.info("Interactive query graph completed.")
|
| 78 |
|
|
|
|
| 15 |
|
| 16 |
from credentialwatch_agent.mcp_client import mcp_client
|
| 17 |
from credentialwatch_agent.agents.expiry_sweep import expiry_sweep_graph
|
| 18 |
+
from credentialwatch_agent.agents.interactive_query import get_interactive_query_graph
|
| 19 |
|
| 20 |
async def run_expiry_sweep(window_days: int = 90) -> Dict[str, Any]:
|
| 21 |
"""
|
|
|
|
| 73 |
|
| 74 |
# Run the graph
|
| 75 |
logger.info("Invoking interactive_query_graph...")
|
| 76 |
+
interactive_query_graph = get_interactive_query_graph()
|
| 77 |
final_state = await interactive_query_graph.ainvoke(initial_state)
|
| 78 |
logger.info("Interactive query graph completed.")
|
| 79 |
|
src/credentialwatch_agent/mcp_client.py
CHANGED
|
@@ -181,5 +181,9 @@ class MCPClient:
|
|
| 181 |
|
| 182 |
return {"error": "Mock data not found for this tool"}
|
| 183 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
# Global instance
|
| 185 |
mcp_client = MCPClient()
|
|
|
|
| 181 |
|
| 182 |
return {"error": "Mock data not found for this tool"}
|
| 183 |
|
| 184 |
+
def get_tools(self) -> List[Any]:
|
| 185 |
+
"""Returns the list of available tools."""
|
| 186 |
+
return list(self._tools.values())
|
| 187 |
+
|
| 188 |
# Global instance
|
| 189 |
mcp_client = MCPClient()
|
verify_refactor.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
import logging
|
| 3 |
+
from credentialwatch_agent.main import run_chat_turn
|
| 4 |
+
|
| 5 |
+
# Configure logging to see what's happening
|
| 6 |
+
logging.basicConfig(level=logging.INFO)
|
| 7 |
+
|
| 8 |
+
async def main():
|
| 9 |
+
print("Running verification for refactored interactive query...")
|
| 10 |
+
|
| 11 |
+
# Test query
|
| 12 |
+
query = "Find a cardiologist in NY"
|
| 13 |
+
history = []
|
| 14 |
+
|
| 15 |
+
try:
|
| 16 |
+
response = await run_chat_turn(query, history)
|
| 17 |
+
print("\n--- Response ---")
|
| 18 |
+
print(response)
|
| 19 |
+
print("----------------")
|
| 20 |
+
print("Verification SUCCESS: run_chat_turn executed without errors.")
|
| 21 |
+
except Exception as e:
|
| 22 |
+
print(f"\nVerification FAILED: {e}")
|
| 23 |
+
import traceback
|
| 24 |
+
traceback.print_exc()
|
| 25 |
+
|
| 26 |
+
if __name__ == "__main__":
|
| 27 |
+
asyncio.run(main())
|