Added support for chat_history (#7555)

#7469

Co-authored-by: Leonid Kuligin <kuligin@google.com>
This commit is contained in:
Leonid Kuligin
2023-07-11 21:27:26 +02:00
committed by GitHub
parent 406a9dc11f
commit 6674b33cf5
3 changed files with 39 additions and 55 deletions

View File

@@ -12,7 +12,7 @@ from unittest.mock import Mock, patch
import pytest
from langchain.chat_models import ChatVertexAI
from langchain.chat_models.vertexai import _MessagePair, _parse_chat_history
from langchain.chat_models.vertexai import _parse_chat_history
from langchain.schema.messages import AIMessage, HumanMessage, SystemMessage
@@ -43,6 +43,8 @@ def test_vertexai_single_call_with_context() -> None:
def test_parse_chat_history_correct() -> None:
from vertexai.language_models import ChatMessage
text_context = (
"My name is Ned. You are my personal assistant. My "
"favorite movies are Lord of the Rings and Hobbit."
@@ -58,22 +60,14 @@ def test_parse_chat_history_correct() -> None:
)
answer = AIMessage(content=text_answer)
history = _parse_chat_history([context, question, answer, question, answer])
assert history.system_message == context
assert len(history.history) == 2
assert history.history[0] == _MessagePair(question=question, answer=answer)
def test_parse_chat_history_wrong_sequence() -> None:
text_question = (
"Hello, could you recommend a good movie for me to watch this evening, please?"
)
question = HumanMessage(content=text_question)
with pytest.raises(ValueError) as exc_info:
_ = _parse_chat_history([question, question])
assert (
str(exc_info.value)
== "A human message should follow a bot one, got human, human."
)
assert history.context == context.content
assert len(history.history) == 4
assert history.history == [
ChatMessage(content=text_question, author="user"),
ChatMessage(content=text_answer, author="bot"),
ChatMessage(content=text_question, author="user"),
ChatMessage(content=text_answer, author="bot"),
]
def test_vertexai_single_call_failes_no_message() -> None: