pvanand commited on
Commit
285b61b
1 Parent(s): 60c3069

Update document_generator.py

Browse files
Files changed (1) hide show
  1. document_generator.py +58 -11
document_generator.py CHANGED
@@ -39,6 +39,49 @@ FORMAT YOUR OUTPUT AS MARKDOWN ENCLOSED IN <response></response> tags
39
 
40
  DOCUMENT_SECTION_PROMPT_USER = """<prompt>Output the content for the section "{section_or_subsection_title}" formatted as markdown. Follow this instruction: {content_instruction}</prompt>"""
41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  # File: app.py
43
  import os
44
  import json
@@ -146,10 +189,10 @@ class DocumentGenerator:
146
  return content.lstrip()
147
 
148
  @log_execution
149
- def generate_document_outline(self, query: str, max_retries: int = 3) -> Optional[Dict]:
150
  messages = [
151
- {"role": "system", "content": DOCUMENT_OUTLINE_PROMPT_SYSTEM},
152
- {"role": "user", "content": DOCUMENT_OUTLINE_PROMPT_USER.format(query=query)}
153
  ]
154
 
155
  for attempt in range(max_retries):
@@ -168,10 +211,11 @@ class DocumentGenerator:
168
  return None
169
 
170
  @log_execution
171
- def generate_content(self, title: str, content_instruction: str, section_number: str) -> str:
 
