-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Mustafa Kerem Kurban
committed
Oct 3, 2024
1 parent
ce6a0c7
commit 4ff3e1e
Showing
6 changed files
with
234 additions
and
238 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,125 +1,126 @@ | ||
from typing import Any, AsyncIterator | ||
from pydantic import BaseModel, Field, ValidationError | ||
from langgraph.graph import StateGraph, START, END | ||
from langgraph.errors import NodeInterrupt | ||
from neuroagent.tools.bluenaas_tool import BlueNaaSTool, InputBlueNaaS, BlueNaaSOutput | ||
from neuroagent.tools.get_me_model_tool import GetMEModelTool | ||
from neuroagent.tools.electrophys_tool import ElectrophysFeatureTool | ||
from neuroagent.app.dependencies import get_settings, get_kg_token, get_httpx_client | ||
# from typing import Any, AsyncIterator | ||
# from pydantic import BaseModel, Field, ValidationError | ||
# from langgraph.graph import StateGraph, START, END | ||
# from langgraph.errors import NodeInterrupt | ||
# from neuroagent.tools.bluenaas_tool import BlueNaaSTool, InputBlueNaaS, BlueNaaSOutput | ||
# # from neuroagent.tools.get_me_model_tool import GetMEModelTool | ||
# # from neuroagent.tools.electrophys_tool import ElectrophysFeatureTool | ||
# # from neuroagent.app.dependencies import get_settings, get_kg_token, get_httpx_client | ||
# from neuroagent.agents import BaseAgent | ||
|
||
class BluenaasSimAgent(BaseAgent): | ||
"""Agent for running BlueNaaS simulations with iterative configuration improvement.""" | ||
# class BluenaasSimAgent(BaseAgent): | ||
# """Agent for running BlueNaaS simulations with iterative configuration improvement.""" | ||
|
||
async def arun(self, query: str) -> Any: | ||
"""Run the agent against a query.""" | ||
state_graph = StateGraph() | ||
state_graph.add_node("parse_input", self.parse_input) | ||
state_graph.add_node("validate_config", self.validate_config) | ||
state_graph.add_node("prompt_user_for_missing_fields", self.prompt_user_for_missing_fields) | ||
state_graph.add_node("finalize_config", self.finalize_config) | ||
state_graph.add_node("run_simulation", self.run_simulation) | ||
state_graph.add_node("process_results", self.process_results) | ||
state_graph.add_node("handle_interruption", self.handle_interruption) | ||
# async def arun(self, query: str) -> Any: | ||
# """Run the agent against a query.""" | ||
# state_graph = StateGraph() | ||
# state_graph.add_node("parse_input", self.parse_input) | ||
# state_graph.add_node("validate_config", self.validate_config) | ||
# state_graph.add_node("prompt_user_for_missing_fields", self.prompt_user_for_missing_fields) | ||
# state_graph.add_node("finalize_config", self.finalize_config) | ||
# state_graph.add_node("run_simulation", self.run_simulation) | ||
# state_graph.add_node("process_results", self.process_results) | ||
# state_graph.add_node("handle_interruption", self.handle_interruption) | ||
|
||
state_graph.add_edge("parse_input", "validate_config") | ||
state_graph.add_edge("validate_config", "prompt_user_for_missing_fields", condition=lambda x: not x["valid"]) | ||
state_graph.add_edge("validate_config", "finalize_config", condition=lambda x: x["valid"]) | ||
state_graph.add_edge("prompt_user_for_missing_fields", "validate_config") | ||
state_graph.add_edge("finalize_config", "run_simulation") | ||
state_graph.add_edge("run_simulation", "process_results") | ||
state_graph.add_edge("finalize_config", "handle_interruption", condition=lambda x: x.get("interrupted", False)) | ||
# state_graph.add_edge("parse_input", "validate_config") | ||
# state_graph.add_edge("validate_config", "prompt_user_for_missing_fields", condition=lambda x: not x["valid"]) | ||
# state_graph.add_edge("validate_config", "finalize_config", condition=lambda x: x["valid"]) | ||
# state_graph.add_edge("prompt_user_for_missing_fields", "validate_config") | ||
# state_graph.add_edge("finalize_config", "run_simulation") | ||
# state_graph.add_edge("run_simulation", "process_results") | ||
# state_graph.add_edge("finalize_config", "handle_interruption", condition=lambda x: x.get("interrupted", False)) | ||
|
||
initial_state = {"query": query} | ||
result = await state_graph.run(initial_state) | ||
return result | ||
# initial_state = {"query": query} | ||
# result = await state_graph.run(initial_state) | ||
# return result | ||
|
||
async def parse_input(self, state: dict) -> dict: | ||
"""Parse user input to create initial simulation configuration.""" | ||
# Implement parsing logic here | ||
parsed_config = { | ||
"me_model_id": None, # Placeholder, should be parsed from user input | ||
"currentInjection": { | ||
"injectTo": "soma", | ||
"stimulus": { | ||
"stimulusType": "current_clamp", | ||
"stimulusProtocol": "fire_pattern", | ||
"amplitudes": [0.05] | ||
} | ||
}, | ||
"recordFrom": [ | ||
{"section": "soma", "offset": 0.5} | ||
], | ||
"conditions": { | ||
"celsius": 34.0, | ||
"vinit": -70.0, | ||
"hypamp": 0.1, | ||
"max_time": 1000.0, | ||
"time_step": 0.025, | ||
"seed": 42 | ||
}, | ||
"simulationType": "single-neuron-simulation", | ||
"simulationDuration": 1000 | ||
} | ||
state["config"] = parsed_config | ||
return state | ||
# async def parse_input(self, state: dict) -> dict: | ||
# """Parse user input to create initial simulation configuration.""" | ||
# # Implement parsing logic here | ||
# parsed_config = { | ||
# "me_model_id": None, # Placeholder, should be parsed from user input | ||
# "currentInjection": { | ||
# "injectTo": "soma", | ||
# "stimulus": { | ||
# "stimulusType": "current_clamp", | ||
# "stimulusProtocol": "fire_pattern", | ||
# "amplitudes": [0.05] | ||
# } | ||
# }, | ||
# "recordFrom": [ | ||
# {"section": "soma", "offset": 0.5} | ||
# ], | ||
# "conditions": { | ||
# "celsius": 34.0, | ||
# "vinit": -70.0, | ||
# "hypamp": 0.1, | ||
# "max_time": 1000.0, | ||
# "time_step": 0.025, | ||
# "seed": 42 | ||
# }, | ||
# "simulationType": "single-neuron-simulation", | ||
# "simulationDuration": 1000 | ||
# } | ||
# state["config"] = parsed_config | ||
# return state | ||
|
||
async def validate_config(self, state: dict) -> dict: | ||
"""Validate the simulation configuration using Pydantic.""" | ||
try: | ||
config = InputBlueNaaS(**state["config"]) | ||
state["valid"] = True | ||
except ValidationError as e: | ||
state["valid"] = False | ||
state["errors"] = e.errors() | ||
return state | ||
# async def validate_config(self, state: dict) -> dict: | ||
# """Validate the simulation configuration using Pydantic.""" | ||
# try: | ||
# config = InputBlueNaaS(**state["config"]) | ||
# state["valid"] = True | ||
# except ValidationError as e: | ||
# state["valid"] = False | ||
# state["errors"] = e.errors() | ||
# return state | ||
|
||
async def prompt_user_for_missing_fields(self, state: dict) -> dict: | ||
"""Prompt the user for missing fields in the configuration.""" | ||
# Implement logic to prompt user for missing fields | ||
missing_fields = [error["loc"][0] for error in state["errors"]] | ||
user_response = await self.metadata["llm"].ainvoke({ | ||
"messages": [ | ||
{"role": "system", "content": f"The following fields are missing or invalid: {missing_fields}"}, | ||
{"role": "user", "content": "Please provide the missing values."} | ||
] | ||
}) | ||
# Update state with user-provided values | ||
state["config"].update(user_response) | ||
return state | ||
# async def prompt_user_for_missing_fields(self, state: dict) -> dict: | ||
# """Prompt the user for missing fields in the configuration.""" | ||
# # Implement logic to prompt user for missing fields | ||
# missing_fields = [error["loc"][0] for error in state["errors"]] | ||
# user_response = await self.metadata["llm"].ainvoke({ | ||
# "messages": [ | ||
# {"role": "system", "content": f"The following fields are missing or invalid: {missing_fields}"}, | ||
# {"role": "user", "content": "Please provide the missing values."} | ||
# ] | ||
# }) | ||
# # Update state with user-provided values | ||
# state["config"].update(user_response) | ||
# return state | ||
|
||
async def finalize_config(self, state: dict) -> dict: | ||
"""Finalize the simulation configuration and prompt user for approval.""" | ||
user_response = await self.metadata["llm"].ainvoke({ | ||
"messages": [ | ||
{"role": "system", "content": "Here is the final simulation configuration:"}, | ||
{"role": "system", "content": str(state["config"])}, | ||
{"role": "user", "content": "Do you approve this configuration? (yes/no)"} | ||
] | ||
}) | ||
if user_response.lower() != "yes": | ||
state["interrupted"] = True | ||
raise NodeInterrupt("User did not approve the configuration.") | ||
return state | ||
# async def finalize_config(self, state: dict) -> dict: | ||
# """Finalize the simulation configuration and prompt user for approval.""" | ||
# user_response = await self.metadata["llm"].ainvoke({ | ||
# "messages": [ | ||
# {"role": "system", "content": "Here is the final simulation configuration:"}, | ||
# {"role": "system", "content": str(state["config"])}, | ||
# {"role": "user", "content": "Do you approve this configuration? (yes/no)"} | ||
# ] | ||
# }) | ||
# if user_response.lower() != "yes": | ||
# state["interrupted"] = True | ||
# raise NodeInterrupt("User did not approve the configuration.") | ||
# return state | ||
|
||
async def run_simulation(self, state: dict) -> dict: | ||
"""Run the simulation using the BlueNaaSTool.""" | ||
tool = BlueNaaSTool(metadata=self.metadata) | ||
result = await tool._arun(**state["config"]) | ||
state["simulation_result"] = result | ||
return state | ||
# async def run_simulation(self, state: dict) -> dict: | ||
# """Run the simulation using the BlueNaaSTool.""" | ||
# tool = BlueNaaSTool(metadata=self.metadata) | ||
# result = await tool._arun(**state["config"]) | ||
# state["simulation_result"] = result | ||
# return state | ||
|
||
async def process_results(self, state: dict) -> dict: | ||
"""Process the simulation results and run electrophysiological analysis.""" | ||
# Implement logic to process simulation results and run electrophysiological analysis | ||
return state | ||
# async def process_results(self, state: dict) -> dict: | ||
# """Process the simulation results and run electrophysiological analysis.""" | ||
# # Implement logic to process simulation results and run electrophysiological analysis | ||
# return state | ||
|
||
async def handle_interruption(self, state: dict) -> dict: | ||
"""Handle interruptions in the state graph.""" | ||
# Implement logic to handle interruptions, such as user disapproval | ||
await self.metadata["llm"].ainvoke({ | ||
"messages": [ | ||
{"role": "system", "content": "The simulation configuration was not approved by the user."}, | ||
{"role": "user", "content": "Please provide the necessary changes to proceed."} | ||
] | ||
}) | ||
return state | ||
# async def handle_interruption(self, state: dict) -> dict: | ||
# """Handle interruptions in the state graph.""" | ||
# # Implement logic to handle interruptions, such as user disapproval | ||
# await self.metadata["llm"].ainvoke({ | ||
# "messages": [ | ||
# {"role": "system", "content": "The simulation configuration was not approved by the user."}, | ||
# {"role": "user", "content": "Please provide the necessary changes to proceed."} | ||
# ] | ||
# }) | ||
# return state |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.