Process a single section in its own thread or process. Returns (section_name, result_json, total_input_token, total_output_token).
(
section_name,
outline,
raw_content,
raw_outline,
template,
create_actor_agent,
MAX_ATTEMPT
)
| 24 | MAX_ATTEMPT = 10 |
| 25 | |
| 26 | def gen_content_process_section( |
| 27 | section_name, |
| 28 | outline, |
| 29 | raw_content, |
| 30 | raw_outline, |
| 31 | template, |
| 32 | create_actor_agent, |
| 33 | MAX_ATTEMPT |
| 34 | ): |
| 35 | """ |
| 36 | Process a single section in its own thread or process. |
| 37 | Returns (section_name, result_json, total_input_token, total_output_token). |
| 38 | """ |
| 39 | # Create a fresh ActorAgent instance for each parallel call |
| 40 | actor_agent = create_actor_agent() |
| 41 | |
| 42 | section_outline = '' |
| 43 | num_attempts = 0 |
| 44 | total_input_token = 0 |
| 45 | total_output_token = 0 |
| 46 | result_json = None |
| 47 | |
| 48 | while True: |
| 49 | print(f"[Thread] Generating content for section: {section_name}") |
| 50 | |
| 51 | if len(section_outline) == 0: |
| 52 | # Initialize the section outline |
| 53 | section_outline = json.dumps(outline[section_name], indent=4) |
| 54 | |
| 55 | # Render prompt using Jinja template |
| 56 | jinja_args = { |
| 57 | 'json_outline': section_outline, |
| 58 | 'json_content': raw_content, |
| 59 | } |
| 60 | prompt = template.render(**jinja_args) |
| 61 | |
| 62 | # Step the actor_agent and track tokens |
| 63 | response = actor_agent.step(prompt) |
| 64 | input_token, output_token = account_token(response) |
| 65 | total_input_token += input_token |
| 66 | total_output_token += output_token |
| 67 | |
| 68 | # Parse JSON and possibly adjust text length |
| 69 | result_json = get_json_from_response(response.msgs[0].content) |
| 70 | new_section_outline, suggested = generate_length_suggestions( |
| 71 | result_json, |
| 72 | json.dumps(outline[section_name]), |
| 73 | raw_outline[section_name] |
| 74 | ) |
| 75 | section_outline = json.dumps(new_section_outline, indent=4) |
| 76 | |
| 77 | if not suggested: |
| 78 | # No more adjustments needed |
| 79 | break |
| 80 | |
| 81 | print(f"[Thread] Adjusting text length for section: {section_name}...") |
| 82 | |
| 83 | num_attempts += 1 |
nothing calls this directly
no test coverage detected