Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

openai: developer message just openai #28791

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 30 additions & 5 deletions libs/partners/openai/langchain_openai/chat_models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def _convert_dict_to_message(_dict: Mapping[str, Any]) -> BaseMessage:
tool_calls=tool_calls,
invalid_tool_calls=invalid_tool_calls,
)
elif role == "system":
elif role == "system" or role == "developer":
return SystemMessage(content=_dict.get("content", ""), name=name, id=id_)
elif role == "function":
return FunctionMessage(
Expand Down Expand Up @@ -180,7 +180,9 @@ def _format_message_content(content: Any) -> Any:
return formatted_content


def _convert_message_to_dict(message: BaseMessage) -> dict:
def _convert_message_to_dict(
message: BaseMessage, *, system_message_role: str = "system"
) -> dict:
"""Convert a LangChain message to a dictionary.

Args:
Expand Down Expand Up @@ -233,7 +235,7 @@ def _convert_message_to_dict(message: BaseMessage) -> dict:
)
message_dict["audio"] = audio
elif isinstance(message, SystemMessage):
message_dict["role"] = "system"
message_dict["role"] = system_message_role
elif isinstance(message, FunctionMessage):
message_dict["role"] = "function"
elif isinstance(message, ToolMessage):
Expand Down Expand Up @@ -482,6 +484,7 @@ class BaseChatOpenAI(BaseChatModel):
However this does not prevent a user from directly passed in the parameter during
invocation.
"""
system_message_role: str = "system"

model_config = ConfigDict(populate_by_name=True)

Expand All @@ -493,6 +496,20 @@ def build_extra(cls, values: Dict[str, Any]) -> Any:
values = _build_model_kwargs(values, all_required_field_names)
return values

@model_validator(mode="before")
@classmethod
def validate_system_message_role(cls, values: Dict[str, Any]) -> Any:
"""Ensure that the system message role is correctly set for the model."""
if "system_message_role" in values:
return values

model = values.get("model_name") or values.get("model") or ""
if model.startswith("o1"):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this change is not future compatible -- it'll force users to update their client when a new model gets released (or update their code w/ system_message_role attribute)

values["system_message_role"] = "developer"
# otherwise default is "system"

return values

@model_validator(mode="before")
@classmethod
def validate_temperature(cls, values: Dict[str, Any]) -> Any:
Expand Down Expand Up @@ -701,7 +718,12 @@ def _get_request_payload(
kwargs["stop"] = stop

return {
"messages": [_convert_message_to_dict(m) for m in messages],
"messages": [
_convert_message_to_dict(
m, system_message_role=self.system_message_role
)
for m in messages
],
**self._default_params,
**kwargs,
}
Expand Down Expand Up @@ -936,7 +958,10 @@ def get_num_tokens_from_messages(
" for information on how messages are converted to tokens."
)
num_tokens = 0
messages_dict = [_convert_message_to_dict(m) for m in messages]
messages_dict = [
_convert_message_to_dict(m, system_message_role=self.system_message_role)
for m in messages
]
for message in messages_dict:
num_tokens += tokens_per_message
for key, value in message.items():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1097,3 +1097,31 @@ def test_o1_max_tokens() -> None:
"how are you"
)
assert isinstance(response, AIMessage)


@pytest.mark.parametrize(
"model",
[
"gpt-4o",
"gpt-4o-mini",
"o1",
# "o1-mini", neither supported
"gpt-3.5-turbo",
],
)
@pytest.mark.parametrize("role", ["system", "developer", None])
def test_system_message_roles(model: str, role: Optional[str]) -> None:
init_kwargs = {"model": model}
if role is not None:
init_kwargs["system_message_role"] = role
llm = ChatOpenAI(**init_kwargs) # type: ignore[arg-type]
if role is None:
if model.startswith("o1"):
assert llm.system_message_role == "developer"
else:
assert llm.system_message_role == "system"
history = [SystemMessage("You talk like a pirate"), HumanMessage("Hello there")]

out = llm.invoke(history)
assert isinstance(out, AIMessage)
assert out.content
Loading