Open lo5twind opened 4 months ago
To resolve the "TypeError: '<' not supported between instances of 'NoneType' and 'int'" error you're encountering with your custom Moderation extension, you should utilize the _position.yaml
configuration for managing extension positions. This approach eliminates the need to manually set positions in the code, which is causing the error due to a None
value in position_map
.
Here's what you need to do:
Ensure _position.yaml
Exists: Create or update the _position.yaml
file in the directory with your extensions. This file should list the names of all extensions, including your custom one, in the desired load order.
Match Extension Names: The names in _position.yaml
must exactly match your extension names. These names are typically the directory names of the extensions or a specified identifier within the extension.
Format and Placement: Verify that _position.yaml
is correctly formatted and placed. Incorrect formatting or placement can lead to errors.
By following these steps, you should be able to load your custom Moderation extension successfully without encountering the TypeError. This method simplifies extension position management and avoids manual position assignments in the Python code, addressing the root cause of the error you've experienced.
To continue the conversation, mention @dosu.
@dosu It seems that _position.yaml is only used in model provider, not with code base extensions.
To address the issue with loading a custom Moderation extension and the TypeError: '<' not supported between instances of 'NoneType' and 'int'
error in Dify version 0.6.5, you can add a position
attribute directly in your custom extension class. This attribute will specify the order in which the extension should be loaded, avoiding the comparison with None
.
Here's an example of how to implement this in your custom extension class:
class YourCustomModerationExtension:
# Define a position attribute with an integer value
position: int = 10 # Adjust the value based on the desired load order
# Your existing extension code...
By setting a specific position
value, you ensure that your extension can be sorted and loaded correctly among other extensions. This method circumvents the issue caused by a None
value in position_map
for non-builtin extensions.
However, be cautious with this approach as it could lead to sorting conflicts, inconsistencies with built-in extensions, and maintenance challenges, especially if the load order needs to be adjusted in the future or if Dify's handling of extension ordering changes. Always ensure that the position
values are unique and carefully managed to avoid conflicts with other extensions.
To continue the conversation, mention @dosu.
@dosu The position
variable is not update in position_map
if extension is not buildin. So no matter what you add within CustomExtension, it will not work. To solve this problem, the only way is to modify the code, not the CustomExtension
To effectively solve the problem where the position
variable is not updated in position_map
for non-__builtin__
extensions in Dify, leading to a TypeError
, you need to modify the code to ensure that all extensions, regardless of being __builtin__
or not, have their positions properly recognized and sorted. This involves creating or updating a _position.yaml
file for managing the load order of extensions and adjusting the code to read this configuration.
Here's a concise approach to address the issue:
Create or Update _position.yaml
: For each extension, including non-__builtin__
ones, ensure there's a _position.yaml
file in their directory specifying the load order. This file should map extension names to their desired positions.
Adjust Code to Use _position.yaml
: Modify the extension loading mechanism to read the _position.yaml
file and create a position_map
that includes all extensions with their specified positions. If an extension does not have a position specified, it should default to being placed at the end of the load order.
Implement Sorting Logic: Use the sort_by_position_map
function, ensuring it handles cases where an extension's position might be undefined by assigning a default value that places it at the end of the list. This prevents the TypeError
by avoiding comparisons between NoneType
and int
.
By following these steps, you ensure that every extension, whether __builtin__
or custom, is accounted for in the position_map
with a valid position, thus resolving the sorting issue and preventing the TypeError
. This method provides a scalable and maintainable way to manage extension load order in Dify.
To continue the conversation, mention @dosu.
May I see how you have configured the moderation?
@lo5twind Hi, have you finally solved it
Self Checks
Dify version
0.6.5
Cloud or Self Hosted
Self Hosted (Docker)
Steps to reproduce
When I wrote a custom Moderation extension according to the official documentation, a loading error occurred during the restart of the service. The error message is as follows:
When I check the source code, I found that a non-builtin extension will set a
None
inposition_map
that will make this TypeError, please help to fix that.I try to modify the code at api/core/extension/extensible.py line 68, like below:
✔️ Expected Behavior
load extension success
❌ Actual Behavior
TypeError exception was thrown.