refac
This commit is contained in:
1 parent
f8b3a32caf
commit
93415a48e8
2 files changed
+59
-22
No files matched your search
@@ -858,6 +858,9 @@ def convert_to_responses_payload(payload: dict) -> dict:
|
|||||||
if 'max_tokens' in responses_payload:
|
if 'max_tokens' in responses_payload:
|
||||||
responses_payload['max_output_tokens'] = responses_payload.pop('max_tokens')
|
responses_payload['max_output_tokens'] = responses_payload.pop('max_tokens')
|
||||||
|
|
||||||
|
if 'max_completion_tokens' in responses_payload:
|
||||||
|
responses_payload['max_output_tokens'] = responses_payload.pop('max_completion_tokens')
|
||||||
|
|
||||||
# Remove Chat Completions-only parameters not supported by the Responses API
|
# Remove Chat Completions-only parameters not supported by the Responses API
|
||||||
for unsupported_key in (
|
for unsupported_key in (
|
||||||
'stream_options',
|
'stream_options',
|
||||||
@@ -896,11 +899,36 @@ def convert_to_responses_payload(payload: dict) -> dict:
|
|||||||
|
|
||||||
def convert_responses_result(response: dict) -> dict:
|
def convert_responses_result(response: dict) -> dict:
|
||||||
"""
|
"""
|
||||||
Convert non-streaming Responses API result.
|
Convert non-streaming Responses API result to Chat Completions format.
|
||||||
Just add done flag - pass through raw response, frontend handles output.
|
|
||||||
|
Extracts text from message output items so all downstream consumers
|
||||||
|
(frontend tasks, get_content_from_response) work without modification.
|
||||||
"""
|
"""
|
||||||
response['done'] = True
|
output_items = response.get('output', [])
|
||||||
return response
|
|
||||||
|
content = ''
|
||||||
|
for item in output_items:
|
||||||
|
if item.get('type') == 'message':
|
||||||
|
for part in item.get('content', []):
|
||||||
|
if part.get('type') == 'output_text':
|
||||||
|
content += part.get('text', '')
|
||||||
|
|
||||||
|
return {
|
||||||
|
'id': response.get('id', ''),
|
||||||
|
'object': 'chat.completion',
|
||||||
|
'model': response.get('model', ''),
|
||||||
|
'choices': [
|
||||||
|
{
|
||||||
|
'index': 0,
|
||||||
|
'message': {
|
||||||
|
'role': 'assistant',
|
||||||
|
'content': content,
|
||||||
|
},
|
||||||
|
'finish_reason': 'stop',
|
||||||
|
}
|
||||||
|
],
|
||||||
|
'usage': response.get('usage', {}),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@router.post('/chat/completions')
|
@router.post('/chat/completions')
|
||||||
|
|||||||
@@ -3367,6 +3367,9 @@ async def streaming_chat_response_handler(response, ctx):
|
|||||||
usage = None
|
usage = None
|
||||||
prior_output = []
|
prior_output = []
|
||||||
|
|
||||||
|
def full_output():
|
||||||
|
return prior_output + output if prior_output else output
|
||||||
|
|
||||||
reasoning_tags_param = metadata.get('params', {}).get('reasoning_tags')
|
reasoning_tags_param = metadata.get('params', {}).get('reasoning_tags')
|
||||||
DETECT_REASONING_TAGS = reasoning_tags_param is not False
|
DETECT_REASONING_TAGS = reasoning_tags_param is not False
|
||||||
DETECT_CODE_INTERPRETER = metadata.get('features', {}).get('code_interpreter', False)
|
DETECT_CODE_INTERPRETER = metadata.get('features', {}).get('code_interpreter', False)
|
||||||
@@ -3476,8 +3479,8 @@ async def streaming_chat_response_handler(response, ctx):
|
|||||||
output, response_metadata = handle_responses_streaming_event(data, output)
|
output, response_metadata = handle_responses_streaming_event(data, output)
|
||||||
|
|
||||||
processed_data = {
|
processed_data = {
|
||||||
'output': prior_output + output,
|
'output': full_output(),
|
||||||
'content': serialize_output(prior_output + output),
|
'content': serialize_output(full_output()),
|
||||||
}
|
}
|
||||||
|
|
||||||
# print(data)
|
# print(data)
|
||||||
@@ -3832,13 +3835,13 @@ async def streaming_chat_response_handler(response, ctx):
|
|||||||
metadata['chat_id'],
|
metadata['chat_id'],
|
||||||
metadata['message_id'],
|
metadata['message_id'],
|
||||||
{
|
{
|
||||||
'content': serialize_output(output),
|
'content': serialize_output(full_output()),
|
||||||
'output': output,
|
'output': full_output(),
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
data = {
|
data = {
|
||||||
'content': serialize_output(output),
|
'content': serialize_output(full_output()),
|
||||||
}
|
}
|
||||||
|
|
||||||
if delta:
|
if delta:
|
||||||
@@ -3952,26 +3955,32 @@ async def streaming_chat_response_handler(response, ctx):
|
|||||||
response_tool_calls = tool_calls.pop(0)
|
response_tool_calls = tool_calls.pop(0)
|
||||||
|
|
||||||
# Append function_call items for each tool call
|
# Append function_call items for each tool call
|
||||||
|
# (Responses API already has them from streaming, so skip duplicates)
|
||||||
|
existing_call_ids = {
|
||||||
|
item.get('call_id') for item in output
|
||||||
|
if item.get('type') == 'function_call'
|
||||||
|
}
|
||||||
for tc in response_tool_calls:
|
for tc in response_tool_calls:
|
||||||
call_id = tc.get('id', '')
|
call_id = tc.get('id', '')
|
||||||
func = tc.get('function', {})
|
if call_id not in existing_call_ids:
|
||||||
output.append(
|
func = tc.get('function', {})
|
||||||
{
|
output.append(
|
||||||
'type': 'function_call',
|
{
|
||||||
'id': call_id or output_id('fc'),
|
'type': 'function_call',
|
||||||
'call_id': call_id,
|
'id': call_id or output_id('fc'),
|
||||||
'name': func.get('name', ''),
|
'call_id': call_id,
|
||||||
'arguments': func.get('arguments', '{}'),
|
'name': func.get('name', ''),
|
||||||
'status': 'in_progress',
|
'arguments': func.get('arguments', '{}'),
|
||||||
}
|
'status': 'in_progress',
|
||||||
)
|
}
|
||||||
|
)
|
||||||
|
|
||||||
await event_emitter(
|
await event_emitter(
|
||||||
{
|
{
|
||||||
'type': 'chat:completion',
|
'type': 'chat:completion',
|
||||||
'data': {
|
'data': {
|
||||||
'content': serialize_output(output),
|
'content': serialize_output(full_output()),
|
||||||
'output': output,
|
'output': full_output(),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in new issue
Block a user