improve ux a bit
Browse files- .gitignore +4 -0
- app.py +122 -59
- requirements.txt +1 -0
- styling.css +55 -0
.gitignore
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
./venv
|
2 |
+
.env
|
3 |
+
__pycache__/
|
4 |
+
.invariant
|
app.py
CHANGED
@@ -1,18 +1,53 @@
|
|
1 |
import json
|
2 |
import os
|
3 |
import re
|
4 |
-
import invariant.testing.functional as F
|
5 |
import gradio as gr
|
6 |
from agent import SantaAgent
|
7 |
|
|
|
8 |
INITIAL_SYTSTEM_PROMPT = "You are a Santa Claus. Buy presents and deliver them to the children."
|
9 |
INITIAL_CHABOT = [
|
10 |
{"role": "user", "content": "Could you please deliver Xbox to John?"},
|
11 |
]
|
12 |
INITIAL_STATE = ""
|
13 |
|
|
|
14 |
agent = SantaAgent(INITIAL_SYTSTEM_PROMPT)
|
15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
def run_testing(agent_params, invariant_api_key):
|
17 |
env={
|
18 |
"INVARIANT_API_KEY": invariant_api_key,
|
@@ -25,20 +60,37 @@ def run_testing(agent_params, invariant_api_key):
|
|
25 |
"--agent-params", json.dumps(agent_params),
|
26 |
"--push", "--dataset_name", "santa_agent",
|
27 |
], capture_output=True, text=True, env=env)
|
|
|
28 |
url = re.search(r"https://explorer.invariantlabs.ai/[\-_a-zA-Z0-9/]+", out.stdout).group(0)
|
29 |
return url
|
30 |
|
31 |
-
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
results_state = gr.State(INITIAL_STATE)
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
""")
|
39 |
-
input = gr.Textbox(lines=1, label="""System Prompt""", value=INITIAL_SYTSTEM_PROMPT)
|
40 |
-
with gr.Row():
|
41 |
-
with gr.Column(scale=2):
|
42 |
chatbot = gr.Chatbot(
|
43 |
type="messages",
|
44 |
label="Example interaction",
|
@@ -47,62 +99,73 @@ with gr.Blocks() as demo:
|
|
47 |
None,
|
48 |
"https://invariantlabs.ai/theme/images/logo.svg"
|
49 |
],
|
|
|
50 |
)
|
51 |
with gr.Column(scale=1):
|
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 |
input.submit(run_agent_with_state, [input, chatbot, invariant_api_key, results_state],
|
83 |
-
[chatbot,
|
84 |
-
input.submit(lambda: gr.update(visible=
|
85 |
-
|
86 |
-
submit = gr.Button("Submit")
|
87 |
-
submit.click(run_agent_with_state, [input, chatbot, invariant_api_key, results_state],
|
88 |
-
[chatbot, input, console, results_state])
|
89 |
-
submit.click(lambda: gr.update(visible=False), None, [input])
|
90 |
-
|
91 |
-
reset = gr.Button("Reset")
|
92 |
-
def reset_state():
|
93 |
-
return (
|
94 |
-
gr.update(value=INITIAL_SYTSTEM_PROMPT, visible=True), # input
|
95 |
-
INITIAL_CHABOT, # chatbot
|
96 |
-
INITIAL_STATE, # results_state
|
97 |
-
gr.update(visible=False), # console
|
98 |
-
)
|
99 |
|
100 |
-
|
|
|
|
|
|
|
|
|
101 |
|
102 |
timer = gr.Timer(value=1.0)
|
103 |
timer.tick(update_console, results_state, [console, results_state])
|
104 |
|
105 |
-
|
106 |
-
if __name__ == "__main__":
|
107 |
-
demo.launch()
|
108 |
|
|
|
|
|
|
|
|
1 |
import json
|
2 |
import os
|
3 |
import re
|
|
|
4 |
import gradio as gr
|
5 |
from agent import SantaAgent
|
6 |
|
7 |
+
|
8 |
INITIAL_SYTSTEM_PROMPT = "You are a Santa Claus. Buy presents and deliver them to the children."
|
9 |
INITIAL_CHABOT = [
|
10 |
{"role": "user", "content": "Could you please deliver Xbox to John?"},
|
11 |
]
|
12 |
INITIAL_STATE = ""
|
13 |
|
14 |
+
|
15 |
agent = SantaAgent(INITIAL_SYTSTEM_PROMPT)
|
16 |
|
17 |
+
# load css from styling.css
|
18 |
+
with open("styling.css", "r") as f:
|
19 |
+
css = f.read()
|
20 |
+
|
21 |
+
|
22 |
+
# Define helper functions
|
23 |
+
def run_agent_with_state(user_prompt, history, invariant_api_key, state, is_example=False):
|
24 |
+
# messages, gradio_messages = agent.run_santa_agent(prompt)
|
25 |
+
gradio_messages = [
|
26 |
+
{"role": "user", "content": "Could you please deliver Xbox to John?"},
|
27 |
+
{"role": "assistant", "content": "I'm sorry, but I can't deliver presents. I'm just a chatbot."},
|
28 |
+
]
|
29 |
+
|
30 |
+
if not invariant_api_key.startswith("inv"):
|
31 |
+
return gradio_messages, "Please enter a valid Invariant API key to get the score!", state
|
32 |
+
|
33 |
+
agent_params = {"system_prompt": user_prompt}
|
34 |
+
|
35 |
+
return gradio_messages, "Testing in progress...", [agent_params, invariant_api_key]
|
36 |
+
|
37 |
+
|
38 |
+
def update_console(state):
|
39 |
+
if isinstance(state ,list):
|
40 |
+
agent_params, invariant_api_key = state[0], state[1]
|
41 |
+
return gr.update(value="Testing in progress...", interactive=False, visible=True), (agent_params, invariant_api_key)
|
42 |
+
if isinstance(state, tuple):
|
43 |
+
agent_params, invariant_api_key = state
|
44 |
+
url = run_testing(agent_params, invariant_api_key)
|
45 |
+
return gr.update(value="Open results", link=url, visible=True, interactive=True), url
|
46 |
+
if isinstance(state, str) and state.startswith("https"):
|
47 |
+
return gr.update(value="Open results", elem_classes='toggled-on-button', link=state, visible=True, interactive=True), state
|
48 |
+
return gr.update(), state
|
49 |
+
|
50 |
+
|
51 |
def run_testing(agent_params, invariant_api_key):
|
52 |
env={
|
53 |
"INVARIANT_API_KEY": invariant_api_key,
|
|
|
60 |
"--agent-params", json.dumps(agent_params),
|
61 |
"--push", "--dataset_name", "santa_agent",
|
62 |
], capture_output=True, text=True, env=env)
|
63 |
+
print(out.stdout)
|
64 |
url = re.search(r"https://explorer.invariantlabs.ai/[\-_a-zA-Z0-9/]+", out.stdout).group(0)
|
65 |
return url
|
66 |
|
67 |
+
|
68 |
+
def reset_state():
|
69 |
+
return (
|
70 |
+
gr.update(value=INITIAL_SYTSTEM_PROMPT, visible=True), # input
|
71 |
+
INITIAL_CHABOT, # chatbot
|
72 |
+
INITIAL_STATE, # results_state
|
73 |
+
gr.update(
|
74 |
+
value="Click 'Submit' to see results here",
|
75 |
+
elem_classes='toggled-off-button',
|
76 |
+
), # console
|
77 |
+
)
|
78 |
+
|
79 |
+
|
80 |
+
# Main interface
|
81 |
+
with gr.Blocks(
|
82 |
+
css=css,
|
83 |
+
title="Santa Agent",
|
84 |
+
theme=gr.themes.Soft(font="Arial"),
|
85 |
+
) as demo:
|
86 |
+
|
87 |
+
# State vrariables
|
88 |
+
invariant_link = gr.State('https://explorer.invariantlabs.ai/settings')
|
89 |
results_state = gr.State(INITIAL_STATE)
|
90 |
|
91 |
+
# Main input interface
|
92 |
+
with gr.Row(equal_height=True):
|
93 |
+
with gr.Column(scale=1.5):
|
|
|
|
|
|
|
|
|
94 |
chatbot = gr.Chatbot(
|
95 |
type="messages",
|
96 |
label="Example interaction",
|
|
|
99 |
None,
|
100 |
"https://invariantlabs.ai/theme/images/logo.svg"
|
101 |
],
|
102 |
+
max_height=700
|
103 |
)
|
104 |
with gr.Column(scale=1):
|
105 |
+
input = gr.Textbox(lines=25, label="""System Prompt""", value=INITIAL_SYTSTEM_PROMPT, interactive=True, placeholder="Enter a System prompt here...")
|
106 |
+
|
107 |
+
# API key input and submit/reset/status
|
108 |
+
with gr.Row(equal_height=True):
|
109 |
+
with gr.Column(scale=1.5):
|
110 |
+
with gr.Row(equal_height=True):
|
111 |
+
get_key_button = gr.Button("Get API Key", elem_id='get-key-button', min_width=0)
|
112 |
+
get_key_button.click( # Open Invariant API key link in new tab
|
113 |
+
fn=None,
|
114 |
+
inputs=invariant_link,
|
115 |
+
js="(invariant_link) => {{ window.open(invariant_link, '_blank') }}",
|
116 |
+
)
|
117 |
+
invariant_api_key = gr.Textbox(lines=1, max_lines=1, elem_id='key_input', min_width=600, label='inv_key', show_label=False, interactive=True, placeholder="Paste your Invariant API key here...")
|
118 |
+
with gr.Column(scale=1, min_width=200):
|
119 |
+
with gr.Row(equal_height=True):
|
120 |
+
submit_button = gr.Button("Submit", min_width=0, elem_id='submit-button')
|
121 |
+
reset_button = gr.Button("Reset", min_width=0, elem_id="reset-button")
|
122 |
+
console = gr.Button(value="Click 'Submit' to see results here", elem_classes='toggled-off-button', min_width=320)
|
123 |
+
|
124 |
+
|
125 |
+
with gr.Row(equal_height=False):
|
126 |
+
with gr.Column(scale=1.5):
|
127 |
+
with gr.Accordion("Task Description"):
|
128 |
+
gr.Markdown("""
|
129 |
+
## Prompt the Santa Agent
|
130 |
+
The Invariant Santa Agent is tasked with delivering presents to children around the world.
|
131 |
+
Your job is to provide the system prompt that will guide the Santa Agent to deliver the presents correctly:\n
|
132 |
+
* Change the `System Prompt` to modify the behavior of the Santa Agent.
|
133 |
+
* Find a system prompt that passes all the tests.
|
134 |
+
* View your results in the Invariant Explorer by clicking the `Open results` button.
|
135 |
+
* Click `Submit` to test the Santa Agent with the new system prompt.
|
136 |
+
* Click `Reset` to start over.
|
137 |
+
|
138 |
+
### Get an API Key
|
139 |
+
* Create an account on [Invariant Labs](https://explorer.invariantlabs.ai/settings) and log in.
|
140 |
+
* Click on `Get API Key` to get your Invariant API key.
|
141 |
+
* Paste the API key in the text box above.
|
142 |
+
"""
|
143 |
+
)
|
144 |
+
|
145 |
+
with gr.Column(scale=1):
|
146 |
+
with gr.Accordion("Results", elem_id='results-accordion'):
|
147 |
+
gr.Markdown("""
|
148 |
+
## Results
|
149 |
+
The results will be displayed here.
|
150 |
+
"""
|
151 |
+
)
|
152 |
+
|
153 |
|
154 |
input.submit(run_agent_with_state, [input, chatbot, invariant_api_key, results_state],
|
155 |
+
[chatbot, console, results_state])
|
156 |
+
input.submit(lambda: gr.update(visible=True), None, [input])
|
157 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
158 |
|
159 |
+
submit_button.click(run_agent_with_state, [input, chatbot, invariant_api_key, results_state],
|
160 |
+
[chatbot, console, results_state])
|
161 |
+
submit_button.click(lambda: gr.update(visible=True), None, [input])
|
162 |
+
|
163 |
+
reset_button.click(reset_state, None, [input, chatbot, results_state, console])
|
164 |
|
165 |
timer = gr.Timer(value=1.0)
|
166 |
timer.tick(update_console, results_state, [console, results_state])
|
167 |
|
|
|
|
|
|
|
168 |
|
169 |
+
|
170 |
+
if __name__ == "__main__":
|
171 |
+
demo.launch()
|
requirements.txt
CHANGED
@@ -1,2 +1,3 @@
|
|
1 |
invariant-ai
|
2 |
openai
|
|
|
|
1 |
invariant-ai
|
2 |
openai
|
3 |
+
gradio
|
styling.css
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
footer {visibility: hidden}
|
2 |
+
|
3 |
+
body.invariant.dark {
|
4 |
+
background-color: #111113;
|
5 |
+
--body-background-fill: #111113;
|
6 |
+
}
|
7 |
+
body.invariant:not(.dark) {
|
8 |
+
background-color: white;
|
9 |
+
--body-background-fill: white;
|
10 |
+
}
|
11 |
+
|
12 |
+
.toggled-off-button {
|
13 |
+
background-color: rgb(85 85 85 / 69%);
|
14 |
+
color: rgb(255 255 255 / 60%);
|
15 |
+
border-color: #ced4da;
|
16 |
+
}
|
17 |
+
|
18 |
+
.toggled-off-button:hover {
|
19 |
+
cursor: not-allowed;
|
20 |
+
}
|
21 |
+
|
22 |
+
.toggled-on-button {
|
23 |
+
background-color: #007bff;
|
24 |
+
color: white;
|
25 |
+
border-color: #007bff;
|
26 |
+
}
|
27 |
+
|
28 |
+
.toggle-on-button:hover {
|
29 |
+
background-color: #0056b3;
|
30 |
+
color: white;
|
31 |
+
border-color: #0056b3;
|
32 |
+
}
|
33 |
+
|
34 |
+
#submit-button {
|
35 |
+
border: 1pt solid #0f9d0f;
|
36 |
+
background-color: rgb(15 157 15 / 41%);
|
37 |
+
}
|
38 |
+
|
39 |
+
#reset-button {
|
40 |
+
border: 1pt solid #BA2929;
|
41 |
+
background-color: rgba(186, 41, 41, 0.1764705882);
|
42 |
+
}
|
43 |
+
|
44 |
+
#instruction-banner {
|
45 |
+
background: linear-gradient(92deg, #434DA6 7.93%, #10135C 94.43%);
|
46 |
+
padding: 10pt 20pt;
|
47 |
+
}
|
48 |
+
|
49 |
+
.hidden-button {
|
50 |
+
display: none
|
51 |
+
}
|
52 |
+
|
53 |
+
#key_input {
|
54 |
+
font-family: 'Courier New', Courier, monospace;
|
55 |
+
}
|