entities.py 1.7 KB

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