\n\n"
for section in document['Sections']:
markdown += "
\n\n"
section_number = section['PageNumber']
section_title = section['Title']
markdown += f"
{section_number}. {section_title}
\n\n"
markdown += f"
\n\n{section['Content']}\n\n
\n\n"
# for subsection in section.get('Subsections', []):
# subsection_number = subsection['PageNumber']
# subsection_title = subsection['Title']
# markdown += f"
{subsection_number} {subsection_title}
\n\n"
# markdown += f"
\n\n{subsection['Content']}\n\n
\n\n"
markdown += "
"
return markdown
router = APIRouter()
class JsonDocumentResponse(BaseModel):
json_document: Dict
# class JsonDocumentRequest(BaseModel):
# query: str
# template: bool = False
# images: Optional[List[UploadFile]] = File(None)
# documents: Optional[List[UploadFile]] = File(None)
# conversation_id: str = ""
class MarkdownDocumentRequest(BaseModel):
json_document: Dict
query: str
template: bool = False
conversation_id: str = ""
MESSAGE_DELIMITER = b"\n---DELIMITER---\n"
def yield_message(message):
message_json = json.dumps(message, ensure_ascii=False).encode('utf-8')
return message_json + MESSAGE_DELIMITER
async def generate_document_stream(document_generator: DocumentGenerator, document_outline: Dict, query: str, template: bool = False, conversation_id: str = ""):
document_generator.document_outline = document_outline
db_manager = DatabaseManager()
overall_objective = query
document_layout = json.dumps(document_generator.document_outline, indent=2)
cache_key = f"image_context_{conversation_id}"
image_context = await FastAPICache.get_backend().get(cache_key)
SECTION_PROMPT_SYSTEM = DOCUMENT_SECTION_PROMPT_SYSTEM if not template else DOCUMENT_TEMPLATE_SECTION_PROMPT_SYSTEM
document_generator.content_messages = [
{
"role": "system",
"content": SECTION_PROMPT_SYSTEM.format(
overall_objective=overall_objective,
document_layout=document_layout
)
}
]
if image_context:
document_generator.content_messages[0]["content"] += f"