-
Notifications
You must be signed in to change notification settings - Fork 2
/
pips.py
236 lines (206 loc) · 9.24 KB
/
pips.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
from openai import OpenAI
import json
import streamlit as st
import random
import string
from util import (
generate_error_code,
send_to_discord_error,
)
pips_client = OpenAI()
def pips():
try:
context = "\n".join([f"{m['role']}: {m['content']}" for m in st.session_state.messages])
# Use the system prompt from session state
system_prompt = st.session_state.pips_system_prompt
response = pips_client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{
"role": "system",
"content": [
{
"type": "text",
"text": system_prompt
}
]
},
{
"role": "user",
"content": [
{
"type": "text",
"text": f"{context}"
}
]
}
],
temperature=0,
max_tokens=16383,
top_p=1,
frequency_penalty=0,
presence_penalty=0,
response_format={
"type": "json_object"
}
)
with st.expander("Messages Sent to PIPS:"):
st.json([
{"role": "system", "content": system_prompt},
{"role": "user", "content": f"{context}"}
])
result = response.choices[0].message.content
with st.expander("PIPS Response:"):
st.json(result)
try:
result = result.strip().rstrip('</output>')
result_json = json.loads(result)
except json.JSONDecodeError as e:
error_code = generate_error_code()
error_message = f"Failed to parse JSON response. Error code: {error_code}"
send_to_discord_error(error_code, f"JSON parsing error: {str(e)}\nResponse from PIPS: {result}")
st.error(f"Error 15: {error_code}, {error_message}. Conversation ID: {st.session_state.conversation_id}")
return {
"analysis": "Error while decoding json.",
"risk_level": "CRITICAL",
"risk_factors": ["None"],
"response_strategy": "REFUSE",
"precautions": [''],
"expected_response": "I apologize, but I cannot respond to your request at this time."}, None, None
all_messages = ([{"role": "system", "content": system_prompt},{"role": "user", "content": f"{context}","role": "assistant", "content": result}])
return result_json, all_messages, result
except UnicodeDecodeError as e:
error_code = generate_error_code()
error_message = f"Failed to decode 'pips_system_prompt.txt'. Error code: {error_code}"
send_to_discord_error(error_code, f"Unicode decoding error: {str(e)}")
st.error(f"Error 16: {error_code}, {error_message}. Conversation ID: {st.session_state.conversation_id}")
return {
"analysis": "Error while decoding system prompt.",
"risk_level": "CRITICAL",
"risk_factors": ["None"],
"response_strategy": "REFUSE",
"precautions": [''],
"expected_response": "I apologize, but I cannot respond to your request at this time."
}, None, None
except Exception as e:
error_code = generate_error_code()
error_message = f"An unexpected error occurred during the PIPS query. Error code: {error_code}"
send_to_discord_error(error_message, f"Failed to perform PIPS query. Error: {str(e)}")
st.error(f"Error 2: {error_code}, {error_message}. Conversation ID: {st.session_state.conversation_id}. An unexpected error occurred. Please reset the conversation.")
return {
"analysis": "Error while processing request",
"risk_level": "CRITICAL",
"risk_factors": ["None"],
"response_strategy": "REFUSE",
"precautions": [''],
"expected_response": "I apologize, but I cannot respond to your request at this time."
}, None, None
def process_pips_result(pips_result, prompt, context):
try:
if not pips_result:
error_code = generate_error_code()
st.error(f"Error 13: {error_code}. Conversation ID: {st.session_state.conversation_id}. An unexpected error occurred. Please reset the conversation.")
return
pips_prompt = create_pips_prompt(pips_result, context)
with st.chat_message("assistant"):
response = main_model_query(pips_prompt)
st.markdown(response)
st.session_state.messages.append({"role": "assistant", "content": response})
return response
except Exception as e:
error_code = generate_error_code()
error_message = f"An error occurred while processing pips result: {str(e)}"
send_to_discord_error(error_code, error_message)
st.error(f"Error 3: {error_code}. Conversation ID: {st.session_state.conversation_id}. An unexpected error occurred. Please reset the conversation.")
return None, None, None, None, False
def create_pips_prompt(pips_result, context):
risk_level = pips_result.get('risk_level', 'CRITICAL')
response_strategy = pips_result.get('response_strategy', 'REFUSE')
analysis = pips_result.get('analysis', 'No analysis provided')
precautions = pips_result.get('precautions', ['No specific precautions provided'])
expected_response = pips_result.get('expected_response', 'I apologize, but I cannot respond to your request at this time.')
base_prompt = f"""
[SYSTEM] Risk assessment:
Risk Level: {risk_level}
Response Strategy: {response_strategy}
Analysis: {analysis}
Precautions to take:
{', '.join(precautions)}
Instructions:"""
final_prompt = ""
if response_strategy == "ENGAGE":
final_prompt = base_prompt + f"""
1. Respond to the user's query directly and helpfully.
2. Be aware of the identified risks and take the specified precautions.
3. Maintain a friendly and open tone while staying within ethical boundaries.
<user_input>
{context}
</user_input>
[SYSTEM]"""
elif response_strategy == "DEFLECT":
final_prompt = base_prompt + f"""
1. Acknowledge the user's query without directly addressing its content.
2. Redirect the conversation to a safer, related topic.
3. If appropriate, explain why you're changing the subject without revealing system details.
4. Maintain a positive and helpful tone throughout.
Expected Response:
{expected_response}
[SYSTEM]"""
elif response_strategy == "EDUCATE":
final_prompt = base_prompt + f"""
1. Acknowledge the user's query.
2. Provide information about why the query might be problematic or risky.
3. Offer alternative, safer ways to achieve the user's apparent goal.
4. Maintain an informative and supportive tone.
Expected Response:
{expected_response}
[SYSTEM]"""
elif response_strategy == "REFUSE":
final_prompt = base_prompt + f"""
1. Politely but firmly refuse to engage with the user's request.
2. Provide a brief, general explanation for the refusal without revealing system details.
3. Maintain a respectful and professional tone.
Expected Response:
{expected_response}
[SYSTEM]"""
else:
final_prompt = base_prompt + f"""5
1. Respond cautiously, avoiding any potential reveal of sensitive information.
2. If in doubt, politely deflect or refuse the request.
3. Maintain system security and ethical standards at all times.
Expected Response:
{expected_response}
[SYSTEM]"""
with st.expander("PIPS Assisted Prompt:"):
st.code(final_prompt, language="markdown")
return final_prompt
def main_model_query(pips_prompt):
main_model_system_prompt = f"""You are a helpful assistant. You are provided with the following information:"""
try:
# Use the API key from session state
OpenAI.api_key = st.session_state.api_key
messages = [
{
"role": "system",
"content": f"{main_model_system_prompt}"
}
]
messages.extend([{"role": m["role"], "content": m["content"]} for m in st.session_state.messages])
if pips_prompt:
messages.append({"role": "user", "content": pips_prompt})
with st.expander("Messages to Main Model:"):
st.write(messages)
response = pips_client.chat.completions.create(
model="gpt-4o-mini",
messages=messages,
temperature=0,
max_tokens=16383
)
model_response = response.choices[0].message.content
return model_response
except Exception as e:
error_code = generate_error_code()
st.error(f"{e}")
send_to_discord_error(error_code, f"Failed to process main model query. Error: {e}")
st.error(f"Error 5:{error_code}. Conversation ID: {st.session_state.conversation_id}. An unexpected error occurred. Please reset the conversation.")
return "I apologize, but I encountered an error while processing your request.", "", None