core[patch]: support final AIMessage responses in tool_example_to_messages (#28267)

We have a test
[test_structured_few_shot_examples](ad4333ca03/libs/standard-tests/langchain_tests/integration_tests/chat_models.py (L546))
in standard integration tests that implements a version of tool-calling
few shot examples that works with ~all tested providers. The formulation
supported by ~all providers is: `human message, tool call, tool message,
AI reponse`.

Here we update
`langchain_core.utils.function_calling.tool_example_to_messages` to
support this formulation.

The `tool_example_to_messages` util is undocumented outside of our API
reference. IMO, if we are testing that this function works across all
providers, it can be helpful to feature it in our guides. The structured
few-shot examples we document at the moment require users to implement
this function and can be simplified.
This commit is contained in:
ccurme
2024-11-22 10:38:49 -05:00
committed by GitHub
parent a5fcbe69eb
commit a433039a56
3 changed files with 45 additions and 28 deletions

View File

@@ -679,6 +679,24 @@ def test_tool_outputs() -> None:
]
assert messages[2].content == "Output1"
# Test final AI response
messages = tool_example_to_messages(
input="This is an example",
tool_calls=[
FakeCall(data="ToolCall1"),
],
tool_outputs=["Output1"],
ai_response="The output is Output1",
)
assert len(messages) == 4
assert isinstance(messages[0], HumanMessage)
assert isinstance(messages[1], AIMessage)
assert isinstance(messages[2], ToolMessage)
assert isinstance(messages[3], AIMessage)
response = messages[3]
assert response.content == "The output is Output1"
assert not response.tool_calls
@pytest.mark.parametrize("use_extension_typed_dict", [True, False])
@pytest.mark.parametrize("use_extension_annotated", [True, False])