From aa48a3be3d3a467d374cb3c4cf2ad3856779e283 Mon Sep 17 00:00:00 2001 From: Harrison Chase Date: Mon, 29 Apr 2024 16:37:16 -0700 Subject: [PATCH] cr --- langgraph/prebuilt/chat_agent_executor.py | 15 ++++++++++++++- langgraph/version.py | 1 + 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/langgraph/prebuilt/chat_agent_executor.py b/langgraph/prebuilt/chat_agent_executor.py index 03d90c890..453e06438 100644 --- a/langgraph/prebuilt/chat_agent_executor.py +++ b/langgraph/prebuilt/chat_agent_executor.py @@ -2,7 +2,7 @@ import json from typing import Annotated, Optional, Sequence, TypedDict, Union from langchain_core.language_models import LanguageModelLike -from langchain_core.messages import BaseMessage, FunctionMessage +from langchain_core.messages import BaseMessage, FunctionMessage, SystemMessage from langchain_core.runnables import RunnableLambda from langchain_core.tools import BaseTool from langchain_core.utils.function_calling import convert_to_openai_function @@ -137,6 +137,7 @@ def create_function_calling_executor( def create_tool_calling_executor( model: LanguageModelLike, tools: Union[ToolExecutor, Sequence[BaseTool]], + system_message: Optional[Union[str, SystemMessage]] = None, checkpointer: Optional[BaseCheckpointSaver] = None, interrupt_before: Optional[Sequence[str]] = None, interrupt_after: Optional[Sequence[str]] = None, @@ -147,6 +148,12 @@ def create_tool_calling_executor( Args: model (LanguageModelLike): The chat model that supports OpenAI tool calling. tools (Union[ToolExecutor, Sequence[BaseTool]]): A list of tools or a ToolExecutor instance. + system_message: (Optional[Union[str, SystemMessage]]): An optional system message to pass in + to the model. Is appended at the start of the messages. + checkpointer (Optional[BaseCheckpointSaver]): An optional checkpoint saver object. + interrupt_before (Optional[Sequence[str]]): An optional list of node names to interrupt before. + interrupt_after (Optional[Sequence[str]]): An optional list of node names to interrupt after. + debug (bool): A flag indicating whether to enable debug mode. Returns: Runnable: A compiled LangChain runnable that can be used for chat interactions. @@ -188,6 +195,12 @@ def create_tool_calling_executor( # Define the function that calls the model def call_model(state: AgentState): messages = state["messages"] + if system_message is not None: + if isinstance(system_message, str): + _system_message: BaseMessage = SystemMessage(content=system_message) + else: + _system_message = system_message + messages = [_system_message] + list(messages) response = model.invoke(messages) # We return a list, because this will get added to the existing list return {"messages": [response]} diff --git a/langgraph/version.py b/langgraph/version.py index ac7aeef6f..3368893c0 100644 --- a/langgraph/version.py +++ b/langgraph/version.py @@ -1,4 +1,5 @@ """Main entrypoint into package.""" + from importlib import metadata try: