-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat: application chat log #3230
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 |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| # coding=utf-8 | ||
| """ | ||
| @project: MaxKB | ||
| @Author:虎虎 | ||
| @file: application_chat.py | ||
| @date:2025/6/10 13:54 | ||
| @desc: | ||
| """ | ||
| from django.utils.translation import gettext_lazy as _ | ||
| from drf_spectacular.types import OpenApiTypes | ||
| from drf_spectacular.utils import OpenApiParameter | ||
|
|
||
| from application.serializers.application_chat import ApplicationChatQuerySerializers, \ | ||
| ApplicationChatResponseSerializers, ApplicationChatRecordExportRequest | ||
| from common.mixins.api_mixin import APIMixin | ||
| from common.result import ResultSerializer, ResultPageSerializer | ||
|
|
||
|
|
||
| class ApplicationChatListResponseSerializers(ResultSerializer): | ||
| def get_data(self): | ||
| return ApplicationChatResponseSerializers(many=True) | ||
|
|
||
|
|
||
| class ApplicationChatPageResponseSerializers(ResultPageSerializer): | ||
| def get_data(self): | ||
| return ApplicationChatResponseSerializers(many=True) | ||
|
|
||
|
|
||
| class ApplicationChatQueryAPI(APIMixin): | ||
| @staticmethod | ||
| def get_request(): | ||
| return ApplicationChatQuerySerializers | ||
|
|
||
| @staticmethod | ||
| def get_parameters(): | ||
| return [ | ||
| OpenApiParameter( | ||
| name="workspace_id", | ||
| description="工作空间id", | ||
| type=OpenApiTypes.STR, | ||
| location='path', | ||
| required=True, | ||
| ), | ||
| OpenApiParameter( | ||
| name="application_id", | ||
| description="application ID", | ||
| type=OpenApiTypes.STR, | ||
| location='path', | ||
| required=True, | ||
| ), OpenApiParameter( | ||
| name="start_time", | ||
| description="start Time", | ||
| type=OpenApiTypes.STR, | ||
| required=True, | ||
| ), | ||
| OpenApiParameter( | ||
| name="end_time", | ||
| description="end Time", | ||
| type=OpenApiTypes.STR, | ||
| required=True, | ||
| ), | ||
| OpenApiParameter( | ||
| name="abstract", | ||
| description="summary", | ||
| type=OpenApiTypes.STR, | ||
| required=False, | ||
| ), | ||
| OpenApiParameter( | ||
| name="min_star", | ||
| description=_("Minimum number of likes"), | ||
| type=OpenApiTypes.INT, | ||
| required=False, | ||
| ), | ||
| OpenApiParameter( | ||
| name="min_trample", | ||
| description=_("Minimum number of clicks"), | ||
| type=OpenApiTypes.INT, | ||
| required=False, | ||
| ), | ||
| OpenApiParameter( | ||
| name="comparer", | ||
| description=_("Comparator"), | ||
| type=OpenApiTypes.STR, | ||
| required=False, | ||
| ), | ||
| ] | ||
|
|
||
| @staticmethod | ||
| def get_response(): | ||
| return ApplicationChatListResponseSerializers | ||
|
|
||
|
|
||
| class ApplicationChatQueryPageAPI(APIMixin): | ||
| @staticmethod | ||
| def get_request(): | ||
| return ApplicationChatQueryAPI.get_request() | ||
|
|
||
| @staticmethod | ||
| def get_parameters(): | ||
| return [ | ||
| *ApplicationChatQueryAPI.get_parameters(), | ||
| OpenApiParameter( | ||
| name="current_page", | ||
| description=_("Current page"), | ||
| type=OpenApiTypes.INT, | ||
| location='path', | ||
| required=True, | ||
| ), | ||
| OpenApiParameter( | ||
| name="page_size", | ||
| description=_("Page size"), | ||
| type=OpenApiTypes.INT, | ||
| location='path', | ||
| required=True, | ||
| ), | ||
|
|
||
| ] | ||
|
|
||
| @staticmethod | ||
| def get_response(): | ||
| return ApplicationChatPageResponseSerializers | ||
|
|
||
|
|
||
| class ApplicationChatExportAPI(APIMixin): | ||
| @staticmethod | ||
| def get_request(): | ||
| return ApplicationChatRecordExportRequest | ||
|
|
||
| @staticmethod | ||
| def get_parameters(): | ||
| return ApplicationChatQueryAPI.get_parameters() | ||
|
|
||
| @staticmethod | ||
| def get_response(): | ||
| return None | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,180 @@ | ||
| # coding=utf-8 | ||
| """ | ||
| @project: MaxKB | ||
| @Author:虎虎 | ||
| @file: application_chat_record.py | ||
| @date:2025/6/10 15:19 | ||
| @desc: | ||
| """ | ||
| from django.utils.translation import gettext_lazy as _ | ||
| from drf_spectacular.types import OpenApiTypes | ||
| from drf_spectacular.utils import OpenApiParameter | ||
|
|
||
| from application.serializers.application_chat_record import ApplicationChatRecordAddKnowledgeSerializer, \ | ||
| ApplicationChatRecordImproveInstanceSerializer | ||
| from common.mixins.api_mixin import APIMixin | ||
|
|
||
|
|
||
| class ApplicationChatRecordQueryAPI(APIMixin): | ||
| @staticmethod | ||
| def get_response(): | ||
| pass | ||
|
|
||
| @staticmethod | ||
| def get_request(): | ||
| pass | ||
|
|
||
| @staticmethod | ||
| def get_parameters(): | ||
| return [ | ||
| OpenApiParameter( | ||
| name="workspace_id", | ||
| description="工作空间id", | ||
| type=OpenApiTypes.STR, | ||
| location='path', | ||
| required=True, | ||
| ), | ||
| OpenApiParameter( | ||
| name="application_id", | ||
| description="Application ID", | ||
| type=OpenApiTypes.STR, | ||
| location='path', | ||
| required=True, | ||
| ), | ||
| OpenApiParameter( | ||
| name="chat_id", | ||
| description=_("Chat ID"), | ||
| type=OpenApiTypes.STR, | ||
| location='path', | ||
| required=True, | ||
| ), | ||
| OpenApiParameter( | ||
| name="order_asc", | ||
| description=_("Is it in order"), | ||
| type=OpenApiTypes.BOOL, | ||
| required=True, | ||
| ) | ||
| ] | ||
|
|
||
|
|
||
| class ApplicationChatRecordPageQueryAPI(APIMixin): | ||
| @staticmethod | ||
| def get_response(): | ||
| pass | ||
|
|
||
| @staticmethod | ||
| def get_request(): | ||
| pass | ||
|
|
||
| @staticmethod | ||
| def get_parameters(): | ||
| return [*ApplicationChatRecordQueryAPI.get_parameters(), | ||
| OpenApiParameter( | ||
| name="current_page", | ||
| description=_("Current page"), | ||
| type=OpenApiTypes.INT, | ||
| location='path', | ||
| required=True, | ||
| ), | ||
| OpenApiParameter( | ||
| name="page_size", | ||
| description=_("Page size"), | ||
| type=OpenApiTypes.INT, | ||
| location='path', | ||
| required=True, | ||
| )] | ||
|
|
||
|
|
||
| class ApplicationChatRecordImproveParagraphAPI(APIMixin): | ||
| @staticmethod | ||
| def get_response(): | ||
| pass | ||
|
|
||
| @staticmethod | ||
| def get_request(): | ||
| return ApplicationChatRecordImproveInstanceSerializer | ||
|
|
||
| @staticmethod | ||
| def get_parameters(): | ||
| return [OpenApiParameter( | ||
| name="workspace_id", | ||
| description="工作空间id", | ||
| type=OpenApiTypes.STR, | ||
| location='path', | ||
| required=True, | ||
| ), | ||
| OpenApiParameter( | ||
| name="application_id", | ||
| description="Application ID", | ||
| type=OpenApiTypes.STR, | ||
| location='path', | ||
| required=True, | ||
| ), | ||
| OpenApiParameter( | ||
| name="chat_id", | ||
| description=_("Chat ID"), | ||
| type=OpenApiTypes.STR, | ||
| location='path', | ||
| required=True, | ||
| ), | ||
| OpenApiParameter( | ||
| name="chat_record_id", | ||
| description=_("Chat Record ID"), | ||
| type=OpenApiTypes.STR, | ||
| location='path', | ||
| required=True, | ||
| ), | ||
| OpenApiParameter( | ||
| name="knowledge_id", | ||
| description=_("Knowledge ID"), | ||
| type=OpenApiTypes.STR, | ||
| location='path', | ||
| required=True, | ||
| ), | ||
| OpenApiParameter( | ||
| name="document_id", | ||
| description=_("Document ID"), | ||
| type=OpenApiTypes.STR, | ||
| location='path', | ||
| required=True, | ||
| ) | ||
| ] | ||
|
|
||
| class Operate(APIMixin): | ||
| @staticmethod | ||
| def get_parameters(): | ||
| return [*ApplicationChatRecordImproveParagraphAPI.get_parameters(), OpenApiParameter( | ||
| name="paragraph_id", | ||
| description=_("Paragraph ID"), | ||
| type=OpenApiTypes.STR, | ||
| location='path', | ||
| required=True, | ||
| )] | ||
|
|
||
|
|
||
| class ApplicationChatRecordAddKnowledgeAPI(APIMixin): | ||
| @staticmethod | ||
| def get_request(): | ||
| return ApplicationChatRecordAddKnowledgeSerializer | ||
|
|
||
| @staticmethod | ||
| def get_response(): | ||
| return None | ||
|
|
||
| @staticmethod | ||
| def get_parameters(): | ||
| return [ | ||
| OpenApiParameter( | ||
| name="workspace_id", | ||
| description="工作空间id", | ||
| type=OpenApiTypes.STR, | ||
| location='path', | ||
| required=True, | ||
| ), | ||
| OpenApiParameter( | ||
| name="application_id", | ||
| description="Application ID", | ||
| type=OpenApiTypes.STR, | ||
| location='path', | ||
| required=True, | ||
| )] | ||
|
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 are several areas of improvement and corrections needed in your code:
Here’s an improved version of the code with these considerations addressed: # coding=utf-8
"""
@project: MaxKB
@Author:虎虎
@file: application_chat_record.py
@date:2025/6/10 15:19
@desc:
"""
from django.utils.translation import gettext_lazy as _
from drf_spectacular.types import OpenApiTypes
from drf_spectacular.utils import OpenApiParameter
from application.serializers.application_chat_record import (
ApplicationChatRecordAddKnowledgeSerializer,
ApplicationChatRecordImproveInstanceSerializer,
)
from common.mixins.api_mixin import APIMixin
class ApplicationChatRecordQueryAPI(APIMixin):
@staticmethod
def get_response():
"""Define response for query API."""
# Add your logic here
pass
@staticmethod
def get_request():
"""Define request body for query API."""
# Add your logic here
return {}
@staticmethod
def get_parameters():
"""
Define parameters for query API.
:return: A list of OpenApiParameters defining various parameters
"""
return [
OpenApiParameter(
name="workspace_id",
description="工作空间ID",
type=OpenApiTypes.STR,
location="path",
required=True,
),
OpenApiParameter(
name="application_id",
description=_('Application ID'),
type=OpenApiTypes.STR,
location='path',
required=True,
),
OpenApiParameter(
name="chat_id",
description=_("Chat ID"),
type=OpenApiTypes.STR,
location='path',
required=True,
),
OpenApiParameter(
name="order_asc",
description=_("Is it ordered?"),
type=OpenApiTypes.BOOL,
location='query',
required=True,
)
]
class ApplicationChatRecordPageQueryAPI(APIMixin):
@staticmethod
def get_response():
"""Define response for pagination query API."""
# Add your logic here
pass
@staticmethod
def get_request():
"""Define request body for pagination query API."""
# Add your logic here
return {}
@staticmethod
def get_parameters():
"""
Combine parent parameters and add pagination parameters.
:return: A combination of OpenApiParameters from parent and new ones
"""
combined_params = [*ApplicationChatRecordQueryAPI.get_parameters()]
combined_params.append(OpenApiParameter(
name="current_page",
description=_("当前页码"), # Use proper Chinese term
type=OpenApiTypes.INT,
location='query', # Typically used in GET requests, switch to 'path' if path-based paging
required=True
))
combined_params.append(OpenApiParameter(
name="page_size",
description=("每页显示条数"), # Use proper Chinese term
type=OpenApiTypes.INT,
location='query',
required=True
))
return combined_params
class ApplicationChatRecordImproveParagraphAPI(APIMixin):
@staticmethod
def get_request():
"""Schema for improving paragraph within chat record by knowledge example."""
# Use the serializer defined above
return ApplicationChatRecordImproveInstanceSerializer
@staticmethod
def get_response():
"""Response after successful improvements."""
# No specific output, can be None
return None
@staticmethod
def get_parameters():
"""
Parameters required for improving a paragraph.
:return: List containing multiple OpenApiParameters
"""
return [
OpenApiParameter(
name="work_space_id", # Correction of typo "worck space"
description="工作空间id",
type=OpenApiTypes.STR,
location='path',
required=True,
),
OpenApiParameter(
name="application_id",
description=_("Application ID"),
type=OpenApiTypes.STR,
location='path',
required=True,
),
OpenApiParameter(
name="chat_id",
description=_("聊天ID"),
type=OpenApiTypes.STR,
location='path',
required=True,
),
OpenApiParameter(
name="chat_record_id",
description=_("聊天记录ID"),
type=OpenApiTypes.STR,
location='path',
required=True,
),
OpenApiParameter(
name="knowledge_id",
description=_("知识ID"),
type=OpenApiTypes.STR,
location='path',
required=True,
),
OpenApiParameter(
name="document_id",
description=_("文档ID"),
type=OpenApiTypes.STR,
location='path',
required=True,
)
]
class Operation(APIMixin): # Corrected spelling mistake to Class Name 'Operation'
@staticmethod
def get_parameters():
"""
Additional parameters specifically for performing an operation within improve_paragraph endpoint.
:return: List of OpenApiParameters
"""
return [
*(ApplicationChatRecordImproveParagraphAPI.get_parameters()),
OpenApiParameter(
name="paragraph_id",
description=_("段落ID"),
type=OpenApiTypes.STR,
location='path',
required=True,
),
]Key Corrections Made:
|
||
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.
There are several issues and optimizations I can suggest for the provided Python code:
Issues
Inconsistent Naming Conventions: The class attributes
get_datainResultSerializer,ApplicationChatListResponseSerializers, andApplicationChatPageResponseSerializersshould be consistent with standard naming conventions.Empty Response Serializer (
None) in Export API: In theApplicationChatExportAPI, there is an issue where the response serializer is set toNone. This may not allow returning expected data formats like CSV or Excel files. Consider implementing a serialization module for exporting chat records.Static Method Return Types: Static methods currently do not return anything (i.e., they return
None). If these methods were intended to perform some operations and return something, modify them accordingly. For example:Potential Improper Use of Path Variables: Ensure that the path variables (
workspace_idandapplication_id) match those defined in your URL patterns or view configurations correctly.Error Handling: Add more robust error handling to manage exceptions during API execution, especially related to invalid inputs or failed database queries.
Documentation: Enhance the docstrings within classes and functions for better clarity, possibly providing examples when applicable.
Logging: Adding logging before performing database queries would help trace potential issues and debug easier.
Suggestions
Consistent Naming Convention
Update all static method return types from
Noneto appropriate serializers:Error Handling
Add exception handling near critical sections of your API logic:
Modularization
Consider separating business logic into models/service/serializer layers for better modularity and maintainability.
By addressing these points, you can make the codebase cleaner, more maintainable, and improve its overall functionality and reliability.