-
Notifications
You must be signed in to change notification settings - Fork 2k
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
feat: The demonstration page supports modifying dialogue summaries #2348
Changes from all commits
752e1eb
b50db41
cd03e6b
b1a3d97
43e2e5e
3d7a9fc
fbca792
f034f2a
097c5f8
83254bf
3518433
8b293ae
b6de93e
131b186
1a4cc6e
99b8448
5869af0
a6525d3
28c2d6b
ff99830
16cfca6
efd6de3
22fb799
1368d37
3e347d2
3361acf
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# coding=utf-8 | ||
|
||
from .impl import * |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# coding=utf-8 | ||
|
||
from typing import Type | ||
|
||
from django.utils.translation import gettext_lazy as _ | ||
from rest_framework import serializers | ||
|
||
from application.flow.i_step_node import INode, NodeResult | ||
from common.util.field_message import ErrMessage | ||
|
||
|
||
class VariableAssignNodeParamsSerializer(serializers.Serializer): | ||
variable_list = serializers.ListField(required=True, | ||
error_messages=ErrMessage.list(_("Reference Field"))) | ||
|
||
|
||
class IVariableAssignNode(INode): | ||
type = 'variable-assign-node' | ||
|
||
def get_node_params_serializer_class(self) -> Type[serializers.Serializer]: | ||
return VariableAssignNodeParamsSerializer | ||
|
||
def _run(self): | ||
return self.execute(**self.node_params_serializer.data, **self.flow_params_serializer.data) | ||
|
||
def execute(self, variable_list, stream, **kwargs) -> NodeResult: | ||
pass |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# coding=utf-8 | ||
""" | ||
@project: maxkb | ||
@Author:虎 | ||
@file: __init__.py | ||
@date:2024/6/11 17:49 | ||
@desc: | ||
""" | ||
from .base_variable_assign_node import * |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# coding=utf-8 | ||
import json | ||
from typing import List | ||
|
||
from application.flow.i_step_node import NodeResult | ||
from application.flow.step_node.variable_assign_node.i_variable_assign_node import IVariableAssignNode | ||
|
||
|
||
class BaseVariableAssignNode(IVariableAssignNode): | ||
def save_context(self, details, workflow_manage): | ||
self.context['variable_list'] = details.get('variable_list') | ||
self.context['result_list'] = details.get('result_list') | ||
|
||
def execute(self, variable_list, stream, **kwargs) -> NodeResult: | ||
# | ||
result_list = [] | ||
for variable in variable_list: | ||
if 'fields' not in variable: | ||
continue | ||
if 'global' == variable['fields'][0]: | ||
result = { | ||
'name': variable['name'], | ||
'input_value': self.get_reference_content(variable['fields']), | ||
} | ||
if variable['source'] == 'custom': | ||
if variable['type'] in ['dict', 'array']: | ||
if isinstance(variable['value'], dict) or isinstance(variable['value'], list): | ||
val = variable['value'] | ||
else: | ||
val = json.loads(variable['value']) | ||
self.workflow_manage.context[variable['fields'][1]] = val | ||
result['output_value'] = variable['value'] = val | ||
else: | ||
self.workflow_manage.context[variable['fields'][1]] = variable['value'] | ||
result['output_value'] = variable['value'] | ||
else: | ||
reference = self.get_reference_content(variable['reference']) | ||
self.workflow_manage.context[variable['fields'][1]] = reference | ||
result['output_value'] = reference | ||
result_list.append(result) | ||
|
||
return NodeResult({'variable_list': variable_list, 'result_list': result_list}, {}) | ||
|
||
def get_reference_content(self, fields: List[str]): | ||
return str(self.workflow_manage.get_reference_field( | ||
fields[0], | ||
fields[1:])) | ||
|
||
def get_details(self, index: int, **kwargs): | ||
return { | ||
'name': self.node.properties.get('stepName'), | ||
"index": index, | ||
'run_time': self.context.get('run_time'), | ||
'type': self.node.type, | ||
'variable_list': self.context.get('variable_list'), | ||
'result_list': self.context.get('result_list'), | ||
'status': self.status, | ||
'err_message': self.err_message | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,34 @@ def get_request_params_api(): | |
description=_('Application ID')) | ||
] | ||
|
||
class Operate(ApiMixin): | ||
@staticmethod | ||
def get_request_params_api(): | ||
return [openapi.Parameter(name='application_id', | ||
in_=openapi.IN_PATH, | ||
type=openapi.TYPE_STRING, | ||
required=True, | ||
description=_('Application ID')), | ||
openapi.Parameter(name='chat_id', | ||
in_=openapi.IN_PATH, | ||
type=openapi.TYPE_STRING, | ||
required=True, | ||
description=_('Conversation ID')), | ||
] | ||
|
||
class ReAbstract(ApiMixin): | ||
@staticmethod | ||
def get_request_body_api(): | ||
return openapi.Schema( | ||
type=openapi.TYPE_OBJECT, | ||
required=['abstract'], | ||
properties={ | ||
'abstract': openapi.Schema(type=openapi.TYPE_STRING, title=_("abstract"), | ||
description=_("abstract")) | ||
|
||
} | ||
) | ||
|
||
|
||
class OpenAIChatApi(ApiMixin): | ||
@staticmethod | ||
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 snippet looks generally structured but contains some areas that could benefit from improvements, such as documentation clarity and adherence to best practices. Here's a review with suggestions: General Comments:
Implementation Suggestions:
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -150,7 +150,7 @@ def post(self, request: Request, chat_id: str): | |
operation_id=_("Get the conversation list"), | ||
manual_parameters=ChatApi.get_request_params_api(), | ||
responses=result.get_api_array_response(ChatApi.get_response_body_api()), | ||
tags=[_("Application/Conversation Log")] | ||
tags=[_("Application/Conversation Log")] | ||
) | ||
@has_permissions( | ||
ViewPermission([RoleConstants.ADMIN, RoleConstants.USER, RoleConstants.APPLICATION_KEY], | ||
|
@@ -222,6 +222,23 @@ def delete(self, request: Request, application_id: str, chat_id: str): | |
data={'application_id': application_id, 'user_id': request.user.id, | ||
'chat_id': chat_id}).logic_delete()) | ||
|
||
@action(methods=['PUT'], detail=False) | ||
@swagger_auto_schema(operation_summary=_("Client modifies dialogue summary"), | ||
operation_id=_("Client modifies dialogue summary"), | ||
request_body=ChatClientHistoryApi.Operate.ReAbstract.get_request_body_api(), | ||
tags=[_("Application/Conversation Log")]) | ||
@has_permissions(ViewPermission( | ||
[RoleConstants.APPLICATION_ACCESS_TOKEN], | ||
[lambda r, keywords: Permission(group=Group.APPLICATION, operate=Operate.USE, | ||
dynamic_tag=keywords.get('application_id'))], | ||
compare=CompareConstants.AND), | ||
compare=CompareConstants.AND) | ||
def put(self, request: Request, application_id: str, chat_id: str): | ||
return result.success( | ||
ChatSerializers.Operate( | ||
data={'application_id': application_id, 'user_id': request.user.id, | ||
'chat_id': chat_id}).re_abstract(request.data)) | ||
|
||
class Page(APIView): | ||
authentication_classes = [TokenAuth] | ||
|
||
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 mostly clean and functional. However, I do have a few optimizations and suggestions:
These suggested changes should help optimize and clarify the given codebase. |
||
|
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.
Here are some recommendations for optimizing and improving the code:
Class Naming Conventions: Consider using PascalCase or CamelCase for class names to make them more readable.
Serializer Structure Simplification: The
ChatSerializers
has a nested serializer in itsOperate
class which adds unnecessary complexity. It might be better to have separate classes for different operations instead of nesting serializers.Error Handling Cleanup: Use consistent error handling throughout. Currently, it's okay but ensure that all exceptions raise the same type of exception.
Optimization in Logic Delete Method: The method can be optimized by directly accessing the database without filtering first, then updating based on conditions.
ReAbstract Instance Method:
.get()
inside the loop if you're using Python 3.7+, as dictionaries maintain insertion order.Add a check before attempting to update the abstract field to ensure there is an abstract value to overwrite.
General Formatting: Ensure consistent formatting including spacing between statements, variable definitions, etc., this improves readability.
Example refactored version:
This version consolidates similar functionalities under separate methods and ensures cleaner, more efficient execution of each operation.