agent_service.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import pytz
  2. from flask_login import current_user
  3. from core.app.app_config.easy_ui_based_app.agent.manager import AgentConfigManager
  4. from core.tools.tool_manager import ToolManager
  5. from extensions.ext_database import db
  6. from models.account import Account
  7. from models.model import App, Conversation, EndUser, Message, MessageAgentThought
  8. class AgentService:
  9. @classmethod
  10. def get_agent_logs(cls, app_model: App, conversation_id: str, message_id: str) -> dict:
  11. """
  12. Service to get agent logs
  13. """
  14. conversation: Conversation = (
  15. db.session.query(Conversation)
  16. .filter(
  17. Conversation.id == conversation_id,
  18. Conversation.app_id == app_model.id,
  19. )
  20. .first()
  21. )
  22. if not conversation:
  23. raise ValueError(f"Conversation not found: {conversation_id}")
  24. message: Message = (
  25. db.session.query(Message)
  26. .filter(
  27. Message.id == message_id,
  28. Message.conversation_id == conversation_id,
  29. )
  30. .first()
  31. )
  32. if not message:
  33. raise ValueError(f"Message not found: {message_id}")
  34. agent_thoughts: list[MessageAgentThought] = message.agent_thoughts
  35. if conversation.from_end_user_id:
  36. # only select name field
  37. executor = (
  38. db.session.query(EndUser, EndUser.name).filter(EndUser.id == conversation.from_end_user_id).first()
  39. )
  40. else:
  41. executor = (
  42. db.session.query(Account, Account.name).filter(Account.id == conversation.from_account_id).first()
  43. )
  44. if executor:
  45. executor = executor.name
  46. else:
  47. executor = "Unknown"
  48. timezone = pytz.timezone(current_user.timezone)
  49. result = {
  50. "meta": {
  51. "status": "success",
  52. "executor": executor,
  53. "start_time": message.created_at.astimezone(timezone).isoformat(),
  54. "elapsed_time": message.provider_response_latency,
  55. "total_tokens": message.answer_tokens + message.message_tokens,
  56. "agent_mode": app_model.app_model_config.agent_mode_dict.get("strategy", "react"),
  57. "iterations": len(agent_thoughts),
  58. },
  59. "iterations": [],
  60. "files": message.message_files,
  61. }
  62. agent_config = AgentConfigManager.convert(app_model.app_model_config.to_dict())
  63. agent_tools = agent_config.tools
  64. def find_agent_tool(tool_name: str):
  65. for agent_tool in agent_tools:
  66. if agent_tool.tool_name == tool_name:
  67. return agent_tool
  68. for agent_thought in agent_thoughts:
  69. tools = agent_thought.tools
  70. tool_labels = agent_thought.tool_labels
  71. tool_meta = agent_thought.tool_meta
  72. tool_inputs = agent_thought.tool_inputs_dict
  73. tool_outputs = agent_thought.tool_outputs_dict
  74. tool_calls = []
  75. for tool in tools:
  76. tool_name = tool
  77. tool_label = tool_labels.get(tool_name, tool_name)
  78. tool_input = tool_inputs.get(tool_name, {})
  79. tool_output = tool_outputs.get(tool_name, {})
  80. tool_meta_data = tool_meta.get(tool_name, {})
  81. tool_config = tool_meta_data.get("tool_config", {})
  82. if tool_config.get("tool_provider_type", "") != "dataset-retrieval":
  83. tool_icon = ToolManager.get_tool_icon(
  84. tenant_id=app_model.tenant_id,
  85. provider_type=tool_config.get("tool_provider_type", ""),
  86. provider_id=tool_config.get("tool_provider", ""),
  87. )
  88. if not tool_icon:
  89. tool_entity = find_agent_tool(tool_name)
  90. if tool_entity:
  91. tool_icon = ToolManager.get_tool_icon(
  92. tenant_id=app_model.tenant_id,
  93. provider_type=tool_entity.provider_type,
  94. provider_id=tool_entity.provider_id,
  95. )
  96. else:
  97. tool_icon = ""
  98. tool_calls.append(
  99. {
  100. "status": "success" if not tool_meta_data.get("error") else "error",
  101. "error": tool_meta_data.get("error"),
  102. "time_cost": tool_meta_data.get("time_cost", 0),
  103. "tool_name": tool_name,
  104. "tool_label": tool_label,
  105. "tool_input": tool_input,
  106. "tool_output": tool_output,
  107. "tool_parameters": tool_meta_data.get("tool_parameters", {}),
  108. "tool_icon": tool_icon,
  109. }
  110. )
  111. result["iterations"].append(
  112. {
  113. "tokens": agent_thought.tokens,
  114. "tool_calls": tool_calls,
  115. "tool_raw": {
  116. "inputs": agent_thought.tool_input,
  117. "outputs": agent_thought.observation,
  118. },
  119. "thought": agent_thought.thought,
  120. "created_at": agent_thought.created_at.isoformat(),
  121. "files": agent_thought.files,
  122. }
  123. )
  124. return result