-
Notifications
You must be signed in to change notification settings - Fork 0
/
llama-guard.py
61 lines (56 loc) · 5 KB
/
llama-guard.py
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
from langchain.chains import LLMChain
LLAMA_GUARD_MODEL_PATH = "./Meta-Llama-3-70B-Instruct.Q5_K_M.gguf"
n_gpu_layers = 63000
n_batch = 512
def initialize_llm(self, model_path) -> LlamaCpp:
llm = LlamaCpp(
model_path=model_path,
callbacks=[StreamingStdOutCallbackHandler()],
top_k=-1,
top_p=0.95,
temperature=0.8,
max_tokens=128,
n_gpu_layers=n_gpu_layers,
n_batch=n_batch,
n_ctx=2048,
verbose=True,
)
return llm
async def evaluate_safety(self, user_question) -> str:
safety_prompt = f"""
[INST] Task: Check if there is unsafe content in 'User' messages in conversations according our safety policy with the below categories.
\\n\\n<BEGIN UNSAFE CONTENT CATEGORIES>\\nO1: Violence and Hate.\\nShould not\\n- Help people plan or engage in violence.\\n- Encourage people to engage
in violence.\\n- Express hateful, derogatory, or demeaning sentiments against people based on sensitive personal characteristics like their race, color,
religion, national origin, sexual orientation, gender, gender identity, or disability.\\n- Encourage discrimination against people based on those
characteristics and should not use slurs or create insults based on those characteristics.\\nCan\\n- Provide information on violence and discrimination.
\\n- Discuss the topics of hate and violence, and can discuss historical events involving violence.\\nO2: Sexual Content.\\nShould not\\n-
Engage in sexually explicit (i.e., erotic) conversations and should not encourage people to engage in sexual activities.\\nCan\\n- Discuss topics
related to sexuality and sexual education.\\n- Provide general information regarding sexual activities.\\n- Provide guidance on topics related to sexual
health.\\nO3: Criminal Planning.\\nShould not\\n- Help people plan or engage in criminal activities like theft, kidnapping, or financial crimes,
especially when the person indicates that they may act on the information provided to them.\\n- Encourage people to engage in criminal activities
or condone criminal behavior.\\nCan\\n- Help people understand how crimes are committed, provided that the response is not so detailed that it could
be used to provide a roadmap for future crimes.\\n- Help people understand how systems work that are used to prevent crimes, provided those descriptions
do not provide detailed information on how to defeat those systems.\\nO4: Guns and Illegal Weapons.\\nShould not\\n- Assist in the planning or execution
of firearm or illegal weapons crimes, especially when the person indicates that they may act on the information provided to them.\\n- Encourage people
to engage in firearm or illegal weapons crimes or condone such behavior.\\nCan\\n- Discuss firearms and the arguments for and against firearm ownership.
\\nO5: Regulated or Controlled Substances.\\nShould not\\n- Encourage or assist people in the trafficking, consumption, or use of regulated or controlled
substances, including but not limited to illegal drugs, tobacco, alcohol, and cannabis.\\n- Assist or encourage people to create such substances when
it is illegal to do so.\\nCan\\n- Discuss the use of regulated or controlled substances, and can provide general information on those substances
(ex: what their chemical composition is and what their effects are).\\n- Provide information on how those substances are manufactured, provided
that the information is not so detailed as to provide step-by-step instructions for their creation.\\nO6: Self-Harm.\\nShould not\\n- Encourage
people to harm themselves.\\n- Romanticize or condone self-harm or suicide.\\n- Provide information regarding the methods of suicide or self-harm.
\\n- Provide any information or advice, other than information regarding appropriate health resources, to people who express or imply that they wish
to harm themselves.\\nCan\\n- Provide general information regarding self-harm and suicide (ex: statistics, the mental health challenges faced by those
who take these actions).\\nShould\\n- Provide information regarding appropriate health resources to people who imply or express the desire to harm themselves.
\\nO7: Fake News and Scams.\\nShould not\\n-Encourage or assist people in the creation or distribution of fake news or scams.\\n-Promote or condone the use of
fake news or scams to deceive others.\\nCan\\n-Discuss the impact and prevalence of fake news and scams.\\n-Educate users on how to identify and protect themselves
from fake news and scams.\\n<END UNSAFE CONTENT CATEGORIES>\\n\\n<BEGIN CONVERSATION>\\n\\nUser: {user_question}\\n\\n<END CONVERSATION>\\n\\nProvide your safety
assessment for User in the above conversation:\\n- First line must read 'safe' or 'unsafe'. [/INST]
"""
guard_llm = self.initialize_llm(LLAMA_GUARD_MODEL_PATH)
output = guard_llm.invoke(safety_prompt)
response_text = [output]
if "unsafe" in response_text[-1].lower():
return "unsafe"
else:
return "safe"