Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 4.2.15 on 2025-01-20 03:20

from django.db import migrations, models
import smartdoc.conf


class Migration(migrations.Migration):

dependencies = [
('application', '0023_application_stt_autosend'),
]

operations = [
migrations.AddField(
model_name='applicationaccesstoken',
name='language',
field=models.CharField(default=smartdoc.conf.Config.get_language_code, max_length=10, verbose_name='语言'),
),
]
3 changes: 3 additions & 0 deletions apps/application/models/api_key_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from application.models import Application
from common.mixins.app_model_mixin import AppModelMixin
from smartdoc.const import CONFIG
from users.models import User


Expand Down Expand Up @@ -45,6 +46,8 @@ class ApplicationAccessToken(AppModelMixin):
, default=list)
show_source = models.BooleanField(default=False, verbose_name="是否显示知识来源")

language = models.CharField(max_length=10, verbose_name="语言", default=CONFIG.get_language_code)

class Meta:
db_table = "application_access_token"

Expand Down
5 changes: 5 additions & 0 deletions apps/application/serializers/application_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,8 @@ class AccessTokenEditSerializer(serializers.Serializer):
show_source = serializers.BooleanField(required=False,
error_messages=ErrMessage.boolean(
_("Whether to display knowledge sources")))
language = serializers.CharField(required=False, allow_blank=True, allow_null=True,
error_messages=ErrMessage.char(_("language")))

def edit(self, instance: Dict, with_valid=True):
if with_valid:
Expand All @@ -358,6 +360,8 @@ def edit(self, instance: Dict, with_valid=True):
application_access_token.white_list = instance.get('white_list')
if 'show_source' in instance and instance.get('show_source') is not None:
application_access_token.show_source = instance.get('show_source')
if 'language' in instance and instance.get('language') is not None:
application_access_token.language = instance.get('language')
application_access_token.save()
application_setting_model = DBModelManage.get_model('application_setting')
xpack_cache = DBModelManage.get_model('xpack_cache')
Expand Down Expand Up @@ -980,6 +984,7 @@ def profile(self, with_valid=True):
'file_upload_setting': application.file_upload_setting,
'work_flow': application.work_flow,
'show_source': application_access_token.show_source,
'language': application_access_token.language,
**application_setting_dict})

@transaction.atomic
Expand Down
54 changes: 32 additions & 22 deletions apps/application/swagger_api/application_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ def get_request_body_api():
'show_source': openapi.Schema(type=openapi.TYPE_BOOLEAN,
title=_("Whether to display knowledge sources"),
description=_("Whether to display knowledge sources")),
'language': openapi.Schema(type=openapi.TYPE_STRING,
title=_("language"),
description=_("language"))
}
)

Expand Down Expand Up @@ -282,24 +285,24 @@ def get_request_body_api():
properties={
'prompt': openapi.Schema(type=openapi.TYPE_STRING, title=_("Prompt word"),
description=_("Prompt word"),
default=_("""
Known information:
{data}
Answer requirements:
- If you don't know the answer or don't get the answer, please answer "No relevant information found in the knowledge base, it is recommended to consult relevant technical support or refer to official documents for operation".
- Avoid mentioning that you got the knowledge from <data></data>.
- Please keep the answer consistent with the description in <data></data>.
- Please use markdown syntax to optimize the format of the answer.
- Please return the image link, link address and script language in <data></data> completely.
- Please answer in the same language as the question.
Question:
{question}
""")),
default=_(("Known information:\n"
"{data}\n"
"Answer requirements:\n"
"- If you don't know the answer or don't get the answer, please answer \"No relevant information found in the knowledge base, it is recommended to consult relevant technical support or refer to official documents for operation\".\n"
"- Avoid mentioning that you got the knowledge from <data></data>.\n"
"- Please keep the answer consistent with the description in <data></data>.\n"
"- Please use markdown syntax to optimize the format of the answer.\n"
"- Please return the image link, link address and script language in <data></data> completely.\n"
"- Please answer in the same language as the question.\n"
"Question:\n"
"{question}"))),

