base_to_response.py 861 B

123456789101112131415161718192021222324252627282930
  1. # coding=utf-8
  2. """
  3. @project: MaxKB
  4. @Author:虎
  5. @file: base_to_response.py
  6. @date:2024/9/6 16:04
  7. @desc:
  8. """
  9. from abc import ABC, abstractmethod
  10. from rest_framework import status
  11. class BaseToResponse(ABC):
  12. @abstractmethod
  13. def to_block_response(self, chat_id, chat_record_id, content, is_end, completion_tokens,
  14. prompt_tokens, other_params: dict = None,
  15. _status=status.HTTP_200_OK):
  16. pass
  17. @abstractmethod
  18. def to_stream_chunk_response(self, chat_id, chat_record_id, node_id, up_node_id_list, content, is_end,
  19. completion_tokens,
  20. prompt_tokens, other_params: dict = None):
  21. pass
  22. @staticmethod
  23. def format_stream_chunk(response_str):
  24. return 'data: ' + response_str + '\n\n'