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

feat: ReAct Implementation #1515

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open

Conversation

GitHoobar
Copy link
Collaborator

Description

Implementing ReAct : Synergizing Reasoning and Acting in Language Models: https://arxiv.org/pdf/2210.03629 with structured output

Motivation and Context

close #1495

  • I have raised an issue to propose this change (required for new features and bug fixes)

Types of changes

What types of changes does your code introduce? Put an x in all the boxes that apply:

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds core functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation (update in the documentation)
  • Example (update in the folder of example)

Implemented Tasks

  • ReAct Agent and examples

Checklist

Go over all the following points, and put an x in all the boxes that apply.
If you are unsure about any of these, don't hesitate to ask. We are here to help!

  • I have read the CONTRIBUTION guide. (required)
  • My change requires a change to the documentation.
  • I have updated the tests accordingly. (required for a bug fix or a new feature)
  • I have updated the documentation accordingly.

@GitHoobar GitHoobar linked an issue Jan 27, 2025 that may be closed by this pull request
2 tasks
@GitHoobar
Copy link
Collaborator Author

will be adding tests in some time!

@GitHoobar GitHoobar marked this pull request as ready for review January 28, 2025 10:35
@Wendong-Fan Wendong-Fan modified the milestones: Sprint 5, Sprint 22 Jan 28, 2025
@Wendong-Fan Wendong-Fan requested review from hallerite and yiyiyi0817 and removed request for Asher-hss February 5, 2025 20:14
Copy link
Member

@yiyiyi0817 yiyiyi0817 left a comment

Choose a reason for hiding this comment

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

Thanks for your code. Leave some comments.

I also have a question and would be grateful for your answer:
Maybe it's because I haven't carefully read the ReAct paper. I noticed that the code implements step using "Thought," "Action," and "Observation." If "Thought" is just the response content of one step in ChatAgent, "Action" is function calling, and "Observation" is the agent's memory, then isn't ReActAgent very similar to our existing ChatAgent? What might be the possible differences between them?

camel/agents/react_agent.py Outdated Show resolved Hide resolved
camel/agents/react_agent.py Outdated Show resolved Hide resolved
},
)

def _parse_react_components(
Copy link
Member

Choose a reason for hiding this comment

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

I see that more LLM structured output is extracted by generating JSON or XML, which may have better results than using colons and strings.
And since camel-ai already has a module for structured output, I think it would be more robust to use camel's existing structured input and output. What do you think?
reference: https://github.com/camel-ai/camel/tree/master/examples/structured_response

components["Observation"],
)

def _execute_action(self, action: str) -> str:
Copy link
Member

Choose a reason for hiding this comment

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

If we use more structured output, maybe we can also input the action type (such as ReActActionSpace) and parameters of actions here.

examples/agents/react_agent_example.py Outdated Show resolved Hide resolved
Comment on lines 243 to 246
if hasattr(tool, 'can_handle') and tool.can_handle(action):
logger.debug("Found tool to handle action")
if hasattr(tool, 'execute'):
return tool.execute(action)
Copy link
Member

@yiyiyi0817 yiyiyi0817 Feb 6, 2025

Choose a reason for hiding this comment

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

Maybe I didn't quite understand this part. I noticed that the functions can_handle and execute are used here. But the example provided is MathToolkit, it seems have no execute method. And also the action space includes search, lookup, and finish. Is there some inconsistency here?

@GitHoobar
Copy link
Collaborator Author

Thanks for your code. Leave some comments.

I also have a question and would be grateful for your answer: Maybe it's because I haven't carefully read the ReAct paper. I noticed that the code implements step using "Thought," "Action," and "Observation." If "Thought" is just the response content of one step in ChatAgent, "Action" is function calling, and "Observation" is the agent's memory, then isn't ReActAgent very similar to our existing ChatAgent? What might be the possible differences between them?

