-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathideate_ui.py
344 lines (288 loc) · 11 KB
/
ideate_ui.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# Original ChatGPT ideation tab - needs updating to work
import streamlit as st
from streamlit_agraph import agraph, Node, Edge, Config
import logging
import openai
import json
import random
import base64
import ast
# Yes yes - move all the non-ui stuff into a controller or something already
CHATGPT_KEY = "chatgpt"
def arrows_uri(input: str | dict) -> str:
"""
Generates a URI for an arrows app visualization from a json object. WARNING! May overwrite existing arrows drawing.
Args:
input: A dictionary or string representing an arrows compatible .json configuration
Returns:
A string URI for an arrows app visualization
"""
# Convert dict to string if needed
if isinstance(input, dict):
input = json.dumps(input)
# Convert the diction object into a base 64 json string
b = input.encode('utf-8')
base64_str = base64.b64encode(b).decode('utf-8')
result = f"https://arrows.app/#/import/json={base64_str}"
logging.debug(f'\n\nOutput arrows uri from {input} with base64 JSON: \n{result}')
return result
def arrows_dictionary(nodes: list[Node], edges: list[Edge], name: str = "GraphGPT Generated Model") -> dict:
"""
Generates an arrows.app compatible .json file from agraph nodes and edges
Args:
nodes: List of agraph Nodes
edges: List of agraph Edges
Returns:
A dictionary matching arrows .json schema
"""
result_nodes = []
result_relationships = []
for n in nodes:
random_x = round(random.uniform(-600, 600), 14)
random_y = round(random.uniform(-600, 600), 14)
result_nodes.append({
"id": n.id,
"position":{
"x" : random_x,
"y": random_y
},
"caption": n.label,
"style":{},
"labels":[],
"properties":{}
})
for idx, e in enumerate(edges):
logging.debug(f'Processing edge to relationships: {e.__dict__}')
ns = e.source
nt = e.to
type = e.label.replace(" ", "_")
result_relationships.append(
{
"id":f"n{idx}",
"type":type,
"fromId":ns,
"toId":nt,
"style":{},
"properties":{}
}
)
result = {
"graph": {
"style": {
"font-family": "sans-serif",
"background-color": "#ffffff",
"background-image": "",
"background-size": "100%",
"node-color": "#ffffff",
"border-width": 4,
"border-color": "#000000",
"radius": 50,
"node-padding": 5,
"node-margin": 2,
"outside-position": "auto",
"node-icon-image": "",
"node-background-image": "",
"icon-position": "inside",
"icon-size": 64,
"caption-position": "inside",
"caption-max-width": 200,
"caption-color": "#000000",
"caption-font-size": 50,
"caption-font-weight": "normal",
"label-position": "inside",
"label-display": "pill",
"label-color": "#000000",
"label-background-color": "#ffffff",
"label-border-color": "#000000",
"label-border-width": 4,
"label-font-size": 40,
"label-padding": 5,
"label-margin": 4,
"directionality": "directed",
"detail-position": "inline",
"detail-orientation": "parallel",
"arrow-width": 5,
"arrow-color": "#000000",
"margin-start": 5,
"margin-end": 5,
"margin-peer": 20,
"attachment-start": "normal",
"attachment-end": "normal",
"relationship-icon-image": "",
"type-color": "#000000",
"type-background-color": "#ffffff",
"type-border-color": "#000000",
"type-border-width": 0,
"type-font-size": 16,
"type-padding": 5,
"property-position": "outside",
"property-alignment": "colon",
"property-color": "#000000",
"property-font-size": 16,
"property-font-weight": "normal"
},
"nodes":result_nodes,
"relationships":result_relationships,
"diagramName": name
}
}
logging.debug(f'\n\nProcessed incoming nodes: {nodes}, edges: {edges} to:\n {result}')
return result
def triples_prompt(prompt: str)-> str:
# Full prompt string to query openai with and finasse expected response
full_prompt = f"""
Given a prompt, extrapolate as many relationships as possible from it and provide a list of updates.
If an update is a relationship, provide [ENTITY 1, RELATIONSHIP, ENTITY 2]. The relationship is directed, so the order matters.
Each relationship must have 3 items in the list.
Limit the number of relationships to 12.
Return only the data, do not explain.
Only return a list of 3 item lists.
For example, the prompt: `Alice is Bob's roommate` should return [["Alice", "roommate", "Bob"]]
prompt: {prompt}
"""
return full_prompt
def agraph_nodes_edges(response: str | list) -> tuple[list[Node], list[Edge]]:
"""
Converts an openai response into agraph nodes and relationships
Args:
response: String or list in the format of [["node_id_string", "edge_id_string", "another_node_id_string"],...]
Returns:
A tuple of agraph nodes in a list and agraph edges in a list
Raises:
...
"""
logging.debug(f'agraph_nodes_edges() response recieved: {response}')
# Response will be a list of 3 item tuples
# Convert to list of lists - if needed
if isinstance(response, str):
try:
answers = json.loads(response)
except:
logging.debug(f'Unable to parse string to list with json.loads. Attempting ast...')
answers = ast.literal_eval(response)
elif isinstance(response, list):
answers = response
else:
raise ValueError(f'Response is not a string or list. Response: {response}')
logging.debug(f'JSON parsed: {answers}')
nodes = set()
result_edges = []
for item in answers:
# Each should be a tuple of 3 items, node-edge-node
n1 = item[0]
r = item[1]
n2 = item[2]
# Standardize casing
r = r.upper()
n1 = n1.title()
n2 = n2.title()
nodes.add(n1)
nodes.add(n2)
edge = Edge(source=n1, target=n2, label=r)
result_edges.append(edge)
result_nodes = []
for node_label in list(nodes):
node = Node(id=node_label, label=node_label)
result_nodes.append(node)
logging.debug(f'Nodes returning: {result_nodes}')
logging.debug(f'Edges returning: {result_edges}')
return result_nodes, result_edges
@st.cache_data
def generate_openai_response(prompt)-> str:
# TODO: Make this configurable
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
# model="gpt-4",
messages=[{"role": "user", "content": prompt}]
)
# TODO: Validate reponse
content = response.choices[0].message.content
logging.debug(f'OpenAI Response: {response}, type: {type(content)}')
return content
def agraph_from_sample(prompt: str):
# TODO: Pull from a file of samples
openai_response = '[["Sharks", "eat", "big fish"], ["Big fish", "eat", "small fish"], ["Small fish", "eat", "bugs"]]'
nodes, edges = agraph_nodes_edges(openai_response)
config = Config(height=400, width=1000, directed=True)
if nodes is not None:
agraph(nodes=nodes,
edges=edges,
config=config)
def ideate_ui():
st.markdown("Use a variation of Varun Shenoy's original [GraphGPT](https://graphgpt.vercel.app) to convert a natural language description into a graph data model")
# OPENAI TEXTFIELD
new_open_ai_key = st.text_input(f'OpenAI KEY', type="password", value=st.session_state["OPENAI_API_KEY"])
# Set openAI key
openai.api_key = new_open_ai_key
# Display prompt for user input
sample_prompt = "Sharks eat big fish. Big fish eat small fish. Small fish eat bugs."
if st.button('Load Sample', key="graphgpt_sample"):
st.session_state["SAMPLE_PROMPT"] = sample_prompt
prompt = st.text_area("Prompt", value=st.session_state["SAMPLE_PROMPT"])
if prompt is None or prompt == "":
return
if prompt == sample_prompt:
# Load vetted response to save on hitting openai for the same thing
response = [["Sharks", "eat", "big fish"], ["Big fish", "eat", "small fish"], ["Small fish", "eat", "bugs"]]
else:
# Send completion request to openAI
full_prompt = triples_prompt(prompt)
response = generate_openai_response(full_prompt)
# Convert response to agraph nodes and edges
nodes, edges = agraph_nodes_edges(response)
# Configure and display agraph
config = Config(width=1000, height=400, directed=True)
if nodes is None:
return
# Display data
st.write('Graph Viewer')
agraph(nodes=nodes,
edges=edges,
config=config)
# For displaying JSON schema. This can be quite long though
# st.write('JSON Representation')
# arrows_str = json.dumps(arrows_dict, indent=4)
# st.code(arrows_str)
# Prep arrows compatible dictioary for button options
arrows_dict = arrows_dictionary(nodes, edges)
b1, b2, b3 = st.columns([1,1,3])
with b1:
if st.button("Edit in Arrows"):
# Prep arrows compatible json
uri = arrows_uri(arrows_dict)
st.session_state["ARROWS_URI"] = uri
st.warning("Close and reopen 'Arrows Data Modeler' to refresh")
with b2:
if st.button("Push to Generator"):
st.session_state["ARROWS_DICT"] = arrows_dict
# def agraph_sample():
# # Agraph
# nodes = []
# edges = []
# nodes.append( Node(id="Spiderman",
# label="Peter Parker",
# size=25,
# shape="circularImage",
# image="http://marvel-force-chart.surge.sh/marvel_force_chart_img/top_spiderman.png")
# ) # includes **kwargs
# nodes.append( Node(id="Captain_Marvel",
# size=25,
# shape="circularImage",
# image="http://marvel-force-chart.surge.sh/marvel_force_chart_img/top_captainmarvel.png")
# )
# edges.append( Edge(source="Captain_Marvel",
# label="friend_of",
# target="Spiderman",
# # **kwargs
# )
# )
# config = Config(width=750,
# height=950,
# directed=True,
# physics=True,
# hierarchical=False,
# # **kwargs
# )
# agraph(nodes=nodes,
# edges=edges,
# config=config)