Skip to content

Commit df7c320

Browse files
add crag
1 parent 4505719 commit df7c320

3 files changed

Lines changed: 97 additions & 18 deletions

File tree

code.ipynb

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
" print(\"RESULT CODE EXECUTION:\", result)\n",
4141
" except BaseException as e:\n",
4242
" 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",
4444
"\n"
4545
]
4646
},
@@ -128,21 +128,19 @@
128128
" HumanMessage(content=message)]\n",
129129
" result = model.invoke(messages)\n",
130130
" state[\"file_path\"] = result.content\n",
131-
" print(f\"STATE bei Identifier filepath: {state}\")\n",
132131
" return state\n",
133132
"\n",
134133
"\n",
135134
"def execute_code_with_model(state: AgentState):\n",
136135
"\n",
137136
" code = read_code_from_file(state[\"file_path\"])\n",
138-
" print(\"CODE:\", code)\n",
139137
"\n",
140138
" model = ChatOpenAI()\n",
141139
" model_with_tools = model.bind_tools([python_repl])\n",
142140
"\n",
143141
" 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",
146144
" HumanMessage(content=code)\n",
147145
" ]\n",
148146
"\n",
@@ -152,39 +150,41 @@
152150
" for tool_call in ai_msg.tool_calls:\n",
153151
" selected_tool = {\"python_repl\": python_repl}[tool_call[\"name\"].lower()]\n",
154152
" tool_output = selected_tool.invoke(tool_call[\"args\"])\n",
155-
" state[\"code\"] = tool_output\n",
153+
" state[\"error_message\"] = tool_output\n",
156154
" messages.append(ToolMessage(tool_output, tool_call_id=tool_call[\"id\"]))\n",
157155
"\n",
158156
" result = model_with_tools.invoke(messages)\n",
159-
" print(\"EXECUTE_RESULT\", result.content)\n",
157+
" print(\"EVALUATION RESULT:\", result)\n",
160158
" state[\"error\"] = result.content\n",
161-
" print(f\"STATE bei read_code_from_file: {state}\")\n",
162159
" return state\n",
163160
"\n",
164161
"\n",
165162
"def rewrite_code(state: AgentState):\n",
166163
"\n",
167164
" code = state[\"code\"]\n",
165+
" error = state[\"error_message\"]\n",
168166
" state[\"iterations\"] += 1\n",
169167
" model = ChatOpenAI()\n",
170168
" 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",
173171
" ]\n",
174172
" ai_msg = model.invoke(messages)\n",
173+
" print(\"NEW SUGGESTED CODE:\", ai_msg.content)\n",
175174
" write_code_to_file(file_path=f'{state[\"file_path\"]}', code=ai_msg.content)\n",
176175
" state[\"code\"] = ai_msg.content\n",
177-
" print(f\"STATE bei rewrite_code: {state}\")\n",
178176
" return state\n",
179177
"\n",
180178
"\n",
181179
"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",
185181
" print(\"Max Iterations done.... Exit Agent\")\n",
186182
" 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",
188188
" return \"ok\""
189189
]
190190
},
@@ -235,7 +235,7 @@
235235
"metadata": {},
236236
"outputs": [],
237237
"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})"
239239
]
240240
}
241241
],

crag.ipynb

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 2,
6+
"metadata": {},
7+
"outputs": [
8+
{
9+
"data": {
10+
"text/plain": [
11+
"True"
12+
]
13+
},
14+
"execution_count": 2,
15+
"metadata": {},
16+
"output_type": "execute_result"
17+
}
18+
],
19+
"source": [
20+
"from dotenv import load_dotenv\n",
21+
"\n",
22+
"load_dotenv()"
23+
]
24+
},
25+
{
26+
"cell_type": "markdown",
27+
"metadata": {},
28+
"source": [
29+
"#### Creating VectorDatabase"
30+
]
31+
},
32+
{
33+
"cell_type": "code",
34+
"execution_count": 3,
35+
"metadata": {},
36+
"outputs": [],
37+
"source": [
38+
"from langchain.schema import Document\n",
39+
"from langchain_openai import OpenAIEmbeddings\n",
40+
"from langchain_community.vectorstores import Chroma\n",
41+
"\n",
42+
"embedding_function = OpenAIEmbeddings()\n",
43+
"\n",
44+
"docs = [\n",
45+
" Document(\n",
46+
" page_content=\"the dog loves to eat pizza\", metadata={\"source\": \"animal.txt\"}\n",
47+
" ),\n",
48+
" Document(\n",
49+
" page_content=\"the cat loves to eat lasagna\", metadata={\"source\": \"animal.txt\"}\n",
50+
" ),\n",
51+
"]\n",
52+
"\n",
53+
"\n",
54+
"db = Chroma.from_documents(docs, embedding_function)\n",
55+
"retriever = db.as_retriever()"
56+
]
57+
}
58+
],
59+
"metadata": {
60+
"kernelspec": {
61+
"display_name": "app",
62+
"language": "python",
63+
"name": "python3"
64+
},
65+
"language_info": {
66+
"codemirror_mode": {
67+
"name": "ipython",
68+
"version": 3
69+
},
70+
"file_extension": ".py",
71+
"mimetype": "text/x-python",
72+
"name": "python",
73+
"nbconvert_exporter": "python",
74+
"pygments_lexer": "ipython3",
75+
"version": "3.11.0"
76+
}
77+
},
78+
"nbformat": 4,
79+
"nbformat_minor": 2
80+
}

testscript.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
x = 2
22
y = "test"
3-
4-
print(x + y)
3+
print(str(x) + y)

0 commit comments

Comments
 (0)