flock-core 0.3.6__py3-none-any.whl → 0.3.10__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.

Potentially problematic release.


This version of flock-core might be problematic. Click here for more details.

@@ -0,0 +1,234 @@
1
+ """Agent-based router implementation for the Flock framework."""
2
+
3
+ from typing import Any
4
+
5
+ from flock.core.context.context import FlockContext
6
+ from flock.core.flock_agent import FlockAgent
7
+ from flock.core.flock_router import (
8
+ FlockRouter,
9
+ FlockRouterConfig,
10
+ HandOffRequest,
11
+ )
12
+ from flock.core.logging.formatters.themes import OutputTheme
13
+ from flock.core.logging.logging import get_logger
14
+ from flock.evaluators.declarative.declarative_evaluator import (
15
+ DeclarativeEvaluator,
16
+ DeclarativeEvaluatorConfig,
17
+ )
18
+ from flock.modules.output.output_module import OutputModule, OutputModuleConfig
19
+ from flock.routers.agent.handoff_agent import (
20
+ AgentInfo,
21
+ HandoffAgent,
22
+ )
23
+
24
+ logger = get_logger("agent_router")
25
+
26
+
27
+ class AgentRouterConfig(FlockRouterConfig):
28
+ """Configuration for the agent router.
29
+
30
+ This class extends FlockRouterConfig with parameters specific to the agent router.
31
+ """
32
+
33
+ with_output: bool = False
34
+ confidence_threshold: float = 0.5 # No additional parameters needed for now
35
+
36
+
37
+ class AgentRouter(FlockRouter):
38
+ """Router that uses a FlockAgent to determine the next agent in a workflow.
39
+
40
+ This class is responsible for:
41
+ 1. Creating and managing a HandoffAgent
42
+ 2. Analyzing available agents in the registry
43
+ 3. Using the HandoffAgent to determine the best next agent
44
+ 4. Creating a HandOff object with the selected agent
45
+ """
46
+
47
+ def __init__(
48
+ self,
49
+ name: str = "agent_router",
50
+ config: AgentRouterConfig | None = None,
51
+ ):
52
+ """Initialize the AgentRouter.
53
+
54
+ Args:
55
+ registry: The agent registry containing all available agents
56
+ name: The name of the router
57
+ config: The router configuration
58
+ """
59
+ super().__init__(
60
+ name=name, config=config or AgentRouterConfig(name=name)
61
+ )
62
+
63
+ async def route(
64
+ self,
65
+ current_agent: FlockAgent,
66
+ result: dict[str, Any],
67
+ context: FlockContext,
68
+ ) -> HandOffRequest:
69
+ """Determine the next agent to hand off to based on the current agent's output.
70
+
71
+ Args:
72
+ current_agent: The agent that just completed execution
73
+ result: The output from the current agent
74
+ context: The global execution context
75
+
76
+ Returns:
77
+ A HandOff object containing the next agent and input data
78
+ """
79
+ # Get all available agents from context.agent_definitions
80
+ agent_definitions = context.agent_definitions
81
+ handoff_agent = HandoffAgent(model=current_agent.model)
82
+ handoff_agent.evaluator = DeclarativeEvaluator(
83
+ name="evaluator",
84
+ config=DeclarativeEvaluatorConfig(
85
+ model=current_agent.model,
86
+ use_cache=True,
87
+ max_tokens=1000,
88
+ temperature=0.0,
89
+ ),
90
+ )
91
+ if self.config.with_output:
92
+ handoff_agent.add_module(
93
+ OutputModule(
94
+ name="output",
95
+ config=OutputModuleConfig(
96
+ theme=OutputTheme.abernathy,
97
+ ),
98
+ )
99
+ )
100
+ available_agents = self._get_available_agents(
101
+ agent_definitions, current_agent.name
102
+ )
103
+
104
+ if not available_agents:
105
+ logger.warning("No available agents for agent-based routing")
106
+ return HandOffRequest(
107
+ next_agent="",
108
+ hand_off_mode="add",
109
+ override_next_agent=None,
110
+ override_context=None,
111
+ )
112
+
113
+ # Prepare input for the handoff agent
114
+ handoff_input = {
115
+ "current_agent_name": current_agent.name,
116
+ "current_agent_description": current_agent.description,
117
+ "current_agent_input": current_agent.input,
118
+ "current_agent_output": current_agent.output,
119
+ "current_result": result,
120
+ "available_agents": available_agents,
121
+ }
122
+
123
+ try:
124
+ # Run the handoff agent to determine the next agent
125
+ handoff_result = await handoff_agent.run_async(handoff_input)
126
+
127
+ # Extract the decision
128
+ next_agent_name = handoff_result.get("agent_name")
129
+ confidence = handoff_result.get("confidence")
130
+ reasoning = handoff_result.get("reasoning")
131
+ logger.info(
132
+ f"Agent router selected agent '{next_agent_name}' with confidence {confidence} and reasoning: {reasoning}"
133
+ )
134
+
135
+ if confidence < self.config.confidence_threshold:
136
+ logger.info(
137
+ f"No suitable next agent found (best score: {confidence})"
138
+ )
139
+ return HandOffRequest(
140
+ next_agent="",
141
+ hand_off_mode="add",
142
+ override_next_agent=None,
143
+ override_context=None,
144
+ )
145
+
146
+ next_agent = agent_definitions.get(next_agent_name)
147
+ if not next_agent:
148
+ logger.error(
149
+ f"Selected agent '{next_agent_name}' not found in agent definitions"
150
+ )
151
+ return HandOffRequest(
152
+ next_agent="",
153
+ hand_off_mode="add",
154
+ override_next_agent=None,
155
+ override_context=None,
156
+ )
157
+
158
+ logger.info(
159
+ f"Agent router selected agent '{next_agent_name}' with confidence {confidence}"
160
+ )
161
+ return HandOffRequest(
162
+ next_agent=next_agent_name,
163
+ hand_off_mode="add",
164
+ override_next_agent=None,
165
+ override_context=None,
166
+ )
167
+
168
+ except Exception as e:
169
+ logger.error(f"Error in agent-based routing: {e}")
170
+ return HandOffRequest(
171
+ next_agent="",
172
+ hand_off_mode="add",
173
+ override_next_agent=None,
174
+ override_context=None,
175
+ )
176
+
177
+ def _get_available_agents(
178
+ self, agent_definitions: dict[str, Any], current_agent_name: str
179
+ ) -> list[AgentInfo]:
180
+ """Get all available agents except the current one and the handoff agent.
181
+
182
+ Args:
183
+ agent_definitions: Dictionary of available agents
184
+ current_agent_name: Name of the current agent to exclude
185
+
186
+ Returns:
187
+ List of available agents as AgentInfo objects
188
+ """
189
+ agents = []
190
+ for agent_name in agent_definitions:
191
+ if agent_name != current_agent_name:
192
+ agent = agent_definitions[agent_name]
193
+ agent_info = AgentInfo(
194
+ name=agent_name,
195
+ description=agent.agent_data["description"]
196
+ if agent.agent_data["description"]
197
+ else "",
198
+ input_schema=agent.agent_data["input"],
199
+ output_schema=agent.agent_data["output"],
200
+ )
201
+ agents.append(agent_info)
202
+ return agents
203
+
204
+ def _get_schema_from_agent(
205
+ self, agent: Any, schema_type: str
206
+ ) -> dict[str, Any]:
207
+ """Extract input or output schema from an agent.
208
+
209
+ Args:
210
+ agent: The agent to extract schema from
211
+ schema_type: Either "input" or "output"
212
+
213
+ Returns:
214
+ Dictionary representation of the schema
215
+ """
216
+ schema = {}
217
+ schema_str = agent.agent_data.get(schema_type, "")
218
+
219
+ # Parse the schema string to extract field names, types, and descriptions
220
+ if schema_str:
221
+ fields = schema_str.split(",")
222
+ for field in fields:
223
+ field = field.strip()
224
+ if ":" in field:
225
+ name, rest = field.split(":", 1)
226
+ name = name.strip()
227
+ schema[name] = rest.strip()
228
+ else:
229
+ schema[field] = "Any"
230
+
231
+ return schema
232
+
233
+ # The _create_next_input method is no longer needed since we're using hand_off_mode="add"
234
+ # instead of manually preparing inputs for the next agent
@@ -0,0 +1,58 @@
1
+ """Handoff agent for the agent-based router."""
2
+
3
+ from pydantic import BaseModel
4
+
5
+ from flock.core.flock_agent import FlockAgent
6
+
7
+
8
+ class AgentInfo(BaseModel):
9
+ """Information about an agent for handoff decisions."""
10
+
11
+ name: str
12
+ description: str = ""
13
+ input_schema: str = ""
14
+ output_schema: str = ""
15
+
16
+
17
+ class HandoffDecision(BaseModel):
18
+ """Decision about which agent to hand off to."""
19
+
20
+ agent_name: str
21
+ confidence: float
22
+ reasoning: str
23
+
24
+
25
+ class HandoffAgent(FlockAgent):
26
+ """Agent that decides which agent to hand off to next.
27
+
28
+ This agent analyzes the current agent's output and available agents
29
+ to determine the best next agent in the workflow.
30
+ """
31
+
32
+ def __init__(
33
+ self,
34
+ name: str = "handoff_agent",
35
+ model: str | None = None,
36
+ description: str = "Decides which agent to hand off to next",
37
+ ):
38
+ """Initialize the HandoffAgent.
39
+
40
+ Args:
41
+ name: The name of the agent
42
+ model: The model to use (e.g., 'openai/gpt-4o')
43
+ description: A human-readable description of the agent
44
+ """
45
+ super().__init__(
46
+ name=name,
47
+ model=model,
48
+ description=description,
49
+ input=(
50
+ "current_agent_name: str | Name of the current agent, "
51
+ "current_agent_description: str | Description of the current agent, "
52
+ "current_agent_input: str | Input schema of the current agent, "
53
+ "current_agent_output: str | Output schema of the current agent, "
54
+ "current_result: dict | Output from the current agent, "
55
+ "available_agents: list[AgentInfo] | List of available agents"
56
+ ),
57
+ output="agent_name: str | Name of the agent to hand off to, confidence: float | Confidence in the decision, reasoning: str | Reasoning for the decision",
58
+ )
@@ -0,0 +1 @@
1
+ """Default router implementation for the Flock framework."""
@@ -0,0 +1,76 @@
1
+ """Default router implementation for the Flock framework."""
2
+
3
+ from collections.abc import Callable
4
+ from typing import Any
5
+
6
+ from pydantic import Field
7
+
8
+ from flock.core.context.context import FlockContext
9
+ from flock.core.flock_agent import FlockAgent
10
+ from flock.core.flock_router import (
11
+ FlockRouter,
12
+ FlockRouterConfig,
13
+ HandOffRequest,
14
+ )
15
+ from flock.core.logging.logging import get_logger
16
+
17
+ logger = get_logger("default_router")
18
+
19
+
20
+ class DefaultRouterConfig(FlockRouterConfig):
21
+ """Configuration for the default router."""
22
+
23
+ hand_off: str | HandOffRequest | Callable[..., HandOffRequest] = Field(
24
+ default="", description="Next agent to hand off to"
25
+ )
26
+
27
+
28
+ class DefaultRouter(FlockRouter):
29
+ """Default router implementation.
30
+
31
+ This router simply uses the agent's hand_off property to determine the next agent.
32
+ It does not perform any dynamic routing.
33
+ """
34
+
35
+ name: str = "default_router"
36
+ config: DefaultRouterConfig = Field(
37
+ default_factory=DefaultRouterConfig, description="Output configuration"
38
+ )
39
+
40
+ def __init__(
41
+ self,
42
+ name: str = "default_router",
43
+ config: DefaultRouterConfig | None = None,
44
+ ):
45
+ """Initialize the DefaultRouter.
46
+
47
+ Args:
48
+ name: The name of the router
49
+ config: The router configuration
50
+ """
51
+ super().__init__(
52
+ name=name, config=config or DefaultRouterConfig(name=name)
53
+ )
54
+
55
+ async def route(
56
+ self,
57
+ current_agent: FlockAgent,
58
+ result: dict[str, Any],
59
+ context: FlockContext,
60
+ ) -> HandOffRequest:
61
+ """Determine the next agent to hand off to based on the current agent's output.
62
+
63
+ Args:
64
+ current_agent: The agent that just completed execution
65
+ result: The output from the current agent
66
+ context: The global execution context
67
+
68
+ Returns:
69
+ A HandOff object containing the next agent and input data
70
+ """
71
+ handoff = self.config.hand_off
72
+ if callable(handoff):
73
+ handoff = handoff(context, result)
74
+ if isinstance(handoff, str):
75
+ handoff = HandOffRequest(next_agent=handoff, hand_off_mode="match")
76
+ return handoff
@@ -0,0 +1 @@
1
+ """LLM-based router implementation for the Flock framework."""