Skip to content
Open
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
4 changes: 2 additions & 2 deletions apps/common/handle/impl/response/openai_to_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def to_block_response(self, chat_id, chat_record_id, content, is_end, prompt_tok
BlockChoice(finish_reason='stop', index=0, chat_id=chat_id,
answer_list=other_params.get('answer_list', ""),
message=ChatCompletionMessage(role='assistant', content=content))],
created=timezone.now().second, model='', object='chat.completion',
created=int(timezone.now().timestamp()), model='', object='chat.completion',
usage=CompletionUsage(completion_tokens=completion_tokens,
prompt_tokens=prompt_tokens,
total_tokens=completion_tokens + prompt_tokens)
Expand All @@ -42,7 +42,7 @@ def to_stream_chunk_response(self, chat_id, chat_record_id, node_id, up_node_id_
if other_params is None:
other_params = {}
chunk = ChatCompletionChunk(id=chat_record_id, model='', object='chat.completion.chunk',
created=timezone.now().second, choices=[
created=int(timezone.now().timestamp()), 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,
Expand Down
23 changes: 23 additions & 0 deletions apps/common/tests_openai_to_response.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import datetime
import json
import unittest
from unittest import mock

from common.handle.impl.response.openai_to_response import OpenaiToResponse


class OpenaiToResponseCreatedTestCase(unittest.TestCase):
"""`created` must be a Unix timestamp, as in the OpenAI API, not the seconds field of the clock."""

NOW = datetime.datetime(2026, 11, 3, 2, 35, 7, tzinfo=datetime.timezone.utc)

def test_block_response_created_is_unix_timestamp(self):
with mock.patch('common.handle.impl.response.openai_to_response.timezone.now', return_value=self.NOW):
response = OpenaiToResponse().to_block_response('chat', 'record', 'hi', True, 1, 2)
self.assertEqual(json.loads(response.content)['created'], int(self.NOW.timestamp()))

def test_stream_chunk_created_is_unix_timestamp(self):
with mock.patch('common.handle.impl.response.openai_to_response.timezone.now', return_value=self.NOW):
chunk = OpenaiToResponse().to_stream_chunk_response('chat', 'record', 'node', [], 'hi', False, 1, 2)
payload = json.loads(chunk[len('data: '):])
self.assertEqual(payload['created'], int(self.NOW.timestamp()))