2024-09-09 06:47:25 +00:00
|
|
|
|
# coding=utf-8
|
|
|
|
|
|
"""
|
|
|
|
|
|
@project: MaxKB
|
|
|
|
|
|
@Author:虎
|
|
|
|
|
|
@file: openai_to_response.py
|
|
|
|
|
|
@date:2024/9/6 16:08
|
|
|
|
|
|
@desc:
|
|
|
|
|
|
"""
|
|
|
|
|
|
import datetime
|
|
|
|
|
|
|
|
|
|
|
|
from django.http import JsonResponse
|
|
|
|
|
|
from openai.types import CompletionUsage
|
|
|
|
|
|
from openai.types.chat import ChatCompletionChunk, ChatCompletionMessage, ChatCompletion
|
|
|
|
|
|
from openai.types.chat.chat_completion import Choice as BlockChoice
|
|
|
|
|
|
from openai.types.chat.chat_completion_chunk import Choice, ChoiceDelta
|
|
|
|
|
|
from rest_framework import status
|
|
|
|
|
|
|
|
|
|
|
|
from common.handle.base_to_response import BaseToResponse
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class OpenaiToResponse(BaseToResponse):
|
|
|
|
|
|
def to_block_response(self, chat_id, chat_record_id, content, is_end, completion_tokens, prompt_tokens,
|
2024-11-29 11:26:16 +00:00
|
|
|
|
other_params: dict = None,
|
2024-09-09 06:47:25 +00:00
|
|
|
|
_status=status.HTTP_200_OK):
|
2025-02-25 06:22:51 +00:00
|
|
|
|
if other_params is None:
|
|
|
|
|
|
other_params = {}
|
2024-09-09 06:47:25 +00:00
|
|
|
|
data = ChatCompletion(id=chat_record_id, choices=[
|
|
|
|
|
|
BlockChoice(finish_reason='stop', index=0, chat_id=chat_id,
|
2025-03-04 11:31:49 +00:00
|
|
|
|
answer_list=other_params.get('answer_list', ""),
|
2024-09-09 06:47:25 +00:00
|
|
|
|
message=ChatCompletionMessage(role='assistant', content=content))],
|
|
|
|
|
|
created=datetime.datetime.now().second, model='', object='chat.completion',
|
|
|
|
|
|
usage=CompletionUsage(completion_tokens=completion_tokens,
|
|
|
|
|
|
prompt_tokens=prompt_tokens,
|
|
|
|
|
|
total_tokens=completion_tokens + prompt_tokens)
|
|
|
|
|
|
).dict()
|
2024-09-20 10:46:49 +00:00
|
|
|
|
return JsonResponse(data=data, status=_status)
|
2024-09-09 06:47:25 +00:00
|
|
|
|
|
2025-02-25 06:22:51 +00:00
|
|
|
|
def to_stream_chunk_response(self, chat_id, chat_record_id, node_id, up_node_id_list, content, is_end,
|
|
|
|
|
|
completion_tokens,
|
2024-11-29 11:26:16 +00:00
|
|
|
|
prompt_tokens, other_params: dict = None):
|
2025-02-25 06:22:51 +00:00
|
|
|
|
if other_params is None:
|
|
|
|
|
|
other_params = {}
|
2024-09-09 06:47:25 +00:00
|
|
|
|
chunk = ChatCompletionChunk(id=chat_record_id, model='', object='chat.completion.chunk',
|
2025-02-25 06:22:51 +00:00
|
|
|
|
created=datetime.datetime.now().second, choices=[
|
|
|
|
|
|
Choice(delta=ChoiceDelta(content=content, reasoning_content=other_params.get('reasoning_content', ""),
|
|
|
|
|
|
chat_id=chat_id),
|
|
|
|
|
|
finish_reason='stop' if is_end else None,
|
2024-09-09 06:47:25 +00:00
|
|
|
|
index=0)],
|
|
|
|
|
|
usage=CompletionUsage(completion_tokens=completion_tokens,
|
|
|
|
|
|
prompt_tokens=prompt_tokens,
|
|
|
|
|
|
total_tokens=completion_tokens + prompt_tokens)).json()
|
|
|
|
|
|
return super().format_stream_chunk(chunk)
|