'system': openapi.Schema(type=openapi.TYPE_STRING, title=_("System prompt words (role)"),
description=_("System prompt words (role)")),
'no_references_prompt': openapi.Schema(type=openapi.TYPE_STRING, title=_("No citation segmentation prompt"),
default="{question}", description=_("No citation segmentation prompt"))
'no_references_prompt': openapi.Schema(type=openapi.TYPE_STRING,
title=_("No citation segmentation prompt"),
default="{question}",
description=_("No citation segmentation prompt"))

}
)
Expand All @@ -323,22 +326,29 @@ def get_request_body_api():
required=['name', 'desc', 'model_id', 'dialogue_number', 'dataset_setting', 'model_setting',
'problem_optimization', 'stt_model_enable', 'stt_model_enable', 'tts_type'],
properties={
'name': openapi.Schema(type=openapi.TYPE_STRING, title=_("Application Name"), description=_("Application Name")),
'desc': openapi.Schema(type=openapi.TYPE_STRING, title=_("Application Description"), description=_("Application Description")),
'model_id': openapi.Schema(type=openapi.TYPE_STRING, title=_("Model id"), description=_("Model id")),
"dialogue_number": openapi.Schema(type=openapi.TYPE_NUMBER, title=_("Number of multi-round conversations"),
'name': openapi.Schema(type=openapi.TYPE_STRING, title=_("Application Name"),
description=_("Application Name")),
'desc': openapi.Schema(type=openapi.TYPE_STRING, title=_("Application Description"),
description=_("Application Description")),
'model_id': openapi.Schema(type=openapi.TYPE_STRING, title=_("Model id"),
description=_("Model id")),
"dialogue_number": openapi.Schema(type=openapi.TYPE_NUMBER,
title=_("Number of multi-round conversations"),
description=_("Number of multi-round conversations")),
'prologue': openapi.Schema(type=openapi.TYPE_STRING, title=_("Opening remarks"), description=_("Opening remarks")),
'prologue': openapi.Schema(type=openapi.TYPE_STRING, title=_("Opening remarks"),
description=_("Opening remarks")),
'dataset_id_list': openapi.Schema(type=openapi.TYPE_ARRAY,
items=openapi.Schema(type=openapi.TYPE_STRING),
title=_("List of associated knowledge base IDs"), description=_("List of associated knowledge base IDs")),
title=_("List of associated knowledge base IDs"),
description=_("List of associated knowledge base IDs")),
'dataset_setting': ApplicationApi.DatasetSetting.get_request_body_api(),
'model_setting': ApplicationApi.ModelSetting.get_request_body_api(),
'problem_optimization': openapi.Schema(type=openapi.TYPE_BOOLEAN, title=_("Problem Optimization"),
description=_("Problem Optimization"), default=True),
'type': openapi.Schema(type=openapi.TYPE_STRING, title=_("Application Type"),
description=_("Application Type SIMPLE | WORK_FLOW")),
'problem_optimization_prompt': openapi.Schema(type=openapi.TYPE_STRING, title=_('Question optimization tips'),
'problem_optimization_prompt': openapi.Schema(type=openapi.TYPE_STRING,
title=_('Question optimization tips'),
description=_("Question optimization tips"),
default=_(
"() contains the user's question. Answer the guessed user's question based on the context ({question}) Requirement: Output a complete question and put it in the <data></data> tag")),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The provided code snippet appears to be part of an OpenAPI schema definition for a REST API. Here are some observations and improvements:

Irregularities/Potential Issues

  1. Duplicate stt_model_enable Property: The request body defines two instances of the same property ("stt_model_enable"), which might not be intended.

  2. Missing Comma Before Final Property: In the dataset_setting, model_setting, and other nested objects, there is no comma before the final property declaration, which may lead to syntax errors if used directly as JSON or Swagger files.

  3. Default String Formatting: The default strings contain newline characters at specific positions within {data} tags, which doesn't align with typical markdown formatting practices.

  4. Language Handling: There's a redundant mention of language handling in the get_request_body_api() function description, but it doesn't appear to be necessary or consistently implemented throughout.

  5. Prologue and Dialogue Number Documentation: These fields describe the opening remark ("prologue") and the number of multi-round conversations without specifying their types accurately (e.g., "number").

  6. No Citation Segmentation Prompt Translation: The translation string for "no_references_prompt" seems incomplete or missing content.

Optimization Suggestions

  1. Simplify Default Strings: Simplify the default strings for better readability. For example, remove unnecessary line breaks and ensure consistency in markdown usage.

  2. Fix Duplicate Properties: Ensure only one instance of "stt_model_enable" is present.

  3. Add Missing Commas: Add commas between each parameter list item to improve clarity when used as JSON or YAML.

  4. Consistent Language Usage: Clearly document how the answer should maintain consistency with the provided data and respect specified languages.

Here’s an optimized version of the code:

def get_request_body_api():
    return {
        'operationId': 'create_application',
        'parameters': [
            (
                openai.Parameter('Request Body', 'body'),
                None,
                {}
            ),
        ],
        'responses': {
        },
        'requestBody': {
            'required': True,
            'content': {
                'application/json': {
                    'schema': {
                        '$ref': '#/components/schemas/CreateApplicationReq'
                    }
                }
            }
        }
    }

And the corresponding definition for CreateApplicationReq schema:

def CreateApplicationReq_schema(name,
                             desc, 
                             model_id, 
                             dialogue_number, 
                             dataset_setting, 
                             model_setting, 
                             problem_optimization=False, 
                             type='SIMPLE', 
                             stt_model_enable=True):
    
    components = {"schemas": {}}
    schemas = components['schemas']

    applicationApiDatasetSetting = ApplicationApi.DatasetSetting.get_request_body_api()
    applicationModelSetting = ApplicationApi.ModelSetting.get_request_body_api()

    schemas["CreateApplicationReq"] = {
        "type": "object",
        "properties": {
            "name": {"type": "string", "title": _("Application Name"), "description": _("Application Name")},
            "desc": {"type": "string", "title": _("Application Description"), "description": _("Application Description")},
            "model_id": {"type": "string", "title": _("Model ID"), "description": _("Model ID")},
            "dialogue_number": {"type": "integer", "title": _("Number of Multi-Round Conversations"), "description": _("Number of Multi-Round Conversations")},
            "dataset_ids_list": {"type": "array", "items": {"type": "string"}, "title": _("List of Associated Knowledge Base IDs"), "description": _("List of associated knowledge base IDs")},
            "dataset_setting": {"$ref": "#/components/schemas/ApplicationApiDatasetSetting"},
            "model_setting": {"$ref": "#/components/schemas/ApplicationModelSetting"},
            "problem_optimization": {"type": "boolean", "title": _("Problem Optimization"), "description": _("Problem Optimization"), "default": False},
            "type": {"type": "string", "title": _("Application Type"), "description": _("Application Type (SIMPLE | WORK_FLOW)"}, 
            "problem_optimization_prompt": {"type": "string", "title": "_Question Optimization Tips_",
                                          "description": _("Question optimization tips"),
                                          "default": _("(_contains the user\'s question_) Output a complete question based on the context ({question}). Put it in the `<data></data>` tag")}
        }
    }

    return components

@app.route("/v1/applications", methods=["POST"])
@RestController("/v1")
@api_response(code=201)
@endpoint_description("Creates a new application.")
@parameter(parameter="applicationJson", location="json", required=True,
         description="JSON representation of the application configuration")
@ApiOperation(description="Create a new application.")
@RequestMapping(value="/applications", method={RequestMethod.POST})
public ResponseEntity createNewApplication(CreateApplication Req reqObject){
        // Implementation logic here
        return ResponseEntity.status(HttpStatus.CREATED).build();
}

class CreateApplicationReq{
    private String name;
    private String desc;
    private String model_id;
    private Integer dialogue_number;
    private List<String> dataset_ids_list;
    private ApplicationApiDatasetSetting dataset_setting;
    private ApplicationModelSetting model_setting;
    private Boolean problem_optimization;
    private String type;
    private String problem_optimization_prompt;

    // getters and setters
}

Replace applicationApiDatasetSetting and applicationModelSetting with actual implementations or references as per your requirements. Adjust class names and properties according to your project design needs. This approach improves organization, maintainability, and readability while addressing the identified issues.

Expand Down
6 changes: 3 additions & 3 deletions apps/common/mixins/app_model_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
@desc:
"""
from django.db import models
from django.utils.translation import gettext_lazy as _


class AppModelMixin(models.Model):
create_time = models.DateTimeField(verbose_name=_('Create time'), auto_now_add=True)
update_time = models.DateTimeField(verbose_name=_('Update time'), auto_now=True)
create_time = models.DateTimeField(verbose_name="创建时间", auto_now_add=True)
update_time = models.DateTimeField(verbose_name="修改时间", auto_now=True)

class Meta:
abstract = True
Expand Down
Loading
Loading