-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautonomous_app_build.py
More file actions
313 lines (266 loc) · 12.2 KB
/
autonomous_app_build.py
File metadata and controls
313 lines (266 loc) · 12.2 KB
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
import os
from langchain_google_genai import ChatGoogleGenerativeAI
import operator
from typing import Annotated, List, Tuple, TypedDict
from typing import Literal
from langchain_core.output_parsers import StrOutputParser
import subprocess
from langchain import hub
from langgraph.prebuilt import create_react_agent
from langgraph.graph import StateGraph, START,END
from langchain.prompts import MessagesPlaceholder
from langchain.prompts import ChatPromptTemplate
from pydantic import BaseModel, Field
import asyncio
import re
import streamlit as st
st.title("InstaDev")
#Adding Memory
if "messages" not in st.session_state:
st.session_state.messages = []
for idx,message in enumerate(st.session_state.messages):
with st.chat_message(message["role"]):
if message["role"] == "assistant":
st.code(message["content"][0], language='python')
st.download_button(
label="Download Code",
data=message["content"][0], # Data to be downloaded
file_name="download_app.py", # Name of the downloaded file
mime="python", # File type
key = str(idx)
)
st.download_button(
label="Download Readme",
data=message["content"][1], # Data to be downloaded
file_name="ReadMe.txt", # Name of the downloaded file
mime="text/plain", # File type
key = 'r' + str(idx)
)
else:
st.code(message["content"], language='python')
# CONNECTING GEMINI
from google.auth.transport.requests import Request
from google.oauth2.service_account import Credentials
import os
key_path='C:/Users/amogh.prabhu/Downloads/rich-tome-436705-f7-8244445b57d1.json'
credentials = Credentials.from_service_account_file(key_path)
PROJECT_ID = 'rich-tome-436705-f7'
REGION = 'us-central1'
import vertexai
vertexai.init(project = PROJECT_ID, location = REGION, credentials = credentials)
from langchain_google_vertexai import ChatVertexAI
llm = ChatVertexAI(
model="gemini-1.5-pro",
temperature=0,
max_tokens=None,
max_retries=6,
stop=None,
)
tools = []
prompt = ChatPromptTemplate.from_messages(
[
("system", "You are a python code generator and you are a part big chain your only task is to generate code as per the user requirement at the end you shoud generate code for the given task."),
("system", "generate only code dont do any extra things and generate code between ``` code ```"),
MessagesPlaceholder(variable_name="messages"),
]
)
code_agent_executor = create_react_agent(llm, tools, state_modifier=prompt)
doc_prompt = ChatPromptTemplate.from_messages(
[
("system", "you are a documentation creater for the code given by user carefully study and the code and neatly create a professional documentation for the code"),
MessagesPlaceholder(variable_name="messages"),
]
)
document_generator = create_react_agent(llm,tools, state_modifier=doc_prompt)
class PlanExecute(TypedDict):
input: str
plan: List[str]
past_steps: Annotated[List[Tuple], operator.add]
response: str
test_status: str | int
done : bool = False
class Plan(BaseModel):
"""Plan to follow in future"""
steps: str = Field(description="single step instruction to build the complete app")
from langchain_core.prompts import ChatPromptTemplate
planner_prompt = ChatPromptTemplate.from_messages(
[
(
"system",
"""For the given objective, generate a single step instruction that instructs the coder to write the entire code for the desired application. \
The step should be in the form of a single string. Format it as follows: \
"generate a code for <application description> using <module or framework> with extra features if required". \
Make sure to specify the correct module or framework based on the type of application. For example:
Follow this exact structure for all tasks Remember only one step instruction and dont give instructions for installation since modules are already installed."""
),
("user","For a calculator app:"),
("system",'''generate a code for complete calculator app using tkinter module'''),
("user","For a snake game app:"),
("system",'''generate a code for snake game using pygame module'''),
("placeholder", "{messages}"),
]
)
planner = planner_prompt | llm.with_structured_output(Plan) # LLM chain
replanner_prompt = ChatPromptTemplate.from_template("""
Your objective was this:
{input}
And your previous plan was
{plan}
And your previous code was
{past_steps}
And Error message was
{test_status}
{done}
considering the above case generate a single step like below
- For a calculator app: "generate a code for complete calculator app using tkinter module"
- For a snake game app: "generate a code for snake game using pygame module"
Follow this exact structure for all tasks. If any module is not found make the step such that it should use subprocess to install the module"""
)
replanner = replanner_prompt | llm.with_structured_output(Plan)
parser = StrOutputParser()
class Dep_List(BaseModel):
"""Plan to follow in future"""
modules: List[str] = Field(description="list of names(string format) of modules that need to be installed")
install_dep_prompt = ChatPromptTemplate.from_messages(
[
(
"system","""Given a Python code snippet, extract all external dependencies that need to be installed via pip. Return a list of valid Python package names that are compatible with pip install. If the module is part of the Python standard library, exclude it from the list. Ensure that all names are in the correct format for pip, so each item in the list can be installed directly using pip install <module_name>.
The response should be a Python list format with module names in string format.The modules names need nod be same so research ,think and output take care of _ and - and Cases"""),
("placeholder", "{messages}"),
]
)
inst_dep_chain = install_dep_prompt | llm.with_structured_output(Dep_List) # LLM chain
agent_response = ""
async def execute_step(state: PlanExecute):
plan = state["plan"]
task = plan[0]
task_formatted = plan
print("TASK FORMATTED",task_formatted)
task_formatted = task_formatted + "generate only code dont do any extra things and generate code between ``` code ``` and dont generate extra things and dont use ``` except for code"
global agent_response
agent_response = await code_agent_executor.ainvoke({"messages": [("user", task_formatted)]})
print("agent response : ",agent_response)
text = agent_response["messages"][-1].content
in_bet = re.findall(r"```python(.*?)```", text, re.DOTALL)[0]
code_block = parser.parse(in_bet)
return {
"past_steps": [(task, code_block)],"done":False,
}
async def install_dependencies(state:PlanExecute):
text = agent_response["messages"][-1].content
in_bet = re.findall(r"```python(.*?)```", text, re.DOTALL)[0]
code_block = parser.parse(in_bet)
p = inst_dep_chain.invoke({"messages": [("user",code_block)]})
# print(p.modules)
res = subprocess.run(['pip','list'],capture_output=True,text = True)
# print("RES ",res)
res = res.stdout
# if type(res) !=
if not isinstance(p, list):
return{"done":False}
for module in p.modules:
s1 = module
s2 = module.replace('-','_')
s3 = module.replace('_','-')
if (s1 not in res) and (s2 not in res) and (s3 not in res):
res = subprocess.run(['pip','install',module],capture_output=True,text = True)
print(f"succesfully installed {module}")
else:
print(f"{module} already installed")
return {"done":False}
async def test_code(state:PlanExecute):
code_block = state["past_steps"][-1][1]
try :
with open('app.py', 'w') as f:
f.write(code_block)
f.close()
if 'streamlit' in code_block:
res = subprocess.run(['streamlit','run','app.py'],capture_output=True,text = True)
else:
res = subprocess.run(['python' ,'app.py'],capture_output=True,text = True)
if len(res.stderr) > 0:
raise Exception(res.stderr)
# st.session_state.messages.append({"role": "assistant", "content": code_block})
# if st.button("END"):
# os._exit(0)
return {"test_status": 1}
except Exception as e:
print("Exception Occured")
return {"test_status": str(e)}
async def plan_step(state: PlanExecute):
p = await planner.ainvoke({"messages": [("user", state["input"])]})
return {"plan": p.steps}
async def replan_step(state: PlanExecute):
if state["test_status"] == 1:
return {"done":True}
output = await replanner.ainvoke(state)
return {"plan": output.steps,"done":False,"test_status":0}
async def doc_gen(state: PlanExecute):
text = agent_response["messages"][-1].content
in_bet = re.findall(r"```python(.*?)```", text, re.DOTALL)[0]
code_block = parser.parse(in_bet)
doc_gen_response = await document_generator.ainvoke({"messages": [("user",code_block)]})
documentation = doc_gen_response["messages"][-1].content
# print("Documentation : ",doc_gen_response["messages"][-1].content)
with st.chat_message("user"):
st.markdown(st.session_state.messages[-1]["content"])
st.session_state.messages.append({"role": "assistant", "content": [code_block,documentation]})
with st.chat_message("assistant"):
st.code(code_block, language='python')
# st.markdown(code_block)
st.download_button(
label="Download Code",
data=code_block, # Data to be downloaded
file_name="download_app.py", # Name of the downloaded file
mime="python", # File type
key = "Down"
)
st.download_button(
label="Download Readme",
data=documentation, # Data to be downloaded
file_name="ReadMe", # Name of the downloaded file
mime="text/plain", # File type
key = "Doc"
)
return {"done":True}
def should_end(state: PlanExecute) -> Literal["agent", "document_gen"]:
if state['done']:
return "document_gen"
return "agent"
workflow = StateGraph(PlanExecute)
workflow.add_node("planner", plan_step)
workflow.add_node("agent", execute_step)
workflow.add_node("install",install_dependencies)
workflow.add_node("test_", test_code)
workflow.add_node("document_gen",doc_gen)
workflow.add_node("replan", replan_step)
workflow.add_edge(START, "planner")
workflow.add_edge("planner", "agent")
workflow.add_edge("agent","install")
workflow.add_edge("install", "test_")
workflow.add_edge("test_", "replan")
workflow.add_edge("document_gen",END)
workflow.add_conditional_edges("replan",should_end)
app = workflow.compile()
config = {"recursion_limit": 50}
# inputs = {"input": "Build calculator app in python with gui with all functionalites and shows error when divided by zero"}
# expression = st.text_input("Enter The Input Format:")
# st.button("SUBMIT",key = "button")
# x = input("ENTER THE INPUT PROMPT : ")
async def process_events(inputs):
async for event in app.astream(inputs, config=config):
for k, v in event.items():
if k != "__end__":
print(v)
if prompt := st.chat_input("Enter The Input Prompt : ?"):
# Add user message to chat history
st.session_state.messages.append({"role": "user", "content": prompt})
# Display user message in chat message container
x = prompt
inputs = {"input": x}
asyncio.run(process_events(inputs))
# with st.chat_message("user"):
# st.markdown(prompt)
# print(event)
# if st.session_state.button == True:
# asyncio.run(process_events())