GeezerVison is showing github.com — feat: GenAI SDK client - Update input handling inside code execution … · googleapis/python-aiplatform@68392e5
File treeExpand file treeCollapse file tree Open diff view settings tests/unit/vertexai/genai/replays
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: tests/unit/vertexai/genai/replays/test_execute_code_agent_engine_sandbox.py +17-3Lines changed: 17 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -33,16 +33,30 @@ def test_execute_code_sandbox(client): | 33 | 33 | config=types.CreateAgentEngineSandboxConfig(display_name="test_sandbox"), | 34 | 34 | ) | 35 | 35 | assert isinstance(operation, types.AgentEngineSandboxOperation) | | 36 | + | | 37 | + code = """ | | 38 | +with open("test.txt", "r") as input: | | 39 | + with open("output.txt", "w") as output_txt: | | 40 | + for line in input: | | 41 | + output_txt.write(line) | | 42 | +""" | 36 | 43 | input_data = { | 37 | | - "language": "python", | 38 | | - "code": 'with open("hello.txt","w") as file:\n file.write("Hello, world!")', | | 44 | + "code": code, | | 45 | + "files": [ | | 46 | + { | | 47 | + "name": "test.txt", | | 48 | + "mimeType": "text/plain", | | 49 | + "content": b"Hello, world!", | | 50 | + } | | 51 | + ], | 39 | 52 | } | 40 | 53 | response = client.agent_engines.sandboxes.execute_code( | 41 | 54 | name=operation.response.name, | 42 | 55 | input_data=input_data, | 43 | 56 | ) | 44 | 57 | assert response.outputs[0].mime_type == "application/json" | 45 | | - | | 58 | + assert response.outputs[1].data == b"Hello, world!" | | 59 | + assert response.outputs[1].metadata.attributes.get("file_name") == b"output.txt" | 46 | 60 |
| 47 | 61 | pytestmark = pytest_helper.setup( | 48 | 62 | file=__file__, | |
Collapse file Copy file name to clipboardExpand all lines: vertexai/_genai/sandboxes.py +69-7Lines changed: 69 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 19 | 19 | import functools | 20 | 20 | import json | 21 | 21 | import logging | | 22 | +import mimetypes | 22 | 23 | from typing import Any, Iterator, Optional, Union | 23 | 24 | from urllib.parse import urlencode | 24 | 25 |
| @@ -528,6 +529,22 @@ def _get_sandbox_operation( | 528 | 529 | self._api_client._verify_response(return_value) | 529 | 530 | return return_value | 530 | 531 |
| | 532 | + _NEEDED_BASE64_ENCODING_MIME_TYPES = [ | | 533 | + "image/jpeg", | | 534 | + "image/png", | | 535 | + "image/gif", | | 536 | + "image/bmp", | | 537 | + "image/webp", | | 538 | + "audio/mpeg", | | 539 | + "audio/wav", | | 540 | + "audio/aac", | | 541 | + "audio/ogg", | | 542 | + "video/mp4", | | 543 | + "video/webm", | | 544 | + "video/ogg", | | 545 | + "video/mpeg", | | 546 | + ] | | 547 | + | 531 | 548 | def create( | 532 | 549 | self, | 533 | 550 | *, | @@ -618,20 +635,65 @@ def execute_code( | 618 | 635 | Returns: | 619 | 636 | ExecuteSandboxEnvironmentResponse: The response from executing the code. | 620 | 637 | """ | 621 | | - json_string = json.dumps(input_data) | 622 | | - | 623 | | - base64_bytes = base64.b64encode(json_string.encode("utf-8")) | 624 | | - base64_string = base64_bytes.decode("utf-8") | | 638 | + input_chunks = [] | | 639 | + | | 640 | + if input_data.get("code") is not None: | | 641 | + code = input_data.get("code", "") | | 642 | + json_code = json.dumps({"code": code}).encode("utf-8") | | 643 | + input_chunks.append( | | 644 | + types.Chunk( | | 645 | + mime_type="application/json", | | 646 | + data=json_code, | | 647 | + ) | | 648 | + ) | 625 | 649 |
| 626 | | - # Only single JSON input is supported for now. | 627 | | - inputs = [{"mime_type": "application/json", "data": base64_string}] | | 650 | + for file in input_data.get("files", []): | | 651 | + file_name = file.get("name", "") | | 652 | + mime_type = file.get("mimeType", "") | | 653 | + if mime_type is None: | | 654 | + mime_type, _ = mimetypes.guess_type(file_name) | | 655 | + if mime_type in self._NEEDED_BASE64_ENCODING_MIME_TYPES: | | 656 | + base64_bytes = base64.b64encode(file.get("content", b"")) | | 657 | + content = base64_bytes.decode("utf-8") | | 658 | + else: | | 659 | + content = file.get("content", b"") | | 660 | + input_chunks.append( | | 661 | + types.Chunk( | | 662 | + mime_type=mime_type, | | 663 | + data=content, | | 664 | + metadata={"attributes": {"file_name": file_name.encode("utf-8")}}, | | 665 | + ) | | 666 | + ) | 628 | 667 |
| 629 | 668 | response = self._execute_code( | 630 | 669 | name=name, | 631 | | - inputs=inputs, | | 670 | + inputs=input_chunks, | 632 | 671 | config=config, | 633 | 672 | ) | 634 | 673 |
| | 674 | + output_chunks = [] | | 675 | + for output in response.outputs: | | 676 | + if output.mime_type != "application/json": | | 677 | + mime_type = output.mime_type | | 678 | + # if mime_type is not available, try to guess the mime_type from the file_name. | | 679 | + if ( | | 680 | + mime_type is None | | 681 | + and output.metadata is not None | | 682 | + and output.metadata.attributes is not None | | 683 | + ): | | 684 | + file_name = output.metadata.attributes.get("file_name", b"").decode( | | 685 | + "utf-8" | | 686 | + ) | | 687 | + mime_type, _ = mimetypes.guess_type(file_name) | | 688 | + | | 689 | + # if the mime_type is in the list of mime_types that need base64 encoding, | | 690 | + # decode the data. | | 691 | + if mime_type in self._NEEDED_BASE64_ENCODING_MIME_TYPES: | | 692 | + output.data = base64.b64decode(output.data) | | 693 | + output_chunks.append(output) | | 694 | + | | 695 | + response = types.ExecuteSandboxEnvironmentResponse(outputs=output_chunks) | | 696 | + | 635 | 697 | return response | 636 | 698 |
| 637 | 699 | def get( | |
|