mirror of
https://github.com/jumpserver/jumpserver.git
synced 2025-09-05 17:30:30 +00:00
feat: chat prompt
This commit is contained in:
@@ -4,9 +4,13 @@ from django.conf import settings
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from rest_framework import status
|
||||
from rest_framework.generics import GenericAPIView
|
||||
from rest_framework.views import Response
|
||||
from rest_framework.response import Response
|
||||
|
||||
from common.api import JMSModelViewSet
|
||||
from common.permissions import IsValidUser, OnlySuperUser
|
||||
from .. import serializers
|
||||
from ..models import ChatPrompt
|
||||
from ..prompt import DefaultChatPrompt
|
||||
|
||||
|
||||
class ChatAITestingAPI(GenericAPIView):
|
||||
@@ -76,3 +80,39 @@ class ChatAITestingAPI(GenericAPIView):
|
||||
_status, msg = status.HTTP_400_BAD_REQUEST, error
|
||||
|
||||
return Response(status=_status, data={'msg': msg})
|
||||
|
||||
|
||||
class ChatPromptViewSet(JMSModelViewSet):
|
||||
serializer_classes = {
|
||||
'default': serializers.ChatPromptSerializer,
|
||||
}
|
||||
permission_classes = [IsValidUser]
|
||||
queryset = ChatPrompt.objects.all()
|
||||
http_method_names = ['get', 'options']
|
||||
filterset_fields = ['name']
|
||||
search_fields = filterset_fields
|
||||
|
||||
def get_permissions(self):
|
||||
if self.action in ['create', 'update', 'partial_update', 'destroy']:
|
||||
self.permission_classes = [OnlySuperUser]
|
||||
return super().get_permissions()
|
||||
|
||||
def filter_default_prompts(self):
|
||||
lang = self.request.LANGUAGE_CODE
|
||||
default_prompts = DefaultChatPrompt.get_prompts(lang)
|
||||
search_query = self.request.query_params.get('search')
|
||||
search_query = search_query or self.request.query_params.get('name')
|
||||
if not search_query:
|
||||
return default_prompts
|
||||
|
||||
search_query = search_query.lower()
|
||||
filtered_prompts = [
|
||||
prompt for prompt in default_prompts
|
||||
if search_query in prompt['name'].lower()
|
||||
]
|
||||
return filtered_prompts
|
||||
|
||||
def filter_queryset(self, queryset):
|
||||
queryset = super().filter_queryset(queryset)
|
||||
default_prompts = self.filter_default_prompts()
|
||||
return list(queryset) + default_prompts
|
||||
|
Reference in New Issue
Block a user