Skip to content

Commit

Permalink
Fix summarize_page in a text-only context, and for unknown models. (#…
Browse files Browse the repository at this point in the history
…5388)

WebSurfer's summarize_page was failing when the model was text-only, or
unknown.
  • Loading branch information
afourney authored Feb 6, 2025
1 parent 7947464 commit d86540e
Showing 1 changed file with 33 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@
from ._types import InteractiveRegion, UserContent
from .playwright_controller import PlaywrightController

DEFAULT_CONTEXT_SIZE = 128000


class MultimodalWebSurferConfig(BaseModel):
name: str
Expand Down Expand Up @@ -855,35 +857,50 @@ async def _summarize_page(
buffer = ""
# for line in re.split(r"([\r\n]+)", page_markdown):
for line in page_markdown.splitlines():
message = UserMessage(
# content=[
trial_message = UserMessage(
content=prompt + buffer + line,
# ag_image,
# ],
source=self.name,
)

remaining = self._model_client.remaining_tokens(messages + [message])
if remaining > self.SCREENSHOT_TOKENS:
buffer += line
else:
try:
remaining = self._model_client.remaining_tokens(messages + [trial_message])
except KeyError:
# Use the default if the model isn't found
remaining = DEFAULT_CONTEXT_SIZE - self._model_client.count_tokens(messages + [trial_message])

if self._model_client.model_info["vision"] and remaining <= 0:
break

if self._model_client.model_info["vision"] and remaining <= self.SCREENSHOT_TOKENS:
break

buffer += line

# Nothing to do
buffer = buffer.strip()
if len(buffer) == 0:
return "Nothing to summarize."

# Append the message
messages.append(
UserMessage(
content=[
prompt + buffer,
ag_image,
],
source=self.name,
if self._model_client.model_info["vision"]:
# Multimodal
messages.append(
UserMessage(
content=[
prompt + buffer,
ag_image,
],
source=self.name,
)
)
else:
# Text only
messages.append(
UserMessage(
content=prompt + buffer,
source=self.name,
)
)
)

# Generate the response
response = await self._model_client.create(messages, cancellation_token=cancellation_token)
Expand Down

0 comments on commit d86540e

Please sign in to comment.