Skip to content

Commit

Permalink
standard-tests: test that only one chunk sets input_tokens (langchain…
Browse files Browse the repository at this point in the history
  • Loading branch information
efriis authored Oct 8, 2024
1 parent 9b7bdf1 commit b84e002
Showing 1 changed file with 15 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import base64
import json
from typing import List, Optional
from typing import List, Optional, cast

import httpx
import pytest
Expand Down Expand Up @@ -209,10 +209,21 @@ def test_usage_metadata(self, model: BaseChatModel) -> None:
def test_usage_metadata_streaming(self, model: BaseChatModel) -> None:
if not self.returns_usage_metadata:
pytest.skip("Not implemented.")
full: Optional[BaseMessageChunk] = None
for chunk in model.stream("Hello"):
full: Optional[AIMessageChunk] = None
for chunk in model.stream("Write me 2 haikus. Only include the haikus."):
assert isinstance(chunk, AIMessageChunk)
full = chunk if full is None else full + chunk
# only one chunk is allowed to set usage_metadata.input_tokens
# if multiple do, it's likely a bug that will result in overcounting
# input tokens
if full and full.usage_metadata and full.usage_metadata["input_tokens"]:
assert (
not chunk.usage_metadata or not chunk.usage_metadata["input_tokens"]
), (
"Only one chunk should set input_tokens,"
" the rest should be 0 or None"
)
full = chunk if full is None else cast(AIMessageChunk, full + chunk)

assert isinstance(full, AIMessageChunk)
assert full.usage_metadata is not None
assert isinstance(full.usage_metadata["input_tokens"], int)
Expand Down

0 comments on commit b84e002

Please sign in to comment.