Ahh nice observation, While ReActAgent and ChatAgent share some similarities in their step-by-step processing, there are differences like:

  • ReActAgent enforces a strict "Thought-Action-Observation" format in each step
  • ReActAgent has a predefined action space (Search, Lookup, Finish)
class ReActActionSpace(Enum):
    SEARCH = "Search"
    LOOKUP = "Lookup"
    FINISH = "Finish"
  • It maintains a memory of previous thoughts/action/observations
  • It terminates an action only if "Finish" is seen (or max steps reached)
  • It encourages to use tools before getting to answer, which is not the case with ChatAgent

Copy link
Member

@Wendong-Fan Wendong-Fan left a comment

Choose a reason for hiding this comment

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

Thanks @GitHoobar for the contribution! Left some comments below, and I also wanted to highlight some points that @yiyiyi0817 mentioned

camel/agents/react_agent.py Outdated Show resolved Hide resolved
camel/agents/react_agent.py Outdated Show resolved Hide resolved
camel/agents/react_agent.py Outdated Show resolved Hide resolved
camel/agents/react_agent.py Outdated Show resolved Hide resolved
examples/agents/react_agent_example.py Outdated Show resolved Hide resolved
@yiyiyi0817
Copy link
Member

yiyiyi0817 commented Feb 11, 2025

Thanks for your code. Leave some comments.
I also have a question and would be grateful for your answer: Maybe it's because I haven't carefully read the ReAct paper. I noticed that the code implements step using "Thought," "Action," and "Observation." If "Thought" is just the response content of one step in ChatAgent, "Action" is function calling, and "Observation" is the agent's memory, then isn't ReActAgent very similar to our existing ChatAgent? What might be the possible differences between them?

Ahh nice observation, While ReActAgent and ChatAgent share some similarities in their step-by-step processing, there are differences like:

  • ReActAgent enforces a strict "Thought-Action-Observation" format in each step
  • ReActAgent has a predefined action space (Search, Lookup, Finish)
class ReActActionSpace(Enum):
    SEARCH = "Search"
    LOOKUP = "Lookup"
    FINISH = "Finish"
  • It maintains a memory of previous thoughts/action/observations
  • It terminates an action only if "Finish" is seen (or max steps reached)
  • It encourages to use tools before getting to answer, which is not the case with ChatAgent

Thanks for your explaination.
Are actions in ReAct the same as using tools? Is ReAct designed for general tools for common scenarios, or for some specific search tasks? ReActActionSpace seems to be designed for some web tasks. I'm not sure if duckduckgo's action space is inconsistent with this, because there is only one search function.
And ChatAgent can also use tools extensively and proceed to the result based on the tool's return.

Copy link
Member

@yiyiyi0817 yiyiyi0817 left a comment

Choose a reason for hiding this comment

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

Still a little confused about some places, I left some references for discussion.



if __name__ == "__main__":
main()
Copy link
Member

Choose a reason for hiding this comment

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

I pull this branch and run the scripts, the response is

