-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(pj/ai): added text/output methods to response and chunk wrappers (…
- Loading branch information
Showing
4 changed files
with
152 additions
and
22 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
#!/usr/bin/env python3 | ||
# | ||
# Copyright 2025 Google LLC | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
"""Tests for the action module.""" | ||
|
||
from genkit.ai.model import ( | ||
GenerateResponseChunkWrapper, | ||
GenerateResponseWrapper, | ||
) | ||
from genkit.core.typing import ( | ||
GenerateRequest, | ||
GenerateResponse, | ||
GenerateResponseChunk, | ||
Message, | ||
TextPart, | ||
) | ||
|
||
|
||
def test_response_wrapper_text() -> None: | ||
wrapper = GenerateResponseWrapper( | ||
response=GenerateResponse( | ||
message=Message( | ||
role='model', | ||
content=[TextPart(text='hello'), TextPart(text=' world')], | ||
) | ||
), | ||
request=GenerateRequest( | ||
messages=[], # doesn't matter for now | ||
), | ||
) | ||
|
||
assert wrapper.text == 'hello world' | ||
|
||
|
||
def test_response_wrapper_output() -> None: | ||
wrapper = GenerateResponseWrapper( | ||
response=GenerateResponse( | ||
message=Message( | ||
role='model', | ||
content=[TextPart(text='{"foo":'), TextPart(text='"bar')], | ||
) | ||
), | ||
request=GenerateRequest( | ||
messages=[], # doesn't matter for now | ||
), | ||
) | ||
|
||
assert wrapper.output == {'foo': 'bar'} | ||
|
||
|
||
def test_chunk_wrapper_text() -> None: | ||
wrapper = GenerateResponseChunkWrapper( | ||
chunk=GenerateResponseChunk( | ||
content=[TextPart(text='hello'), TextPart(text=' world')] | ||
), | ||
index=0, | ||
previous_chunks=[], | ||
) | ||
|
||
assert wrapper.text == 'hello world' | ||
|
||
|
||
def test_chunk_wrapper_accumulated_text() -> None: | ||
wrapper = GenerateResponseChunkWrapper( | ||
GenerateResponseChunk(content=[TextPart(text=' PS: aliens')]), | ||
index=0, | ||
previous_chunks=[ | ||
GenerateResponseChunk( | ||
content=[TextPart(text='hello'), TextPart(text=' ')] | ||
), | ||
GenerateResponseChunk(content=[TextPart(text='world!')]), | ||
], | ||
) | ||
|
||
assert wrapper.accumulated_text == 'hello world! PS: aliens' | ||
|
||
|
||
def test_chunk_wrapper_output() -> None: | ||
wrapper = GenerateResponseChunkWrapper( | ||
GenerateResponseChunk(content=[TextPart(text=', "baz":[1,2,')]), | ||
index=0, | ||
previous_chunks=[ | ||
GenerateResponseChunk( | ||
content=[TextPart(text='{"foo":'), TextPart(text='"ba')] | ||
), | ||
GenerateResponseChunk(content=[TextPart(text='r"')]), | ||
], | ||
) | ||
|
||
assert wrapper.output == {'foo': 'bar', 'baz': [1, 2]} |