swarmauri-base 0.6.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.
- swarmauri_base/agents/AgentBase.py +33 -0
- swarmauri_base/agents/AgentConversationMixin.py +9 -0
- swarmauri_base/agents/AgentRetrieveMixin.py +9 -0
- swarmauri_base/agents/AgentSystemContextMixin.py +15 -0
- swarmauri_base/agents/AgentToolMixin.py +10 -0
- swarmauri_base/agents/AgentVectorStoreMixin.py +9 -0
- swarmauri_base/agents/__init__.py +0 -0
- swarmauri_base/chains/ChainBase.py +35 -0
- swarmauri_base/chains/ChainContextBase.py +50 -0
- swarmauri_base/chains/ChainStepBase.py +21 -0
- swarmauri_base/chains/PromptContextChainBase.py +160 -0
- swarmauri_base/chains/__init__.py +0 -0
- swarmauri_base/chunkers/ChunkerBase.py +25 -0
- swarmauri_base/chunkers/__init__.py +0 -0
- swarmauri_base/control_panels/ControlPanelBase.py +105 -0
- swarmauri_base/control_panels/__init__.py +0 -0
- swarmauri_base/conversations/ConversationBase.py +51 -0
- swarmauri_base/conversations/ConversationSystemContextMixin.py +9 -0
- swarmauri_base/conversations/__init__.py +0 -0
- swarmauri_base/dataconnectors/DataConnectorBase.py +80 -0
- swarmauri_base/dataconnectors/__init__.py +0 -0
- swarmauri_base/distances/DistanceBase.py +36 -0
- swarmauri_base/distances/VisionDistanceBase.py +9 -0
- swarmauri_base/distances/__init__.py +0 -0
- swarmauri_base/document_stores/DocumentStoreBase.py +98 -0
- swarmauri_base/document_stores/DocumentStoreRetrieveBase.py +29 -0
- swarmauri_base/document_stores/__init__.py +0 -0
- swarmauri_base/documents/DocumentBase.py +16 -0
- swarmauri_base/documents/__init__.py +0 -0
- swarmauri_base/embeddings/EmbeddingBase.py +13 -0
- swarmauri_base/embeddings/VisionEmbeddingBase.py +8 -0
- swarmauri_base/embeddings/__init__.py +1 -0
- swarmauri_base/factories/FactoryBase.py +32 -0
- swarmauri_base/factories/_init__.py +0 -0
- swarmauri_base/image_gens/ImageGenBase.py +53 -0
- swarmauri_base/image_gens/__init__.py +0 -0
- swarmauri_base/llms/LLMBase.py +71 -0
- swarmauri_base/llms/__init__.py +0 -0
- swarmauri_base/measurements/MeasurementAggregateMixin.py +25 -0
- swarmauri_base/measurements/MeasurementBase.py +28 -0
- swarmauri_base/measurements/MeasurementCalculateMixin.py +34 -0
- swarmauri_base/measurements/MeasurementThresholdMixin.py +7 -0
- swarmauri_base/measurements/__init__.py +0 -0
- swarmauri_base/messages/MessageBase.py +13 -0
- swarmauri_base/messages/__init__.py +0 -0
- swarmauri_base/ocrs/OCRBase.py +71 -0
- swarmauri_base/ocrs/__init__.py +0 -0
- swarmauri_base/parsers/ParserBase.py +30 -0
- swarmauri_base/parsers/__init__.py +0 -0
- swarmauri_base/pipelines/PipelineBase.py +92 -0
- swarmauri_base/pipelines/__init__.py +0 -0
- swarmauri_base/prompt_templates/PromptTemplateBase.py +47 -0
- swarmauri_base/prompt_templates/__init__.py +0 -0
- swarmauri_base/prompts/PromptBase.py +40 -0
- swarmauri_base/prompts/PromptGeneratorBase.py +43 -0
- swarmauri_base/prompts/PromptMatrixBase.py +41 -0
- swarmauri_base/prompts/__init__.py +0 -0
- swarmauri_base/schema_converters/SchemaConverterBase.py +19 -0
- swarmauri_base/schema_converters/__init__.py +0 -0
- swarmauri_base/service_registries/ServiceRegistryBase.py +67 -0
- swarmauri_base/service_registries/__init__.py +0 -0
- swarmauri_base/state/StateBase.py +49 -0
- swarmauri_base/state/__init__.py +0 -0
- swarmauri_base/stt/STTBase.py +71 -0
- swarmauri_base/stt/__init__.py +0 -0
- swarmauri_base/swarms/SwarmBase.py +103 -0
- swarmauri_base/swarms/__init__.py +0 -0
- swarmauri_base/task_mgmt_strategies/TaskMgmtStrategyBase.py +64 -0
- swarmauri_base/task_mgmt_strategies/__init__.py +0 -0
- swarmauri_base/tool_llms/ToolLLMBase.py +71 -0
- swarmauri_base/tool_llms/__init__.py +0 -0
- swarmauri_base/toolkits/ToolkitBase.py +104 -0
- swarmauri_base/toolkits/__init__.py +0 -0
- swarmauri_base/tools/ParameterBase.py +15 -0
- swarmauri_base/tools/ToolBase.py +23 -0
- swarmauri_base/tools/__init__.py +0 -0
- swarmauri_base/transports/TransportBase.py +56 -0
- swarmauri_base/transports/__init__.py +0 -0
- swarmauri_base/tts/TTSBase.py +71 -0
- swarmauri_base/tts/__init__.py +0 -0
- swarmauri_base/vector_stores/VectorStoreBase.py +137 -0
- swarmauri_base/vector_stores/VectorStoreCloudMixin.py +25 -0
- swarmauri_base/vector_stores/VectorStorePersistentMixin.py +33 -0
- swarmauri_base/vector_stores/VectorStoreRetrieveMixin.py +21 -0
- swarmauri_base/vector_stores/VectorStoreSaveLoadMixin.py +167 -0
- swarmauri_base/vector_stores/VisionVectorStoreBase.py +8 -0
- swarmauri_base/vector_stores/__init__.py +0 -0
- swarmauri_base/vectors/VectorBase.py +29 -0
- swarmauri_base/vectors/__init__.py +0 -0
- swarmauri_base/vlms/VLMBase.py +63 -0
- swarmauri_base/vlms/__init__.py +0 -0
- swarmauri_base-0.6.0.dist-info/LICENSE +201 -0
- swarmauri_base-0.6.0.dist-info/METADATA +45 -0
- swarmauri_base-0.6.0.dist-info/RECORD +95 -0
- swarmauri_base-0.6.0.dist-info/WHEEL +4 -0
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
from typing import Any, Optional, Dict, Union, Literal
|
|
2
|
+
from pydantic import ConfigDict, Field
|
|
3
|
+
from swarmauri_core.ComponentBase import ComponentBase, ResourceTypes, SubclassUnion
|
|
4
|
+
from swarmauri_core.messages.IMessage import IMessage
|
|
5
|
+
from swarmauri_core.agents.IAgent import IAgent
|
|
6
|
+
from swarmauri_base.llms.LLMBase import LLMBase
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@ComponentBase.register_model()
|
|
10
|
+
class AgentBase(IAgent, ComponentBase):
|
|
11
|
+
llm: SubclassUnion[LLMBase]
|
|
12
|
+
llm_kwargs: Optional[Dict] = {}
|
|
13
|
+
resource: ResourceTypes = Field(default=ResourceTypes.AGENT.value)
|
|
14
|
+
model_config = ConfigDict(extra="forbid", arbitrary_types_allowed=True)
|
|
15
|
+
type: Literal["AgentBase"] = "AgentBase"
|
|
16
|
+
|
|
17
|
+
def exec(
|
|
18
|
+
self,
|
|
19
|
+
input_str: Optional[Union[str, IMessage]] = "",
|
|
20
|
+
llm_kwargs: Optional[Dict] = {},
|
|
21
|
+
) -> Any:
|
|
22
|
+
raise NotImplementedError(
|
|
23
|
+
"The `exec` function has not been implemented on this class."
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
async def aexec(
|
|
27
|
+
self,
|
|
28
|
+
input_str: Optional[Union[str, IMessage]] = "",
|
|
29
|
+
llm_kwargs: Optional[Dict] = {},
|
|
30
|
+
) -> Any:
|
|
31
|
+
raise NotImplementedError(
|
|
32
|
+
"The `aexec` function has not been implemented on this class."
|
|
33
|
+
)
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
from pydantic import BaseModel, ConfigDict
|
|
2
|
+
from swarmauri_core.ComponentBase import SubclassUnion
|
|
3
|
+
from swarmauri_core.agents.IAgentConversation import IAgentConversation
|
|
4
|
+
from swarmauri_base.conversations.ConversationBase import ConversationBase
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class AgentConversationMixin(IAgentConversation, BaseModel):
|
|
8
|
+
conversation: SubclassUnion[ConversationBase] # 🚧 Placeholder
|
|
9
|
+
model_config = ConfigDict(extra="forbid", arbitrary_types_allowed=True)
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
from typing import List
|
|
2
|
+
from pydantic import BaseModel, ConfigDict, Field
|
|
3
|
+
from swarmauri_standard.documents.Document import Document
|
|
4
|
+
from swarmauri_core.agents.IAgentRetrieve import IAgentRetrieve
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class AgentRetrieveMixin(IAgentRetrieve, BaseModel):
|
|
8
|
+
last_retrieved: List[Document] = Field(default_factory=list)
|
|
9
|
+
model_config = ConfigDict(extra="forbid", arbitrary_types_allowed=True)
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
from typing import Union
|
|
2
|
+
from pydantic import BaseModel, field_validator
|
|
3
|
+
|
|
4
|
+
from swarmauri_standard.messages.SystemMessage import SystemMessage
|
|
5
|
+
from swarmauri_core.agents.IAgentSystemContext import IAgentSystemContext
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class AgentSystemContextMixin(IAgentSystemContext, BaseModel):
|
|
9
|
+
system_context: Union[SystemMessage, str]
|
|
10
|
+
|
|
11
|
+
@field_validator("system_context", mode="before")
|
|
12
|
+
def set_system_context(cls, value: Union[str, SystemMessage]) -> SystemMessage:
|
|
13
|
+
if isinstance(value, str):
|
|
14
|
+
return SystemMessage(content=value)
|
|
15
|
+
return value
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
from pydantic import BaseModel, ConfigDict
|
|
2
|
+
|
|
3
|
+
from swarmauri_core.agents.IAgentToolkit import IAgentToolkit
|
|
4
|
+
from swarmauri_core.ComponentBase import SubclassUnion
|
|
5
|
+
from swarmauri_base.toolkits.ToolkitBase import ToolkitBase
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class AgentToolMixin(IAgentToolkit, BaseModel):
|
|
9
|
+
toolkit: SubclassUnion[ToolkitBase]
|
|
10
|
+
model_config = ConfigDict(extra="forbid", arbitrary_types_allowed=True)
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
from pydantic import BaseModel, ConfigDict
|
|
2
|
+
from swarmauri_core.ComponentBase import SubclassUnion
|
|
3
|
+
from swarmauri_core.agents.IAgentVectorStore import IAgentVectorStore
|
|
4
|
+
from swarmauri_base.vector_stores.VectorStoreBase import VectorStoreBase
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class AgentVectorStoreMixin(IAgentVectorStore, BaseModel):
|
|
8
|
+
vector_store: SubclassUnion[VectorStoreBase]
|
|
9
|
+
model_config = ConfigDict(extra="forbid", arbitrary_types_allowed=True)
|
|
File without changes
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
from typing import List, Dict, Any, Optional, Literal
|
|
2
|
+
from pydantic import Field, ConfigDict
|
|
3
|
+
|
|
4
|
+
from swarmauri_base.chains.ChainStepBase import ChainStepBase
|
|
5
|
+
from swarmauri_core.chains.IChain import IChain
|
|
6
|
+
from swarmauri_core.ComponentBase import ComponentBase, ResourceTypes
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@ComponentBase.register_model()
|
|
10
|
+
class ChainBase(IChain, ComponentBase):
|
|
11
|
+
"""
|
|
12
|
+
A base implementation of the IChain interface.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
steps: List[ChainStepBase] = []
|
|
16
|
+
resource: Optional[str] = Field(default=ResourceTypes.CHAIN.value)
|
|
17
|
+
model_config = ConfigDict(extra="forbid", arbitrary_types_allowed=True)
|
|
18
|
+
type: Literal["ChainBase"] = "ChainBase"
|
|
19
|
+
|
|
20
|
+
def add_step(self, step: ChainStepBase) -> None:
|
|
21
|
+
self.steps.append(step)
|
|
22
|
+
|
|
23
|
+
def remove_step(self, step: ChainStepBase) -> None:
|
|
24
|
+
"""
|
|
25
|
+
Removes an existing step from the chain. This alters the chain's execution sequence
|
|
26
|
+
by excluding the specified step from subsequent executions of the chain.
|
|
27
|
+
|
|
28
|
+
Parameters:
|
|
29
|
+
step (ChainStepBase): The Callable representing the step to remove from the chain.
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
raise NotImplementedError("This is not yet implemented")
|
|
33
|
+
|
|
34
|
+
def execute(self, *args, **kwargs) -> Dict[str, Any]:
|
|
35
|
+
raise NotImplementedError("This is not yet implemented")
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
from typing import Any, Dict, List, Optional, Literal
|
|
2
|
+
from pydantic import Field, ConfigDict
|
|
3
|
+
import re
|
|
4
|
+
from swarmauri_base.chains.ChainStepBase import ChainStepBase
|
|
5
|
+
from swarmauri_core.chains.IChainContext import IChainContext
|
|
6
|
+
from swarmauri_core.ComponentBase import ComponentBase, ResourceTypes
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@ComponentBase.register_model()
|
|
10
|
+
class ChainContextBase(IChainContext, ComponentBase):
|
|
11
|
+
steps: List[ChainStepBase] = []
|
|
12
|
+
context: Dict = {}
|
|
13
|
+
resource: Optional[str] = Field(default=ResourceTypes.CHAIN.value)
|
|
14
|
+
model_config = ConfigDict(extra="forbid", arbitrary_types_allowed=True)
|
|
15
|
+
type: Literal["ChainContextBase"] = "ChainContextBase"
|
|
16
|
+
|
|
17
|
+
def update(self, **kwargs):
|
|
18
|
+
self.context.update(kwargs)
|
|
19
|
+
|
|
20
|
+
def get_value(self, key: str) -> Any:
|
|
21
|
+
return self.context.get(key)
|
|
22
|
+
|
|
23
|
+
def _resolve_fstring(self, template: str) -> str:
|
|
24
|
+
pattern = re.compile(r"{([^}]+)}")
|
|
25
|
+
|
|
26
|
+
def replacer(match):
|
|
27
|
+
expression = match.group(1)
|
|
28
|
+
try:
|
|
29
|
+
return str(eval(expression, {}, self.context))
|
|
30
|
+
except Exception as e:
|
|
31
|
+
print(f"Failed to resolve expression: {expression}. Error: {e}")
|
|
32
|
+
return f"{{{expression}}}"
|
|
33
|
+
|
|
34
|
+
return pattern.sub(replacer, template)
|
|
35
|
+
|
|
36
|
+
def _resolve_placeholders(self, value: Any) -> Any:
|
|
37
|
+
if isinstance(value, str):
|
|
38
|
+
return self._resolve_fstring(value)
|
|
39
|
+
elif isinstance(value, dict):
|
|
40
|
+
return {k: self._resolve_placeholders(v) for k, v in value.items()}
|
|
41
|
+
elif isinstance(value, list):
|
|
42
|
+
return [self._resolve_placeholders(v) for v in value]
|
|
43
|
+
else:
|
|
44
|
+
return value
|
|
45
|
+
|
|
46
|
+
def _resolve_ref(self, value: Any) -> Any:
|
|
47
|
+
if isinstance(value, str) and value.startswith("$"):
|
|
48
|
+
placeholder = value[1:]
|
|
49
|
+
return placeholder
|
|
50
|
+
return value
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
from typing import Any, Tuple, Dict, Optional, Literal
|
|
2
|
+
from pydantic import Field
|
|
3
|
+
|
|
4
|
+
from swarmauri_base.tools.ToolBase import ToolBase
|
|
5
|
+
from swarmauri_core.chains.IChainStep import IChainStep
|
|
6
|
+
from swarmauri_core.ComponentBase import SubclassUnion, ComponentBase, ResourceTypes
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@ComponentBase.register_model()
|
|
10
|
+
class ChainStepBase(IChainStep, ComponentBase):
|
|
11
|
+
"""
|
|
12
|
+
Represents a single step within an execution chain.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
key: str
|
|
16
|
+
method: SubclassUnion[ToolBase]
|
|
17
|
+
args: Tuple = Field(default_factory=tuple)
|
|
18
|
+
kwargs: Dict[str, Any] = Field(default_factory=dict)
|
|
19
|
+
ref: Optional[str] = Field(default=None)
|
|
20
|
+
resource: Optional[str] = Field(default=ResourceTypes.CHAINSTEP.value)
|
|
21
|
+
type: Literal["ChainStepBase"] = "ChainStepBase"
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
from typing import List, Dict, Any, Optional, Literal
|
|
2
|
+
from pydantic import Field
|
|
3
|
+
import re
|
|
4
|
+
import numpy as np
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
from swarmauri_base.chains.ChainStepBase import ChainStepBase
|
|
8
|
+
from swarmauri_base.chains.ChainContextBase import ChainContextBase
|
|
9
|
+
from swarmauri_base.prompts.PromptMatrixBaseBase import PromptMatrixBase
|
|
10
|
+
from swarmauri_base.agents.AgentBase import AgentBase
|
|
11
|
+
|
|
12
|
+
from swarmauri_core.chains.IChainDependencyResolver import IChainDependencyResolver
|
|
13
|
+
from swarmauri_core.ComponentBase import SubclassUnion, ComponentBase, ResourceTypes
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@ComponentBase.register_model()
|
|
17
|
+
class PromptContextChainBase(IChainDependencyResolver, ChainContextBase, ComponentBase):
|
|
18
|
+
prompt_matrix: PromptMatrixBase
|
|
19
|
+
agents: List[SubclassUnion[AgentBase]] = Field(default_factory=list)
|
|
20
|
+
context: Dict[str, Any] = Field(default_factory=dict)
|
|
21
|
+
llm_kwargs: Dict[str, Any] = Field(default_factory=dict)
|
|
22
|
+
response_matrix: Optional[PromptMatrixBase] = None
|
|
23
|
+
current_step_index: int = 0
|
|
24
|
+
steps: List[Any] = Field(default_factory=list)
|
|
25
|
+
resource: Optional[str] = Field(default=ResourceTypes.CHAIN.value)
|
|
26
|
+
type: Literal["PromptContextChainBase"] = "PromptContextChainBase"
|
|
27
|
+
|
|
28
|
+
def __init__(self, **data: Any):
|
|
29
|
+
super().__init__(**data)
|
|
30
|
+
# Now that the instance is created, we can safely access `prompt_matrix.shape`
|
|
31
|
+
self.response_matrix = PromptMatrixBase(
|
|
32
|
+
matrix=[
|
|
33
|
+
[None for _ in range(self.prompt_matrix.shape[1])]
|
|
34
|
+
for _ in range(self.prompt_matrix.shape[0])
|
|
35
|
+
]
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
def execute(self, build_dependencies=True) -> None:
|
|
39
|
+
"""
|
|
40
|
+
Execute the chain of prompts based on the state of the prompt matrix.
|
|
41
|
+
Iterates through each sequence in the prompt matrix, resolves dependencies,
|
|
42
|
+
and executes prompts in the resolved order.
|
|
43
|
+
"""
|
|
44
|
+
if build_dependencies:
|
|
45
|
+
self.steps = self.build_dependencies()
|
|
46
|
+
self.current_step_index = 0
|
|
47
|
+
|
|
48
|
+
while self.current_step_index < len(self.steps):
|
|
49
|
+
step = self.steps[self.current_step_index]
|
|
50
|
+
method = step.method
|
|
51
|
+
args = step.args
|
|
52
|
+
ref = step.ref
|
|
53
|
+
result = method(*args)
|
|
54
|
+
self.context[ref] = result
|
|
55
|
+
prompt_index = self._extract_step_number(ref)
|
|
56
|
+
self._update_response_matrix(args[0], prompt_index, result)
|
|
57
|
+
self.current_step_index += 1 # Move to the next step
|
|
58
|
+
else:
|
|
59
|
+
print("All steps have been executed.")
|
|
60
|
+
|
|
61
|
+
def execute_next_step(self):
|
|
62
|
+
"""
|
|
63
|
+
Execute the next step in the steps list if available.
|
|
64
|
+
"""
|
|
65
|
+
if self.current_step_index < len(self.steps):
|
|
66
|
+
step = self.steps[self.current_step_index]
|
|
67
|
+
method = step.method
|
|
68
|
+
args = step.args
|
|
69
|
+
ref = step.ref
|
|
70
|
+
result = method(*args)
|
|
71
|
+
self.context[ref] = result
|
|
72
|
+
prompt_index = self._extract_step_number(ref)
|
|
73
|
+
self._update_response_matrix(args[0], prompt_index, result)
|
|
74
|
+
self.current_step_index += 1 # Move to the next step
|
|
75
|
+
else:
|
|
76
|
+
print("All steps have been executed.")
|
|
77
|
+
|
|
78
|
+
def _execute_prompt(self, agent_index: int, prompt: str, ref: str):
|
|
79
|
+
"""
|
|
80
|
+
Executes a given prompt using the specified agent and updates the response.
|
|
81
|
+
"""
|
|
82
|
+
formatted_prompt = prompt.format(
|
|
83
|
+
**self.context
|
|
84
|
+
) # Using context for f-string formatting
|
|
85
|
+
|
|
86
|
+
agent = self.agents[agent_index]
|
|
87
|
+
# get the unformatted version
|
|
88
|
+
unformatted_system_context = agent.system_context
|
|
89
|
+
# use the formatted version
|
|
90
|
+
agent.system_context = agent.system_context.content.format(**self.context)
|
|
91
|
+
response = agent.exec(formatted_prompt, model_kwargs=self.model_kwargs)
|
|
92
|
+
# reset back to the unformatted version
|
|
93
|
+
agent.system_context = unformatted_system_context
|
|
94
|
+
|
|
95
|
+
self.context[ref] = response
|
|
96
|
+
prompt_index = self._extract_step_number(ref)
|
|
97
|
+
self._update_response_matrix(agent_index, prompt_index, response)
|
|
98
|
+
return response
|
|
99
|
+
|
|
100
|
+
def _update_response_matrix(
|
|
101
|
+
self, agent_index: int, prompt_index: int, response: Any
|
|
102
|
+
):
|
|
103
|
+
self.response_matrix.matrix[agent_index][prompt_index] = response
|
|
104
|
+
|
|
105
|
+
def _extract_agent_number(self, text):
|
|
106
|
+
# Regular expression to match the pattern and capture the agent number
|
|
107
|
+
match = re.search(r"\{Agent_(\d+)_Step_\d+_response\}", text)
|
|
108
|
+
if match:
|
|
109
|
+
# Return the captured group, which is the agent number
|
|
110
|
+
return int(match.group(1))
|
|
111
|
+
else:
|
|
112
|
+
# Return None if no match is found
|
|
113
|
+
return None
|
|
114
|
+
|
|
115
|
+
def _extract_step_number(self, ref):
|
|
116
|
+
# This regex looks for the pattern '_Step_' followed by one or more digits.
|
|
117
|
+
match = re.search(r"_Step_(\d+)_", ref)
|
|
118
|
+
if match:
|
|
119
|
+
return int(match.group(1)) # Convert the extracted digits to an integer
|
|
120
|
+
else:
|
|
121
|
+
return None # If no match is found, return None
|
|
122
|
+
|
|
123
|
+
def build_dependencies(self) -> List[ChainStepBase]:
|
|
124
|
+
"""
|
|
125
|
+
Build the chain steps in the correct order by resolving dependencies first.
|
|
126
|
+
"""
|
|
127
|
+
steps = []
|
|
128
|
+
|
|
129
|
+
for i in range(self.prompt_matrix.shape[1]):
|
|
130
|
+
try:
|
|
131
|
+
sequence = np.array(self.prompt_matrix.matrix)[:, i].tolist()
|
|
132
|
+
execution_order = self.resolve_dependencies(sequence=sequence)
|
|
133
|
+
for j in execution_order:
|
|
134
|
+
prompt = sequence[j]
|
|
135
|
+
if prompt:
|
|
136
|
+
ref = f"Agent_{j}_Step_{i}_response" # Using a unique reference string
|
|
137
|
+
step = ChainStepBase(
|
|
138
|
+
key=f"Agent_{j}_Step_{i}",
|
|
139
|
+
method=self._execute_prompt,
|
|
140
|
+
args=[j, prompt, ref],
|
|
141
|
+
ref=ref,
|
|
142
|
+
)
|
|
143
|
+
steps.append(step)
|
|
144
|
+
except Exception as e:
|
|
145
|
+
print(str(e))
|
|
146
|
+
return steps
|
|
147
|
+
|
|
148
|
+
def resolve_dependencies(self, sequence: List[Optional[str]]) -> List[int]:
|
|
149
|
+
"""
|
|
150
|
+
Resolve dependencies within a specific sequence of the prompt matrix.
|
|
151
|
+
|
|
152
|
+
Args:
|
|
153
|
+
matrix (List[List[Optional[str]]]): The prompt matrix.
|
|
154
|
+
sequence_index (int): The index of the sequence to resolve dependencies for.
|
|
155
|
+
|
|
156
|
+
Returns:
|
|
157
|
+
List[int]: The execution order of the agents for the given sequence.
|
|
158
|
+
"""
|
|
159
|
+
|
|
160
|
+
return [x for x in range(0, len(sequence), 1)]
|
|
File without changes
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
from abc import abstractmethod
|
|
2
|
+
|
|
3
|
+
from typing import Optional, Union, List, Any, Literal
|
|
4
|
+
from pydantic import Field
|
|
5
|
+
|
|
6
|
+
from swarmauri_core.chunkers.IChunker import IChunker
|
|
7
|
+
from swarmauri_core.ComponentBase import ComponentBase, ResourceTypes
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@ComponentBase.register_model()
|
|
11
|
+
class ChunkerBase(IChunker, ComponentBase):
|
|
12
|
+
"""
|
|
13
|
+
Interface for chunking text into smaller pieces.
|
|
14
|
+
|
|
15
|
+
This interface defines abstract methods for chunking texts. Implementing classes
|
|
16
|
+
should provide concrete implementations for these methods tailored to their specific
|
|
17
|
+
chunking algorithms.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
resource: Optional[str] = Field(default=ResourceTypes.CHUNKER.value)
|
|
21
|
+
type: Literal["ChunkerBase"] = "ChunkerBase"
|
|
22
|
+
|
|
23
|
+
@abstractmethod
|
|
24
|
+
def chunk_text(self, text: Union[str, Any], *args, **kwargs) -> List[Any]:
|
|
25
|
+
pass
|
|
File without changes
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
from typing import Any, Callable, List, Literal
|
|
3
|
+
from pydantic import Field, ConfigDict
|
|
4
|
+
|
|
5
|
+
from swarmauri_core.ComponentBase import ComponentBase, ResourceTypes, SubclassUnion
|
|
6
|
+
from swarmauri_core.control_panels.IControlPanel import IControlPlane
|
|
7
|
+
from swarmauri_base.service_registries.ServiceRegistryBase import ServiceRegistryBase
|
|
8
|
+
from swarmauri_base.factories.FactoryBase import FactoryBase
|
|
9
|
+
from swarmauri_base.task_mgmt_strategies.TaskMgmtStrategyBase import (
|
|
10
|
+
TaskMgmtStrategyBase,
|
|
11
|
+
)
|
|
12
|
+
from swarmauri_base.transports.TransportBase import TransportBase
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@ComponentBase.register_model()
|
|
16
|
+
class ControlPanelBase(IControlPlane, ComponentBase):
|
|
17
|
+
"""
|
|
18
|
+
Implementation of the ControlPlane abstract class.
|
|
19
|
+
This class orchestrates agents, manages tasks, and ensures task distribution
|
|
20
|
+
and transport between agents and services.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
resource: ResourceTypes = Field(default=ResourceTypes.CONTROL_PANEL.value)
|
|
24
|
+
model_config = ConfigDict(extra="forbid", arbitrary_types_allowed=True)
|
|
25
|
+
type: Literal["ControlPanelBase"] = "ControlPanelBase"
|
|
26
|
+
|
|
27
|
+
agent_factory: SubclassUnion[FactoryBase]
|
|
28
|
+
service_registry: SubclassUnion[ServiceRegistryBase]
|
|
29
|
+
task_mgmt_strategy: SubclassUnion[TaskMgmtStrategyBase]
|
|
30
|
+
transport: SubclassUnion[TransportBase]
|
|
31
|
+
|
|
32
|
+
# Agent management methods
|
|
33
|
+
def create_agent(
|
|
34
|
+
self, name: str, resource_class: Callable, role: str, **kwargs: Any
|
|
35
|
+
) -> Any:
|
|
36
|
+
"""
|
|
37
|
+
Create an agent with the given name and role, and register it in the service registry.
|
|
38
|
+
"""
|
|
39
|
+
if name not in self.agent_factory.get_agents():
|
|
40
|
+
self.agent_factory.register(name, resource_class)
|
|
41
|
+
|
|
42
|
+
agent = self.agent_factory.create(name, **kwargs)
|
|
43
|
+
self.service_registry.register_service(name, {"role": role, "status": "active"})
|
|
44
|
+
logging.info(f"Agent '{name}' with role '{role}' created and registered.")
|
|
45
|
+
return agent
|
|
46
|
+
|
|
47
|
+
def remove_agent(self, name: str) -> None:
|
|
48
|
+
"""
|
|
49
|
+
Remove the agent with the specified name and unregister it from the service registry.
|
|
50
|
+
"""
|
|
51
|
+
if name not in self.agent_factory.get_agents():
|
|
52
|
+
raise ValueError(f"Agent '{name}' not found.")
|
|
53
|
+
self.service_registry.unregister_service(name)
|
|
54
|
+
del self.agent_factory._registry[name]
|
|
55
|
+
logging.info(f"Agent '{name}' removed and unregistered.")
|
|
56
|
+
|
|
57
|
+
def list_active_agents(self) -> List[str]:
|
|
58
|
+
"""
|
|
59
|
+
List all active agent names.
|
|
60
|
+
"""
|
|
61
|
+
active_agents = self.agent_factory.get_agents()
|
|
62
|
+
logging.info(f"Active agents listed: {active_agents}")
|
|
63
|
+
return active_agents
|
|
64
|
+
|
|
65
|
+
# Task management methods
|
|
66
|
+
def submit_tasks(self, tasks: List[Any]) -> None:
|
|
67
|
+
"""
|
|
68
|
+
Submit one or more tasks to the task management strategy for processing.
|
|
69
|
+
"""
|
|
70
|
+
for task in tasks:
|
|
71
|
+
self.task_mgmt_strategy.add_task(task)
|
|
72
|
+
logging.info(
|
|
73
|
+
f"Task '{task.get('task_id', 'unknown')}' submitted to the strategy."
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
def process_tasks(self) -> None:
|
|
77
|
+
"""
|
|
78
|
+
Process and assign tasks from the queue, then transport them to their assigned services.
|
|
79
|
+
"""
|
|
80
|
+
try:
|
|
81
|
+
self.task_mgmt_strategy.process_tasks(
|
|
82
|
+
self.service_registry.get_services, self.transport
|
|
83
|
+
)
|
|
84
|
+
logging.info("Tasks processed and transported successfully.")
|
|
85
|
+
except Exception as e:
|
|
86
|
+
logging.error(f"Error while processing tasks: {e}")
|
|
87
|
+
raise ValueError(f"Error processing tasks: {e}")
|
|
88
|
+
|
|
89
|
+
def distribute_tasks(self, task: Any) -> None:
|
|
90
|
+
"""
|
|
91
|
+
Distribute tasks using the task strategy (manual or on-demand assignment).
|
|
92
|
+
"""
|
|
93
|
+
self.task_mgmt_strategy.assign_task(task, self.service_registry.get_services)
|
|
94
|
+
logging.info(
|
|
95
|
+
f"Task '{task.get('task_id', 'unknown')}' distributed to a service."
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
# Orchestration method
|
|
99
|
+
def orchestrate_agents(self, tasks: List[Any]) -> None:
|
|
100
|
+
"""
|
|
101
|
+
Orchestrate agents for task distribution and transportation.
|
|
102
|
+
"""
|
|
103
|
+
self.submit_tasks(tasks) # Add task to the strategy
|
|
104
|
+
self.process_tasks() # Process and transport the task
|
|
105
|
+
logging.info("Agents orchestrated successfully.")
|
|
File without changes
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
from typing import List, Union, Literal
|
|
2
|
+
from pydantic import Field, PrivateAttr, ConfigDict
|
|
3
|
+
|
|
4
|
+
from swarmauri_core.conversations.IConversation import IConversation
|
|
5
|
+
from swarmauri_core.ComponentBase import ComponentBase, ResourceTypes, SubclassUnion
|
|
6
|
+
from swarmauri_base.messages.MessageBase import MessageBase
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@ComponentBase.register_model()
|
|
10
|
+
class ConversationBase(IConversation, ComponentBase):
|
|
11
|
+
"""
|
|
12
|
+
Concrete implementation of IConversation, managing conversation history and operations.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
_history: List[SubclassUnion[MessageBase]] = PrivateAttr(default_factory=list)
|
|
16
|
+
resource: ResourceTypes = Field(default=ResourceTypes.CONVERSATION.value)
|
|
17
|
+
model_config = ConfigDict(extra="forbid", arbitrary_types_allowed=True)
|
|
18
|
+
type: Literal["ConversationBase"] = "ConversationBase"
|
|
19
|
+
|
|
20
|
+
@property
|
|
21
|
+
def history(self) -> List[SubclassUnion[MessageBase]]:
|
|
22
|
+
"""
|
|
23
|
+
Provides read-only access to the conversation history.
|
|
24
|
+
"""
|
|
25
|
+
return self._history
|
|
26
|
+
|
|
27
|
+
def add_message(self, message: SubclassUnion[MessageBase]):
|
|
28
|
+
self._history.append(message)
|
|
29
|
+
|
|
30
|
+
def remove_message(self, message: SubclassUnion[MessageBase]):
|
|
31
|
+
"""
|
|
32
|
+
Remove a message from the history
|
|
33
|
+
|
|
34
|
+
@param message: Message to remove from the history
|
|
35
|
+
@return None
|
|
36
|
+
"""
|
|
37
|
+
if self._history and message in self._history:
|
|
38
|
+
self._history.remove(message)
|
|
39
|
+
return None
|
|
40
|
+
|
|
41
|
+
def add_messages(self, messages: List[SubclassUnion[MessageBase]]):
|
|
42
|
+
for message in messages:
|
|
43
|
+
self._history.append(message)
|
|
44
|
+
|
|
45
|
+
def get_last(self) -> Union[SubclassUnion[MessageBase], None]:
|
|
46
|
+
if self._history:
|
|
47
|
+
return self._history[-1]
|
|
48
|
+
return None
|
|
49
|
+
|
|
50
|
+
def clear_history(self):
|
|
51
|
+
self._history.clear()
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
from typing import Optional
|
|
2
|
+
from pydantic import BaseModel
|
|
3
|
+
|
|
4
|
+
from swarmauri_core.conversations.ISystemContext import ISystemContext
|
|
5
|
+
from swarmauri_standard.messages.SystemMessage import SystemMessage
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class ConversationSystemContextMixin(ISystemContext, BaseModel):
|
|
9
|
+
system_context: Optional[SystemMessage]
|
|
File without changes
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
from swarmauri_core.dataconnectors.IDataConnector import IDataConnector
|
|
2
|
+
from swarmauri_core.ComponentBase import ComponentBase
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class DataConnectorBase(IDataConnector, ComponentBase):
|
|
6
|
+
"""
|
|
7
|
+
Base implementation of IDataConnector that raises NotImplementedError
|
|
8
|
+
for all abstract methods, ensuring explicit implementation in child classes.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
def authenticate(self, **kwargs):
|
|
12
|
+
"""
|
|
13
|
+
Raises NotImplementedError to enforce implementation in child classes.
|
|
14
|
+
|
|
15
|
+
:param kwargs: Authentication parameters
|
|
16
|
+
:raises NotImplementedError: Always raised to require specific implementation
|
|
17
|
+
"""
|
|
18
|
+
raise NotImplementedError(
|
|
19
|
+
"Authenticate method must be implemented by child classes."
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
def fetch_data(self, query: str, **kwargs):
|
|
23
|
+
"""
|
|
24
|
+
Raises NotImplementedError to enforce implementation in child classes.
|
|
25
|
+
|
|
26
|
+
:param query: Query string or parameters
|
|
27
|
+
:param kwargs: Additional parameters
|
|
28
|
+
:raises NotImplementedError: Always raised to require specific implementation
|
|
29
|
+
"""
|
|
30
|
+
raise NotImplementedError(
|
|
31
|
+
"Fetch data method must be implemented by child classes."
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
def insert_data(self, data, **kwargs):
|
|
35
|
+
"""
|
|
36
|
+
Raises NotImplementedError to enforce implementation in child classes.
|
|
37
|
+
|
|
38
|
+
:param data: Data to be inserted
|
|
39
|
+
:param kwargs: Additional parameters
|
|
40
|
+
:raises NotImplementedError: Always raised to require specific implementation
|
|
41
|
+
"""
|
|
42
|
+
raise NotImplementedError(
|
|
43
|
+
"Insert data method must be implemented by child classes."
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
def update_data(self, identifier, data, **kwargs):
|
|
47
|
+
"""
|
|
48
|
+
Raises NotImplementedError to enforce implementation in child classes.
|
|
49
|
+
|
|
50
|
+
:param identifier: Unique identifier of the data to update
|
|
51
|
+
:param data: Updated data
|
|
52
|
+
:param kwargs: Additional parameters
|
|
53
|
+
:raises NotImplementedError: Always raised to require specific implementation
|
|
54
|
+
"""
|
|
55
|
+
raise NotImplementedError(
|
|
56
|
+
"Update data method must be implemented by child classes."
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
def delete_data(self, identifier, **kwargs):
|
|
60
|
+
"""
|
|
61
|
+
Raises NotImplementedError to enforce implementation in child classes.
|
|
62
|
+
|
|
63
|
+
:param identifier: Unique identifier of the data to delete
|
|
64
|
+
:param kwargs: Additional parameters
|
|
65
|
+
:raises NotImplementedError: Always raised to require specific implementation
|
|
66
|
+
"""
|
|
67
|
+
raise NotImplementedError(
|
|
68
|
+
"Delete data method must be implemented by child classes."
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
def test_connection(self, **kwargs):
|
|
72
|
+
"""
|
|
73
|
+
Raises NotImplementedError to enforce implementation in child classes.
|
|
74
|
+
|
|
75
|
+
:param kwargs: Connection parameters
|
|
76
|
+
:raises NotImplementedError: Always raised to require specific implementation
|
|
77
|
+
"""
|
|
78
|
+
raise NotImplementedError(
|
|
79
|
+
"Test connection method must be implemented by child classes."
|
|
80
|
+
)
|
|
File without changes
|