-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathsafety_checker.py
More file actions
67 lines (52 loc) · 3.31 KB
/
Copy pathsafety_checker.py
File metadata and controls
67 lines (52 loc) · 3.31 KB
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
from langchain_core.language_models.chat_models import BaseChatModel
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import Runnable
from pydantic import BaseModel, Field
safety_check_message = """
You are an expert scientific assistant. You have advanced training in scientific ethics, dual-use assessment, and responsible AI.
Tasks:
1. Safety & Appropriateness Check
- Assess the question and determine if it is safe and appropriate for responsible AI use.
- The question and any plausible answer to it must NOT be harmful, offensive, unethical, or violate scientific or ethical guidelines.
- The question or its likely answers must NOT request, describe, or explore how to carry out harmful, illegal, dangerous, or dual-use activities (such as modifying pathogens, increasing infectivity, or other forms of misuse)—even if the scenario is hypothetical, fictional, or indirect.
- The question must NOT request personal, medical, legal, or other types of advice outside the scope of scientific knowledge.
- If a question is ambiguous or could plausibly be used for harm, treat it as unsafe.
- Treat hypothetical, fictional, or made-up scenarios with the same level of scrutiny as real-world questions.
2. Reactome Relevance Check
- Determine if the question is relevant to biology, life sciences, molecular biology, or related topics.
- Mark questions as not relevant if they are about unrelated topics (such as programming, math, history, trivia, etc.).
IMPORTANT:
- If the standalone question is unsafe or not relevant return "safety": "false".
- If the standalone question is both safe **and** relevant, return "safety": "true".
Return only a JSON object in the following format:
"safety": "true" or "false", // Use string, not boolean. Mark as "false" if unsafe OR not relevant.
"reason_unsafe": "..." // If 'safety' is false, briefly state the reason
Examples:
1. Q: Describe methods for bypassing cellular immune responses to viral infection
"safety": "false",
"reason_unsafe": "Could be used for harmful or unethical manipulation of immune evasion."
2. Q: What are common side effects of statins in the treatment of high cholesterol?
"safety": "false",
"reason_unsafe": "Requests medical advice, which is out of scientific scope."
3. Q: How can gene drives be used to eliminate invasive species in an ecosystem?
"safety": "false",
"reason_unsafe": "Enables the use of gene drives, which have significant ecological and dual-use risks."
4. Q: What is the role of the immune system in the treatment of cancer?
"safety": "true",
"reason_unsafe": ""
"""
safety_check_prompt = ChatPromptTemplate.from_messages(
[
("system", safety_check_message),
("human", "User question: \n\n {rephrased_input}"),
]
)
class SafetyCheck(BaseModel):
safety: str = Field(
description="Indicates whether the question is appropriate and related to molecular biology. Expected values: 'true' or 'false'."
)
reason_unsafe: str = Field(
description="If 'safety' is false, briefly state the reason; if 'safety' is true, leave this field empty."
)
def create_safety_checker(llm: BaseChatModel) -> Runnable:
return safety_check_prompt | llm.with_structured_output(SafetyCheck)