Introduction #
The document generation agent uses multi-step streaming processing to automatically generate complete Word documents (.docx) from natural language requirements. Core flow: 1) Use LLM to extract requirements and chapter structure -> 2) Generate content chapter by chapter via LLM -> 3) Save as Word document using ds_save_doc(). Supports streaming progress feedback.
Use Cases #
- Operation documents, proposals, tender documents generation
Usage #
LLM connections are abstracted as data sources, see Basics -> First AI Scene
Create a Python data source
- Create dataset, type: Agent, connector: Python, name “Document Generation”
--- Extract requirements ---
base_prompt="""$prompt"""
yield 'Extracting requirements...<br>'
ds = ds_gpt('smtgpt', f'Extract information based on requirements, output json: {{"prompt":"extracted requirements", "input_file":"input file, empty if none", "output_file":"output file path, or generate a word filename", "header":"article title, or generate one based on requirements"}}, here are my requirements: {base_prompt}', tool=1)
header = ds.get('header', 'General Proposal')
input_file = ds.get('input_file', '')
output_file = ds.get('output_file', 'output.docx')
prompt = ds.get('prompt', base_prompt)
yield f'Requirement extraction complete: {str(ds)}<br>'
--- Get chapter prompts ---
context_content = ds_read(input_file)
p0 = f"""
{prompt}
【Reference Material】
Please reference this information for planning:
---
{context_content}
---
Task: Plan the document chapter structure and generate a detailed writing prompt for each chapter.
Requirements:
1. Prompts should be specific.
2. Output only a JSON 2D array: [["chapter 1 prompt"], ["chapter 2 prompt"], ...]
3. Important: No newlines in prompts, use single quotes instead of double quotes to avoid conflicts.
4. No extra explanation text.
"""
prompts=ds_gpt('smtgpt', p0, tool=1)
yield f'Generated prompts: {str(prompts)}<br>'
--- Generate by chapter ---
markdown_content=''
markdown_content='#'+header +'\n'
for index, chapter_group in enumerate(prompts):
if not chapter_group:
continue
prompt_instruction = chapter_group[0]
current_chap_num = index + 1
yield f"📝 Processing chapter {current_chap_num}...<br>"
full_prompt = f"""
Document logic: {prompts}, currently writing chapter {current_chap_num}.
Chapter topic/requirements: {prompt_instruction}
Requirements:
1. Output professional proposal content.
2. **Must use standard Markdown format** (including ## headings, **bold**, - lists, | tables, etc.).
3. Word count: 800-1000 words.
4. No chapter or title numbers.
"""
response = ds_gpt('smtgpt', full_prompt)
markdown_content = markdown_content + '\n' + response['msg']
yield ds_save_doc(markdown_content,output_file)
Document Generation Functions #
| Function | Description |
|---|---|
ds_read(file_path) |
Read file content (as reference material for LLM) |
ds_save_doc(markdown, output_file) |
Save Markdown content as Word document |
yield 'text' |
Stream progress info to frontend |
ds_gpt('smtgpt', prompt, tool=1) |
Call LLM and parse returned JSON tool calls |
Prerequisites: Requires configured LLM data source
smtgptand Python data source connector. Documents are saved on server, downloadable via link.