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

fixed sequential human messages with tool calling #516

Merged
merged 1 commit into from
Sep 26, 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
6 changes: 5 additions & 1 deletion libs/vertexai/langchain_google_vertexai/chat_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,11 @@ def _convert_to_parts(message: BaseMessage) -> List[Part]:
if system_parts is not None:
parts = system_parts + parts
system_parts = None
vertex_messages.append(Content(role=role, parts=parts))
if vertex_messages and vertex_messages[-1].role == "user":
prev_parts = list(vertex_messages[-1].parts)
vertex_messages[-1] = Content(role=role, parts=prev_parts + parts)
else:
vertex_messages.append(Content(role=role, parts=parts))
elif isinstance(message, AIMessage):
prev_ai_message = message
role = "model"
Expand Down
56 changes: 56 additions & 0 deletions libs/vertexai/tests/integration_tests/test_chat_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -856,3 +856,59 @@ def test_json_serializable() -> None:
llm.prediction_client
llm.async_prediction_client
json.loads(llm.model_dump_json())


@pytest.mark.release
def test_langgraph_example() -> None:
llm = ChatVertexAI(
model_name="gemini-1.5-pro-001",
max_output_tokens=8192,
temperature=0.2,
)

add_declaration = {
"name": "add",
"description": "Adds a and b.",
"parameters": {
"properties": {
"a": {"description": "first int", "type": "integer"},
"b": {"description": "second int", "type": "integer"},
},
"required": ["a", "b"],
"type": "object",
},
}

multiply_declaration = {
"name": "multiply",
"description": "Multiply a and b.",
"parameters": {
"properties": {
"a": {"description": "first int", "type": "integer"},
"b": {"description": "second int", "type": "integer"},
},
"required": ["a", "b"],
"type": "object",
},
}

messages = [
SystemMessage(
content=(
"You are a helpful assistant tasked with performing "
"arithmetic on a set of inputs."
)
),
HumanMessage(content="Multiply 2 and 3"),
HumanMessage(content="No, actually multiply 3 and 3!"),
]
step1 = llm.invoke(
messages,
tools=[{"function_declarations": [add_declaration, multiply_declaration]}],
)
step2 = llm.invoke(
messages
+ [step1, ToolMessage(content="9", tool_call_id=step1.tool_calls[0]["id"])], # type: ignore[attr-defined]
tools=[{"function_declarations": [add_declaration, multiply_declaration]}],
)
assert isinstance(step2, AIMessage)
Loading