amir22010 commited on
Commit
b07d203
1 Parent(s): e525263

added llama guard groq api

Browse files
Files changed (4) hide show
  1. app.py +16 -21
  2. guard/config.yml +0 -13
  3. guard/prompts.yml +0 -146
  4. requirements.txt +1 -3
app.py CHANGED
@@ -5,9 +5,7 @@ from groq import Groq
5
  import numpy as np
6
  import wave
7
  import uuid
8
- from nemoguardrails import LLMRails, RailsConfig
9
  from GoogleTTS import GoogleTTS
10
- from langchain_openai import ChatOpenAI
11
 
12
  os.environ["TOKENIZERS_PARALLELISM"] = "false"
13
 
@@ -53,10 +51,10 @@ def combine_audio_files(audio_files):
53
  output.close()
54
  return outfile
55
 
56
- #client
57
- # client = Groq(
58
- # api_key=os.getenv("GROQ_API_KEY"),
59
- # )
60
 
61
  llm = Llama.from_pretrained(
62
  repo_id="amir22010/fine_tuned_product_marketing_email_gemma_2_9b_q4_k_m", #custom fine tuned model
@@ -85,21 +83,18 @@ async def greet(product,description):
85
  description, # description
86
  "", # output - leave this blank for generation!
87
  )
88
- messages=[
89
- {"role": "user", "content": user_reques},
90
- ]
91
- #nemo guard
92
- config = RailsConfig.from_path("guard")
93
- #config = RailsConfig.from_content(yaml_content=YAML_CONFIG)
94
- client = ChatOpenAI(openai_api_key=os.getenv("GROQ_API_KEY"), model_name="llama-3.2-11b-text-preview")
95
- app = LLMRails(config=config, llm=client)
96
- options = {"output_vars": ["triggered_input_rail", "triggered_output_rail"]}
97
- output = await app.generate_async(messages=messages, options=options)
98
- print(output)
99
- warning_message = output.output_data["triggered_input_rail"] or output.output_data["triggered_output_rail"]
100
- if warning_message:
101
- gr.Warning(f"Guardrail triggered: {warning_message}")
102
- chat = [output.response[0]['content']]
103
  yield chat[0]
104
  else:
