Skip to content

Commit

Permalink
fixed system instruction (#201)
Browse files Browse the repository at this point in the history
  • Loading branch information
lkuligin authored Apr 30, 2024
1 parent bfb6eb9 commit 60331a9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
6 changes: 5 additions & 1 deletion libs/genai/langchain_google_genai/chat_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -772,10 +772,14 @@ def _prepare_chat(
convert_system_message_to_human=self.convert_system_message_to_human,
)
message = history.pop()
if self.client._system_instruction != system_instruction:
if (
self.client._system_instruction != system_instruction
and not self.convert_system_message_to_human
):
self.client = genai.GenerativeModel(
model_name=self.model, system_instruction=system_instruction
)
client = self.client
chat = client.start_chat(history=history)
return params, chat, message

Expand Down
24 changes: 12 additions & 12 deletions libs/genai/tests/integration_tests/test_chat_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,6 @@ def test_chat_google_genai_invoke_multimodal() -> None:
assert len(chunk.content.strip()) > 0


def test_system_message() -> None:
messages = [
SystemMessage(content="Be a helful assistant."),
HumanMessage(content="Hi, how are you?"),
]
llm = ChatGoogleGenerativeAI(model="models/gemini-1.0-pro-latest")
answer = llm.invoke(messages)
assert isinstance(answer.content, str)


def test_chat_google_genai_invoke_multimodal_too_many_messages() -> None:
# Only supports 1 turn...
messages: list = [
Expand Down Expand Up @@ -188,8 +178,17 @@ def test_chat_google_genai_single_call_with_history() -> None:
assert isinstance(response.content, str)


def test_chat_google_genai_system_message() -> None:
model = ChatGoogleGenerativeAI(model=_MODEL, convert_system_message_to_human=True)
@pytest.mark.parametrize(
"model_name,convert_system_message_to_human",
[(_MODEL, True), ("models/gemini-1.5-pro-latest", False)],
)
def test_chat_google_genai_system_message(
model_name: str, convert_system_message_to_human: bool
) -> None:
model = ChatGoogleGenerativeAI(
model=model_name,
convert_system_message_to_human=convert_system_message_to_human,
)
text_question1, text_answer1 = "How much is 2+2?", "4"
text_question2 = "How much is 3+3?"
system_message = SystemMessage(content="You're supposed to answer math questions.")
Expand Down Expand Up @@ -236,6 +235,7 @@ def test_safety_settings_gemini() -> None:
assert len(out2.content) > 0


@pytest.mark.xfail(reason="on the model's side")
def test_chat_function_calling_with_multiple_parts() -> None:
@tool
def search(
Expand Down

0 comments on commit 60331a9

Please sign in to comment.