-
Notifications
You must be signed in to change notification settings - Fork 2.2k
refactor: Workflow execution logic #1913
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,7 @@ | |
from rest_framework import serializers | ||
from rest_framework.exceptions import ValidationError, ErrorDetail | ||
|
||
from application.flow.common import Answer | ||
from application.flow.common import Answer, NodeChunk | ||
from application.models import ChatRecord | ||
from application.models.api_key_model import ApplicationPublicAccessClient | ||
from common.constants.authentication_type import AuthenticationType | ||
|
@@ -175,6 +175,7 @@ def __init__(self, node, workflow_params, workflow_manage, up_node_id_list=None, | |
if up_node_id_list is None: | ||
up_node_id_list = [] | ||
self.up_node_id_list = up_node_id_list | ||
self.node_chunk = NodeChunk() | ||
self.runtime_node_id = sha1(uuid.NAMESPACE_DNS.bytes + bytes(str(uuid.uuid5(uuid.NAMESPACE_DNS, | ||
"".join([*sorted(up_node_id_list), | ||
node.id]))), | ||
|
@@ -214,6 +215,7 @@ def get_flow_params_serializer_class(self) -> Type[serializers.Serializer]: | |
|
||
def get_write_error_context(self, e): | ||
self.status = 500 | ||
self.answer_text = str(e) | ||
self.err_message = str(e) | ||
self.context['run_time'] = time.time() - self.context['start_time'] | ||
|
||
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. The code snippet you provided seems to be part of a Django REST framework view that handles API requests and flow execution in an application. Here are some points for review: Irregularities:
Potential Issues:
Optimization Suggestions:
Here is your suggested improvement in terms of handling these points: from rest_framework import serializers
from rest_framework.exceptions import ValidationError, ErrorDetail
import uuid
# Assuming NodeChunk is defined elsewhere
from application.flow.common import Answer, NodeChunk
from application.models import ChatRecord
from application.models.api_key_model import ApplicationPublicAccessClient
from common.constants.authentication_type import AuthenticationType
import hashlib
class MyView(APIView):
def __init__(self, node, workflow_params, workflow_manage, up_node_id_list=None):
super().__init__()
if not up_node_id_list:
up_node_id_list = []
# Initialize node chunk outside the loop
self.node_chunk = NodeChunk()
# Create runtime_node_id using a consistent method
self.runtime_node_id = hashlib.sha1(
b''.join([
uuid.NAMESPACE_DNS.bytes,
bytes(str(uuid.uuid5(uuid.NAMESPACE_DNS,
u"".join(sorted(list(up_node_id_list))) + str(node.id)))).encode('utf-8')
]
).hexdigest()
def get_flow_params_serializer_class(self) -> Type[serializers.Serializer]:
return YourFlowParamsSerializer # Replace with actual serializer class name
def post(self, request, format=None):
try:
# Handle incoming data or other logic here
pass
except Exception as e: # Catch specific exceptions if necessary
self.set_exception_response(e, request)
def set_exception_response(self, e, request):
self.status = 500
self.answer_text = str(e)
self.err_message = str(e)
self.context['run_time'] = time.time() - self.context.get('start_time', 0) # Safe access with default value
# Example usage of the improved setup This updated version ensures proper initialization of attributes outside loops, better performance by creating the |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -80,7 +80,7 @@ class Config(dict): | |
"DB_PORT": 5432, | ||
"DB_USER": "root", | ||
"DB_PASSWORD": "Password123@postgres", | ||
"DB_ENGINE": "django.db.backends.postgresql_psycopg2", | ||
"DB_ENGINE": "dj_db_conn_pool.backends.postgresql", | ||
# 向量模型 | ||
"EMBEDDING_MODEL_NAME": "shibing624/text2vec-base-chinese", | ||
"EMBEDDING_DEVICE": "cpu", | ||
|
@@ -108,7 +108,11 @@ def get_db_setting(self) -> dict: | |
"PORT": self.get('DB_PORT'), | ||
"USER": self.get('DB_USER'), | ||
"PASSWORD": self.get('DB_PASSWORD'), | ||
"ENGINE": self.get('DB_ENGINE') | ||
"ENGINE": self.get('DB_ENGINE'), | ||
"POOL_OPTIONS": { | ||
"POOL_SIZE": 20, | ||
"MAX_OVERFLOW": 5 | ||
} | ||
} | ||
|
||
def __init__(self, *args): | ||
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. The provided code looks generally correct and should work as intended. However, I can offer a few suggestions for improvement:
This approach allows for flexible defaults that don't break the function when certain environment variables are not defined. Overall, the code is well-structured and follows best practices, so these minor changes won't affect its functionality significantly. |
||
|
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.
The provided code seems generally robust and well-structured. However, here are some minor suggestions for improvement:
Docstring Consistency: The class methods could benefit from adding docstrings to explain their purpose and usage.
Type Hinting: It's a good practice to include type hints for better readability and maintainability.
Error Handling: Ensure that error handling is implemented in appropriate places, especially when dealing with
chunk_list
or other data structures.Code Comments: Add comments where needed to clarify complex logic or flow control.
Variable Naming: Use descriptive variable names instead of single-character abbreviations like
view_type
.Here's an updated version of the code incorporating these suggestions:
This revised code includes:
TreeNode
explaining its functionality.add_chunk
.to_dict()
for easy conversion of objects to dictionaries.