@ -1,20 +1,21 @@
from collections . abc import Generator , Sequence
from collections . abc import Generator , Sequence
from typing import Optional , Union
from typing import Union
from langchain import PromptTemplate
from langchain . agents . structured_chat . base import HUMAN_MESSAGE_TEMPLATE
from langchain . agents . structured_chat . prompt import PREFIX , SUFFIX
from langchain . schema import AgentAction
from core . app . entities . app_invoke_entities import ModelConfigWithCredentialsEntity
from core . app . entities . app_invoke_entities import ModelConfigWithCredentialsEntity
from core . model_manager import ModelInstance
from core . model_manager import ModelInstance
from core . model_runtime . entities . llm_entities import LLMUsage
from core . model_runtime . entities . llm_entities import LLMUsage
from core . model_runtime . entities . message_entities import PromptMessage , PromptMessageRole , PromptMessageTool
from core . model_runtime . entities . message_entities import PromptMessage , PromptMessageRole , PromptMessageTool
from core . prompt . advanced_prompt_transform import AdvancedPromptTransform
from core . prompt . advanced_prompt_transform import AdvancedPromptTransform
from core . prompt . entities . advanced_prompt_entities import ChatModelMessage
from core . prompt . entities . advanced_prompt_entities import ChatModelMessage , CompletionModelPromptTemplate
from core . rag . retrieval . output_parser . react_output import ReactAction
from core . rag . retrieval . output_parser . structured_chat import StructuredChatOutputParser
from core . rag . retrieval . output_parser . structured_chat import StructuredChatOutputParser
from core . workflow . nodes . llm . llm_node import LLMNode
from core . workflow . nodes . llm . llm_node import LLMNode
PREFIX = """ Respond to the human as helpfully and accurately as possible. You have access to the following tools: """
SUFFIX = """ Begin! Reminder to ALWAYS respond with a valid json blob of a single action. Use tools if necessary. Respond directly if appropriate. Format is Action:```$JSON_BLOB```then Observation:.
Thought : """
FORMAT_INSTRUCTIONS = """ Use a json blob to specify a tool by providing an action key (tool name) and an action_input key (tool input).
FORMAT_INSTRUCTIONS = """ Use a json blob to specify a tool by providing an action key (tool name) and an action_input key (tool input).
The nouns in the format of " Thought " , " Action " , " Action Input " , " Final Answer " must be expressed in English .
The nouns in the format of " Thought " , " Action " , " Action Input " , " Final Answer " must be expressed in English .
Valid " action " values : " Final Answer " or { tool_names }
Valid " action " values : " Final Answer " or { tool_names }
@ -86,7 +87,6 @@ class ReactMultiDatasetRouter:
tenant_id : str ,
tenant_id : str ,
prefix : str = PREFIX ,
prefix : str = PREFIX ,
suffix : str = SUFFIX ,
suffix : str = SUFFIX ,
human_message_template : str = HUMAN_MESSAGE_TEMPLATE ,
format_instructions : str = FORMAT_INSTRUCTIONS ,
format_instructions : str = FORMAT_INSTRUCTIONS ,
) - > Union [ str , None ] :
) - > Union [ str , None ] :
if model_config . mode == " chat " :
if model_config . mode == " chat " :
@ -95,7 +95,6 @@ class ReactMultiDatasetRouter:
tools = tools ,
tools = tools ,
prefix = prefix ,
prefix = prefix ,
suffix = suffix ,
suffix = suffix ,
human_message_template = human_message_template ,
format_instructions = format_instructions ,
format_instructions = format_instructions ,
)
)
else :
else :
@ -103,7 +102,6 @@ class ReactMultiDatasetRouter:
tools = tools ,
tools = tools ,
prefix = prefix ,
prefix = prefix ,
format_instructions = format_instructions ,
format_instructions = format_instructions ,
input_variables = None
)
)
stop = [ ' Observation: ' ]
stop = [ ' Observation: ' ]
# handle invoke result
# handle invoke result
@ -127,9 +125,9 @@ class ReactMultiDatasetRouter:
tenant_id = tenant_id
tenant_id = tenant_id
)
)
output_parser = StructuredChatOutputParser ( )
output_parser = StructuredChatOutputParser ( )
agen t_decision = output_parser . parse ( result_text )
reac t_decision = output_parser . parse ( result_text )
if isinstance ( agent_decision, Agen tAction) :
if isinstance ( react_decision, Reac tAction) :
return agen t_decision. tool
return reac t_decision. tool
return None
return None
def _invoke_llm ( self , completion_param : dict ,
def _invoke_llm ( self , completion_param : dict ,
@ -139,7 +137,6 @@ class ReactMultiDatasetRouter:
) - > tuple [ str , LLMUsage ] :
) - > tuple [ str , LLMUsage ] :
"""
"""
Invoke large language model
Invoke large language model
: param node_data : node data
: param model_instance : model instance
: param model_instance : model instance
: param prompt_messages : prompt messages
: param prompt_messages : prompt messages
: param stop : stop
: param stop : stop
@ -197,7 +194,6 @@ class ReactMultiDatasetRouter:
tools : Sequence [ PromptMessageTool ] ,
tools : Sequence [ PromptMessageTool ] ,
prefix : str = PREFIX ,
prefix : str = PREFIX ,
suffix : str = SUFFIX ,
suffix : str = SUFFIX ,
human_message_template : str = HUMAN_MESSAGE_TEMPLATE ,
format_instructions : str = FORMAT_INSTRUCTIONS ,
format_instructions : str = FORMAT_INSTRUCTIONS ,
) - > list [ ChatModelMessage ] :
) - > list [ ChatModelMessage ] :
tool_strings = [ ]
tool_strings = [ ]
@ -227,16 +223,13 @@ class ReactMultiDatasetRouter:
tools : Sequence [ PromptMessageTool ] ,
tools : Sequence [ PromptMessageTool ] ,
prefix : str = PREFIX ,
prefix : str = PREFIX ,
format_instructions : str = FORMAT_INSTRUCTIONS ,
format_instructions : str = FORMAT_INSTRUCTIONS ,
input_variables : Optional [ list [ str ] ] = None ,
) - > CompletionModelPromptTemplate :
) - > PromptTemplate :
""" Create prompt in the style of the zero shot agent.
""" Create prompt in the style of the zero shot agent.
Args :
Args :
tools : List of tools the agent will have access to , used to format the
tools : List of tools the agent will have access to , used to format the
prompt .
prompt .
prefix : String to put before the list of tools .
prefix : String to put before the list of tools .
input_variables : List of input variables the final prompt will expect .
Returns :
Returns :
A PromptTemplate with the template assembled from the pieces here .
A PromptTemplate with the template assembled from the pieces here .
"""
"""
@ -249,6 +242,4 @@ Thought: {agent_scratchpad}
tool_names = " , " . join ( [ tool . name for tool in tools ] )
tool_names = " , " . join ( [ tool . name for tool in tools ] )
format_instructions = format_instructions . format ( tool_names = tool_names )
format_instructions = format_instructions . format ( tool_names = tool_names )
template = " \n \n " . join ( [ prefix , tool_strings , format_instructions , suffix ] )
template = " \n \n " . join ( [ prefix , tool_strings , format_instructions , suffix ] )
if input_variables is None :
return CompletionModelPromptTemplate ( text = template )
input_variables = [ " input " , " agent_scratchpad " ]
return PromptTemplate ( template = template , input_variables = input_variables )