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

added system message support #128

Merged
merged 1 commit into from
Apr 9, 2024
Merged
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
42 changes: 14 additions & 28 deletions libs/genai/langchain_google_genai/chat_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import json
import logging
import os
import warnings
from io import BytesIO
from typing import (
Any,
Expand Down Expand Up @@ -300,27 +301,16 @@ def _convert_to_parts(

def _parse_chat_history(
input_messages: Sequence[BaseMessage], convert_system_message_to_human: bool = False
) -> List[genai.types.ContentDict]:
) -> Tuple[Optional[genai.types.ContentDict], List[genai.types.ContentDict]]:
messages: List[genai.types.MessageDict] = []

raw_system_message: Optional[SystemMessage] = None
for i, message in enumerate(input_messages):
if (
i == 0
and isinstance(message, SystemMessage)
and not convert_system_message_to_human
):
raise ValueError(
"""SystemMessages are not yet supported!

To automatically convert the leading SystemMessage to a HumanMessage,
set `convert_system_message_to_human` to True. Example:
if convert_system_message_to_human:
warnings.warn("Convert_system_message_to_human will be deprecated!")

llm = ChatGoogleGenerativeAI(model="gemini-pro", convert_system_message_to_human=True)
"""
)
elif i == 0 and isinstance(message, SystemMessage):
raw_system_message = message
system_instruction: Optional[genai.types.ContentDict] = None
for i, message in enumerate(input_messages):
if i == 0 and isinstance(message, SystemMessage):
system_instruction = _convert_to_parts(message.content)
continue
elif isinstance(message, AIMessage):
role = "model"
Expand Down Expand Up @@ -365,16 +355,8 @@ def _parse_chat_history(
f"Unexpected message with type {type(message)} at the position {i}."
)

if raw_system_message:
if role == "model":
raise ValueError(
"SystemMessage should be followed by a HumanMessage and "
"not by AIMessage."
)
parts = _convert_to_parts(raw_system_message.content) + parts
raw_system_message = None
messages.append({"role": role, "parts": parts})
return messages
return system_instruction, messages


def _parse_response_candidate(
Expand Down Expand Up @@ -659,11 +641,15 @@ def _prepare_chat(
)

params = self._prepare_params(stop, **kwargs)
history = _parse_chat_history(
system_instruction, history = _parse_chat_history(
messages,
convert_system_message_to_human=self.convert_system_message_to_human,
)
message = history.pop()
if self.client._system_instruction != system_instruction:
self.client = genai.GenerativeModel(
model_name=self.model, system_instruction=system_instruction
)
chat = client.start_chat(history=history)
return params, chat, message

Expand Down
Loading
Loading