-
Notifications
You must be signed in to change notification settings - Fork 15.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Batch Size kwarg to the llm start callback (#13483)
So you can more easily use the token counts directly from the API endpoint for batch size of 1
- Loading branch information
Showing
6 changed files
with
147 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
71 changes: 71 additions & 0 deletions
71
libs/core/tests/unit_tests/language_models/chat_models/test_base.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
"""Test base chat model.""" | ||
import pytest | ||
|
||
from langchain_core.messages import HumanMessage, SystemMessage | ||
from langchain_core.tracers.context import collect_runs | ||
from tests.unit_tests.fake.chat_model import FakeListChatModel | ||
|
||
|
||
@pytest.fixture | ||
def messages() -> list: | ||
return [ | ||
SystemMessage(content="You are a test user."), | ||
HumanMessage(content="Hello, I am a test user."), | ||
] | ||
|
||
|
||
@pytest.fixture | ||
def messages_2() -> list: | ||
return [ | ||
SystemMessage(content="You are a test user."), | ||
HumanMessage(content="Hello, I not a test user."), | ||
] | ||
|
||
|
||
def test_batch_size(messages: list, messages_2: list) -> None: | ||
# The base endpoint doesn't support native batching, | ||
# so we expect batch_size to always be 1 | ||
llm = FakeListChatModel(responses=[str(i) for i in range(100)]) | ||
with collect_runs() as cb: | ||
llm.batch([messages, messages_2], {"callbacks": [cb]}) | ||
assert len(cb.traced_runs) == 2 | ||
assert all([(r.extra or {}).get("batch_size") == 1 for r in cb.traced_runs]) | ||
with collect_runs() as cb: | ||
llm.batch([messages], {"callbacks": [cb]}) | ||
assert all([(r.extra or {}).get("batch_size") == 1 for r in cb.traced_runs]) | ||
assert len(cb.traced_runs) == 1 | ||
|
||
with collect_runs() as cb: | ||
llm.invoke(messages) | ||
assert len(cb.traced_runs) == 1 | ||
assert (cb.traced_runs[0].extra or {}).get("batch_size") == 1 | ||
|
||
with collect_runs() as cb: | ||
list(llm.stream(messages)) | ||
assert len(cb.traced_runs) == 1 | ||
assert (cb.traced_runs[0].extra or {}).get("batch_size") == 1 | ||
|
||
|
||
async def test_async_batch_size(messages: list, messages_2: list) -> None: | ||
llm = FakeListChatModel(responses=[str(i) for i in range(100)]) | ||
# The base endpoint doesn't support native batching, | ||
# so we expect batch_size to always be 1 | ||
with collect_runs() as cb: | ||
await llm.abatch([messages, messages_2], {"callbacks": [cb]}) | ||
assert all([(r.extra or {}).get("batch_size") == 1 for r in cb.traced_runs]) | ||
assert len(cb.traced_runs) == 2 | ||
with collect_runs() as cb: | ||
await llm.abatch([messages], {"callbacks": [cb]}) | ||
assert all([(r.extra or {}).get("batch_size") == 1 for r in cb.traced_runs]) | ||
assert len(cb.traced_runs) == 1 | ||
|
||
with collect_runs() as cb: | ||
await llm.ainvoke(messages) | ||
assert len(cb.traced_runs) == 1 | ||
assert (cb.traced_runs[0].extra or {}).get("batch_size") == 1 | ||
|
||
with collect_runs() as cb: | ||
async for _ in llm.astream(messages): | ||
pass | ||
assert len(cb.traced_runs) == 1 | ||
assert (cb.traced_runs[0].extra or {}).get("batch_size") == 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 8 additions & 8 deletions
16
libs/core/tests/unit_tests/runnables/__snapshots__/test_runnable.ambr
Large diffs are not rendered by default.
Oops, something went wrong.