| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  | from enum import StrEnum | 
					
						
							|  |  |  | from typing import Any, Optional, Union | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-25 12:20:47 +08:00
										 |  |  | from pydantic import BaseModel, Field | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  | from core.tools.entities.tool_entities import ToolInvokeMessage, ToolProviderType | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | class AgentToolEntity(BaseModel): | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     Agent Tool Entity. | 
					
						
							|  |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |     provider_type: ToolProviderType | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     provider_id: str | 
					
						
							|  |  |  |     tool_name: str | 
					
						
							| 
									
										
										
										
											2025-02-25 12:20:47 +08:00
										 |  |  |     tool_parameters: dict[str, Any] = Field(default_factory=dict) | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |     plugin_unique_identifier: str | None = None | 
					
						
							| 
									
										
										
										
											2025-07-17 17:18:44 +08:00
										 |  |  |     credential_id: str | None = None | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class AgentPromptEntity(BaseModel): | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     Agent Prompt Entity. | 
					
						
							|  |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     first_prompt: str | 
					
						
							|  |  |  |     next_iteration: str | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class AgentScratchpadUnit(BaseModel): | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     Agent First Prompt Entity. | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     class Action(BaseModel): | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         Action Entity. | 
					
						
							|  |  |  |         """
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |         action_name: str | 
					
						
							|  |  |  |         action_input: Union[dict, str] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-11 18:34:17 +08:00
										 |  |  |         def to_dict(self) -> dict: | 
					
						
							|  |  |  |             """
 | 
					
						
							|  |  |  |             Convert to dictionary. | 
					
						
							|  |  |  |             """
 | 
					
						
							|  |  |  |             return { | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  |                 "action": self.action_name, | 
					
						
							|  |  |  |                 "action_input": self.action_input, | 
					
						
							| 
									
										
										
										
											2024-04-11 18:34:17 +08:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     agent_response: Optional[str] = None | 
					
						
							|  |  |  |     thought: Optional[str] = None | 
					
						
							|  |  |  |     action_str: Optional[str] = None | 
					
						
							|  |  |  |     observation: Optional[str] = None | 
					
						
							|  |  |  |     action: Optional[Action] = None | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-11 18:34:17 +08:00
										 |  |  |     def is_final(self) -> bool: | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         Check if the scratchpad unit is final. | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         return self.action is None or ( | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  |             "final" in self.action.action_name.lower() and "answer" in self.action.action_name.lower() | 
					
						
							| 
									
										
										
										
											2024-04-11 18:34:17 +08:00
										 |  |  |         ) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | class AgentEntity(BaseModel): | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     Agent Entity. | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |     class Strategy(StrEnum): | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |         """
 | 
					
						
							|  |  |  |         Agent Strategy. | 
					
						
							|  |  |  |         """
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         CHAIN_OF_THOUGHT = "chain-of-thought" | 
					
						
							|  |  |  |         FUNCTION_CALLING = "function-calling" | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     provider: str | 
					
						
							|  |  |  |     model: str | 
					
						
							|  |  |  |     strategy: Strategy | 
					
						
							|  |  |  |     prompt: Optional[AgentPromptEntity] = None | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |     tools: Optional[list[AgentToolEntity]] = None | 
					
						
							| 
									
										
										
										
											2025-05-29 09:51:56 +08:00
										 |  |  |     max_iteration: int = 10 | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class AgentInvokeMessage(ToolInvokeMessage): | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     Agent Invoke Message. | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     pass |