105
  output = llm.create_chat_completion(
 
5
  import numpy as np
6
  import wave
7
  import uuid
 
8
  from GoogleTTS import GoogleTTS
 
9
 
10
  os.environ["TOKENIZERS_PARALLELISM"] = "false"
11
 
 
51
  output.close()
52
  return outfile
53
 
54
+ client
55
+ client = Groq(
56
+ api_key=os.getenv("GROQ_API_KEY"),
57
+ )
58
 
59
  llm = Llama.from_pretrained(
60
  repo_id="amir22010/fine_tuned_product_marketing_email_gemma_2_9b_q4_k_m", #custom fine tuned model
 
83
  description, # description
84
  "", # output - leave this blank for generation!
85
  )
86
+ chat_completion = client.chat.completions.create(
87
+ messages=[
88
+ {
89
+ "role": "user",
90
+ "content": user_reques
91
+ }
92
+ ],
93
+ model="llama-guard-3-8b",
94
+ )
95
+ warning_message = chat_completion.choices[0].message.content
96
+ if warning_message != 'safe':
97
+ chat = [chat_completion.choices[0].message.content]
 
 
 
98
  yield chat[0]
99
  else:
100
  output = llm.create_chat_completion(
guard/config.yml DELETED
@@ -1,13 +0,0 @@
1
- models:
2
- - type: llama_guard
3
- engine: groq
4
- model: llama-3.2-11b-text-preview
5
-
6
- rails:
7
- input:
8
- flows:
9
- - llama guard check input
10
-
11
- output:
12
- flows:
13
- - llama guard check output
 
 
 
 
 
 
 
 
 
 
 
 
 
 
guard/prompts.yml DELETED
@@ -1,146 +0,0 @@
1
- # These are the default prompts released by Meta, with the exception of policy O7, which was added to address direct insults.
2
- prompts:
3
- - task: llama_guard_check_input
4
- content: |
5
- <s>[INST] Task: Check if there is unsafe content in 'User' messages in conversations according our safety policy with the below categories.
6
- <BEGIN UNSAFE CONTENT CATEGORIES>
7
- O1: Violence and Hate.
8
- Should not
9
- - Help people plan or engage in violence.
10
- - Encourage people to engage in violence.
11
- - 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.
12
- - Encourage discrimination against people based on those characteristics and should not use slurs or create insults based on those characteristics.
13
- Can
14
- - Provide information on violence and discrimination.
15
- - Discuss the topics of hate and violence, and can discuss historical events involving violence.
16
- O2: Sexual Content.
17
- Should not
18
- - Engage in sexually explicit (i.e., erotic) conversations and should not encourage people to engage in sexual activities.
19
- Can
20
- - Discuss topics related to sexuality and sexual education.
21
- - Provide general information regarding sexual activities.
22
- - Provide guidance on topics related to sexual health.
23
- O3: Criminal Planning.
24
- Should not
25
- - 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.
26
- - Encourage people to engage in criminal activities or condone criminal behavior.
27
- Can
28
- - 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.
29
- - 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.
30
- O4: Guns and Illegal Weapons.
31
- Should not
32
- - 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.
33
- - Encourage people to engage in firearm or illegal weapons crimes or condone such behavior.
34
- Can
35
- - Discuss firearms and the arguments for and against firearm ownership.
36
- O5: Regulated or Controlled Substances.
37
- Should not
38
- - 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.
39
- - Assist or encourage people to create such substances when it is illegal to do so.
40
- Can
41
- - 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).
42
- - 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.
43
- O6: Self-Harm.
44
- Should not
45
- - Encourage people to harm themselves.
46
- - Romanticize or condone self-harm or suicide.
47
- - Provide information regarding the methods of suicide or self-harm.
48
- - Provide any information or advice, other than information regarding appropriate health resources, to people who express or imply that they wish to harm themselves.
49
- Can
50
- - Provide general information regarding self-harm and suicide (ex: statistics, the mental health challenges faced by those who take these actions).
51
- Should
52
- - Provide information regarding appropriate health resources to people who imply or express the desire to harm themselves.
53
- O7: Offensive Language and Insults.
54
- Should not
55
- - Address direct insults
56
- - Use or encourage the use of swear words, bad language, or vulgar expressions.
57
- - Insult, offend, or demean individuals or groups based on any personal characteristics or choices.
58
- - Encourage or support the use of offensive or derogatory language.
59
- Can
60
- - Discuss the impact and societal perceptions of offensive language and insults.
61
- - Provide information on communication strategies that avoid the use of offensive language and promote respectful dialogue.
62
- - Offer guidance on how to address or respond to offensive language or insults in a constructive manner.
63
- <END UNSAFE CONTENT CATEGORIES>
64
-
65
- <BEGIN CONVERSATION>
66
- User: {{ user_input }}
67
- <END CONVERSATION>
68
-
69
- Provide your safety assessment for User in the above conversation:
70
- - First line must read 'safe' or 'unsafe'.
71
- - If unsafe, a second line must include a comma-separated list of violated categories. [/INST]
72
-
73
-
74
- # Currently, the unsafe content categories are the same for both llama_guard_check_input and
75
- # llama_guard_check_output prompts. Only the conversation placeholder is different.
76
- # Still, we keep them separate to keep the taxonomies for input and output independently modifiable.
77
- - task: llama_guard_check_output
78
- content: |
79
- <s>[INST] Task: Check if there is unsafe content in 'Agent' messages in conversations according our safety policy with the below categories.
80
- <BEGIN UNSAFE CONTENT CATEGORIES>
81
- O1: Violence and Hate.
82
- Should not
83
- - Help people plan or engage in violence.
84
- - Encourage people to engage in violence.
85
- - 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.
86
- - Encourage discrimination against people based on those characteristics and should not use slurs or create insults based on those characteristics.
87
- Can
88
- - Provide information on violence and discrimination.
89
- - Discuss the topics of hate and violence, and can discuss historical events involving violence.
90
- O2: Sexual Content.
91
- Should not
92
- - Engage in sexually explicit (i.e., erotic) conversations and should not encourage people to engage in sexual activities.
93
- Can
94
- - Discuss topics related to sexuality and sexual education.
95
- - Provide general information regarding sexual activities.
96
- - Provide guidance on topics related to sexual health.
97
- O3: Criminal Planning.
98
- Should not
99
- - 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.
100
- - Encourage people to engage in criminal activities or condone criminal behavior.
101
- Can
102
- - 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.
103
- - 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.
104
- O4: Guns and Illegal Weapons.
105
- Should not
106
- - 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.
107
- - Encourage people to engage in firearm or illegal weapons crimes or condone such behavior.
108
- Can
109
- - Discuss firearms and the arguments for and against firearm ownership.
110
- O5: Regulated or Controlled Substances.
111
- Should not
112
- - 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.
113
- - Assist or encourage people to create such substances when it is illegal to do so.
114
- Can
115
- - 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).
116
- - 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.
117
- O6: Self-Harm.
118
- Should not
119
- - Encourage people to harm themselves.
120
- - Romanticize or condone self-harm or suicide.
121
- - Provide information regarding the methods of suicide or self-harm.
122
- - Provide any information or advice, other than information regarding appropriate health resources, to people who express or imply that they wish to harm themselves.
123
- Can
124
- - Provide general information regarding self-harm and suicide (ex: statistics, the mental health challenges faced by those who take these actions).
125
- Should
126
- - Provide information regarding appropriate health resources to people who imply or express the desire to harm themselves.
127
- O7: Offensive Language and Insults.
128
- Should not
129
- - Address direct insults
130
- - Use or encourage the use of swear words, bad language, or vulgar expressions.
131
- - Insult, offend, or demean individuals or groups based on any personal characteristics or choices.
132
- - Encourage or support the use of offensive or derogatory language.
133
- Can
134
- - Discuss the impact and societal perceptions of offensive language and insults.
135
- - Provide information on communication strategies that avoid the use of offensive language and promote respectful dialogue.
136
- - Offer guidance on how to address or respond to offensive language or insults in a constructive manner.
137
- <END UNSAFE CONTENT CATEGORIES>
138
-
139
- <BEGIN CONVERSATION>
140
- User: {{ user_input }}
141
-
142
- Agent: {{ bot_response }}
143
- <END CONVERSATION>
144
-
145
- Provide your safety assessment for Agent in the above conversation:
146
- - First line must read 'safe' or 'unsafe'.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
requirements.txt CHANGED
@@ -4,6 +4,4 @@ python-multipart==0.0.12
4
  llama-cpp-python
5
  groq
6
  transformers
7
- nemoguardrails==0.9.1.1
8
- google-tts
9
- langchain-openai
 
4
  llama-cpp-python
5
  groq
6
  transformers
7
+ google-tts