-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiagram.py
262 lines (210 loc) · 8.27 KB
/
diagram.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
import openai
import os
openai.api_key = os.environ["OPENAI_API_KEY"]
from tree_sitter import Language
import graphviz as gv
import ast
from tree_sitter import Parser
from llama_index.chat_engine import SimpleChatEngine
from llama_index.agent import OpenAIAgent
from llama_index.tools.function_tool import FunctionTool
with open('.env') as f:
for line in f:
if line.strip():
key, value = line.strip().split('=')
os.environ[key] = value
openai.api_key = os.getenv("OPENAI_API_KEY")
# Parse AST
filepath = 'indexer.py'
f_contents = None
with open(filepath, 'r') as f:
f_contents = f.read()
ast_tree = ast.parse(f_contents)
ast_index = {}
# Index by file name
ast_index[filepath] = ast_tree
# Index symbols
for node in ast.walk(ast_tree):
if isinstance(node, ast.FunctionDef):
ast_index[node.name] = node
elif isinstance(node, ast.ClassDef):
ast_index[node.name] = node
print(ast_index)
class PrintNodeVisitor(ast.NodeVisitor):
def generic_visit(self, node):
print(f"Node type: {type(node).__name__}")
print(f"Node info: {ast.dump(node)}")
super().generic_visit(node)
# output_string = ""
# for key, node in ast_index.items():
# output_string += f"{key}: {type(node).__name__}\n"
# print(output_string)
# # Create a custom tool. Type annotations and docstring are used for the
# # tool definition sent to the Function calling API.
# def visit_ast_node(key) -> int:
# """
# Adds the two numbers together and returns the result.
# """
# node = ast_index.get(key)
# if node:
# visitor = PrintNodeVisitor()
# visitor.visit(node)
# else:
# print(f"No node found for key: {key}")
# # Use the get_ast_node_by_name function
# node_name = 'foo' # Replace with the actual node name
# visit_ast_node(node_name)
# visit_function_tool = FunctionTool.from_defaults(fn=visit_ast_node)
graph = gv.Digraph(format='png')
def visualize_node(node, parent=None):
"""
Visualize a node in the AST
Parameters
----------
node : ast.AST
The node to visualize
parent : str
The name of the parent node
"""
if isinstance(node, ast.FunctionDef):
graph.node(node.name, shape='box', color='blue', label=f'Function: {node.name}')
if parent:
graph.edge(parent, node.name, color='blue')
elif isinstance(node, ast.ClassDef):
graph.node(node.name, shape='ellipse', color='green', label=f'Class: {node.name}')
if parent:
graph.edge(parent, node.name, color='green')
for child in ast.iter_child_nodes(node):
parent_name = node.name if hasattr(node, 'name') else None
visualize_node(child, parent_name)
# visualize_file(ast_index)
# graph.render('output')
# visualize_node_tool = FunctionTool.from_defaults(fn=visualize_node)
# tools = [visit_function_tool, visualize_node_tool]
# agent = OpenAIAgent.from_tools(tools, verbose=True)
# # use agent
# agent.chat(f"Here are the nodes in the index: {output_string}. Please think about what the user is trying to understand and please select one to visualize.")
def render_project_ticket():
return """
<Project Ticket>
This clone repo doesn't include the depth of the repo. It only clones the first level of the repo. It should clone the entire repo.
</Project Ticket>"""
# return """
# <Project Ticket>
# The token splitting has a bug. It splits the text into too many chunks. It should split the text into fewer chunks.
# </Project Ticket>
# """
def render_code_snippets():
return """
<Code snippet>
def clone_repo(self):
print("Cloning repo...")
os.system(f"git clone {{self.repo_url}} {{self.local_repo_path}}")
</Code snippet>
"""
# return """
# <Code snippet>
# # filepath: indexer.py
# def split_by_token(text, tokenizer):
# # heuristic
# text_token_len = len(tokenizer.encode(text))
# n_chunks = text_token_len // 8000 + 1
# if n_chunks > 1:
# words = text.split(" ")
# chunk_len = len(words) // n_chunks
# chunks = [words[i : i + chunk_len] for i in range(0, len(words), chunk_len)]
# return [" ".join(chunk) for chunk in chunks]
# else:
# return [text]
# </Code snippet>
# """
def render_feedback():
return "You did not return valid Graphviz DOT code. You included text before or after the Graphviz DOT code. Please try again."
def render_ast_nodes():
node_name = 'split_by_token'
node = ast_index.get(node_name)
node_info = ""
if node:
visitor = PrintNodeVisitor()
node_info = f"Node type: {type(node).__name__}\nNode info: {ast.dump(node)}"
visitor.visit(node)
else:
print(f"No node found for key: {node_name}")
return f"""
<AST nodes>
{node_info}
</AST nodes>
"""
prompt = f"""
You're an AI chatbot that creates visualizations using Graphviz DOT code.
You're job, is to visualize the changes needed for a user's bug report, request for changes, or new feature request, Basically a Project Ticket.
Here is the :
{render_project_ticket()}
Here are are the most relevant files and code snippets:
{render_code_snippets()}
Here are the AST nodes in the index that occur in the code snippets:
{render_ast_nodes()}
You're job is to generate Graphviz DOT code that visualizes the changes needed to the user's code to fix their bug, fulfill their request, or implement their feature request.
Generate Graphviz DOT code by looking at provided ticket, code and AST and adding nodes, edges, comments and other information to represent the changes needed. For example:
digraph G {{
UserClass [shape=box]
NewFeatureClass [shape=ellipse]
UserClass -> NewFeatureClass [label="implements"]
}}
Notes:
- Only output valid DOT code
- Please include as much information as possible to help the user understand the changes needed.
- Please include instructions on how to make the changes to the code.
- Please include a visualization of the AST that highlights the changes.
- Use colors to highlight the changes.
- Use shapes to highlight the changes.
- Use labels to highlight the changes.
- Use arrows to highlight the changes.
<Feedback from user>
{render_feedback()}
</Feedback from user>
<Example outputs>
digraph G {{
graph [pad="0.5", nodesep="0.5", ranksep="2"];
node [shape=box];
struct1 [label="split_by_token function | Adjust the behavior of n_chunks and chunk_len", fillcolor=lightblue, style=filled];
struct2 [label="n_chunks calculation | Add a different logic to reduce chunks", fillcolor=yellow, style=filled];
struct3 [label="chunk_len calculation | Modify logic based on the new n_chunks", fillcolor=yellow, style=filled];
struct4 [label="chunks list comprehension | No changes required", fillcolor=green, style=filled];
struct5 [label="Returns | No changes required", fillcolor=green, style=filled];
struct1 -> struct2;
struct2 -> struct3;
struct3 -> struct4;
struct4 -> struct5;
label = "\n\nFlowchart of the changes needed on split_by_token function in indexer.py to fix the bug with token splitting.\n\nYellow: Code adjustment suggested\nGreen: No changes needed";
fontsize = 28;
}}
</Example output>
Finally your out must be valid, parseable Graphviz DOT code. It may not incldue any text before or after the graphviz code.
"""
print(prompt)
out = openai.ChatCompletion.create(
model="gpt-4",
messages=[{'role': 'user', 'content': prompt.strip()}],
max_tokens=2000,
)
parsed_content = out['choices'][0]['message']['content']
print(parsed_content)
print(parsed_content)
with open('output.dot', 'w') as f:
f.write(parsed_content)
os.system('dot -Tpng output.dot -o output.png')
# """
# Given:
# - User's bug, request for changes, or new feature request
# - User's code, semantically retrieved from their bug report/request
# - User's code's AST
# Goal:
# - Generate a list of possible changes to the user's code that would fix their bug,
# fulfill their request, or implement their feature request
# - Generate a visualization of the user's code's AST that highlights the changes
# - Annotate the visualization with instructions on how to make the changes to the diagrams.
# Do this using a AI chatbot that can be trained to understand the user's request and
# generate the changes and visualization.
# The agent will annotate the visualization with instructions on how to make the changes.
# """