forked from langgenius/dify
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
51 changed files
with
604 additions
and
179 deletions.
There are no files selected for viewing
This file contains 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 +1,2 @@ | ||
HIDDEN_VALUE = "[__HIDDEN__]" | ||
UUID_NIL = "00000000-0000-0000-0000-000000000000" |
This file contains 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from constants import UUID_NIL | ||
|
||
|
||
def extract_thread_messages(messages: list[dict]) -> list[dict]: | ||
thread_messages = [] | ||
next_message = None | ||
|
||
for message in messages: | ||
if not message.parent_message_id: | ||
# If the message is regenerated and does not have a parent message, it is the start of a new thread | ||
thread_messages.append(message) | ||
break | ||
|
||
if not next_message: | ||
thread_messages.append(message) | ||
next_message = message.parent_message_id | ||
else: | ||
if next_message in {message.id, UUID_NIL}: | ||
thread_messages.append(message) | ||
next_message = message.parent_message_id | ||
|
||
return thread_messages |
This file contains 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 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
36 changes: 36 additions & 0 deletions
36
api/migrations/versions/2024_09_11_1012-d57ba9ebb251_add_parent_message_id_to_messages.py
This file contains 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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
"""add parent_message_id to messages | ||
Revision ID: d57ba9ebb251 | ||
Revises: 675b5321501b | ||
Create Date: 2024-09-11 10:12:45.826265 | ||
""" | ||
import sqlalchemy as sa | ||
from alembic import op | ||
|
||
import models as models | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = 'd57ba9ebb251' | ||
down_revision = '675b5321501b' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
with op.batch_alter_table('messages', schema=None) as batch_op: | ||
batch_op.add_column(sa.Column('parent_message_id', models.types.StringUUID(), nullable=True)) | ||
|
||
# Set parent_message_id for existing messages to uuid_nil() to distinguish them from new messages with actual parent IDs or NULLs | ||
op.execute('UPDATE messages SET parent_message_id = uuid_nil() WHERE parent_message_id IS NULL') | ||
|
||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
with op.batch_alter_table('messages', schema=None) as batch_op: | ||
batch_op.drop_column('parent_message_id') | ||
|
||
# ### end Alembic commands ### |
This file contains 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 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
91 changes: 91 additions & 0 deletions
91
api/tests/unit_tests/core/prompt/test_extract_thread_messages.py
This file contains 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 |
---|---|---|
@@ -0,0 +1,91 @@ | ||
from uuid import uuid4 | ||
|
||
from constants import UUID_NIL | ||
from core.prompt.utils.extract_thread_messages import extract_thread_messages | ||
|
||
|
||
class TestMessage: | ||
def __init__(self, id, parent_message_id): | ||
self.id = id | ||
self.parent_message_id = parent_message_id | ||
|
||
def __getitem__(self, item): | ||
return getattr(self, item) | ||
|
||
|
||
def test_extract_thread_messages_single_message(): | ||
messages = [TestMessage(str(uuid4()), UUID_NIL)] | ||
result = extract_thread_messages(messages) | ||
assert len(result) == 1 | ||
assert result[0] == messages[0] | ||
|
||
|
||
def test_extract_thread_messages_linear_thread(): | ||
id1, id2, id3, id4, id5 = str(uuid4()), str(uuid4()), str(uuid4()), str(uuid4()), str(uuid4()) | ||
messages = [ | ||
TestMessage(id5, id4), | ||
TestMessage(id4, id3), | ||
TestMessage(id3, id2), | ||
TestMessage(id2, id1), | ||
TestMessage(id1, UUID_NIL), | ||
] | ||
result = extract_thread_messages(messages) | ||
assert len(result) == 5 | ||
assert [msg["id"] for msg in result] == [id5, id4, id3, id2, id1] | ||
|
||
|
||
def test_extract_thread_messages_branched_thread(): | ||
id1, id2, id3, id4 = str(uuid4()), str(uuid4()), str(uuid4()), str(uuid4()) | ||
messages = [ | ||
TestMessage(id4, id2), | ||
TestMessage(id3, id2), | ||
TestMessage(id2, id1), | ||
TestMessage(id1, UUID_NIL), | ||
] | ||
result = extract_thread_messages(messages) | ||
assert len(result) == 3 | ||
assert [msg["id"] for msg in result] == [id4, id2, id1] | ||
|
||
|
||
def test_extract_thread_messages_empty_list(): | ||
messages = [] | ||
result = extract_thread_messages(messages) | ||
assert len(result) == 0 | ||
|
||
|
||
def test_extract_thread_messages_partially_loaded(): | ||
id0, id1, id2, id3 = str(uuid4()), str(uuid4()), str(uuid4()), str(uuid4()) | ||
messages = [ | ||
TestMessage(id3, id2), | ||
TestMessage(id2, id1), | ||
TestMessage(id1, id0), | ||
] | ||
result = extract_thread_messages(messages) | ||
assert len(result) == 3 | ||
assert [msg["id"] for msg in result] == [id3, id2, id1] | ||
|
||
|
||
def test_extract_thread_messages_legacy_messages(): | ||
id1, id2, id3 = str(uuid4()), str(uuid4()), str(uuid4()) | ||
messages = [ | ||
TestMessage(id3, UUID_NIL), | ||
TestMessage(id2, UUID_NIL), | ||
TestMessage(id1, UUID_NIL), | ||
] | ||
result = extract_thread_messages(messages) | ||
assert len(result) == 3 | ||
assert [msg["id"] for msg in result] == [id3, id2, id1] | ||
|
||
|
||
def test_extract_thread_messages_mixed_with_legacy_messages(): | ||
id1, id2, id3, id4, id5 = str(uuid4()), str(uuid4()), str(uuid4()), str(uuid4()), str(uuid4()) | ||
messages = [ | ||
TestMessage(id5, id4), | ||
TestMessage(id4, id2), | ||
TestMessage(id3, id2), | ||
TestMessage(id2, UUID_NIL), | ||
TestMessage(id1, UUID_NIL), | ||
] | ||
result = extract_thread_messages(messages) | ||
assert len(result) == 4 | ||
assert [msg["id"] for msg in result] == [id5, id4, id2, id1] |
Oops, something went wrong.