|
|
|
|
|
|
|
import time |
|
import uuid |
|
|
|
|
|
def format_openai_response(content, model="deepseek70b"): |
|
"""将内容格式化为OpenAI兼容的响应格式 |
|
|
|
Args: |
|
content: 响应内容 |
|
model: 使用的模型名称 |
|
|
|
Returns: |
|
OpenAI格式的响应对象 |
|
""" |
|
|
|
response_id = f"chatcmpl-{uuid.uuid4().hex[:10]}" |
|
|
|
|
|
created_timestamp = int(time.time()) |
|
|
|
return { |
|
"id": response_id, |
|
"object": "chat.completion", |
|
"created": created_timestamp, |
|
"model": model, |
|
"choices": [{ |
|
"index": 0, |
|
"message": { |
|
"role": "assistant", |
|
"content": content |
|
}, |
|
"finish_reason": "stop" |
|
}], |
|
"usage": { |
|
"prompt_tokens": 0, |
|
"completion_tokens": 0, |
|
"total_tokens": 0 |
|
} |
|
} |
|
|
|
|
|
def format_openai_stream_chunk(content, model="deepseek70b", is_first_chunk=False, is_last_chunk=False): |
|
"""格式化流式响应的单个数据块为OpenAI兼容格式 |
|
|
|
Args: |
|
content: 当前数据块的内容 |
|
model: 使用的模型名称 |
|
is_first_chunk: 是否为第一个数据块 |
|
is_last_chunk: 是否为最后一个数据块 |
|
|
|
Returns: |
|
OpenAI格式的流式响应数据块 |
|
""" |
|
|
|
|
|
response_id = f"chatcmpl-{uuid.uuid4().hex[:10]}" |
|
|
|
|
|
created_timestamp = int(time.time()) |
|
|
|
response = { |
|
"id": response_id, |
|
"object": "chat.completion.chunk", |
|
"created": created_timestamp, |
|
"model": model, |
|
"choices": [{ |
|
"index": 0, |
|
"delta": {}, |
|
"finish_reason": None |
|
}] |
|
} |
|
|
|
|
|
if is_first_chunk: |
|
response["choices"][0]["delta"] = { |
|
"role": "assistant", |
|
"content": content |
|
} |
|
|
|
elif is_last_chunk: |
|
response["choices"][0]["delta"] = {"content": content} |
|
response["choices"][0]["finish_reason"] = "stop" |
|
|
|
else: |
|
response["choices"][0]["delta"] = {"content": content} |
|
|
|
return response |