| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  | import json | 
					
						
							| 
									
										
										
										
											2024-02-01 18:11:57 +08:00
										 |  |  | import logging | 
					
						
							| 
									
										
										
										
											2024-02-20 19:03:43 +08:00
										 |  |  | import uuid | 
					
						
							| 
									
										
										
										
											2024-02-09 15:21:33 +08:00
										 |  |  | from typing import Optional, Union, cast | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | from core.agent.entities import AgentEntity, AgentToolEntity | 
					
						
							| 
									
										
										
										
											2024-04-10 14:48:40 +08:00
										 |  |  | from core.app.app_config.features.file_upload.manager import FileUploadConfigManager | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | from core.app.apps.agent_chat.app_config_manager import AgentChatAppConfig | 
					
						
							|  |  |  | from core.app.apps.base_app_queue_manager import AppQueueManager | 
					
						
							|  |  |  | from core.app.apps.base_app_runner import AppRunner | 
					
						
							|  |  |  | from core.app.entities.app_invoke_entities import ( | 
					
						
							|  |  |  |     AgentChatAppGenerateEntity, | 
					
						
							|  |  |  |     ModelConfigWithCredentialsEntity, | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  | from core.callback_handler.agent_tool_callback_handler import DifyAgentCallbackHandler | 
					
						
							|  |  |  | from core.callback_handler.index_tool_callback_handler import DatasetIndexToolCallbackHandler | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  | from core.file import file_manager | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  | from core.memory.token_buffer_memory import TokenBufferMemory | 
					
						
							| 
									
										
										
										
											2024-02-01 18:11:57 +08:00
										 |  |  | from core.model_manager import ModelInstance | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  | from core.model_runtime.entities import ( | 
					
						
							| 
									
										
										
										
											2024-02-20 19:03:43 +08:00
										 |  |  |     AssistantPromptMessage, | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  |     LLMUsage, | 
					
						
							| 
									
										
										
										
											2024-02-20 19:03:43 +08:00
										 |  |  |     PromptMessage, | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  |     PromptMessageContent, | 
					
						
							| 
									
										
										
										
											2024-02-20 19:03:43 +08:00
										 |  |  |     PromptMessageTool, | 
					
						
							|  |  |  |     SystemPromptMessage, | 
					
						
							| 
									
										
										
										
											2024-04-10 14:48:40 +08:00
										 |  |  |     TextPromptMessageContent, | 
					
						
							| 
									
										
										
										
											2024-02-20 19:03:43 +08:00
										 |  |  |     ToolPromptMessage, | 
					
						
							|  |  |  |     UserPromptMessage, | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2024-11-08 18:13:24 +08:00
										 |  |  | from core.model_runtime.entities.message_entities import ImagePromptMessageContent | 
					
						
							| 
									
										
										
										
											2024-01-30 15:25:37 +08:00
										 |  |  | from core.model_runtime.entities.model_entities import ModelFeature | 
					
						
							|  |  |  | from core.model_runtime.model_providers.__base.large_language_model import LargeLanguageModel | 
					
						
							| 
									
										
										
										
											2024-09-22 03:15:11 +08:00
										 |  |  | from core.prompt.utils.extract_thread_messages import extract_thread_messages | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  | from core.tools.__base.tool import Tool | 
					
						
							| 
									
										
										
										
											2024-02-06 13:21:13 +08:00
										 |  |  | from core.tools.entities.tool_entities import ( | 
					
						
							|  |  |  |     ToolParameter, | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2024-02-01 18:11:57 +08:00
										 |  |  | from core.tools.tool_manager import ToolManager | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  | from core.tools.utils.dataset_retriever_tool import DatasetRetrieverTool | 
					
						
							| 
									
										
										
										
											2024-02-01 18:11:57 +08:00
										 |  |  | from extensions.ext_database import db | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  | from factories import file_factory | 
					
						
							|  |  |  | from models.model import Conversation, Message, MessageAgentThought, MessageFile | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | logger = logging.getLogger(__name__) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 15:00:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | class BaseAgentRunner(AppRunner): | 
					
						
							| 
									
										
										
										
											2024-09-10 15:00:25 +08:00
										 |  |  |     def __init__( | 
					
						
							|  |  |  |         self, | 
					
						
							| 
									
										
										
										
											2024-12-24 18:38:51 +08:00
										 |  |  |         *, | 
					
						
							| 
									
										
										
										
											2024-09-10 15:00:25 +08:00
										 |  |  |         tenant_id: str, | 
					
						
							|  |  |  |         application_generate_entity: AgentChatAppGenerateEntity, | 
					
						
							|  |  |  |         conversation: Conversation, | 
					
						
							|  |  |  |         app_config: AgentChatAppConfig, | 
					
						
							|  |  |  |         model_config: ModelConfigWithCredentialsEntity, | 
					
						
							|  |  |  |         config: AgentEntity, | 
					
						
							|  |  |  |         queue_manager: AppQueueManager, | 
					
						
							|  |  |  |         message: Message, | 
					
						
							|  |  |  |         user_id: str, | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |         model_instance: ModelInstance, | 
					
						
							| 
									
										
										
										
											2024-09-10 15:00:25 +08:00
										 |  |  |         memory: Optional[TokenBufferMemory] = None, | 
					
						
							|  |  |  |         prompt_messages: Optional[list[PromptMessage]] = None, | 
					
						
							|  |  |  |     ) -> None: | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |         self.tenant_id = tenant_id | 
					
						
							|  |  |  |         self.application_generate_entity = application_generate_entity | 
					
						
							| 
									
										
										
										
											2024-04-10 14:48:40 +08:00
										 |  |  |         self.conversation = conversation | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |         self.app_config = app_config | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |         self.model_config = model_config | 
					
						
							|  |  |  |         self.config = config | 
					
						
							|  |  |  |         self.queue_manager = queue_manager | 
					
						
							|  |  |  |         self.message = message | 
					
						
							|  |  |  |         self.user_id = user_id | 
					
						
							|  |  |  |         self.memory = memory | 
					
						
							| 
									
										
										
										
											2024-09-10 15:00:25 +08:00
										 |  |  |         self.history_prompt_messages = self.organize_agent_history(prompt_messages=prompt_messages or []) | 
					
						
							| 
									
										
										
										
											2024-01-30 15:25:37 +08:00
										 |  |  |         self.model_instance = model_instance | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # init callback | 
					
						
							|  |  |  |         self.agent_callback = DifyAgentCallbackHandler() | 
					
						
							|  |  |  |         # init dataset tools | 
					
						
							|  |  |  |         hit_callback = DatasetIndexToolCallbackHandler( | 
					
						
							|  |  |  |             queue_manager=queue_manager, | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |             app_id=self.app_config.app_id, | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |             message_id=message.id, | 
					
						
							|  |  |  |             user_id=user_id, | 
					
						
							|  |  |  |             invoke_from=self.application_generate_entity.invoke_from, | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         self.dataset_tools = DatasetRetrieverTool.get_dataset_tools( | 
					
						
							|  |  |  |             tenant_id=tenant_id, | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |             dataset_ids=app_config.dataset.dataset_ids if app_config.dataset else [], | 
					
						
							|  |  |  |             retrieve_config=app_config.dataset.retrieve_config if app_config.dataset else None, | 
					
						
							|  |  |  |             return_resource=app_config.additional_features.show_retrieve_source, | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |             invoke_from=application_generate_entity.invoke_from, | 
					
						
							| 
									
										
										
										
											2024-09-10 15:00:25 +08:00
										 |  |  |             hit_callback=hit_callback, | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |         ) | 
					
						
							|  |  |  |         # get how many agent thoughts have been created | 
					
						
							| 
									
										
										
										
											2024-09-10 15:00:25 +08:00
										 |  |  |         self.agent_thought_count = ( | 
					
						
							|  |  |  |             db.session.query(MessageAgentThought) | 
					
						
							|  |  |  |             .filter( | 
					
						
							|  |  |  |                 MessageAgentThought.message_id == self.message.id, | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |             .count() | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2024-03-10 15:48:31 +08:00
										 |  |  |         db.session.close() | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-30 15:25:37 +08:00
										 |  |  |         # check if model supports stream tool call | 
					
						
							|  |  |  |         llm_model = cast(LargeLanguageModel, model_instance.model_type_instance) | 
					
						
							|  |  |  |         model_schema = llm_model.get_model_schema(model_instance.model, model_instance.credentials) | 
					
						
							| 
									
										
										
										
											2024-11-21 10:34:43 +08:00
										 |  |  |         features = model_schema.features if model_schema and model_schema.features else [] | 
					
						
							|  |  |  |         self.stream_tool_call = ModelFeature.STREAM_TOOL_CALL in features | 
					
						
							|  |  |  |         self.files = application_generate_entity.files if ModelFeature.VISION in features else [] | 
					
						
							| 
									
										
										
										
											2024-12-24 18:38:51 +08:00
										 |  |  |         self.query: Optional[str] = "" | 
					
						
							| 
									
										
										
										
											2024-05-29 15:25:20 +08:00
										 |  |  |         self._current_thoughts: list[PromptMessage] = [] | 
					
						
							| 
									
										
										
										
											2024-04-10 14:48:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 15:00:25 +08:00
										 |  |  |     def _repack_app_generate_entity( | 
					
						
							|  |  |  |         self, app_generate_entity: AgentChatAppGenerateEntity | 
					
						
							|  |  |  |     ) -> AgentChatAppGenerateEntity: | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |         """
 | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |         Repack app generate entity | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |         """
 | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |         if app_generate_entity.app_config.prompt_template.simple_prompt_template is None: | 
					
						
							| 
									
										
										
										
											2024-09-10 15:00:25 +08:00
										 |  |  |             app_generate_entity.app_config.prompt_template.simple_prompt_template = "" | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |         return app_generate_entity | 
					
						
							| 
									
										
										
										
											2024-09-10 15:00:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-09 15:21:33 +08:00
										 |  |  |     def _convert_tool_to_prompt_message_tool(self, tool: AgentToolEntity) -> tuple[PromptMessageTool, Tool]: | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |         """
 | 
					
						
							| 
									
										
										
										
											2024-09-10 15:00:25 +08:00
										 |  |  |         convert tool to prompt message tool | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |         """
 | 
					
						
							| 
									
										
										
										
											2024-03-08 20:31:13 +08:00
										 |  |  |         tool_entity = ToolManager.get_agent_tool_runtime( | 
					
						
							|  |  |  |             tenant_id=self.tenant_id, | 
					
						
							| 
									
										
										
										
											2024-04-23 15:22:42 +08:00
										 |  |  |             app_id=self.app_config.app_id, | 
					
						
							| 
									
										
										
										
											2024-03-08 20:31:13 +08:00
										 |  |  |             agent_tool=tool, | 
					
						
							| 
									
										
										
										
											2024-09-10 15:00:25 +08:00
										 |  |  |             invoke_from=self.application_generate_entity.invoke_from, | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |         ) | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |         assert tool_entity.entity.description | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |         message_tool = PromptMessageTool( | 
					
						
							|  |  |  |             name=tool.tool_name, | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |             description=tool_entity.entity.description.llm, | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |             parameters={ | 
					
						
							|  |  |  |                 "type": "object", | 
					
						
							|  |  |  |                 "properties": {}, | 
					
						
							|  |  |  |                 "required": [], | 
					
						
							| 
									
										
										
										
											2024-09-10 15:00:25 +08:00
										 |  |  |             }, | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |         parameters = tool_entity.get_merged_runtime_parameters() | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |         for parameter in parameters: | 
					
						
							| 
									
										
										
										
											2024-03-08 20:31:13 +08:00
										 |  |  |             if parameter.form != ToolParameter.ToolParameterForm.LLM: | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  |             parameter_type = parameter.type.as_normal_type() | 
					
						
							| 
									
										
										
										
											2024-10-28 16:52:57 +08:00
										 |  |  |             if parameter.type in { | 
					
						
							|  |  |  |                 ToolParameter.ToolParameterType.SYSTEM_FILES, | 
					
						
							|  |  |  |                 ToolParameter.ToolParameterType.FILE, | 
					
						
							|  |  |  |                 ToolParameter.ToolParameterType.FILES, | 
					
						
							|  |  |  |             }: | 
					
						
							|  |  |  |                 continue | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |             enum = [] | 
					
						
							| 
									
										
										
										
											2024-06-03 21:26:58 +08:00
										 |  |  |             if parameter.type == ToolParameter.ToolParameterType.SELECT: | 
					
						
							| 
									
										
										
										
											2024-12-24 18:38:51 +08:00
										 |  |  |                 enum = [option.value for option in parameter.options] if parameter.options else [] | 
					
						
							| 
									
										
										
										
											2024-06-03 21:26:58 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 15:00:25 +08:00
										 |  |  |             message_tool.parameters["properties"][parameter.name] = { | 
					
						
							| 
									
										
										
										
											2024-03-08 20:31:13 +08:00
										 |  |  |                 "type": parameter_type, | 
					
						
							| 
									
										
										
										
											2024-09-10 15:00:25 +08:00
										 |  |  |                 "description": parameter.llm_description or "", | 
					
						
							| 
									
										
										
										
											2024-03-08 20:31:13 +08:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-08 20:31:13 +08:00
										 |  |  |             if len(enum) > 0: | 
					
						
							| 
									
										
										
										
											2024-09-10 15:00:25 +08:00
										 |  |  |                 message_tool.parameters["properties"][parameter.name]["enum"] = enum | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-08 20:31:13 +08:00
										 |  |  |             if parameter.required: | 
					
						
							| 
									
										
										
										
											2024-09-10 15:00:25 +08:00
										 |  |  |                 message_tool.parameters["required"].append(parameter.name) | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return message_tool, tool_entity | 
					
						
							| 
									
										
										
										
											2024-09-10 15:00:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |     def _convert_dataset_retriever_tool_to_prompt_message_tool(self, tool: DatasetRetrieverTool) -> PromptMessageTool: | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         convert dataset retriever tool to prompt message tool | 
					
						
							|  |  |  |         """
 | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |         assert tool.entity.description | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |         prompt_tool = PromptMessageTool( | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |             name=tool.entity.identity.name, | 
					
						
							|  |  |  |             description=tool.entity.description.llm, | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |             parameters={ | 
					
						
							|  |  |  |                 "type": "object", | 
					
						
							|  |  |  |                 "properties": {}, | 
					
						
							|  |  |  |                 "required": [], | 
					
						
							| 
									
										
										
										
											2024-09-10 15:00:25 +08:00
										 |  |  |             }, | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for parameter in tool.get_runtime_parameters(): | 
					
						
							| 
									
										
										
										
											2024-09-10 15:00:25 +08:00
										 |  |  |             parameter_type = "string" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             prompt_tool.parameters["properties"][parameter.name] = { | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |                 "type": parameter_type, | 
					
						
							| 
									
										
										
										
											2024-09-10 15:00:25 +08:00
										 |  |  |                 "description": parameter.llm_description or "", | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if parameter.required: | 
					
						
							| 
									
										
										
										
											2024-09-10 15:00:25 +08:00
										 |  |  |                 if parameter.name not in prompt_tool.parameters["required"]: | 
					
						
							|  |  |  |                     prompt_tool.parameters["required"].append(parameter.name) | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return prompt_tool | 
					
						
							| 
									
										
										
										
											2024-09-10 15:00:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-24 18:38:51 +08:00
										 |  |  |     def _init_prompt_tools(self) -> tuple[dict[str, Tool], list[PromptMessageTool]]: | 
					
						
							| 
									
										
										
										
											2024-04-11 18:34:17 +08:00
										 |  |  |         """
 | 
					
						
							|  |  |  |         Init tools | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         tool_instances = {} | 
					
						
							|  |  |  |         prompt_messages_tools = [] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-24 18:38:51 +08:00
										 |  |  |         for tool in self.app_config.agent.tools or [] if self.app_config.agent else []: | 
					
						
							| 
									
										
										
										
											2024-04-11 18:34:17 +08:00
										 |  |  |             try: | 
					
						
							|  |  |  |                 prompt_tool, tool_entity = self._convert_tool_to_prompt_message_tool(tool) | 
					
						
							|  |  |  |             except Exception: | 
					
						
							|  |  |  |                 # api tool may be deleted | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  |             # save tool entity | 
					
						
							|  |  |  |             tool_instances[tool.tool_name] = tool_entity | 
					
						
							|  |  |  |             # save prompt tool | 
					
						
							|  |  |  |             prompt_messages_tools.append(prompt_tool) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # convert dataset tools into ModelRuntime Tool format | 
					
						
							|  |  |  |         for dataset_tool in self.dataset_tools: | 
					
						
							|  |  |  |             prompt_tool = self._convert_dataset_retriever_tool_to_prompt_message_tool(dataset_tool) | 
					
						
							|  |  |  |             # save prompt tool | 
					
						
							|  |  |  |             prompt_messages_tools.append(prompt_tool) | 
					
						
							|  |  |  |             # save tool entity | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |             tool_instances[dataset_tool.entity.identity.name] = dataset_tool | 
					
						
							| 
									
										
										
										
											2024-04-11 18:34:17 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return tool_instances, prompt_messages_tools | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |     def update_prompt_message_tool(self, tool: Tool, prompt_tool: PromptMessageTool) -> PromptMessageTool: | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         update prompt message tool | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         # try to get tool runtime parameters | 
					
						
							| 
									
										
										
										
											2024-11-21 10:34:43 +08:00
										 |  |  |         tool_runtime_parameters = tool.get_runtime_parameters() | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         for parameter in tool_runtime_parameters: | 
					
						
							| 
									
										
										
										
											2024-03-08 20:31:13 +08:00
										 |  |  |             if parameter.form != ToolParameter.ToolParameterForm.LLM: | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  |             parameter_type = parameter.type.as_normal_type() | 
					
						
							| 
									
										
										
										
											2024-10-28 16:52:57 +08:00
										 |  |  |             if parameter.type in { | 
					
						
							|  |  |  |                 ToolParameter.ToolParameterType.SYSTEM_FILES, | 
					
						
							|  |  |  |                 ToolParameter.ToolParameterType.FILE, | 
					
						
							|  |  |  |                 ToolParameter.ToolParameterType.FILES, | 
					
						
							|  |  |  |             }: | 
					
						
							|  |  |  |                 continue | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |             enum = [] | 
					
						
							| 
									
										
										
										
											2024-06-03 21:26:58 +08:00
										 |  |  |             if parameter.type == ToolParameter.ToolParameterType.SELECT: | 
					
						
							| 
									
										
										
										
											2024-12-24 18:38:51 +08:00
										 |  |  |                 enum = [option.value for option in parameter.options] if parameter.options else [] | 
					
						
							| 
									
										
										
										
											2024-09-10 15:00:25 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |             prompt_tool.parameters["properties"][parameter.name] = { | 
					
						
							| 
									
										
										
										
											2024-03-08 20:31:13 +08:00
										 |  |  |                 "type": parameter_type, | 
					
						
							| 
									
										
										
										
											2024-09-10 15:00:25 +08:00
										 |  |  |                 "description": parameter.llm_description or "", | 
					
						
							| 
									
										
										
										
											2024-03-08 20:31:13 +08:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-08 20:31:13 +08:00
										 |  |  |             if len(enum) > 0: | 
					
						
							| 
									
										
										
										
											2024-09-10 15:00:25 +08:00
										 |  |  |                 prompt_tool.parameters["properties"][parameter.name]["enum"] = enum | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-08 20:31:13 +08:00
										 |  |  |             if parameter.required: | 
					
						
							| 
									
										
										
										
											2024-09-10 15:00:25 +08:00
										 |  |  |                 if parameter.name not in prompt_tool.parameters["required"]: | 
					
						
							|  |  |  |                     prompt_tool.parameters["required"].append(parameter.name) | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return prompt_tool | 
					
						
							| 
									
										
										
										
											2024-09-10 15:00:25 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def create_agent_thought( | 
					
						
							|  |  |  |         self, message_id: str, message: str, tool_name: str, tool_input: str, messages_ids: list[str] | 
					
						
							|  |  |  |     ) -> MessageAgentThought: | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |         """
 | 
					
						
							|  |  |  |         Create agent thought | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         thought = MessageAgentThought( | 
					
						
							|  |  |  |             message_id=message_id, | 
					
						
							|  |  |  |             message_chain_id=None, | 
					
						
							| 
									
										
										
										
											2024-09-10 15:00:25 +08:00
										 |  |  |             thought="", | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |             tool=tool_name, | 
					
						
							| 
									
										
										
										
											2024-09-10 15:00:25 +08:00
										 |  |  |             tool_labels_str="{}", | 
					
						
							|  |  |  |             tool_meta_str="{}", | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |             tool_input=tool_input, | 
					
						
							|  |  |  |             message=message, | 
					
						
							|  |  |  |             message_token=0, | 
					
						
							|  |  |  |             message_unit_price=0, | 
					
						
							|  |  |  |             message_price_unit=0, | 
					
						
							| 
									
										
										
										
											2024-09-10 15:00:25 +08:00
										 |  |  |             message_files=json.dumps(messages_ids) if messages_ids else "", | 
					
						
							|  |  |  |             answer="", | 
					
						
							|  |  |  |             observation="", | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |             answer_token=0, | 
					
						
							|  |  |  |             answer_unit_price=0, | 
					
						
							|  |  |  |             answer_price_unit=0, | 
					
						
							|  |  |  |             tokens=0, | 
					
						
							|  |  |  |             total_price=0, | 
					
						
							|  |  |  |             position=self.agent_thought_count + 1, | 
					
						
							| 
									
										
										
										
											2024-09-10 15:00:25 +08:00
										 |  |  |             currency="USD", | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |             latency=0, | 
					
						
							| 
									
										
										
										
											2024-09-10 15:00:25 +08:00
										 |  |  |             created_by_role="account", | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |             created_by=self.user_id, | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         db.session.add(thought) | 
					
						
							|  |  |  |         db.session.commit() | 
					
						
							| 
									
										
										
										
											2024-03-10 15:48:31 +08:00
										 |  |  |         db.session.refresh(thought) | 
					
						
							|  |  |  |         db.session.close() | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         self.agent_thought_count += 1 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return thought | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 15:00:25 +08:00
										 |  |  |     def save_agent_thought( | 
					
						
							|  |  |  |         self, | 
					
						
							|  |  |  |         agent_thought: MessageAgentThought, | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |         tool_name: str | None, | 
					
						
							|  |  |  |         tool_input: Union[str, dict, None], | 
					
						
							|  |  |  |         thought: str | None, | 
					
						
							| 
									
										
										
										
											2024-12-24 18:38:51 +08:00
										 |  |  |         observation: Union[str, dict, None], | 
					
						
							|  |  |  |         tool_invoke_meta: Union[str, dict, None], | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |         answer: str | None, | 
					
						
							| 
									
										
										
										
											2024-09-10 15:00:25 +08:00
										 |  |  |         messages_ids: list[str], | 
					
						
							| 
									
										
										
										
											2024-12-24 18:38:51 +08:00
										 |  |  |         llm_usage: LLMUsage | None = None, | 
					
						
							|  |  |  |     ): | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |         """
 | 
					
						
							|  |  |  |         Save agent thought | 
					
						
							|  |  |  |         """
 | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |         updated_agent_thought = ( | 
					
						
							| 
									
										
										
										
											2024-12-24 18:38:51 +08:00
										 |  |  |             db.session.query(MessageAgentThought).filter(MessageAgentThought.id == agent_thought.id).first() | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |         if not updated_agent_thought: | 
					
						
							|  |  |  |             raise ValueError("agent thought not found") | 
					
						
							|  |  |  |         agent_thought = updated_agent_thought | 
					
						
							| 
									
										
										
										
											2024-03-10 15:48:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-29 11:52:12 +08:00
										 |  |  |         if thought: | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |             agent_thought.thought = thought | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-29 11:52:12 +08:00
										 |  |  |         if tool_name: | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |             agent_thought.tool = tool_name | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-29 11:52:12 +08:00
										 |  |  |         if tool_input: | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |             if isinstance(tool_input, dict): | 
					
						
							|  |  |  |                 try: | 
					
						
							|  |  |  |                     tool_input = json.dumps(tool_input, ensure_ascii=False) | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |                 except Exception: | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |                     tool_input = json.dumps(tool_input) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |             updated_agent_thought.tool_input = tool_input | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-29 11:52:12 +08:00
										 |  |  |         if observation: | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |             if isinstance(observation, dict): | 
					
						
							|  |  |  |                 try: | 
					
						
							|  |  |  |                     observation = json.dumps(observation, ensure_ascii=False) | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |                 except Exception: | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |                     observation = json.dumps(observation) | 
					
						
							| 
									
										
										
										
											2024-09-10 15:00:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |             updated_agent_thought.observation = observation | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-29 11:52:12 +08:00
										 |  |  |         if answer: | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |             agent_thought.answer = answer | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if messages_ids is not None and len(messages_ids) > 0: | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |             updated_agent_thought.message_files = json.dumps(messages_ids) | 
					
						
							| 
									
										
										
										
											2024-09-10 15:00:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |         if llm_usage: | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |             updated_agent_thought.message_token = llm_usage.prompt_tokens | 
					
						
							|  |  |  |             updated_agent_thought.message_price_unit = llm_usage.prompt_price_unit | 
					
						
							|  |  |  |             updated_agent_thought.message_unit_price = llm_usage.prompt_unit_price | 
					
						
							|  |  |  |             updated_agent_thought.answer_token = llm_usage.completion_tokens | 
					
						
							|  |  |  |             updated_agent_thought.answer_price_unit = llm_usage.completion_price_unit | 
					
						
							|  |  |  |             updated_agent_thought.answer_unit_price = llm_usage.completion_unit_price | 
					
						
							|  |  |  |             updated_agent_thought.tokens = llm_usage.total_tokens | 
					
						
							|  |  |  |             updated_agent_thought.total_price = llm_usage.total_price | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-24 20:14:45 +08:00
										 |  |  |         # check if tool labels is not empty | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |         labels = updated_agent_thought.tool_labels or {} | 
					
						
							|  |  |  |         tools = updated_agent_thought.tool.split(";") if updated_agent_thought.tool else [] | 
					
						
							| 
									
										
										
										
											2024-01-24 20:14:45 +08:00
										 |  |  |         for tool in tools: | 
					
						
							|  |  |  |             if not tool: | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  |             if tool not in labels: | 
					
						
							|  |  |  |                 tool_label = ToolManager.get_tool_label(tool) | 
					
						
							|  |  |  |                 if tool_label: | 
					
						
							|  |  |  |                     labels[tool] = tool_label.to_dict() | 
					
						
							|  |  |  |                 else: | 
					
						
							| 
									
										
										
										
											2024-09-10 15:00:25 +08:00
										 |  |  |                     labels[tool] = {"en_US": tool, "zh_Hans": tool} | 
					
						
							| 
									
										
										
										
											2024-01-24 20:14:45 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |         updated_agent_thought.tool_labels_str = json.dumps(labels) | 
					
						
							| 
									
										
										
										
											2024-01-24 20:14:45 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |         if tool_invoke_meta is not None: | 
					
						
							|  |  |  |             if isinstance(tool_invoke_meta, dict): | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |                 try: | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |                     tool_invoke_meta = json.dumps(tool_invoke_meta, ensure_ascii=False) | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |                 except Exception: | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |                     tool_invoke_meta = json.dumps(tool_invoke_meta) | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |             updated_agent_thought.tool_meta_str = tool_invoke_meta | 
					
						
							| 
									
										
										
										
											2024-12-24 18:38:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-20 19:03:43 +08:00
										 |  |  |         db.session.commit() | 
					
						
							| 
									
										
										
										
											2024-03-10 15:48:31 +08:00
										 |  |  |         db.session.close() | 
					
						
							| 
									
										
										
										
											2024-02-20 19:03:43 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def organize_agent_history(self, prompt_messages: list[PromptMessage]) -> list[PromptMessage]: | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         Organize agent history | 
					
						
							|  |  |  |         """
 | 
					
						
							| 
									
										
										
										
											2024-12-24 18:38:51 +08:00
										 |  |  |         result: list[PromptMessage] = [] | 
					
						
							| 
									
										
										
										
											2024-02-20 19:03:43 +08:00
										 |  |  |         # check if there is a system message in the beginning of the conversation | 
					
						
							| 
									
										
										
										
											2024-04-10 14:48:40 +08:00
										 |  |  |         for prompt_message in prompt_messages: | 
					
						
							|  |  |  |             if isinstance(prompt_message, SystemPromptMessage): | 
					
						
							|  |  |  |                 result.append(prompt_message) | 
					
						
							| 
									
										
										
										
											2024-02-20 19:03:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 15:00:25 +08:00
										 |  |  |         messages: list[Message] = ( | 
					
						
							|  |  |  |             db.session.query(Message) | 
					
						
							|  |  |  |             .filter( | 
					
						
							|  |  |  |                 Message.conversation_id == self.message.conversation_id, | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2024-09-22 03:15:11 +08:00
										 |  |  |             .order_by(Message.created_at.desc()) | 
					
						
							| 
									
										
										
										
											2024-09-10 15:00:25 +08:00
										 |  |  |             .all() | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2024-02-20 19:03:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-22 03:15:11 +08:00
										 |  |  |         messages = list(reversed(extract_thread_messages(messages))) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-20 19:03:43 +08:00
										 |  |  |         for message in messages: | 
					
						
							| 
									
										
										
										
											2024-04-10 14:48:40 +08:00
										 |  |  |             if message.id == self.message.id: | 
					
						
							|  |  |  |                 continue | 
					
						
							| 
									
										
										
										
											2024-05-29 15:25:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-10 14:48:40 +08:00
										 |  |  |             result.append(self.organize_agent_user_prompt(message)) | 
					
						
							| 
									
										
										
										
											2024-02-20 19:03:43 +08:00
										 |  |  |             agent_thoughts: list[MessageAgentThought] = message.agent_thoughts | 
					
						
							| 
									
										
										
										
											2024-02-27 19:15:07 +08:00
										 |  |  |             if agent_thoughts: | 
					
						
							|  |  |  |                 for agent_thought in agent_thoughts: | 
					
						
							|  |  |  |                     tools = agent_thought.tool | 
					
						
							|  |  |  |                     if tools: | 
					
						
							| 
									
										
										
										
											2024-09-10 15:00:25 +08:00
										 |  |  |                         tools = tools.split(";") | 
					
						
							| 
									
										
										
										
											2024-02-27 19:15:07 +08:00
										 |  |  |                         tool_calls: list[AssistantPromptMessage.ToolCall] = [] | 
					
						
							|  |  |  |                         tool_call_response: list[ToolPromptMessage] = [] | 
					
						
							| 
									
										
										
										
											2024-03-26 11:05:10 +08:00
										 |  |  |                         try: | 
					
						
							|  |  |  |                             tool_inputs = json.loads(agent_thought.tool_input) | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |                         except Exception: | 
					
						
							| 
									
										
										
										
											2024-09-10 15:00:25 +08:00
										 |  |  |                             tool_inputs = {tool: {} for tool in tools} | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |                         try: | 
					
						
							|  |  |  |                             tool_responses = json.loads(agent_thought.observation) | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |                         except Exception: | 
					
						
							| 
									
										
										
										
											2024-08-19 09:21:11 +08:00
										 |  |  |                             tool_responses = dict.fromkeys(tools, agent_thought.observation) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-27 19:15:07 +08:00
										 |  |  |                         for tool in tools: | 
					
						
							|  |  |  |                             # generate a uuid for tool call | 
					
						
							|  |  |  |                             tool_call_id = str(uuid.uuid4()) | 
					
						
							| 
									
										
										
										
											2024-09-10 15:00:25 +08:00
										 |  |  |                             tool_calls.append( | 
					
						
							|  |  |  |                                 AssistantPromptMessage.ToolCall( | 
					
						
							|  |  |  |                                     id=tool_call_id, | 
					
						
							|  |  |  |                                     type="function", | 
					
						
							|  |  |  |                                     function=AssistantPromptMessage.ToolCall.ToolCallFunction( | 
					
						
							|  |  |  |                                         name=tool, | 
					
						
							|  |  |  |                                         arguments=json.dumps(tool_inputs.get(tool, {})), | 
					
						
							|  |  |  |                                     ), | 
					
						
							|  |  |  |                                 ) | 
					
						
							|  |  |  |                             ) | 
					
						
							|  |  |  |                             tool_call_response.append( | 
					
						
							|  |  |  |                                 ToolPromptMessage( | 
					
						
							|  |  |  |                                     content=tool_responses.get(tool, agent_thought.observation), | 
					
						
							| 
									
										
										
										
											2024-02-27 19:15:07 +08:00
										 |  |  |                                     name=tool, | 
					
						
							| 
									
										
										
										
											2024-09-10 15:00:25 +08:00
										 |  |  |                                     tool_call_id=tool_call_id, | 
					
						
							| 
									
										
										
										
											2024-02-27 19:15:07 +08:00
										 |  |  |                                 ) | 
					
						
							| 
									
										
										
										
											2024-09-10 15:00:25 +08:00
										 |  |  |                             ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                         result.extend( | 
					
						
							|  |  |  |                             [ | 
					
						
							|  |  |  |                                 AssistantPromptMessage( | 
					
						
							|  |  |  |                                     content=agent_thought.thought, | 
					
						
							|  |  |  |                                     tool_calls=tool_calls, | 
					
						
							|  |  |  |                                 ), | 
					
						
							|  |  |  |                                 *tool_call_response, | 
					
						
							|  |  |  |                             ] | 
					
						
							|  |  |  |                         ) | 
					
						
							| 
									
										
										
										
											2024-02-27 19:15:07 +08:00
										 |  |  |                     if not tools: | 
					
						
							|  |  |  |                         result.append(AssistantPromptMessage(content=agent_thought.thought)) | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 if message.answer: | 
					
						
							|  |  |  |                     result.append(AssistantPromptMessage(content=message.answer)) | 
					
						
							| 
									
										
										
										
											2024-02-20 19:03:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-10 15:48:31 +08:00
										 |  |  |         db.session.close() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-26 11:05:10 +08:00
										 |  |  |         return result | 
					
						
							| 
									
										
										
										
											2024-04-10 14:48:40 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def organize_agent_user_prompt(self, message: Message) -> UserPromptMessage: | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  |         files = db.session.query(MessageFile).filter(MessageFile.message_id == message.id).all() | 
					
						
							| 
									
										
										
										
											2024-11-08 18:13:24 +08:00
										 |  |  |         if not files: | 
					
						
							|  |  |  |             return UserPromptMessage(content=message.query) | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |         if message.app_model_config: | 
					
						
							|  |  |  |             file_extra_config = FileUploadConfigManager.convert(message.app_model_config.to_dict()) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             file_extra_config = None | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-08 18:13:24 +08:00
										 |  |  |         if not file_extra_config: | 
					
						
							|  |  |  |             return UserPromptMessage(content=message.query) | 
					
						
							| 
									
										
										
										
											2024-04-10 14:48:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-08 18:13:24 +08:00
										 |  |  |         image_detail_config = file_extra_config.image_config.detail if file_extra_config.image_config else None | 
					
						
							|  |  |  |         image_detail_config = image_detail_config or ImagePromptMessageContent.DETAIL.LOW | 
					
						
							| 
									
										
										
										
											2024-04-10 14:48:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-08 18:13:24 +08:00
										 |  |  |         file_objs = file_factory.build_from_message_files( | 
					
						
							|  |  |  |             message_files=files, tenant_id=self.tenant_id, config=file_extra_config | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         if not file_objs: | 
					
						
							| 
									
										
										
										
											2024-04-10 14:48:40 +08:00
										 |  |  |             return UserPromptMessage(content=message.query) | 
					
						
							| 
									
										
										
										
											2024-11-08 18:13:24 +08:00
										 |  |  |         prompt_message_contents: list[PromptMessageContent] = [] | 
					
						
							|  |  |  |         prompt_message_contents.append(TextPromptMessageContent(data=message.query)) | 
					
						
							|  |  |  |         for file in file_objs: | 
					
						
							|  |  |  |             prompt_message_contents.append( | 
					
						
							|  |  |  |                 file_manager.to_prompt_message_content( | 
					
						
							|  |  |  |                     file, | 
					
						
							|  |  |  |                     image_detail_config=image_detail_config, | 
					
						
							|  |  |  |                 ) | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |         return UserPromptMessage(content=prompt_message_contents) |