JSON response: {'thought': "I need to find the most recent population data for Paris, as I don't have access to future data directly. The latest available population figure can be used to estimate the 2024 population based on growth trends.", 'action': 'Lookup', 'observation': [{'result_id': 1, 'title': 'LOOKUP function - Microsoft Support', 'description': 'Learn how to use LOOKUP function to search for a value in a single row or column and return a value from the same position in another row or column. Compare LOOKUP with VLOOKUP and XLOOKUP and see examples and syntax.', 'url': 'https://support.microsoft.com/en-us/office/lookup-function-446d94af-663b-451d-8251-369d5e3864cb'}, {'result_id': 2, 'title': 'Excel LOOKUP function | Exceljet', 'description': 'Learn how to use the Excel LOOKUP function to perform an approximate match lookup in a one-column or one-row range, and return the corresponding value from another range. See examples, syntax, and tips for the vector and array forms of LOOKUP.', 'url': 'https://exceljet.net/functions/lookup-function'}, {'result_id': 3, 'title': 'Different Types of Lookup to Apply in Excel (8 Types)', 'description': 'Learn how to use different types of lookup functions and formulas in Excel to search for values in a table or array. Compare and contrast LOOKUP, HLOOKUP, VLOOKUP, XLOOKUP, OFFSET, MATCH, INDEX, XMATCH and more.', 'url': 'https://www.exceldemy.com/types-of-lookup-in-excel/'}, {'result_id': 4, 'title': 'Excel LOOKUP function with formula examples - Ablebits', 'description': 'Learn how to use the Excel LOOKUP function to search and retrieve values in one-column or one-row ranges, or in arrays. See formula examples, tips and alternatives for different scenarios of lookup.', 'url': 'https://www.ablebits.com/office-addins-blog/excel-lookup-function/'}, {'result_id': 5, 'title': '6 Formulas to Lookup in Excel', 'description': 'Learn how to use different Excel functions and formulas to lookup values in tables, from left to right or right to left, horizontally or vertically. Compare the advantages and disadvantages of VLOOKUP, HLOOKUP, INDEX-MATCH, OFFSET-MATCH, LOOKUP and LOOKUP (multiple values).', 'url': 'https://www.exceltip.com/lookup-formulas/6-formulas-to-lookup-in-excel.html'}]}
--------------------------------------------------
JSON response: {'thought': 'I need to find the current population of Paris, which is the capital of France. The previous lookup did not yield the population data directly, so I will search for the latest population statistics for Paris.', 'action': 'Lookup', 'observation': [{'result_id': 1, 'title': 'LOOKUP function - Microsoft Support', 'description': 'Learn how to use LOOKUP function to search for a value in a single row or column and return a value from the same position in another row or column. Compare LOOKUP with VLOOKUP and XLOOKUP and see examples and syntax.', 'url': 'https://support.microsoft.com/en-us/office/lookup-function-446d94af-663b-451d-8251-369d5e3864cb'}, {'result_id': 2, 'title': 'Excel LOOKUP function | Exceljet', 'description': 'Learn how to use the Excel LOOKUP function to perform an approximate match lookup in a one-column or one-row range, and return the corresponding value from another range. See examples, syntax, and tips for the vector and array forms of LOOKUP.', 'url': 'https://exceljet.net/functions/lookup-function'}, {'result_id': 3, 'title': 'Different Types of Lookup to Apply in Excel (8 Types)', 'description': 'Learn how to use different types of lookup functions and formulas in Excel to search for values in a table or array. Compare and contrast LOOKUP, HLOOKUP, VLOOKUP, XLOOKUP, OFFSET, MATCH, INDEX, XMATCH and more.', 'url': 'https://www.exceldemy.com/types-of-lookup-in-excel/'}, {'result_id': 4, 'title': 'Excel LOOKUP function with formula eption': 'Learn how to use the Excel LOOKUP function to perform an approximate match lookup in a one-column or one-row range, and return the corresponding value from another range. See examples, syntax, and tips for the vector and array forms of LOOKUP.', 'url': 'https://exceljet.net/functions/lookup-function'}, {'result_id': 3, 'title': 'Different Types of Lookup to Apply in Excel (8 Types)', 'description': 'Learn how to use different types of lookup functions and formulas in Excel to search for values in a table or array. Compare and contrast LOOKUP, HLOOKUP, VLOOKUP, XLOOKUP, OFFSET, MATCH, INDEX, XMATCH and more.', 'url': 'https://www.exceldemy.com/types-of-lookup-in-excel/'}, {'result_id': 4, 'title': 'Excel LOOKUP function with formula eption': 'Learn how to use the Excel LOOKUP function to perform an approximate match lookup in a one-column or one-row range, and return the corresponding value from another range. See examples, syntax, and tips for the vector and array forms of LOOKUP.', 'url': 'https://exceljet.net/functions/lookup-function'}, {'result_id': 3, 'title': 'Different Types of Lookup to Apply iption': 'Learn how to use the Excel LOOKUP function to perform an approximate match lookup in a one-column or one-row range, and return the corresponding value from another range. See examption': 'Learn how to use the Excel LOOKUP function to perform an approximate match lookup in a one-column or one-row range, and return the corresponding value from another range. See examption': 'Learn how to use the Excel LOOKUP function to perform an approximate match lookup in a one-column or one-row range, and return the corresponding value from another range. See examption': 'Learn how to use the Excel LOOKUP function to perform an approximate match lookup in a one-column or one-row range, and return the corresponding value from another range. See examples, syntax, and tips for the vector and array forms of LOOKUP.', 'url': 'https://exceljet.net/functions/lookup-function'}, {'result_id': 3, 'title': 'Different Types of Lookup to Apply in Excel (8 Types)', 'description': 'Learn how to use different types of lookup functions and formulas in Excel to search for values in a table or array. Compare and contrast LOOKUP, HLOOKUP, VLOOKUP, XLOOKUP, OFFSET, MATCH, INDEX, XMATCH and more.', 'url': 'https://www.exceldemy.com/types-of-lookup-in-excel/'}, {'result_id': 4, 'title': 'Excel LOOKUP function with formula examples - Ablebits', 'description': 'Learn how to use the Excel LOOKUP function to search and retrieve values in one-column or one-row ranges, or in arrays. See formula examples, tips and alternatives for different scenarios of lookup.', 'url': 'https://www.ablebits.com/office-addins-blog/excel-lookup-function/'}, {'result_id': 5, 'title': '6 Formulas to Lookup in Excel', 'n Excel (8 Types)', 'description': 'Learn how to use different types of lookup functions and formulas in Excel to search for values in a table or array. Compare and contrast LOOKUP, HLOOKUP, VLOOKUP, XLOOKUP, OFFSET, MATCH, INDEX, XMATCH and more.', 'url': 'https://www.exceldemy.com/types-of-lookup-in-excel/'}, {'result_id': 4, 'title': 'Excel LOOKUP function with formula examples - Ablebits', 'description': 'Learn how to use the Excel LOOKUP function to search and retrieve values in one-column or one-row ranges, or in arrays. See formula examples, tips and n Excel (8 Types)', 'description': 'Learn how to use different types of lookup functions and formulas in Excel to search for values in a table or array. Compare and contrast LOOKUP, HLOOKUP, VLOOKUP, XLOOKUP, OFFSET, MATCH, INDEX, XMATCH and more.', 'url': 'https://www.exceldemy.com/types-of-lookup-in-excel/'}, {'result_id': 4, 'title': 'Excel LOOKUP function with formula en Excel (8 Types)', 'description': 'Learn how to use different types of lookup functions and formulas in Excel to search for values in a table or array. Compare and contrast LOOKUP, HLOOKUP, VLOOKUP, XLOOKUP, OFFSET, MATCH, INDEX, XMATCH and more.', 'url': 'https://www.exceldemy.com/types-of-lookup-in-excel/'}, {'result_id': 4, 'title': 'Excel LOOKUP function with formula en Excel (8 Types)', 'description': 'Learn how to use different types of lookup functions and formulas in Excel to search for values in a table or array. Compare and contrast LOOKUP, HLOOKUP, VLOOKUP, XLOOKUP, OFFSET, MATCH, INDEX, XMATCH and more.', 'url': 'https://www.exceldemy.com/types-of-lookup-in-excel/'}, {'result_id': 4, 'title': 'Excel LOOKUP function with formula en Excel (8 Types)', 'description': 'Learn how to use different types of lookup functions and formulas in Excel to search for values in a table or array. Compare and contrast LOOKUP, HLOOKUn Excel (8 Types)', 'description': 'Learn how to use different types of lookup functions and formulas in Excel to search for values in a table or array. Compare and contrast LOOKUP, HLOOKUP, VLOOKUP, XLOOKUP, OFFSET, MATCH, INDEX, XMATCH and more.', 'url': 'https://www.exceldemy.com/types-of-lookup-in-excel/'}, {'result_id': 4, 'title': 'Excel LOOKUP function with formula examples - Ablebits', 'description': 'Learn how to use the Excel LOOKUP function to search and retrieve values in one-column or one-row ranges, or in arrays. See formula examples, tips and alternatives for different scenarios of lookup.', 'url': 'https://www.ablebits.com/office-addins-blog/excel-lookup-function/'}, {'result_id': 5, 'title': '6 Formulas to Lookup in Excel', 'description': 'Learn how to use different Excel functions and formulas to lookup values in tables, from left to right or right to left, horizontally or vertically. Compare the advantages and disadvantages of VLOOKUP, HLOOKUP, INDEX-MATCH, OFFSET-MATCH, LOOKUP and LOOKUP (multiple values).', 'url': 'https://www.exceltip.com/lookup-formulas/6-formulas-to-lookup-in-excel.html'}]}

