|
|
import re
|
|
|
from typing import List, Optional, Union
|
|
|
from pydantic import BaseModel, Field
|
|
|
from pydantic_settings import BaseSettings
|
|
|
|
|
|
from api_types import ChatMessage
|
|
|
|
|
|
|
|
|
def parse_think_response(full_response: str):
|
|
|
think_start = full_response.find("<think")
|
|
|
if think_start == -1:
|
|
|
return None, full_response.strip()
|
|
|
|
|
|
think_end = full_response.find("</think>")
|
|
|
if think_end == -1:
|
|
|
reasoning = full_response[think_start:].strip()
|
|
|
content = ""
|
|
|
else:
|
|
|
reasoning = full_response[think_start : think_end + 9].strip()
|
|
|
content = full_response[think_end + 9 :].strip()
|
|
|
|
|
|
|
|
|
reasoning_content = reasoning.replace("<think", "").replace("</think>", "").strip()
|
|
|
return reasoning_content, content
|
|
|
|
|
|
|
|
|
def cleanMessages(messages: List[ChatMessage]):
|
|
|
promptStrList = []
|
|
|
|
|
|
for message in messages:
|
|
|
content = message.content.strip()
|
|
|
content = re.sub(r"\n+", "\n", content)
|
|
|
promptStrList.append(f"{message.role.strip()}: {content}")
|
|
|
|
|
|
return "\n\n".join(promptStrList)
|
|
|
|