Skip to content

Commit 302e17e

Browse files
douglas-reidDouglas Reid
andauthored
fix(agent): don't add blocks that contain only punctuation (#469)
Trailing punctuation blocks are of little to no value (imho). This PR ensures that we don't add those blocks. From: ``` Here is the image you requested: (image/png: B1A98152-A422-4B44-8FD9-D7C954E82D2A) . ``` To: ``` Here is the image you requested: (image/png: B1A98152-A422-4B44-8FD9-D7C954E82D2A) ``` --------- Co-authored-by: Douglas Reid <doug@steamship.com>
1 parent cd17b0e commit 302e17e

2 files changed

Lines changed: 15 additions & 3 deletions

File tree

src/steamship/agents/functional/output_parser.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
import json
22
import re
3+
import string
34
from typing import Dict, List, Optional
45

56
from steamship import Block, MimeTypes, Steamship
67
from steamship.agents.schema import Action, AgentContext, FinishAction, OutputParser, Tool
78
from steamship.data.tags.tag_constants import RoleTag
89

910

10-
# TODO(dougreid): extract shared bits from this and the ReACT output parser into a utility?
11+
def is_punctuation(text: str):
12+
for c in text:
13+
if c not in string.punctuation:
14+
return False
15+
return True
16+
17+
1118
class FunctionsBasedOutputParser(OutputParser):
1219

1320
tools_lookup_dict: Optional[Dict[str, Tool]] = None
@@ -46,7 +53,12 @@ def _blocks_from_text(client: Steamship, text: str) -> List[Block]:
4653
block_id_regex = r"(?:(?:\[|\()?Block)?\(?([A-F0-9]{8}\-[A-F0-9]{4}\-[A-F0-9]{4}\-[A-F0-9]{4}\-[A-F0-9]{12})\)?(?:(\]|\)))?"
4754
remaining_text = last_response
4855
result_blocks: List[Block] = []
49-
while remaining_text is not None and len(remaining_text) > 0:
56+
while remaining_text is not None and len(remaining_text.strip()) > 0:
57+
58+
if is_punctuation(remaining_text.strip()):
59+
remaining_text = ""
60+
continue
61+
5062
match = re.search(block_id_regex, remaining_text)
5163
if match:
5264
pre_block_text = FunctionsBasedOutputParser._remove_block_prefix(

tests/steamship_tests/agents/test_function_agent_output_parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ def test_output_block_ids_are_converted(client: Steamship):
1616
result = output_formatter.parse(test_text, context)
1717

1818
assert isinstance(result, FinishAction)
19-
assert len(result.output) == 3
19+
assert len(result.output) == 2
2020
assert result.output[1].id == block_id

0 commit comments

Comments
 (0)