/
galileo-protect-demo.py
208 lines (176 loc) · 6.57 KB
/
galileo-protect-demo.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
from langchain_community.tools import (
Tool,
DuckDuckGoSearchRun,
ArxivQueryRun,
WikipediaQueryRun,
)
from langchain_community.utilities import WikipediaAPIWrapper
from langchain_openai import ChatOpenAI
from langchain.agents import initialize_agent
from langchain.agents import AgentType
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
from langchain_community.callbacks import StreamlitCallbackHandler
import os
import streamlit as st
import galileo_protect as gp
from galileo_observe import GalileoObserveCallback
# A hack to "clear" the previous result when submitting a new prompt. This avoids
# the "previous run's text is grayed-out but visible during rerun" Streamlit behavior.
class DirtyState:
NOT_DIRTY = "NOT_DIRTY"
DIRTY = "DIRTY"
UNHANDLED_SUBMIT = "UNHANDLED_SUBMIT"
def get_dirty_state() -> str:
return st.session_state.get("dirty_state", DirtyState.NOT_DIRTY)
def set_dirty_state(state: str) -> None:
st.session_state["dirty_state"] = state
def with_clear_container(submit_clicked):
if get_dirty_state() == DirtyState.DIRTY:
if submit_clicked:
set_dirty_state(DirtyState.UNHANDLED_SUBMIT)
st.rerun()
else:
set_dirty_state(DirtyState.NOT_DIRTY)
if submit_clicked or get_dirty_state() == DirtyState.UNHANDLED_SUBMIT:
set_dirty_state(DirtyState.DIRTY)
return True
return False
monitor_handler = GalileoObserveCallback(project_name='demo-galileo-protect')
# metrics = [
# Scorers.context_adherence,
# Scorers.completeness_gpt,
# Scorers.prompt_perplexity,
# Scorers.pii,
# Scorers.chunk_attribution_utilization_gpt
# ]
#
# pq.login("https://console.demo.rungalileo.io")
# If you don't have your GALILEO_USERNAME and GALILEO_PASSWORD exported, login
# galileo_handler = pq.GalileoPromptCallback(
# project_name='sg_chatdemo_1', scorers=metrics, run_name='run_sn3'
# # Make sure the run_name is unique across runs
# )
st.set_page_config(
page_title="Galileo's Customer Service Chatbot",
page_icon="🔭",
layout="wide",
initial_sidebar_state="collapsed",
)
"# 🔭 Galileo's Car Agency Customer Service Chatbot"
user_openai_api_key = os.environ["OPENAI_API_KEY"]
# Looks for openai_api_key
if user_openai_api_key:
openai_api_key = user_openai_api_key
enable_custom = True
else:
openai_api_key = "not_supplied"
enable_custom = False
arxiv = ArxivQueryRun()
wiki = WikipediaQueryRun(api_wrapper=WikipediaAPIWrapper())
llm = ChatOpenAI(temperature=0, openai_api_key=os.environ["OPENAI_API_KEY"])
tools = [
Tool(
name="Arxiv",
func=arxiv.run,
description="useful when you need an answer about encyclopedic general knowledge",
),
Tool(
name="Wikipedia",
func=wiki.run,
description="useful when you need an answer about encyclopedic general knowledge",
)
]
agent = initialize_agent(tools, llm, agent=AgentType.OPENAI_FUNCTIONS, verbose=True)
with st.form(key="form"):
user_input = ""
if enable_custom:
user_input = st.text_input(
"This is a customer service agent. Tell this agent what do you want to know and it will find the answers for you to its best ability."
)
submit_clicked = st.form_submit_button("Submit")
output_container = st.empty()
if with_clear_container(submit_clicked):
output_container = output_container.container()
output_container.chat_message("user").write(user_input)
answer_container = output_container.chat_message("assistant", avatar="🔭")
st_callback = StreamlitCallbackHandler(answer_container)
prompt = "Answer the user's question using the tools provided. For successful task completion: Consider user's question and determine which search tool is best suited based on its capabilities. You will always pass the output to the Protect tool Question: {input}"
input = user_input
answer = agent.invoke(prompt.format(input=input), config=dict(callbacks=[st_callback,monitor_handler]))
answer_container.write(f"**Response from the model:**")
answer_container.write(answer['output'])
payload = {}
payload['input'] = user_input
payload['output'] = answer['output']
response = gp.invoke(
payload=payload,
prioritized_rulesets=[
{
"rules": [
{
"metric": "pii",
"operator": "contains",
"target_value": "address",
},
],
"action": {
"type": "OVERRIDE",
"choices": [
"Personal address detected in the model output. Sorry, I cannot answer that question."
],
},
},
{
"rules": [
{
"metric": "input_toxicity",
"operator": "gte",
"target_value": 0.9,
},
],
"action": {
"type": "OVERRIDE",
"choices": [
"Toxicity detected in the user's prompt. Sorry, I cannot answer that question."
],
},
},
{
"rules": [
{
"metric": "prompt_injection",
"operator": "eq",
"target_value": "impersonation",
},
],
"action": {
"type": "OVERRIDE",
"choices": [
"Prompt injection detected in the user's prompt. Sorry, I cannot answer that question."
],
},
},
{
"rules": [
{
"metric": "prompt_injection",
"operator": "eq",
"target_value": "new_context",
},
],
"action": {
"type": "OVERRIDE",
"choices": [
"Prompt injection detected in the user's prompt. Sorry, I cannot answer that question."
],
},
},
],
stage_id="b67f362f-126e-45c6-a78f-35ce85098a79",
timeout=10,
)
answer_container.write(f"**Response from Galileo Protect:**")
answer_container.write(response.text)
st.button('Integration details')
st.write(response.model_dump())