Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HotPotQA lookup returns the wrong result is asked to look up the same term in 2 different pages sequentially #99

Merged
merged 8 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions packages/hotpotqa/src/aviary/envs/hotpotqa/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,15 @@ class HotPotQAEnvState(BaseModel):
reward: float = Field(
default=0.0, description="Current reward value, reset each environment step."
)

answer: str | None = Field(
default=None,
description="The answer to the question, or None if not yet answered.",
)

last_action: str | None = Field(
default=None,
description="The last action taken by the agent."
"Default is None, as after reset the agent has not yet taken any action.",
)
last_lookup: str | None = Field(
default=None, description="The last lookup keyword."
)
Expand Down Expand Up @@ -340,6 +343,8 @@ def finish(self, answer: str) -> str:

self.state.answer = answer
self.state.reward += self.calculate_reward(answer)

self.state.last_action = self.tools[2].info.name
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw @jamesbraza 's original suggestion, but IMO this is less readable and depends on the order in which self.tools is populated. "Finish" is better. If we want to be extra careful, we could make a constant FINISH_TOOL_NAME = "Finish" and use it in both places.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I agree with Sid that we shouldn't rely on ordering here @albertbou92 . Can you find a way to:

  • Not depend on tool ordering
  • Not depend on string literals (as subclasses can change tool names)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just last_action_is_lookup: bool, since that's the main thing we check with last_action?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this is a good solution actually

return "Finished."

async def search(self, entity: str) -> str:
Expand Down Expand Up @@ -404,6 +409,7 @@ async def search(self, entity: str) -> str:
for s in p.split(". ")
if s.strip()
]
self.state.last_action = self.tools[0].info.name
return " ".join(obs_list[:5])

def construct_lookup_list(self, keyword: str) -> str:
Expand Down Expand Up @@ -441,11 +447,14 @@ def construct_lookup_list(self, keyword: str) -> str:
if not self.state.page:
return "Lookup failed. You have not specified a Wikipedia page yet."

if self.state.last_lookup != keyword:
if (
self.state.last_action != self.tools[1].info.name
or self.state.last_lookup != keyword
):
self.state.last_lookup = keyword
self.state.lookup_results = [
s.strip() + "."
for s in self.state.page.split(". ")
s.strip()
for s in self.state.page.split("\n")
albertbou92 marked this conversation as resolved.
Show resolved Hide resolved
if s.strip() and keyword.lower() in s.lower()
]
self.state.lookup_index = 0
Expand All @@ -458,6 +467,7 @@ def construct_lookup_list(self, keyword: str) -> str:
f" {self.state.lookup_results[self.state.lookup_index]}"
)
self.state.lookup_index += 1
self.state.last_action = self.tools[1].info.name
return obs


Expand Down
38 changes: 38 additions & 0 deletions packages/hotpotqa/tests/test_hotpotqa_env.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import re

import pytest

from aviary.core import Environment, TaskDataset
Expand Down Expand Up @@ -27,3 +29,39 @@ def test_dataset_from_name() -> None:

with pytest.raises(ValueError, match="answer"):
TaskDataset.from_name("hotpotqa", split="test")


@pytest.mark.asyncio
async def test_tool_results() -> None:
hotpotqa_env: HotPotQAEnv = Environment.from_name(
"hotpotqa",
question=("Which country has a larger population: China or France?"),
correct_answer="China",
)
lookup_pattern = r"^\(Result \d+ / \d+\)\s*(.*)"

_, _ = await hotpotqa_env.reset()
obs1 = await hotpotqa_env.search("China")
obs2 = hotpotqa_env.construct_lookup_list("population")

# Check lookup return format
match = re.match(lookup_pattern, obs2)
assert match # Starts with the right pattern
assert (
match.group(1) + "\n" in hotpotqa_env.state.page
) # Everything after the pattern should be a paragraph in current page

obs3 = await hotpotqa_env.search("France")
obs4 = hotpotqa_env.construct_lookup_list("population")

# Check lookup return format
match = re.match(lookup_pattern, obs4)
assert match # Starts with the right pattern
assert (
match.group(1) + "\n" in hotpotqa_env.state.page
) # Everything after the pattern should be a paragraph in current page
albertbou92 marked this conversation as resolved.
Show resolved Hide resolved

obs5 = hotpotqa_env.finish("China")

# Ensure that the observations are different
assert obs1 != obs2 != obs3 != obs4 != obs5
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the intuition behind this assertion?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just making sure the returned outputs change when they are supposed to. Before this PR, if lookup only finds an occurrence, obs2 and obs4 would be equal, which is not correct.