172
  self.content_messages.append({
173
  "role": "user",
174
- "content": DOCUMENT_SECTION_PROMPT_USER.format(
175
  section_or_subsection_title=title,
176
  content_instruction=content_instruction
177
  )
@@ -240,6 +284,7 @@ router = APIRouter()
240
 
241
  class DocumentRequest(BaseModel):
242
  query: str
 
243
 
244
  class JsonDocumentResponse(BaseModel):
245
  json_document: Dict
@@ -247,6 +292,7 @@ class JsonDocumentResponse(BaseModel):
247
  class MarkdownDocumentRequest(BaseModel):
248
  json_document: Dict
249
  query: str
 
250
 
251
  MESSAGE_DELIMITER = b"\n---DELIMITER---\n"
252
 
@@ -254,16 +300,17 @@ 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()
260
  overall_objective = query
261
  document_layout = json.dumps(document_generator.document_outline, indent=2)
262
 
 
263
  document_generator.content_messages = [
264
  {
265
  "role": "system",
266
- "content": DOCUMENT_SECTION_PROMPT_SYSTEM.format(
267
  overall_objective=overall_objective,
268
  document_layout=document_layout
269
  )
@@ -275,7 +322,7 @@ async def generate_document_stream(document_generator: DocumentGenerator, docume
275
  section_number = section.get("SectionNumber", "")
276
  content_instruction = section.get("Content", "")
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",
@@ -291,7 +338,7 @@ async def generate_document_stream(document_generator: DocumentGenerator, docume
291
  subsection_number = subsection.get("SectionNumber", "")
292
  subsection_content_instruction = subsection.get("Content", "")
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",
@@ -322,7 +369,7 @@ async def generate_document_outline_endpoint(request: DocumentRequest):
322
  document_generator = DocumentGenerator(ai_client)
323
 
324
  try:
325
- json_document = document_generator.generate_document_outline(request.query)
326
 
327
  if json_document is None:
328
  raise HTTPException(status_code=500, detail="Failed to generate a valid document outline")
@@ -338,7 +385,7 @@ async def generate_markdown_document_stream_endpoint(request: MarkdownDocumentRe
338
 
339
  async def stream_generator():
340
  try:
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({
 
39
 
40
  DOCUMENT_SECTION_PROMPT_USER = """<prompt>Output the content for the section "{section_or_subsection_title}" formatted as markdown. Follow this instruction: {content_instruction}</prompt>"""
41
 
42
+ ##########################################
43
+
44
+ DOCUMENT_TEMPLATE_OUTLINE_PROMPT_SYSTEM = """You are a document template generator. Provide the outline of the document requested in <prompt></prompt> in JSON format.
45
+ Include sections and subsections if required. Use the "Content" field to provide a specific prompt or instruction for generating template (with empty input fields) that particular section or subsection. Specify that input fields should be empty in each prompt.
46
+ make sure the Sections follow a logical flow and each prompt's content does not overlap with other sections.
47
+ OUTPUT IN FOLLOWING JSON FORMAT enclosed in <output> tags
48
+ <output>
49
+ {
50
+ "Document": {
51
+ "Title": "Document Title",
52
+ "Author": "Author Name",
53
+ "Date": "YYYY-MM-DD",
54
+ "Version": "1.0",
55
+
56
+ "Sections": [
57
+ {
58
+ "SectionNumber": "1",
59
+ "Title": "Section Title",
60
+ "Content": "Specific prompt or instruction for generating empty template for this section",
61
+ "Subsections": [
62
+ {
63
+ "SectionNumber": "1.1",
64
+ "Title": "Subsection Title",
65
+ "Content": "Specific prompt or instruction for generating empty template for this subsection"
66
+ }
67
+ ]
68
+ }
69
+ ]
70
+ }
71
+ }
72
+ </output>"""
73
+
74
+ DOCUMENT_TEMPLATE_PROMPT_USER = """<prompt>{query}</prompt>"""
75
+
76
+ DOCUMENT_TEMPLATE_SECTION_PROMPT_SYSTEM = """You are a document template generator, You need to output only the content requested in the section in the prompt.
77
+ FORMAT YOUR OUTPUT AS MARKDOWN ENCLOSED IN <response></response> tags
78
+ <overall_objective>{overall_objective}</overall_objective>
79
+ <document_layout>{document_layout}</document_layout>"""
80
+
81
+ DOCUMENT_TEMPLATE_SECTION_PROMPT_USER = """<prompt>Output the content for the section "{section_or_subsection_title}" formatted as markdown. Follow this instruction: {content_instruction}</prompt>"""
82
+
83
+
84
+
85
  # File: app.py
86
  import os
87
  import json
 
189
  return content.lstrip()
190
 
191
  @log_execution
192
+ def generate_document_outline(self, query: str, template: bool = False, max_retries: int = 3) -> Optional[Dict]:
193
  messages = [
194
+ {"role": "system", "content": DOCUMENT_OUTLINE_PROMPT_SYSTEM if not template else DOCUMENT_TEMPLATE_OUTLINE_PROMPT_SYSTEM},
195
+ {"role": "user", "content": DOCUMENT_OUTLINE_PROMPT_USER.format(query=query) if not template else DOCUMENT_TEMPLATE_PROMPT_USER.format(query=query)}
196
  ]
197
 
198
  for attempt in range(max_retries):
 
211
  return None
212
 
213
  @log_execution
214
+ def generate_content(self, title: str, content_instruction: str, section_number: str, template: bool = False) -> str:
215
+ SECTION_PROMPT_USER = DOCUMENT_SECTION_PROMPT_USER if not template else DOCUMENT_TEMPLATE_SECTION_PROMPT_USER
216
  self.content_messages.append({
217
  "role": "user",
218
+ "content": SECTION_PROMPT_USER.format(
219
  section_or_subsection_title=title,
220
  content_instruction=content_instruction
221
  )
 
284
 
285
  class DocumentRequest(BaseModel):
286
  query: str
287
+ template: bool = False
288
 
289
  class JsonDocumentResponse(BaseModel):
290
  json_document: Dict
 
292
  class MarkdownDocumentRequest(BaseModel):
293
  json_document: Dict
294
  query: str
295
+ template: bool = False
296
 
297
  MESSAGE_DELIMITER = b"\n---DELIMITER---\n"
298
 
 
300
  message_json = json.dumps(message, ensure_ascii=False).encode('utf-8')
301
  return message_json + MESSAGE_DELIMITER
302
 
303
+ async def generate_document_stream(document_generator: DocumentGenerator, document_outline: Dict, query: str, template: bool = False):
304
  document_generator.document_outline = document_outline
305
  db_manager = DatabaseManager()
306
  overall_objective = query
307
  document_layout = json.dumps(document_generator.document_outline, indent=2)
308
 
309
+ SECTION_PROMPT_SYSTEM = DOCUMENT_SECTION_PROMPT_SYSTEM if not template else DOCUMENT_TEMPLATE_SECTION_PROMPT_SYSTEM
310
  document_generator.content_messages = [
311
  {
312
  "role": "system",
313
+ "content": SECTION_PROMPT_SYSTEM.format(
314
  overall_objective=overall_objective,
315
  document_layout=document_layout
316
  )
 
322
  section_number = section.get("SectionNumber", "")
323
  content_instruction = section.get("Content", "")
324
  logging.info(f"Generating content for section: {section_title}")
325
+ content = document_generator.generate_content(section_title, content_instruction, section_number, template)
326
  section["Content"] = content
327
  yield yield_message({
328
  "type": "document_section",
 
338
  subsection_number = subsection.get("SectionNumber", "")
339
  subsection_content_instruction = subsection.get("Content", "")
340
  logging.info(f"Generating content for subsection: {subsection_title}")
341
+ content = document_generator.generate_content(subsection_title, subsection_content_instruction, subsection_number, template)
342
  subsection["Content"] = content
343
  yield yield_message({
344
  "type": "document_section",
 
369
  document_generator = DocumentGenerator(ai_client)
370
 
371
  try:
372
+ json_document = document_generator.generate_document_outline(request.query, request.template)
373
 
374
  if json_document is None:
375
  raise HTTPException(status_code=500, detail="Failed to generate a valid document outline")
 
385
 
386
  async def stream_generator():
387
  try:
388
+ async for chunk in generate_document_stream(document_generator, request.json_document, request.query, request.template):
389
  yield chunk
390
  except Exception as e:
391
  yield yield_message({