pvanand commited on
Commit
60c3069
·
verified ·
1 Parent(s): bb1bb0e

Update document_generator.py

Browse files
Files changed (1) hide show
  1. document_generator.py +21 -14
document_generator.py CHANGED
@@ -248,6 +248,12 @@ class MarkdownDocumentRequest(BaseModel):
248
  json_document: Dict
249
  query: str
250
 
 
 
 
 
 
 
251
  async def generate_document_stream(document_generator: DocumentGenerator, document_outline: Dict, query: str):
252
  document_generator.document_outline = document_outline
253
  db_manager = DatabaseManager()
@@ -271,14 +277,14 @@ async def generate_document_stream(document_generator: DocumentGenerator, docume
271
  logging.info(f"Generating content for section: {section_title}")
272
  content = document_generator.generate_content(section_title, content_instruction, section_number)
273
  section["Content"] = content
274
- yield json.dumps({
275
  "type": "document_section",
276
  "content": {
277
  "section_number": section_number,
278
  "section_title": section_title,
279
  "content": content
280
  }
281
- }, ensure_ascii=False) + "\n"
282
 
283
  for subsection in section.get("Subsections", []):
284
  subsection_title = subsection.get("Title", "")
@@ -287,27 +293,28 @@ async def generate_document_stream(document_generator: DocumentGenerator, docume
287
  logging.info(f"Generating content for subsection: {subsection_title}")
288
  content = document_generator.generate_content(subsection_title, subsection_content_instruction, subsection_number)
289
  subsection["Content"] = content
290
- yield json.dumps({
291
  "type": "document_section",
292
  "content": {
293
  "section_number": subsection_number,
294
  "section_title": subsection_title,
295
  "content": content
296
  }
297
- }, ensure_ascii=False) + "\n"
298
 
299
  markdown_document = MarkdownConverter.convert_to_markdown(document_generator.document_outline["Document"])
300
- yield json.dumps({
 
301
  "type": "complete_document",
302
- "content": markdown_document,
303
- }, ensure_ascii=False) + "\n"
 
 
 
304
 
305
- yield json.dumps({
306
- "type": "complete_document",
307
- "json": document_generator.document_outline
308
- }, ensure_ascii=False) + "\n"
309
  db_manager.update_database("elevatics", query, markdown_document)
310
 
 
311
  @cache(expire=600*24*7)
312
  @router.post("/generate-document/json", response_model=JsonDocumentResponse)
313
  async def generate_document_outline_endpoint(request: DocumentRequest):
@@ -334,12 +341,12 @@ async def generate_markdown_document_stream_endpoint(request: MarkdownDocumentRe
334
  async for chunk in generate_document_stream(document_generator, request.json_document, request.query):
335
  yield chunk
336
  except Exception as e:
337
- yield json.dumps({
338
  "type": "error",
339
  "content": str(e)
340
- }) + "\n"
341
 
342
- return StreamingResponse(stream_generator(), media_type="text/plain")
343
 
344
  ###########################################
345
  class MarkdownDocumentResponse(BaseModel):
 
248
  json_document: Dict
249
  query: str
250
 
251
+ MESSAGE_DELIMITER = b"\n---DELIMITER---\n"
252
+
253
+ def yield_message(message):
254
+ message_json = json.dumps(message, ensure_ascii=False).encode('utf-8')
255
+ return message_json + MESSAGE_DELIMITER
256
+
257
  async def generate_document_stream(document_generator: DocumentGenerator, document_outline: Dict, query: str):
258
  document_generator.document_outline = document_outline
259
  db_manager = DatabaseManager()
 
277
  logging.info(f"Generating content for section: {section_title}")
278
  content = document_generator.generate_content(section_title, content_instruction, section_number)
279
  section["Content"] = content
280
+ yield yield_message({
281
  "type": "document_section",
282
  "content": {
283
  "section_number": section_number,
284
  "section_title": section_title,
285
  "content": content
286
  }
287
+ })
288
 
289
  for subsection in section.get("Subsections", []):
290
  subsection_title = subsection.get("Title", "")
 
293
  logging.info(f"Generating content for subsection: {subsection_title}")
294
  content = document_generator.generate_content(subsection_title, subsection_content_instruction, subsection_number)
295
  subsection["Content"] = content
296
+ yield yield_message({
297
  "type": "document_section",
298
  "content": {
299
  "section_number": subsection_number,
300
  "section_title": subsection_title,
301
  "content": content
302
  }
303
+ })
304
 
305
  markdown_document = MarkdownConverter.convert_to_markdown(document_generator.document_outline["Document"])
306
+
307
+ yield yield_message({
308
  "type": "complete_document",
309
+ "content": {
310
+ "markdown": markdown_document,
311
+ "json": document_generator.document_outline
312
+ },
313
+ });
314
 
 
 
 
 
315
  db_manager.update_database("elevatics", query, markdown_document)
316
 
317
+
318
  @cache(expire=600*24*7)
319
  @router.post("/generate-document/json", response_model=JsonDocumentResponse)
320
  async def generate_document_outline_endpoint(request: DocumentRequest):
 
341
  async for chunk in generate_document_stream(document_generator, request.json_document, request.query):
342
  yield chunk
343
  except Exception as e:
344
+ yield yield_message({
345
  "type": "error",
346
  "content": str(e)
347
+ })
348
 
349
+ return StreamingResponse(stream_generator(), media_type="application/octet-stream")
350
 
351
  ###########################################
352
  class MarkdownDocumentResponse(BaseModel):