| 
														 | 
														 | 
														 | 
														 | 
														 | 
													
													
												
													
														 | 
														 | 
														    LLMNodeData, | 
														 | 
														 | 
														    LLMNodeData, | 
													
													
												
													
														 | 
														 | 
														    ModelConfig, | 
														 | 
														 | 
														    ModelConfig, | 
													
													
												
													
														 | 
														 | 
														) | 
														 | 
														 | 
														) | 
													
													
												
													
														 | 
														 | 
														 | 
														 | 
														 | 
														from .exc import ( | 
													
													
												
													
														 | 
														 | 
														 | 
														 | 
														 | 
														    InvalidContextStructureError, | 
													
													
												
													
														 | 
														 | 
														 | 
														 | 
														 | 
														    InvalidVariableTypeError, | 
													
													
												
													
														 | 
														 | 
														 | 
														 | 
														 | 
														    LLMModeRequiredError, | 
													
													
												
													
														 | 
														 | 
														 | 
														 | 
														 | 
														    LLMNodeError, | 
													
													
												
													
														 | 
														 | 
														 | 
														 | 
														 | 
														    ModelNotExistError, | 
													
													
												
													
														 | 
														 | 
														 | 
														 | 
														 | 
														    NoPromptFoundError, | 
													
													
												
													
														 | 
														 | 
														 | 
														 | 
														 | 
														    VariableNotFoundError, | 
													
													
												
													
														 | 
														 | 
														 | 
														 | 
														 | 
														) | 
													
													
												
													
														 | 
														 | 
														
  | 
														 | 
														 | 
														
  | 
													
													
												
													
														 | 
														 | 
														if TYPE_CHECKING: | 
														 | 
														 | 
														if TYPE_CHECKING: | 
													
													
												
													
														 | 
														 | 
														    from core.file.models import File | 
														 | 
														 | 
														    from core.file.models import File | 
													
													
												
											
												
													
														 | 
														 | 
														 | 
														 | 
														 | 
														 | 
													
													
												
													
														 | 
														 | 
														            if self.node_data.memory: | 
														 | 
														 | 
														            if self.node_data.memory: | 
													
													
												
													
														 | 
														 | 
														                query = self.graph_runtime_state.variable_pool.get((SYSTEM_VARIABLE_NODE_ID, SystemVariableKey.QUERY)) | 
														 | 
														 | 
														                query = self.graph_runtime_state.variable_pool.get((SYSTEM_VARIABLE_NODE_ID, SystemVariableKey.QUERY)) | 
													
													
												
													
														 | 
														 | 
														                if not query: | 
														 | 
														 | 
														                if not query: | 
													
													
												
													
														 | 
														 | 
														                    raise ValueError("Query not found") | 
														 | 
														 | 
														 | 
													
													
												
													
														 | 
														 | 
														 | 
														 | 
														 | 
														                    raise VariableNotFoundError("Query not found") | 
													
													
												
													
														 | 
														 | 
														                query = query.text | 
														 | 
														 | 
														                query = query.text | 
													
													
												
													
														 | 
														 | 
														            else: | 
														 | 
														 | 
														            else: | 
													
													
												
													
														 | 
														 | 
														                query = None | 
														 | 
														 | 
														                query = None | 
													
													
												
											
												
													
														 | 
														 | 
														 | 
														 | 
														 | 
														 | 
													
													
												
													
														 | 
														 | 
														                    usage = event.usage | 
														 | 
														 | 
														                    usage = event.usage | 
													
													
												
													
														 | 
														 | 
														                    finish_reason = event.finish_reason | 
														 | 
														 | 
														                    finish_reason = event.finish_reason | 
													
													
												
													
														 | 
														 | 
														                    break | 
														 | 
														 | 
														                    break | 
													
													
												
													
														 | 
														 | 
														        except Exception as e: | 
														 | 
														 | 
														 | 
													
													
												
													
														 | 
														 | 
														 | 
														 | 
														 | 
														        except LLMNodeError as e: | 
													
													
												
													
														 | 
														 | 
														            yield RunCompletedEvent( | 
														 | 
														 | 
														            yield RunCompletedEvent( | 
													
													
												
													
														 | 
														 | 
														                run_result=NodeRunResult( | 
														 | 
														 | 
														                run_result=NodeRunResult( | 
													
													
												
													
														 | 
														 | 
														                    status=WorkflowNodeExecutionStatus.FAILED, | 
														 | 
														 | 
														                    status=WorkflowNodeExecutionStatus.FAILED, | 
													
													
												
											
												
													
														 | 
														 | 
														 | 
														 | 
														 | 
														 | 
													
													
												
													
														 | 
														 | 
														            variable_name = variable_selector.variable | 
														 | 
														 | 
														            variable_name = variable_selector.variable | 
													
													
												
													
														 | 
														 | 
														            variable = self.graph_runtime_state.variable_pool.get(variable_selector.value_selector) | 
														 | 
														 | 
														            variable = self.graph_runtime_state.variable_pool.get(variable_selector.value_selector) | 
													
													
												
													
														 | 
														 | 
														            if variable is None: | 
														 | 
														 | 
														            if variable is None: | 
													
													
												
													
														 | 
														 | 
														                raise ValueError(f"Variable {variable_selector.variable} not found") | 
														 | 
														 | 
														 | 
													
													
												
													
														 | 
														 | 
														 | 
														 | 
														 | 
														                raise VariableNotFoundError(f"Variable {variable_selector.variable} not found") | 
													
													
												
													
														 | 
														 | 
														
  | 
														 | 
														 | 
														
  | 
													
													
												
													
														 | 
														 | 
														            def parse_dict(input_dict: Mapping[str, Any]) -> str: | 
														 | 
														 | 
														            def parse_dict(input_dict: Mapping[str, Any]) -> str: | 
													
													
												
													
														 | 
														 | 
														                """ | 
														 | 
														 | 
														                """ | 
													
													
												
											
												
													
														 | 
														 | 
														 | 
														 | 
														 | 
														 | 
													
													
												
													
														 | 
														 | 
														        for variable_selector in variable_selectors: | 
														 | 
														 | 
														        for variable_selector in variable_selectors: | 
													
													
												
													
														 | 
														 | 
														            variable = self.graph_runtime_state.variable_pool.get(variable_selector.value_selector) | 
														 | 
														 | 
														            variable = self.graph_runtime_state.variable_pool.get(variable_selector.value_selector) | 
													
													
												
													
														 | 
														 | 
														            if variable is None: | 
														 | 
														 | 
														            if variable is None: | 
													
													
												
													
														 | 
														 | 
														                raise ValueError(f"Variable {variable_selector.variable} not found") | 
														 | 
														 | 
														 | 
													
													
												
													
														 | 
														 | 
														 | 
														 | 
														 | 
														                raise VariableNotFoundError(f"Variable {variable_selector.variable} not found") | 
													
													
												
													
														 | 
														 | 
														            if isinstance(variable, NoneSegment): | 
														 | 
														 | 
														            if isinstance(variable, NoneSegment): | 
													
													
												
													
														 | 
														 | 
														                inputs[variable_selector.variable] = "" | 
														 | 
														 | 
														                inputs[variable_selector.variable] = "" | 
													
													
												
													
														 | 
														 | 
														            inputs[variable_selector.variable] = variable.to_object() | 
														 | 
														 | 
														            inputs[variable_selector.variable] = variable.to_object() | 
													
													
												
											
												
													
														 | 
														 | 
														 | 
														 | 
														 | 
														 | 
													
													
												
													
														 | 
														 | 
														            for variable_selector in query_variable_selectors: | 
														 | 
														 | 
														            for variable_selector in query_variable_selectors: | 
													
													
												
													
														 | 
														 | 
														                variable = self.graph_runtime_state.variable_pool.get(variable_selector.value_selector) | 
														 | 
														 | 
														                variable = self.graph_runtime_state.variable_pool.get(variable_selector.value_selector) | 
													
													
												
													
														 | 
														 | 
														                if variable is None: | 
														 | 
														 | 
														                if variable is None: | 
													
													
												
													
														 | 
														 | 
														                    raise ValueError(f"Variable {variable_selector.variable} not found") | 
														 | 
														 | 
														 | 
													
													
												
													
														 | 
														 | 
														 | 
														 | 
														 | 
														                    raise VariableNotFoundError(f"Variable {variable_selector.variable} not found") | 
													
													
												
													
														 | 
														 | 
														                if isinstance(variable, NoneSegment): | 
														 | 
														 | 
														                if isinstance(variable, NoneSegment): | 
													
													
												
													
														 | 
														 | 
														                    continue | 
														 | 
														 | 
														                    continue | 
													
													
												
													
														 | 
														 | 
														                inputs[variable_selector.variable] = variable.to_object() | 
														 | 
														 | 
														                inputs[variable_selector.variable] = variable.to_object() | 
													
													
												
											
												
													
														 | 
														 | 
														 | 
														 | 
														 | 
														 | 
													
													
												
													
														 | 
														 | 
														            return variable.value | 
														 | 
														 | 
														            return variable.value | 
													
													
												
													
														 | 
														 | 
														        elif isinstance(variable, NoneSegment | ArrayAnySegment): | 
														 | 
														 | 
														        elif isinstance(variable, NoneSegment | ArrayAnySegment): | 
													
													
												
													
														 | 
														 | 
														            return [] | 
														 | 
														 | 
														            return [] | 
													
													
												
													
														 | 
														 | 
														        raise ValueError(f"Invalid variable type: {type(variable)}") | 
														 | 
														 | 
														 | 
													
													
												
													
														 | 
														 | 
														 | 
														 | 
														 | 
														        raise InvalidVariableTypeError(f"Invalid variable type: {type(variable)}") | 
													
													
												
													
														 | 
														 | 
														
  | 
														 | 
														 | 
														
  | 
													
													
												
													
														 | 
														 | 
														    def _fetch_context(self, node_data: LLMNodeData): | 
														 | 
														 | 
														    def _fetch_context(self, node_data: LLMNodeData): | 
													
													
												
													
														 | 
														 | 
														        if not node_data.context.enabled: | 
														 | 
														 | 
														        if not node_data.context.enabled: | 
													
													
												
											
												
													
														 | 
														 | 
														 | 
														 | 
														 | 
														 | 
													
													
												
													
														 | 
														 | 
														                        context_str += item + "\n" | 
														 | 
														 | 
														                        context_str += item + "\n" | 
													
													
												
													
														 | 
														 | 
														                    else: | 
														 | 
														 | 
														                    else: | 
													
													
												
													
														 | 
														 | 
														                        if "content" not in item: | 
														 | 
														 | 
														                        if "content" not in item: | 
													
													
												
													
														 | 
														 | 
														                            raise ValueError(f"Invalid context structure: {item}") | 
														 | 
														 | 
														 | 
													
													
												
													
														 | 
														 | 
														 | 
														 | 
														 | 
														                            raise InvalidContextStructureError(f"Invalid context structure: {item}") | 
													
													
												
													
														 | 
														 | 
														
  | 
														 | 
														 | 
														
  | 
													
													
												
													
														 | 
														 | 
														                        context_str += item["content"] + "\n" | 
														 | 
														 | 
														                        context_str += item["content"] + "\n" | 
													
													
												
													
														 | 
														 | 
														
  | 
														 | 
														 | 
														
  | 
													
													
												
											
												
													
														 | 
														 | 
														 | 
														 | 
														 | 
														 | 
													
													
												
													
														 | 
														 | 
														        ) | 
														 | 
														 | 
														        ) | 
													
													
												
													
														 | 
														 | 
														
  | 
														 | 
														 | 
														
  | 
													
													
												
													
														 | 
														 | 
														        if provider_model is None: | 
														 | 
														 | 
														        if provider_model is None: | 
													
													
												
													
														 | 
														 | 
														            raise ValueError(f"Model {model_name} not exist.") | 
														 | 
														 | 
														 | 
													
													
												
													
														 | 
														 | 
														 | 
														 | 
														 | 
														            raise ModelNotExistError(f"Model {model_name} not exist.") | 
													
													
												
													
														 | 
														 | 
														
  | 
														 | 
														 | 
														
  | 
													
													
												
													
														 | 
														 | 
														        if provider_model.status == ModelStatus.NO_CONFIGURE: | 
														 | 
														 | 
														        if provider_model.status == ModelStatus.NO_CONFIGURE: | 
													
													
												
													
														 | 
														 | 
														            raise ProviderTokenNotInitError(f"Model {model_name} credentials is not initialized.") | 
														 | 
														 | 
														            raise ProviderTokenNotInitError(f"Model {model_name} credentials is not initialized.") | 
													
													
												
											
												
													
														 | 
														 | 
														 | 
														 | 
														 | 
														 | 
													
													
												
													
														 | 
														 | 
														        # get model mode | 
														 | 
														 | 
														        # get model mode | 
													
													
												
													
														 | 
														 | 
														        model_mode = node_data_model.mode | 
														 | 
														 | 
														        model_mode = node_data_model.mode | 
													
													
												
													
														 | 
														 | 
														        if not model_mode: | 
														 | 
														 | 
														        if not model_mode: | 
													
													
												
													
														 | 
														 | 
														            raise ValueError("LLM mode is required.") | 
														 | 
														 | 
														 | 
													
													
												
													
														 | 
														 | 
														 | 
														 | 
														 | 
														            raise LLMModeRequiredError("LLM mode is required.") | 
													
													
												
													
														 | 
														 | 
														
  | 
														 | 
														 | 
														
  | 
													
													
												
													
														 | 
														 | 
														        model_schema = model_type_instance.get_model_schema(model_name, model_credentials) | 
														 | 
														 | 
														        model_schema = model_type_instance.get_model_schema(model_name, model_credentials) | 
													
													
												
													
														 | 
														 | 
														
  | 
														 | 
														 | 
														
  | 
													
													
												
													
														 | 
														 | 
														        if not model_schema: | 
														 | 
														 | 
														        if not model_schema: | 
													
													
												
													
														 | 
														 | 
														            raise ValueError(f"Model {model_name} not exist.") | 
														 | 
														 | 
														 | 
													
													
												
													
														 | 
														 | 
														 | 
														 | 
														 | 
														            raise ModelNotExistError(f"Model {model_name} not exist.") | 
													
													
												
													
														 | 
														 | 
														
  | 
														 | 
														 | 
														
  | 
													
													
												
													
														 | 
														 | 
														        return model_instance, ModelConfigWithCredentialsEntity( | 
														 | 
														 | 
														        return model_instance, ModelConfigWithCredentialsEntity( | 
													
													
												
													
														 | 
														 | 
														            provider=provider_name, | 
														 | 
														 | 
														            provider=provider_name, | 
													
													
												
											
												
													
														 | 
														 | 
														 | 
														 | 
														 | 
														 | 
													
													
												
													
														 | 
														 | 
														            filtered_prompt_messages.append(prompt_message) | 
														 | 
														 | 
														            filtered_prompt_messages.append(prompt_message) | 
													
													
												
													
														 | 
														 | 
														
  | 
														 | 
														 | 
														
  | 
													
													
												
													
														 | 
														 | 
														        if not filtered_prompt_messages: | 
														 | 
														 | 
														        if not filtered_prompt_messages: | 
													
													
												
													
														 | 
														 | 
														            raise ValueError( | 
														 | 
														 | 
														 | 
													
													
												
													
														 | 
														 | 
														 | 
														 | 
														 | 
														            raise NoPromptFoundError( | 
													
													
												
													
														 | 
														 | 
														                "No prompt found in the LLM configuration. " | 
														 | 
														 | 
														                "No prompt found in the LLM configuration. " | 
													
													
												
													
														 | 
														 | 
														                "Please ensure a prompt is properly configured before proceeding." | 
														 | 
														 | 
														                "Please ensure a prompt is properly configured before proceeding." | 
													
													
												
													
														 | 
														 | 
														            ) | 
														 | 
														 | 
														            ) | 
													
													
												
											
												
													
														 | 
														 | 
														 | 
														 | 
														 | 
														 | 
													
													
												
													
														 | 
														 | 
														                variable_template_parser = VariableTemplateParser(template=prompt_template.text) | 
														 | 
														 | 
														                variable_template_parser = VariableTemplateParser(template=prompt_template.text) | 
													
													
												
													
														 | 
														 | 
														                variable_selectors = variable_template_parser.extract_variable_selectors() | 
														 | 
														 | 
														                variable_selectors = variable_template_parser.extract_variable_selectors() | 
													
													
												
													
														 | 
														 | 
														        else: | 
														 | 
														 | 
														        else: | 
													
													
												
													
														 | 
														 | 
														            raise ValueError(f"Invalid prompt template type: {type(prompt_template)}") | 
														 | 
														 | 
														 | 
													
													
												
													
														 | 
														 | 
														 | 
														 | 
														 | 
														            raise InvalidVariableTypeError(f"Invalid prompt template type: {type(prompt_template)}") | 
													
													
												
													
														 | 
														 | 
														
  | 
														 | 
														 | 
														
  | 
													
													
												
													
														 | 
														 | 
														        variable_mapping = {} | 
														 | 
														 | 
														        variable_mapping = {} | 
													
													
												
													
														 | 
														 | 
														        for variable_selector in variable_selectors: | 
														 | 
														 | 
														        for variable_selector in variable_selectors: |