It seems not get information from duckduckgo.

camel/agents/react_agent.py Outdated Show resolved Hide resolved
camel/agents/react_agent.py Show resolved Hide resolved
camel/agents/react_agent.py Outdated Show resolved Hide resolved
@GitHoobar
Copy link
Collaborator Author

Thanks for your code. Leave some comments.
I also have a question and would be grateful for your answer: Maybe it's because I haven't carefully read the ReAct paper. I noticed that the code implements step using "Thought," "Action," and "Observation." If "Thought" is just the response content of one step in ChatAgent, "Action" is function calling, and "Observation" is the agent's memory, then isn't ReActAgent very similar to our existing ChatAgent? What might be the possible differences between them?

Ahh nice observation, While ReActAgent and ChatAgent share some similarities in their step-by-step processing, there are differences like:

  • ReActAgent enforces a strict "Thought-Action-Observation" format in each step
  • ReActAgent has a predefined action space (Search, Lookup, Finish)
class ReActActionSpace(Enum):
    SEARCH = "Search"
    LOOKUP = "Lookup"
    FINISH = "Finish"
  • It maintains a memory of previous thoughts/action/observations
  • It terminates an action only if "Finish" is seen (or max steps reached)
  • It encourages to use tools before getting to answer, which is not the case with ChatAgent

