|
1 | 1 | """Standard LangChain interface tests""" |
2 | 2 |
|
3 | | -from typing import Literal, Type |
| 3 | +from typing import Literal, Optional, Type |
4 | 4 |
|
5 | 5 | import pytest |
6 | 6 | from langchain_core.exceptions import OutputParserException |
7 | 7 | from langchain_core.language_models import BaseChatModel |
8 | | -from langchain_core.messages import HumanMessage |
| 8 | +from langchain_core.messages import AIMessage, AIMessageChunk, HumanMessage |
9 | 9 | from langchain_core.tools import BaseTool |
10 | 10 | from langchain_tests.integration_tests import ChatModelIntegrationTests |
11 | 11 | from pydantic import BaseModel, Field |
@@ -181,6 +181,30 @@ def test_structured_output_snake_case() -> None: |
181 | 181 | assert isinstance(chunk, ClassifyQuery) |
182 | 182 |
|
183 | 183 |
|
| 184 | +def test_tool_calling_snake_case() -> None: |
| 185 | + model = ChatBedrockConverse(model="anthropic.claude-3-sonnet-20240229-v1:0") |
| 186 | + |
| 187 | + def classify_query(query_type: Literal["cat", "dog"]) -> None: |
| 188 | + pass |
| 189 | + |
| 190 | + chat = model.bind_tools([classify_query], tool_choice="any") |
| 191 | + response = chat.invoke("How big are cats?") |
| 192 | + assert isinstance(response, AIMessage) |
| 193 | + assert len(response.tool_calls) == 1 |
| 194 | + tool_call = response.tool_calls[0] |
| 195 | + assert tool_call["name"] == "classify_query" |
| 196 | + assert tool_call["args"] == {"query_type": "cat"} |
| 197 | + |
| 198 | + full = None |
| 199 | + for chunk in chat.stream("How big are cats?"): |
| 200 | + full = chunk if full is None else full + chunk # type: ignore[assignment] |
| 201 | + assert isinstance(full, AIMessageChunk) |
| 202 | + assert len(full.tool_calls) == 1 |
| 203 | + tool_call = full.tool_calls[0] |
| 204 | + assert tool_call["name"] == "classify_query" |
| 205 | + assert tool_call["args"] == {"query_type": "cat"} |
| 206 | + |
| 207 | + |
184 | 208 | def test_structured_output_streaming() -> None: |
185 | 209 | model = ChatBedrockConverse( |
186 | 210 | model="anthropic.claude-3-sonnet-20240229-v1:0", temperature=0 |
|
0 commit comments