-
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 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 |
|---|---|---|
|
|
@@ -307,7 +307,11 @@ def execute_block(self, message_list: List[BaseMessage], | |
| return manage.get_base_to_response().to_block_response(str(chat_id), str(chat_record_id), | ||
| content, True, | ||
| request_token, response_token, | ||
| {'reasoning_content': reasoning_content}) | ||
| {'reasoning_content': reasoning_content, | ||
| 'answer_list': [{ | ||
| 'content': content, | ||
| 'reasoning_content': reasoning_content | ||
| }]}) | ||
| except Exception as e: | ||
| all_text = 'Exception:' + str(e) | ||
| write_context(self, manage, 0, 0, all_text) | ||
|
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. The provided code is mostly clean and functional. However, it can be optimized for readability and potential improvements based on future needs:
Here's a revised version with some minor adjustments: def execute_block(self, message_list: List[BaseMessage],
chat_id: str = None,
chat_record_id: int = None):
try:
if chat_id and chat_record_id:
result = manage.get_base_to_response().to_block_response(
chat_id=chat_id,
record_id=str(chat_record_id),
content=message_list,
is_system=True,
request_token="",
response_token="",
context={'reasoning_content': '', 'answer_list': [{'content': content, 'reasoning_content': reasoning_content}]}
)
return result
except Exception as e:
all_text = f'Exception:{str(e)}'
write_context(self, manage, 0, 0, all_text)Optimization Suggestion:
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. The code is mostly correct, but there are a couple of potential issues:
Here's the modified version of the function with these considerations: def execute_block(self, message_list: List[BaseMessage],
chat_id: int,
chat_record_id: int,
content: str,
request_token: str,
response_token: Optional[str] = None) -> ResponseBlockData:
try:
# Construct the data payload for managing responses
base_data_payload = {
'chat_id': chat_id,
'chat_record_id': chat_record_id,
'content': content,
'request_token': request_token,
'response_token': response_token,
'reasoning_content': reasoning_content,
'answer_list': [{'content': content, 'reasoning_content': reasoning_content}]}
# Get the block response using the constructed data
return manage.get_base_to_response().to_block_response(str(chat_id), str(chat_record_id),
base_data_payload['content'],
True,
request_token, response_token,
{'reasoning_content': reasoning_content})
except Exception as e:
all_text = f'Exception:{str(e)}'
write_context(self, manage, 0, 0, all_text)Key Changes Made:
|
||
|
|
||
| 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. 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:
Here’s an optimized version of the code based on these points: @@ -337,26 +337,28 @@ def run_block(self, language='zh'):
# Combine all answers into a single list without using reduce
answer_texts = [a.get('content') for sublist in answer_text_list for a in sublist]
# Join all parts into one continuous string separated by two newlines
answer_text = '\n\n'.join(answer_texts)
# Initialize other required variables (assumed present elsewhere in the code)
self.work_flow_post_handler.handler(
params['chat_id'],
params['chat_record_id'],
- answer_text,
+ answer_texts,
self
)
return self.base_to_response.to_block_response(
params['chat_id'],
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_texts}
)
def run_stream(self, current_node, node_result_future, language='zh'):
"""Notes:
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. |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,7 +13,7 @@ | |
| from django.core.cache import cache | ||
|
|
||
| # alg使用的算法 | ||
| HEADER = {'typ': 'JWP', 'alg': 'default'} | ||
| HEADER = {'type': 'JWP', 'alg': 'default'} | ||
| TOKEN_KEY = 'solomon_world_token' | ||
| TOKEN_SALT = 'solomonwanc@gmail.com' | ||
| TIME_OUT = 30 * 60 | ||
|
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. from django.core.cache import cache
# alg使用的算法
HEADER = {'type': 'JWP', 'alg': 'default'}
TOKEN_KEY = 'solomon_world_token'
TOKEN_SALT = 'solomonwanc@gmail.com'
TIME_OUT = 30 * 60The provided code appears to be correctly formatted and free of syntax errors. Here are a few minor suggestions for improvement:
HEADER = {
'typ': 'JWP',
'alg': 'default'
}
HEADER = {'typ': 'JWP', 'alg': 'default'}
TOKEN_KEY = 'SOLOMON_WORLD_TOKEN' # Consistent capitalization
TOKEN_SALT = 'soLomonWANc@gmail.com' # Lowercase email addresses are common practice for keys/salts
TIME_OUT = 30 * 60Additionally, consider if there's any specific context where using These changes will improve consistency and security in your codebase.
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. The code you provided has one issue that needs to be addressed:
In addition, here are some minor improvements or considerations for future maintenance and readability:
Overall, the code looks functional and follows good practices regarding naming and structure. The mentioned typo can safely be resolved without affecting its functionality. |
||
|
|
||
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 has a few minor issues:
Indentation: The
exceptblock's indentation is inconsistent with the rest of the method.Variable Naming: The variable
all_textshould likely be named more clearly, especially if it is intended to store detailed error messages rather than just exception strings.String Formatting: When logging the exception text, using
'Exception:' + str(e)can lead to errors if the exception object does not support string formatting. It would be safer to use f-strings (f'Exception: {str(e)}') if available.Return Type: The function signature specifies returning
Union[MessageBlockResponse, BaseMessage], but there seems to be a logical mismatch between this specification and the actual return value being returned by the function (aDict[str, Any]). Ensure that the return type matches the expected output.Here's a revised version of the code with some addressing these issues:
Explanation:
execute_blockmethod.response_dictfollows that pattern.These changes enhance code clarity and maintainability while ensuring correct behavior.