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

Migrates to langgraph #165

Merged
merged 13 commits into from
Sep 11, 2024
Merged

Migrates to langgraph #165

merged 13 commits into from
Sep 11, 2024

Conversation

filipeximenes
Copy link
Contributor

This PR sunsets the previous use of chains in favor of the more modern, more readable and simpler to use langgraph approach. The migration was done trying to keep the changes as minimal as possible and the both methods as_chain and as_graph are still available. For now as_chain still the default approach but it can be changed by changing the setting AI_ASSISTANT_USE_LANGGRAPH to True. After this changes have been field tested we will sunset the old as_chain approach. Tests are already running with AI_ASSISTANT_USE_LANGGRAPH set to true. During this migration only 5 tests broke because it was required rerunning the cassettes due to small changes in the setup of how messages are organized in the prompt.

@@ -73,13 +77,17 @@ def add_messages(self, messages: Sequence[BaseMessage]) -> None:
messages: A list of BaseMessage objects to store.
"""
with transaction.atomic():
existing_messages = self.get_messages()

messages_to_create = [m for m in messages if m not in existing_messages]
Copy link
Member

Choose a reason for hiding this comment

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

how is the not in making the equality for messages? do you know?

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 point, it worked but I didn't check why it was working, perhaps we should force comparing by id to make this safer and more explicit?

Copy link
Member

Choose a reason for hiding this comment

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

I think so, but does the messages Langchain passes as params contain any ids?
Also, we can optimize this a bit by querying with ids.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The langchain ones receive a uuid id that is added in the built in add_messages function that is called before calling this one and they are update here to the db ids a few lines bellow this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've made the change to compare ids and optimized the query to only return the ids

assert (
response_message.content == "The current temperature in Recife today is 32 degrees Celsius."
)
assert response_message.id == str(messages_ids[1])
Copy link
Member

Choose a reason for hiding this comment

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

I preferred the old approach for readability. If you had trouble with the diffs, it's possible to change pytest to include all the diff.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it was better, the problem is that in the new version of langchain-core there's a bunch of metadata (see example bellow) that is not relevant to tests added to messages:
Screenshot 2024-09-11 at 14 11 45

Copy link
Member

Choose a reason for hiding this comment

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

Got it! We could use something like this: https://kalnytskyi.com/posts/assert-str-matches-regex-in-pytest/
In this case, it would be a comparator that always returns true.
But that's a nitpick.

Copy link
Member

@fjsj fjsj left a comment

Choose a reason for hiding this comment

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

Overall LGTM, but please check comments.
Can you also check the coverage decrease?

@fjsj fjsj merged commit ae0dbe8 into main Sep 11, 2024
13 of 18 checks passed
@fjsj fjsj deleted the langgraph-refactoring branch September 11, 2024 19:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants