system_to_response.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. # coding=utf-8
  2. """
  3. @project: MaxKB
  4. @Author:虎
  5. @file: system_to_response.py
  6. @date:2024/9/6 18:03
  7. @desc:
  8. """
  9. import json
  10. from rest_framework import status
  11. from common.handle.base_to_response import BaseToResponse
  12. from common.result import result
  13. class SystemToResponse(BaseToResponse):
  14. def to_block_response(self, chat_id, chat_record_id, content, is_end, completion_tokens,
  15. prompt_tokens, other_params: dict = None,
  16. _status=status.HTTP_200_OK):
  17. if other_params is None:
  18. other_params = {}
  19. return result.success({'chat_id': str(chat_id), 'id': str(chat_record_id), 'operate': True,
  20. 'content': content, 'is_end': is_end, **other_params,
  21. 'completion_tokens': completion_tokens, 'prompt_tokens': prompt_tokens},
  22. response_status=_status,
  23. code=_status)
  24. def to_stream_chunk_response(self, chat_id, chat_record_id, node_id, up_node_id_list, content, is_end,
  25. completion_tokens,
  26. prompt_tokens, other_params: dict = None):
  27. if other_params is None:
  28. other_params = {}
  29. chunk = json.dumps({'chat_id': str(chat_id), 'chat_record_id': str(chat_record_id), 'operate': True,
  30. 'content': content, 'node_id': node_id, 'up_node_id_list': up_node_id_list,
  31. 'is_end': is_end,
  32. 'usage': {'completion_tokens': completion_tokens,
  33. 'prompt_tokens': prompt_tokens,
  34. 'total_tokens': completion_tokens + prompt_tokens},
  35. **other_params})
  36. return super().format_stream_chunk(chunk)