Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Douglas Reid committed May 24, 2023
1 parent f1b3247 commit a6a4fc9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
28 changes: 25 additions & 3 deletions src/steamship/agents/react/output_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,39 @@ def parse(self, text: str, context: AgentContext) -> Action:
def _blocks_from_text(client: Steamship, text: str) -> List[Block]:
last_response = text.split("AI:")[-1].strip()

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})\)?\]?"
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})\)?(?:(\]|\)))?"
remaining_text = last_response
result_blocks: List[Block] = []
while remaining_text is not None and len(remaining_text) > 0:
match = re.search(block_id_regex, remaining_text)
if match:
result_blocks.append(Block(text=remaining_text[0 : match.start()]))
pre_block_text = ReACTOutputParser._remove_block_prefix(
candidate=remaining_text[0 : match.start()]
)
if len(pre_block_text) > 0:
result_blocks.append(Block(text=pre_block_text))
result_blocks.append(Block.get(client, _id=match.group(1)))
remaining_text = remaining_text[match.end() :]
remaining_text = ReACTOutputParser._remove_block_suffix(
remaining_text[match.end() :]
)
else:
result_blocks.append(Block(text=remaining_text))
remaining_text = ""

return result_blocks

@staticmethod
def _remove_block_prefix(candidate: str) -> str:
removed = candidate
if removed.endswith("(Block") or removed.endswith("[Block"):
removed = removed[len("Block") + 1 :]
elif removed.endswith("Block"):
removed = removed[len("Block") :]
return removed

@staticmethod
def _remove_block_suffix(candidate: str) -> str:
removed = candidate
if removed.startswith(")") or removed.endswith("]"):
removed = removed[1:]
return removed
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import pytest

from steamship import Steamship
from steamship.agents.llms import OpenAI
from steamship.agents.react import ReACTAgent
from steamship.experimental.package_starters.telegram_agent import TelegramAgentService
from steamship.invocable import Invocable, InvocableRequest, Invocation, post
from steamship.utils.url import Verb
Expand Down Expand Up @@ -153,7 +155,11 @@ def test_l32_routes():

@pytest.mark.usefixtures("client")
def test_telegram_agent(client: Steamship):
a = MyAgentService(client=client, config={"botToken": "foo"})
a = MyAgentService(
client=client,
config={"botToken": "foo"},
incoming_message_agent=ReACTAgent(tools=[], llm=OpenAI(client=client)),
)
assert a._method_mappings[Verb.POST]["/answer"] == "answer"
routes = [m["path"] for m in a.__steamship_dir__()["methods"]]
assert "/answer" in routes
Expand Down

0 comments on commit a6a4fc9

Please sign in to comment.