|
40 | 40 | " print(\"RESULT CODE EXECUTION:\", result)\n", |
41 | 41 | " except BaseException as e:\n", |
42 | 42 | " return f\"Failed to execute. Error: {repr(e)}\"\n", |
43 | | - " return f\"Successfully executed:\\n```python\\n{code}\\n```\\nStdout: {result}\"\n", |
| 43 | + " return f\"Executed:\\n```python\\n{code}\\n```\\nStdout: {result}\"\n", |
44 | 44 | "\n" |
45 | 45 | ] |
46 | 46 | }, |
|
128 | 128 | " HumanMessage(content=message)]\n", |
129 | 129 | " result = model.invoke(messages)\n", |
130 | 130 | " state[\"file_path\"] = result.content\n", |
131 | | - " print(f\"STATE bei Identifier filepath: {state}\")\n", |
132 | 131 | " return state\n", |
133 | 132 | "\n", |
134 | 133 | "\n", |
135 | 134 | "def execute_code_with_model(state: AgentState):\n", |
136 | 135 | "\n", |
137 | 136 | " code = read_code_from_file(state[\"file_path\"])\n", |
138 | | - " print(\"CODE:\", code)\n", |
139 | 137 | "\n", |
140 | 138 | " model = ChatOpenAI()\n", |
141 | 139 | " model_with_tools = model.bind_tools([python_repl])\n", |
142 | 140 | "\n", |
143 | 141 | " messages = [\n", |
144 | | - " SystemMessage(content=\"\"\" You have got the task to execute code. Use the python_repl tool to execute it.\n", |
145 | | - " If the code produced an error just return 'True'. If it worked, return 'False'\"\"\"),\n", |
| 142 | + " SystemMessage(content=\"\"\" You have got the task to execute code. Use the python_repl tool to execute it. I will a message and your task is to detect if it was successfully run or produced an error.\n", |
| 143 | + " If the code produced an error just return 'True'. If it was sucessfully executed, return 'False'\"\"\"),\n", |
146 | 144 | " HumanMessage(content=code)\n", |
147 | 145 | " ]\n", |
148 | 146 | "\n", |
|
152 | 150 | " for tool_call in ai_msg.tool_calls:\n", |
153 | 151 | " selected_tool = {\"python_repl\": python_repl}[tool_call[\"name\"].lower()]\n", |
154 | 152 | " tool_output = selected_tool.invoke(tool_call[\"args\"])\n", |
155 | | - " state[\"code\"] = tool_output\n", |
| 153 | + " state[\"error_message\"] = tool_output\n", |
156 | 154 | " messages.append(ToolMessage(tool_output, tool_call_id=tool_call[\"id\"]))\n", |
157 | 155 | "\n", |
158 | 156 | " result = model_with_tools.invoke(messages)\n", |
159 | | - " print(\"EXECUTE_RESULT\", result.content)\n", |
| 157 | + " print(\"EVALUATION RESULT:\", result)\n", |
160 | 158 | " state[\"error\"] = result.content\n", |
161 | | - " print(f\"STATE bei read_code_from_file: {state}\")\n", |
162 | 159 | " return state\n", |
163 | 160 | "\n", |
164 | 161 | "\n", |
165 | 162 | "def rewrite_code(state: AgentState):\n", |
166 | 163 | "\n", |
167 | 164 | " code = state[\"code\"]\n", |
| 165 | + " error = state[\"error_message\"]\n", |
168 | 166 | " state[\"iterations\"] += 1\n", |
169 | 167 | " model = ChatOpenAI()\n", |
170 | 168 | " messages = [\n", |
171 | | - " SystemMessage(content=\"You can to analyze the following code provided in the usermessage. Your task is to fix that code and provide the user the correct new code.\"),\n", |
172 | | - " HumanMessage(content=code)\n", |
| 169 | + " SystemMessage(content=\"You can to analyze the following code and error provided in the usermessage. Your task is to fix that code and provide the user the correct new code. VERY IMPORTANT: ONLY RETURN THE UPDATED CODE, NOTHING ELSE! Dont use a markdown style, just the code as Text\"),\n", |
| 170 | + " HumanMessage(content=f\"Code: {code} | Error: {error}\")\n", |
173 | 171 | " ]\n", |
174 | 172 | " ai_msg = model.invoke(messages)\n", |
| 173 | + " print(\"NEW SUGGESTED CODE:\", ai_msg.content)\n", |
175 | 174 | " write_code_to_file(file_path=f'{state[\"file_path\"]}', code=ai_msg.content)\n", |
176 | 175 | " state[\"code\"] = ai_msg.content\n", |
177 | | - " print(f\"STATE bei rewrite_code: {state}\")\n", |
178 | 176 | " return state\n", |
179 | 177 | "\n", |
180 | 178 | "\n", |
181 | 179 | "def next_step(state: AgentState):\n", |
182 | | - " if bool(state[\"error\"]) == True:\n", |
183 | | - " return \"error\"\n", |
184 | | - " elif state[\"iterations\"] > 5:\n", |
| 180 | + " if state[\"iterations\"] > 3:\n", |
185 | 181 | " print(\"Max Iterations done.... Exit Agent\")\n", |
186 | 182 | " return \"max_iterations\"\n", |
187 | | - " else:\n", |
| 183 | + " if state[\"error\"] == \"True\":\n", |
| 184 | + " print(f\"Error in {state['file_path']}. {state['iterations']} tries done\")\n", |
| 185 | + " return \"error\"\n", |
| 186 | + " if state[\"error\"] == \"False\":\n", |
| 187 | + " print(f\"Code was probably fixed... check out {state['file_path']} if it is correct\")\n", |
188 | 188 | " return \"ok\"" |
189 | 189 | ] |
190 | 190 | }, |
|
235 | 235 | "metadata": {}, |
236 | 236 | "outputs": [], |
237 | 237 | "source": [ |
238 | | - "app.invoke({\"message\": \"Please analyze the testscript.py file\", \"error\": \"\", \"error_message\": \"\", \"file_path\": \"\", \"code\": \"\", \"iterations\": 0})" |
| 238 | + "app.invoke({\"message\": \"Please analyze the testscript.py file\", \"error\": \"\", \"error_message\": \"\", \"file_path\": \"\", \"code\": \"\", \"iterations\": 1})" |
239 | 239 | ] |
240 | 240 | } |
241 | 241 | ], |
|
0 commit comments