Process one step of paper analysis
(client: GPTClient, step_num: int, paper: Dict,
overall_instruction: str, step_instruction: str,
pdf_text: str, previous_results: Dict = None,
log_file: str = None)
| 75 | return None |
| 76 | |
| 77 | async def process_step(client: GPTClient, step_num: int, paper: Dict, |
| 78 | overall_instruction: str, step_instruction: str, |
| 79 | pdf_text: str, previous_results: Dict = None, |
| 80 | log_file: str = None) -> Dict: |
| 81 | """Process one step of paper analysis""" |
| 82 | try: |
| 83 | logging.info(f"Step {step_num}/5: Analyzing paper content...") |
| 84 | |
| 85 | # Construct prompt |
| 86 | metadata_str = json.dumps(paper, indent=2) |
| 87 | prompt = f"{overall_instruction}\n\n{step_instruction}\n\nPaper Metadata:\n{metadata_str}\n\nPaper Content:\n{pdf_text}" |
| 88 | |
| 89 | if previous_results and step_num > 1: |
| 90 | prev_results_str = json.dumps(previous_results, indent=2) |
| 91 | prompt += f"\n\nResults from previous steps:\n{prev_results_str}" |
| 92 | |
| 93 | prompt = truncate_text(prompt, max_tokens=128000) |
| 94 | |
| 95 | # Add system prompt |
| 96 | system_prompt = f"""You are an expert in analyzing research papers. |
| 97 | You are now performing Step {step_num} of the analysis process. |
| 98 | Provide your response in the exact JSON format specified in the instruction.""" |
| 99 | |
| 100 | prompt = system_prompt + "\n\n" + prompt |
| 101 | |
| 102 | # Get response from LLM |
| 103 | response = (await client.chat(prompt, temperature=0.7)).strip() |
| 104 | if response.startswith('```json'): |
| 105 | response = response[7:] |
| 106 | elif response.startswith('```'): |
| 107 | response = response[3:] |
| 108 | if response.endswith('```'): |
| 109 | response = response[:-3] |
| 110 | response = response.strip() |
| 111 | |
| 112 | if response is None: |
| 113 | logging.error(f"No response from LLM for paper: {paper['title']} (Step {step_num})") |
| 114 | return None |
| 115 | |
| 116 | if log_file: |
| 117 | log_output(log_file, paper['title'], step_num, prompt, response) |
| 118 | |
| 119 | try: |
| 120 | return json.loads(response) |
| 121 | except json.JSONDecodeError: |
| 122 | logging.error(f"Failed to parse LLM response for paper: {paper['title']} (Step {step_num})") |
| 123 | return None |
| 124 | |
| 125 | except Exception as e: |
| 126 | logging.error(f"Error processing Step {step_num} for {paper['title']}: {str(e)}") |
| 127 | return None |
| 128 | |
| 129 | def load_instructions() -> Dict[str, str]: |
| 130 | """Load all instruction files""" |
no test coverage detected