Thanks for your explaination. Are actions in ReAct the same as using tools? Is ReAct designed for general tools for common scenarios, or for some specific search tasks? ReActActionSpace seems to be designed for some web tasks. I'm not sure if duckduckgo's action space is inconsistent with this, because there is only one search function. And ChatAgent can also use tools extensively and proceed to the result based on the tool's return.

The ReAct as described in paper is used for search tools, and the Lookup is used to find relevant sources of information from DB or queries.
We can update and include more functionalities if needed, but the paper demonstrates it use in web search but can be extended.
ChatAgent is more general and directly uses tools without the structured reasoning steps

Copy link
Member

@Wendong-Fan Wendong-Fan left a comment

Choose a reason for hiding this comment

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

Hey @GitHoobar , I’ve noticed that the ReActAgent sometimes runs into the Finish tag even when the task isn’t actually finished. There are two things we can look into:

  • For the _set_react_prompt prompt, we could clarify when the agent should trigger this tag.
  • Consider using a more specific tag wording, like CAMEL_TASK_DONE, to avoid misinterpretation.

Let me know what you think!

@GitHoobar
Copy link
Collaborator Author

Hey @GitHoobar , I’ve noticed that the ReActAgent sometimes runs into the Finish tag even when the task isn’t actually finished. There are two things we can look into:

  • For the _set_react_prompt prompt, we could clarify when the agent should trigger this tag.
  • Consider using a more specific tag wording, like CAMEL_TASK_DONE, to avoid misinterpretation.

Let me know what you think!

I don't see any improvement after changing _set_react_prompt or adding any validation have tried a couple of variations

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

[Feature Request] Implement ReAct
3 participants