Skip to content

Commit

Permalink
added system message support (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
lkuligin authored Apr 9, 2024
1 parent e99df22 commit 2d02b12
Show file tree
Hide file tree
Showing 5 changed files with 275 additions and 246 deletions.
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

0 comments on commit 2d02b12

Please sign in to comment.