|
| 1 | +#! /usr/bin/env python |
| 2 | +"""Prescreen based on GenAI""" |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import csv |
| 6 | +from pathlib import Path |
| 7 | +from typing import ClassVar |
| 8 | + |
| 9 | +import pandas as pd |
| 10 | +from litellm import completion |
| 11 | +from pydantic import BaseModel |
| 12 | +from pydantic import Field |
| 13 | + |
| 14 | +import colrev.package_manager.package_base_classes as base_classes |
| 15 | +import colrev.package_manager.package_manager |
| 16 | +import colrev.package_manager.package_settings |
| 17 | +import colrev.record.record |
| 18 | +from colrev.constants import Colors |
| 19 | +from colrev.constants import RecordState |
| 20 | + |
| 21 | + |
| 22 | +# pylint: disable=too-few-public-methods |
| 23 | +# pylint: disable=duplicate-code |
| 24 | + |
| 25 | + |
| 26 | +class PreScreenDecision(BaseModel): |
| 27 | + """ |
| 28 | + Class for a prescreen |
| 29 | + """ |
| 30 | + |
| 31 | + SYSTEM_PROMPT: ClassVar[str] = ( |
| 32 | + "You are an expert screener of scientific literature. " |
| 33 | + "You are tasked with identifying relevant articles for a literature review. " |
| 34 | + "You are provided with the metadata of an article and are asked to determine " |
| 35 | + "whether the article should be included in the review based on an inclusion criterion." |
| 36 | + ) |
| 37 | + included: bool = Field( |
| 38 | + description="Whether the article should be included in the review " |
| 39 | + + "based on the inclusion criterion." |
| 40 | + ) |
| 41 | + explanation: str = Field(description="Explanation of the inclusion decision.") |
| 42 | + |
| 43 | + |
| 44 | +class GenAIPrescreen(base_classes.PrescreenPackageBaseClass): |
| 45 | + """GenAI-based prescreen""" |
| 46 | + |
| 47 | + ci_supported: bool = Field(default=True) |
| 48 | + export_todos_only: bool = True |
| 49 | + |
| 50 | + class GenAIPrescreenSettings( |
| 51 | + colrev.package_manager.package_settings.DefaultSettings, BaseModel |
| 52 | + ): |
| 53 | + """Settings for GenAIPrescreen""" |
| 54 | + |
| 55 | + # pylint: disable=invalid-name |
| 56 | + # pylint: disable=too-many-instance-attributes |
| 57 | + |
| 58 | + endpoint: str |
| 59 | + model: str = "gpt-4o-mini" |
| 60 | + |
| 61 | + settings_class = GenAIPrescreenSettings |
| 62 | + |
| 63 | + def __init__( |
| 64 | + self, |
| 65 | + *, |
| 66 | + prescreen_operation: colrev.ops.prescreen.Prescreen, |
| 67 | + settings: dict, |
| 68 | + ) -> None: |
| 69 | + self.review_manager = prescreen_operation.review_manager |
| 70 | + self.settings = self.settings_class(**settings) |
| 71 | + self.prescreen_decision_explanation_path = ( |
| 72 | + self.review_manager.paths.prescreen |
| 73 | + / Path("prescreen_decision_explanation.csv") |
| 74 | + ) |
| 75 | + |
| 76 | + # pylint: disable=unused-argument |
| 77 | + def run_prescreen( |
| 78 | + self, |
| 79 | + records: dict, |
| 80 | + split: list, |
| 81 | + ) -> dict: |
| 82 | + """Prescreen records based on GenAI""" |
| 83 | + |
| 84 | + if self.review_manager.settings.prescreen.explanation == "": |
| 85 | + print( |
| 86 | + f"\n{Colors.ORANGE}Provide a short explanation of the prescreen{Colors.END} " |
| 87 | + "(why should particular papers be included?):" |
| 88 | + ) |
| 89 | + print( |
| 90 | + 'Example objective: "Include papers that focus on digital technology."' |
| 91 | + ) |
| 92 | + self.review_manager.settings.prescreen.explanation = input("") |
| 93 | + self.review_manager.save_settings() |
| 94 | + else: |
| 95 | + print("\nIn the prescreen, the following process is followed:\n") |
| 96 | + print(" " + self.review_manager.settings.prescreen.explanation) |
| 97 | + print() |
| 98 | + |
| 99 | + # API key needs to be set as an environment variable |
| 100 | + inclusion_criterion = self.review_manager.settings.prescreen.explanation |
| 101 | + |
| 102 | + screening_decisions = [] |
| 103 | + |
| 104 | + for record_dict in records.values(): |
| 105 | + record = colrev.record.record.Record(record_dict) |
| 106 | + response = completion( |
| 107 | + model=self.settings.model, |
| 108 | + max_tokens=1024, |
| 109 | + messages=[ |
| 110 | + { |
| 111 | + "role": "user", |
| 112 | + "content": f"{PreScreenDecision.SYSTEM_PROMPT}\n\n" |
| 113 | + + f"INCLUSION CRITERION:\n\n{inclusion_criterion}\n\n" |
| 114 | + + f"METADATA:\n\n{record}", |
| 115 | + } |
| 116 | + ], |
| 117 | + response_format=PreScreenDecision, |
| 118 | + ) |
| 119 | + prescreen_decision = PreScreenDecision.model_validate_json( |
| 120 | + response.choices[0].message.content |
| 121 | + ) |
| 122 | + if prescreen_decision.included: |
| 123 | + record.set_status(RecordState.rev_prescreen_included) |
| 124 | + else: |
| 125 | + record.set_status(RecordState.rev_prescreen_excluded) |
| 126 | + |
| 127 | + screening_decisions.append( |
| 128 | + { |
| 129 | + "Record": record.get_data()["ID"], |
| 130 | + "Inclusion/Exclusion Decision": ( |
| 131 | + "Included" if prescreen_decision.included else "Excluded" |
| 132 | + ), |
| 133 | + "Explanation": prescreen_decision.explanation, |
| 134 | + } |
| 135 | + ) |
| 136 | + |
| 137 | + self.review_manager.paths.prescreen.mkdir(parents=True, exist_ok=True) |
| 138 | + screening_decisions_df = pd.DataFrame(screening_decisions) |
| 139 | + screening_decisions_df.to_csv( |
| 140 | + self.prescreen_decision_explanation_path, index=False, quoting=csv.QUOTE_ALL |
| 141 | + ) |
| 142 | + self.review_manager.logger.info( |
| 143 | + f"Exported prescreening decisions to {self.prescreen_decision_explanation_path}" |
| 144 | + ) |
| 145 | + |
| 146 | + self.review_manager.dataset.save_records_dict(records) |
| 147 | + self.review_manager.dataset.create_commit( |
| 148 | + msg="Pre-screen (GenAI)", |
| 149 | + manual_author=False, |
| 150 | + ) |
| 151 | + |
| 152 | + return records |
0 commit comments