-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat: Non streaming response answers_list #2461
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 1 commit
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 |
|---|---|---|
|
|
@@ -337,13 +337,15 @@ def run_block(self, language='zh'): | |
| answer_text = '\n\n'.join( | ||
| '\n\n'.join([a.get('content') for a in answer]) for answer in | ||
| answer_text_list) | ||
| answer_list = reduce(lambda pre, _n: [*pre, *_n], answer_text_list, []) | ||
| self.work_flow_post_handler.handler(self.params['chat_id'], self.params['chat_record_id'], | ||
| answer_text, | ||
| self) | ||
| return self.base_to_response.to_block_response(self.params['chat_id'], | ||
| self.params['chat_record_id'], answer_text, True | ||
| , message_tokens, answer_tokens, | ||
| _status=status.HTTP_200_OK if self.status == 200 else status.HTTP_500_INTERNAL_SERVER_ERROR) | ||
| _status=status.HTTP_200_OK if self.status == 200 else status.HTTP_500_INTERNAL_SERVER_ERROR, | ||
| other_params={'answer_list': answer_list}) | ||
|
|
||
| def run_stream(self, current_node, node_result_future, language='zh'): | ||
| """ | ||
|
Contributor
Author
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. There doesn't appear to be any significant errors in the code provided. However, there are some minor optimizations for readability and performance:
Here's an optimized version of the code based on these improvements: def run_block(self, language='zh'):
answer_text = '\n\n'.join('\n\n'.join(a.get('content') for a in answer) for answer in answer_text_list)
self.work_flow_post_handler.handler(
self.params['chat_id'],
self.params['chat_record_id'],
answer_text,
self
)
# Return response without modifying the original text
return self.base_to_response.to_block_response(
self.params['chat_id'],
self.params['chat_record_id'],
answer_text,
True,
message_tokens,
answer_tokens,
_status=status.HTTP_200_OK if self.status == 200 else status.HTTP_500_INTERNAL_SERVER_ERROR,
other_params={'answer_list': answer_text_list}
)
def run_stream(self, current_node, node_result_future, language='zh'):
"""These changes improve both readability and maintainability while removing unnecessary steps. Let me know if you need further adjustments!
Contributor
Author
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. There is an issue with the line answer_list = ','.join(answer.get('content') for answer in answer_text_list)
# Then, proceed with the rest of the code...If you need to maintain the list format after joining, consider converting it back to a list before using answer_list = [answer.get('content') for answer in answer_text_list]
Contributor
Author
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. This code looks mostly correct, but there are a few minor points that can potentially be improved:
Here’s the updated code with these improvements: @@ -337,13 +337,15 @@ def run_block(self, language='zh'):
answer_text = '\n\n'.join(
'\n\n'.join(a.get('content') for a in answer) for answer in answer_text_list).rstrip()
+
+ # Using enumerate to get both index and value for better readability
+ answer_list = [item for item in answer_text_list]
self.work_flow_post_handler.handler(self.params['chat_id'], self.params['chat_record_id'],
answer_text,
self)
return (
self.base_to_response.to_block_response(self.params['chat_id'],
self.params['chat_record_id'], answer_text, True,
- message_tokens, answer_tokens,
- _status=status.HTTP_200_OK if self.status == 200 else status.HTTP_500_INTERNAL_SERVER_ERROR))
+ message_tokens, answer_tokens,
+ _status=status.HTTP_200_OK if self.status == 200 else status.HTTP_500_INTERNAL_SERVER_ERROR,
+ other_params={'answer_list': answer_list})
async def run_stream(self, current_node, node_result_future, language='zh'):
"""These changes will improve the legibility, consistency, and possibly performance slightly, especially when dealing with larger data sets. However, for production-level applications, additional optimizations like caching or parallel processing might be considered depending on the workload. |
||
|
|
||
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 appears to be part of a language model's execution logic where it processes answers and streams responses. Here are some observations and suggestions:
Empty String Check: The
answer_textvariable is initialized as an empty string, but this might not be intended if you expect it to hold the final response content.Reduce Function Overhead: Using the
reducefunction with a lambda expression seems unnecessary here sincejoin()can be more efficient for concatenating lists into strings.Multiple Join Calls: You have multiple calls to
join(), which could lead to redundancy and performance issues. Consider combining these joins into a single call.Default Argument Types: The type hints for parameters like
_statussuggest they should be expected to accept integers rather than enums/types that match Django's HTTP status codes (e.g.,http.HttpResponse). Ensure correct typing.Here’s an optimized version of the code based on these points:
Notes:
params[...]with actual context-dependent parameter names from your application._statusis correctly passed along if necessary, possibly by modifyingself.paramsbefore passing toto_block_response.