generated from langchain-ai/integration-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 1
AIS-308 Optimize Chat History #24
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c47426a
drop ChatMessageHistory
anyxling 138a9b7
change back to ChatMessageHistory
anyxling 3b65f87
add feedback to chat history
anyxling 40da2a7
fix no attr err
anyxling 159774d
add_doc -> add_qa_message
anyxling 3ed7875
fix lint err
anyxling 30f4974
Merge branch 'main' into AIS-308-optimize-chat-history
anyxling ace310c
add get_messages
anyxling 7fa0cf0
optimize get_messages & add time property
anyxling 45a5bb6
test get_messages & add_qa_message
anyxling 9aa543e
add docstring to funcs
anyxling File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| from typing import Any, List, Union | ||
| import time | ||
| from typing import Any, List, Optional, Union | ||
|
|
||
| from arango.database import StandardDatabase | ||
| from langchain_core.chat_history import BaseChatMessageHistory | ||
|
|
@@ -47,10 +48,22 @@ class ArangoChatMessageHistory(BaseChatMessageHistory): | |
| history.add_user_message("Hello! How are you?") | ||
| history.add_ai_message("I'm doing well, thank you!") | ||
|
|
||
| # Add QA message | ||
| history.add_qa_message( | ||
| user_input="Who is the first character?", | ||
| aql_query="FOR doc IN Characters LIMIT 1 RETURN doc", | ||
| result="The first character is Arya Stark." | ||
| ) | ||
|
|
||
| # Retrieve messages | ||
| messages = history.messages | ||
| print(f"Found {len(messages)} messages") | ||
|
|
||
| # Retrieve messages by role | ||
| human_messages = history.get_messages(role="human") | ||
| ai_messages = history.get_messages(role="ai") | ||
| qa_messages = history.get_messages(role="qa") | ||
|
|
||
| # Clear session | ||
| history.clear() | ||
| """ | ||
|
|
@@ -138,6 +151,54 @@ def messages(self, messages: List[BaseMessage]) -> None: | |
| " Use the 'add_messages' instead." | ||
| ) | ||
|
|
||
| def get_messages( | ||
| self, | ||
| role: Optional[str] = None, | ||
| n_messages: int = 10, | ||
| excluded_fields: list[str] = ["_id", "_key", "_rev", "session_id", "time"], | ||
| ) -> list: | ||
| """Retrieve messages from ArangoDB, optionally filtered by role. | ||
|
|
||
| :param role: Optional filter to retrieve messages of a specific role. | ||
| :type role: Optional[str] | ||
| :param n_messages: Number of messages to retrieve. | ||
| :type n_messages: int | ||
| :param excluded_fields: Fields to exclude from the returned messages. | ||
| :type excluded_fields: list[str] | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| # Get all types of messages, default is 10 messages | ||
| messages = history.get_messages() | ||
|
|
||
| # Get the first 20 human messages | ||
| messages = history.get_messages(role="human", n_messages=20) | ||
|
|
||
| # Get the first 20 AI messages | ||
| messages = history.get_messages(role="ai", n_messages=20) | ||
|
|
||
| """ | ||
| query = f""" | ||
| FOR doc IN @@col | ||
| FILTER doc.session_id == @session_id | ||
| {"AND doc.role == @role" if role else ""} | ||
| SORT doc.time DESC | ||
| LIMIT @n | ||
| RETURN UNSET(doc, @excluded_fields) | ||
| """ | ||
| bind_vars = { | ||
| "@col": self._collection_name, | ||
| "session_id": self._session_id, | ||
| "n": n_messages, | ||
| "excluded_fields": excluded_fields, | ||
| } | ||
| if role is not None: | ||
| bind_vars["role"] = role | ||
| cursor = self._db.aql.execute(query, bind_vars=bind_vars) # type: ignore | ||
|
|
||
| # return in chronological order | ||
| return [d for d in cursor][::-1] # type: ignore | ||
|
|
||
| def add_message(self, message: BaseMessage) -> None: | ||
| """Append the message to the record in ArangoDB. | ||
|
|
||
|
|
@@ -170,9 +231,39 @@ def add_message(self, message: BaseMessage) -> None: | |
| "role": message.type, | ||
| "content": message.content, | ||
| "session_id": self._session_id, | ||
| "time": time.time(), | ||
| }, | ||
| ) | ||
|
|
||
| def add_qa_message(self, user_input: str, aql_query: str, result: str) -> None: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for both import time
....
self._db.collection(self._collection_name).insert(
{
...,
"time": time.time()
}
) |
||
| """Add a QA message to the chat history. | ||
|
|
||
| :param user_input: The user's input. | ||
| :type user_input: str | ||
| :param aql_query: The AQL query to execute. | ||
| :type aql_query: str | ||
| :param result: The result of the AQL query. | ||
| :type result: str | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| history.add_qa_message( | ||
| user_input="Who is the first character?", | ||
| aql_query="FOR doc IN Characters LIMIT 1 RETURN doc", | ||
| result="The first character is Arya Stark." | ||
| ) | ||
| """ | ||
| self._db.collection(self._collection_name).insert( | ||
| { | ||
| "role": "qa", | ||
| "session_id": self._session_id, | ||
| "time": time.time(), | ||
| "user_input": user_input, | ||
| "aql_query": aql_query, | ||
| "result": result, | ||
| } | ||
| ) | ||
|
|
||
| def clear(self) -> None: | ||
| """Clear session memory from ArangoDB. | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good catch on not passing
rolehere since we also want to include human messages