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

Adds Reflexion (solves #2316) #4737

Closed
wants to merge 21 commits into from
Closed

Conversation

UmerHA
Copy link
Contributor

@UmerHA UmerHA commented May 15, 2023

Adds Reflexion (solves #2316)

Introduces the concept of trials to AgentExecutor and implements Reflexion as re-try method.
I.e., when the Agent fails, it thinks about what it could have done better & attempts the task again.
Based on https://arxiv.org/abs/2303.11366.

Fixes #2316

Community members can review the PR once tests pass. Tag maintainers/contributors who might be interested:
@vowelparrot

Twitter: @UmerHAdil | Discord: RicChilligerDude#7589

@UmerHA UmerHA mentioned this pull request May 15, 2023
@dev2049 dev2049 requested a review from vowelparrot May 15, 2023 18:05
@dev2049
Copy link
Contributor

dev2049 commented May 15, 2023

cc @mbchang

@mbchang
Copy link
Contributor

mbchang commented May 20, 2023

cc @noahshinn024

@noahshinn
Copy link

Nice!

@vowelparrot
Copy link
Contributor

vowelparrot commented May 22, 2023

This is really cool! And thank you for the PR and patience! The reason I'm taking some time to review this is that it changes a couple pretty core pieces - the agent executor and callback managers. While you do an excellent job of making sure it's all compatible, we may want to look into if a lighter-touch implementation would be appropriate here. I think @hwchase17 will have some stronger opinions than I right now in terms of how the agent framework should evolve

@UmerHA
Copy link
Contributor Author

UmerHA commented May 22, 2023

This is really cool! And thank you for the PR and patience! The reason I'm taking some time to review this is that it changes a couple pretty core pieces - the agent executor and callback managers. While you do an excellent job of making sure it's all compatible, we may want to look into if a lighter-touch implementation would be appropriate here. I think @hwchase17 will have some stronger opinions than I right now in terms of how the agent framework should evolve

Sure! 100% no hurt feelings.

I think we have 2 options:

  1. Implement Reflexion as an agent type. Then there's no need for changes in agent executor & callback managers, but we won't be able to use Reflexion with new agent types.
  2. Introduce the concept of re-trys. Then we can use Reflexion with any agent type, but this requires changes in agent executor & callback managers.

I went with 2 because @noahshinn024 explicitly stated "Reflexion is not a replacement for [...] any [....] decision-guiding approach! It is a simple retry technique that can be used (to) enhance other approaches."

How would you design a light-version? Am happy to implement :)

Edit: formatting

@UmerHA
Copy link
Contributor Author

UmerHA commented May 30, 2023

Hey @hwchase17 any comments?

Copy link

@jeanibarz jeanibarz left a comment

Choose a reason for hiding this comment

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

Thank you for your contribution. I noticed some small typos and made a comment about an implementation detail. I don't understand Langchain enough to make more useful feedbacks, so...

langchain/agents/agent.py Outdated Show resolved Hide resolved
langchain/agents/agent.py Outdated Show resolved Hide resolved
langchain/agents/agent.py Show resolved Hide resolved
langchain/agents/agent.py Outdated Show resolved Hide resolved
total_time_elapsed = 0.0
total_start_time = time.time()
trials = 1
trial_iterations = 0

Choose a reason for hiding this comment

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

trials and trial_iterations is confusing to me. From my understanding trial_iterations indicates the number of iterations done during the current trial, and "trials" indicates the current trial number. Maybe it would be beneficial to add some brief comment to describe each arguments or find a better naming (I don't have ideas thought).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good comment - will think of this when I get further comments from the LC team

langchain/agents/agent.py Outdated Show resolved Hide resolved
langchain/agents/reflexion/react.py Outdated Show resolved Hide resolved
tests/unit_tests/agents/test_reflection.py Outdated Show resolved Hide resolved
@vercel
Copy link

vercel bot commented Jun 22, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
langchain ⬜️ Ignored (Inspect) Jul 14, 2023 3:41am

@vercel vercel bot temporarily deployed to Preview June 22, 2023 10:41 Inactive
@UmerHA
Copy link
Contributor Author

UmerHA commented Jul 14, 2023

Hi @hinthornw! I saw you're now the maintainer for Agents / Tools / Toolkits, so am pinging you. Would love to get a rough direction on this, so I can adjust this to the LC team's preferences and get it merged.

@dosubot dosubot bot added Ɑ: agent Related to agents module 🤖:enhancement A large net-new component, integration, or chain. Use sparingly. The largest features labels Jul 14, 2023
@leo-gan
Copy link
Collaborator

leo-gan commented Sep 13, 2023

@UmerHA Hi , could you, please, resolve the merging issues? After that ping me and I push this PR for the review. Thanks!
Please, let me know if you need help!

@UmerHA
Copy link
Contributor Author

UmerHA commented Sep 21, 2023

Hi @leo-gan, nice to see you're an LC collaborator - congrats! :)

I would rather not fix this PR, because @vowelparrot said the key change in this PR needs to be discussed first. This PR introduces the concept of 'trials'. When an agent execution fails, a new run (new 'trial') is started.

From @vowelparrot's comment, LC's preference is to implement this differently - maybe as a chain?

Would love to get clarity on this issue before investing the effort of bringing this PR up-to-date.

@leo-gan
Copy link
Collaborator

leo-gan commented Sep 21, 2023

@hwchase17 Please, give a feedback to this important PR. Thanks!

@efriis
Copy link
Member

efriis commented Nov 7, 2023

Hopping into this one - @UmerHA would you be game to implement this using Runnables (LCEL) in a template? https://github.com/langchain-ai/langchain/blob/master/templates/docs/CONTRIBUTING.md

Would require starting over for the most part, but I don't think this PR is mergable in its current state anyways (library has evolved a lot since this was last worked on!). Let me know how you'd like to proceed! Happy to close this and you can open a new PR, or you're welcome to work from this PR's current state.

@UmerHA
Copy link
Contributor Author

UmerHA commented Nov 7, 2023

Hey @efriis, I can't promise to work on this due to time constraints, so feel free to close this. If I find time to adapt this, I'll open a new PR!

@efriis
Copy link
Member

efriis commented Nov 7, 2023

Sounds great - thanks @UmerHA !

@efriis efriis closed this Nov 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Ɑ: agent Related to agents module 🤖:enhancement A large net-new component, integration, or chain. Use sparingly. The largest features
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implementation of Reflexion in Langchain
8 participants