File size: 2,643 Bytes
a6bdbe4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import re
from medgemma import medgemma_get_text_response


def evaluation_prompt(defacto_condition):
    # Returns a detailed prompt for the LLM to evaluate a pre-visit report for a specific condition
    return f"""
Your role is to evaluate the helpfulness of a pre-visit report, which is based on a pre-visit patient interview and existing health records.
The patient was de facto diagnosed condition: "{defacto_condition}" which was not known at the time of the interview.

List the specific elements in the previsit report text that are helpful or necessary for the PCP to diagnose the de facto diagnosed condition: "{defacto_condition}". 

This include pertinet positives or negatives. 
List critical elements that are MISSING from the previsit report text that would have been helpful for the PCP to diagnose the de facto diagnosed condition. 
This include pertinet positives or negatives that were missing from the report. 
(keep in mind that the condition "{defacto_condition}" was not known at the time)

The evaluation output should be in HTML format.

REPORT TEMPLATE START

<h3 class="helpful">Helpful Facts:</h3>

<h3 class="missing">What wasn't covered but would be helpful:</h3>

REPORT TEMPLATE END
"""

def evaluate_report(report, condition):
    """Evaluate the pre-visit report based on the condition using MedGemma LLM."""
    evaluation_text = medgemma_get_text_response([
        {
            "role": "system",
            "content": [
                {
                    "type": "text",
                    "text": f"{evaluation_prompt(condition)}"
                }
            ]
        },
        {
            "role": "user",
            "content": [
                {
                    "type": "text",
                    "text": f"Here is the report text:\n{report}"
                }
            ]
        },        
    ])

    # Remove any LLM "thinking" blocks (special tokens sometimes present in output)
    evaluation_text = re.sub(r'<unused94>.*?<unused95>', '', evaluation_text, flags=re.DOTALL)

    return evaluation_text