generated from axioma-ai-labs/python-template
-
Notifications
You must be signed in to change notification settings - Fork 9
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
Showing
13 changed files
with
168 additions
and
25 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
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from src.execution.execution import ExecutionModule | ||
|
||
__all__ = ["ExecutionModule"] |
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from loguru import logger | ||
|
||
from src.core.defs import AgentAction | ||
from src.workflows.analyze_signal import analyze_signal | ||
from src.workflows.research_news import analyze_news_workflow | ||
|
||
|
||
class ExecutionModule: | ||
"""Module to execute agent actions.""" | ||
|
||
async def execute_action(self, action: AgentAction) -> dict: | ||
""" | ||
Execute the given action and return the result. | ||
Args: | ||
action (AgentAction): The action to execute. | ||
context (dict): Context to pass to the action. | ||
Returns: | ||
dict: Outcome and status of the action. | ||
""" | ||
try: | ||
if action == AgentAction.CHECK_SIGNAL: | ||
result = await analyze_signal() | ||
return {"status": "success", "outcome": result or "No signal"} | ||
|
||
elif action == AgentAction.ANALYZE_NEWS: | ||
recent_news = "Placeholder" | ||
result = await analyze_news_workflow(recent_news) | ||
return {"status": "success", "outcome": result or "No outcome"} | ||
|
||
elif action == AgentAction.IDLE: | ||
logger.info("Agent is idling.") | ||
return {"status": "success", "outcome": "Idling"} | ||
|
||
except Exception as e: | ||
logger.error(f"Error executing action {action}: {e}") | ||
return {"status": "error", "outcome": str(e)} |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from src.feedback.feedback import FeedbackModule | ||
|
||
__all__ = ["FeedbackModule"] |
File renamed without changes.
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from src.memory.memory import MemoryModule, get_memory_module | ||
|
||
__all__ = ["MemoryModule", "get_memory_module"] |
File renamed without changes.
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from src.planning.planning import PlanningModule | ||
|
||
__all__ = ["PlanningModule"] |
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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import json | ||
|
||
import httpx | ||
from loguru import logger | ||
|
||
from src.core.config import settings | ||
from src.core.defs import AgentAction, AgentState | ||
|
||
|
||
class PlanningModule: | ||
"""Centralized Planning Module using LLM for action selection.""" | ||
|
||
def __init__( | ||
self, api_url: str = settings.PLANNING_API_URL, model: str = settings.PLANNING_MODEL | ||
): | ||
""" | ||
Initialize the Planning Module. | ||
Args: | ||
api_url (str): The URL for the LLM API. | ||
model (str): The model name (e.g., 'llama3.2'). | ||
""" | ||
self.api_url = api_url | ||
self.model = model | ||
|
||
async def get_next_action(self, state: AgentState) -> AgentAction: | ||
""" | ||
Call the LLM to decide the next action. | ||
Args: | ||
context (dict): Context including state, previous actions, and outcomes. | ||
Returns: | ||
AgentAction: The next action to perform. | ||
""" | ||
prompt = ( | ||
"You are a helpful assistant, who only decides the next action to perform. " | ||
f"Following states are possible: {AgentState.__members__}. " | ||
f"You have the following possible actions: {AgentAction.__members__}. " | ||
"You need to decide the next action to perform. " | ||
"Return only the name of the action to perform and nothing else!" | ||
f"Current state of the agent: {state.value}. " | ||
) | ||
try: | ||
payload = { | ||
"model": self.model, | ||
"prompt": prompt, | ||
"stream": False, | ||
} | ||
async with httpx.AsyncClient() as client: | ||
logger.debug(f"Sending payload: {payload}") | ||
response = await client.post(self.api_url, json=payload) | ||
logger.debug(f"Received response: {response}") | ||
response.raise_for_status() | ||
|
||
data = response.json() | ||
logger.debug(f"LLM response: {data}") | ||
next_action = data.get("response") | ||
if not next_action or next_action not in AgentAction.__members__: | ||
logger.warning(f"Invalid action returned by LLM: {next_action}") | ||
return AgentAction.IDLE | ||
|
||
logger.info(f"LLM suggested action: {next_action}") | ||
return AgentAction[next_action] | ||
except Exception as e: | ||
logger.error(f"Failed to get next action from LLM: {e}") | ||
return AgentAction.IDLE |
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