entities.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. from enum import StrEnum
  2. from typing import Any, Optional, Union
  3. from pydantic import BaseModel, Field
  4. from core.tools.entities.tool_entities import ToolInvokeMessage, ToolProviderType
  5. class AgentToolEntity(BaseModel):
  6. """
  7. Agent Tool Entity.
  8. """
  9. provider_type: ToolProviderType
  10. provider_id: str
  11. tool_name: str
  12. tool_parameters: dict[str, Any] = Field(default_factory=dict)
  13. plugin_unique_identifier: str | None = None
  14. class AgentPromptEntity(BaseModel):
  15. """
  16. Agent Prompt Entity.
  17. """
  18. first_prompt: str
  19. next_iteration: str
  20. class AgentScratchpadUnit(BaseModel):
  21. """
  22. Agent First Prompt Entity.
  23. """
  24. class Action(BaseModel):
  25. """
  26. Action Entity.
  27. """
  28. action_name: str
  29. action_input: Union[dict, str]
  30. def to_dict(self) -> dict:
  31. """
  32. Convert to dictionary.
  33. """
  34. return {
  35. "action": self.action_name,
  36. "action_input": self.action_input,
  37. }
  38. agent_response: Optional[str] = None
  39. thought: Optional[str] = None
  40. action_str: Optional[str] = None
  41. observation: Optional[str] = None
  42. action: Optional[Action] = None
  43. def is_final(self) -> bool:
  44. """
  45. Check if the scratchpad unit is final.
  46. """
  47. return self.action is None or (
  48. "final" in self.action.action_name.lower() and "answer" in self.action.action_name.lower()
  49. )
  50. class AgentEntity(BaseModel):
  51. """
  52. Agent Entity.
  53. """
  54. class Strategy(StrEnum):
  55. """
  56. Agent Strategy.
  57. """
  58. CHAIN_OF_THOUGHT = "chain-of-thought"
  59. FUNCTION_CALLING = "function-calling"
  60. provider: str
  61. model: str
  62. strategy: Strategy
  63. prompt: Optional[AgentPromptEntity] = None
  64. tools: Optional[list[AgentToolEntity]] = None
  65. max_iteration: int = 5
  66. class AgentInvokeMessage(ToolInvokeMessage):
  67. """
  68. Agent Invoke Message.
  69. """
  70. pass