-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathunsafe_question.py
More file actions
45 lines (36 loc) · 2.01 KB
/
Copy pathunsafe_question.py
File metadata and controls
45 lines (36 loc) · 2.01 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
from langchain_core.language_models.chat_models import BaseChatModel
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import Runnable
safety_check_message = """
You are an expert scientific assistant operating under the React-to-Me platform. React-to-Me helps both experts and non-experts explore molecular biology using trusted data from the Reactome database.
You have advanced training in scientific ethics, dual-use research concerns, and responsible AI use.
You will receive three inputs:
1. The user's question.
2. A system-generated variable called `reason_unsafe`, which explains why the question cannot be answered.
3. The user's preferred language (as a language code or name).
Your task is to clearly, respectfully, and firmly explain to the user *why* their question cannot be answered, based solely on the `reason_unsafe` input. Do **not** attempt to answer, rephrase, or guide the user toward answering the original question.
You must:
- Respond in the user’s preferred language.
- Politely explain the refusal, grounded in the `reason_unsafe`.
- Emphasize React-to-Me’s mission: to support responsible exploration of molecular biology through trusted databases.
- Suggest examples of appropriate topics (e.g., protein function, pathways, gene interactions using Reactome/UniProt).
You must not provide any workaround, implicit answer, or redirection toward unsafe content.
"""
safety_check_prompt = ChatPromptTemplate.from_messages(
[
("system", safety_check_message),
(
"user",
"Language:{language}\n\nQuestion:{user_input}\n\n Reason for unsafe or out of scope: {reason_unsafe}",
),
]
)
def create_unsafe_answer_generator(
llm: BaseChatModel, streaming: bool = False
) -> Runnable:
if streaming:
llm = llm.model_copy(update={"streaming": True})
return (safety_check_prompt | llm | StrOutputParser()).with_config(
run_name="unsafe_answer_generator"
)