Skip to content

Devika is the first open-source implementation of an Agentic Software Engineer. Initially started as an open-source alternative to Devin.

Python

fix: infinite retry to llm

[email protected]6d8975bf4556
diff --git a/src/agents/action/action.py b/src/agents/action/action.py
index 5544d60..ec1bd60 100644
--- a/src/agents/action/action.py
+++ b/src/agents/action/action.py
@@ -2,6 +2,7 @@ import json
from jinja2 import Environment, BaseLoader
+from src.services.utils import retry_wrapper
from src.config import Config
from src.llm import LLM
@@ -39,17 +40,11 @@ class Action:
else:
return response["response"], response["action"]
+ @retry_wrapper
def execute(self, conversation: list, project_name: str) -> str:
prompt = self.render(conversation)
response = self.llm.inference(prompt, project_name)
valid_response = self.validate_response(response)
- while not valid_response:
- print("Invalid response from the model, trying again...")
- return self.execute(conversation, project_name)
-
- print("===" * 10)
- print(valid_response)
-
return valid_response
diff --git a/src/agents/answer/answer.py b/src/agents/answer/answer.py
index 94e2575..6e9186a 100644
--- a/src/agents/answer/answer.py
+++ b/src/agents/answer/answer.py
@@ -2,6 +2,7 @@ import json
from jinja2 import Environment, BaseLoader
+from src.services.utils import retry_wrapper
from src.config import Config
from src.llm import LLM
@@ -40,14 +41,11 @@ class Answer:
else:
return response["response"]
+ @retry_wrapper
def execute(self, conversation: list, code_markdown: str, project_name: str) -> str:
prompt = self.render(conversation, code_markdown)
response = self.llm.inference(prompt, project_name)
valid_response = self.validate_response(response)
- while not valid_response:
- print("Invalid response from the model, trying again...")
- return self.execute(conversation, code_markdown, project_name)
-
return valid_response
diff --git a/src/agents/coder/coder.py b/src/agents/coder/coder.py
index 324e261..b6c6ee0 100644
--- a/src/agents/coder/coder.py
+++ b/src/agents/coder/coder.py
@@ -8,6 +8,7 @@ from src.config import Config
from src.llm import LLM
from src.state import AgentState
from src.logger import Logger
+from src.services.utils import retry_wrapper
PROMPT = open("src/agents/coder/prompt.jinja2", "r").read().strip()
@@ -100,6 +101,7 @@ class Coder:
AgentState().add_to_current_state(project_name, new_state)
time.sleep(2)
+ @retry_wrapper
def execute(
self,
step_by_step_plan: str,
@@ -112,9 +114,8 @@ class Coder:
valid_response = self.validate_response(response)
- while not valid_response:
- print("Invalid response from the model, trying again...")
- return self.execute(step_by_step_plan, user_context, search_results, project_name)
+ if not valid_response:
+ return False
print(valid_response)
diff --git a/src/agents/decision/decision.py b/src/agents/decision/decision.py
index 9e98907..923c93d 100644
--- a/src/agents/decision/decision.py
+++ b/src/agents/decision/decision.py
@@ -2,6 +2,7 @@ import json
from jinja2 import Environment, BaseLoader
+from src.services.utils import retry_wrapper
from src.llm import LLM
PROMPT = open("src/agents/decision/prompt.jinja2").read().strip()
@@ -32,14 +33,11 @@ class Decision:
return response
+ @retry_wrapper
def execute(self, prompt: str, project_name: str) -> str:
rendered_prompt = self.render(prompt)
response = self.llm.inference(rendered_prompt, project_name)
valid_response = self.validate_response(response)
-
- while not valid_response:
- print("Invalid response from the model, trying again...")
- return self.execute(prompt, project_name)
return valid_response
\ No newline at end of file
diff --git a/src/agents/feature/feature.py b/src/agents/feature/feature.py
index eecef06..c410b80 100644
--- a/src/agents/feature/feature.py
+++ b/src/agents/feature/feature.py
@@ -7,6 +7,7 @@ from typing import List, Dict, Union
from src.config import Config
from src.llm import LLM
from src.state import AgentState
+from src.services.utils import retry_wrapper
PROMPT = open("src/agents/feature/prompt.jinja2", "r").read().strip()
@@ -96,6 +97,7 @@ class Feature:
AgentState().add_to_current_state(project_name, new_state)
time.sleep(1)
+ @retry_wrapper
def execute(
self,
conversation: list,
@@ -108,9 +110,8 @@ class Feature:
valid_response = self.validate_response(response)
- while not valid_response:
- print("Invalid response from the model, trying again...")
- return self.execute(conversation, code_markdown, system_os, project_name)
+ if not valid_response:
+ return False
self.emulate_code_writing(valid_response, project_name)
diff --git a/src/agents/internal_monologue/internal_monologue.py b/src/agents/internal_monologue/internal_monologue.py
index dd6152c..e1d9503 100644
--- a/src/agents/internal_monologue/internal_monologue.py
+++ b/src/agents/internal_monologue/internal_monologue.py
@@ -3,6 +3,7 @@ import json
from jinja2 import Environment, BaseLoader
from src.llm import LLM
+from src.services.utils import retry_wrapper
PROMPT = open("src/agents/internal_monologue/prompt.jinja2").read().strip()
@@ -33,15 +34,10 @@ class InternalMonologue:
else:
return response["internal_monologue"]
+ @retry_wrapper
def execute(self, current_prompt: str, project_name: str) -> str:
rendered_prompt = self.render(current_prompt)
response = self.llm.inference(rendered_prompt, project_name)
-
valid_response = self.validate_response(response)
-
- while not valid_response:
- print("Invalid response from the model, trying again...")
- return self.execute(current_prompt, project_name)
-
return valid_response
diff --git a/src/agents/patcher/patcher.py b/src/agents/patcher/patcher.py
index 0c019e0..c655393 100644
--- a/src/agents/patcher/patcher.py
+++ b/src/agents/patcher/patcher.py
@@ -7,6 +7,7 @@ from typing import List, Dict, Union
from src.config import Config
from src.llm import LLM
from src.state import AgentState
+from src.services.utils import retry_wrapper
PROMPT = open("src/agents/patcher/prompt.jinja2", "r").read().strip()
@@ -98,6 +99,7 @@ class Patcher:
AgentState().add_to_current_state(project_name, new_state)
time.sleep(1)
+ @retry_wrapper
def execute(
self,
conversation: str,
@@ -118,16 +120,8 @@ class Patcher:
valid_response = self.validate_response(response)
- while not valid_response:
- print("Invalid response from the model, trying again...")
- return self.execute(
- conversation,
- code_markdown,
- commands,
- error,
- system_os,
- project_name
- )
+ if not valid_response:
+ return False
self.emulate_code_writing(valid_response, project_name)
diff --git a/src/agents/reporter/reporter.py b/src/agents/reporter/reporter.py
index 8cfc98d..19adfc5 100644
--- a/src/agents/reporter/reporter.py
+++ b/src/agents/reporter/reporter.py
@@ -2,6 +2,7 @@ import json
from jinja2 import Environment, BaseLoader
+from src.services.utils import retry_wrapper
from src.llm import LLM
PROMPT = open("src/agents/reporter/prompt.jinja2").read().strip()
@@ -26,6 +27,7 @@ class Reporter:
return response
+ @retry_wrapper
def execute(self,
conversation: list,
code_markdown: str,
@@ -36,9 +38,5 @@ class Reporter:
valid_response = self.validate_response(response)
- while not valid_response:
- print("Invalid response from the model, trying again...")
- return self.execute(conversation, code_markdown, project_name)
-
return valid_response
diff --git a/src/agents/researcher/researcher.py b/src/agents/researcher/researcher.py
index d4f7a10..529b01e 100644
--- a/src/agents/researcher/researcher.py
+++ b/src/agents/researcher/researcher.py
@@ -4,6 +4,7 @@ from typing import List
from jinja2 import Environment, BaseLoader
from src.llm import LLM
+from src.services.utils import retry_wrapper
from src.browser.search import BingSearch
PROMPT = open("src/agents/researcher/prompt.jinja2").read().strip()
@@ -41,7 +42,8 @@ class Researcher:
"queries": response["queries"],
"ask_user": response["ask_user"]
}
-
+
+ @retry_wrapper
def execute(self, step_by_step_plan: str, contextual_keywords: List[str], project_name: str) -> dict | bool:
contextual_keywords_str = ", ".join(map(lambda k: k.capitalize(), contextual_keywords))
prompt = self.render(step_by_step_plan, contextual_keywords_str)
@@ -50,8 +52,4 @@ class Researcher:
valid_response = self.validate_response(response)
- while not valid_response:
- print("Invalid response from the model, trying again...")
- return self.execute(step_by_step_plan, contextual_keywords, project_name)
-
return valid_response
diff --git a/src/agents/runner/runner.py b/src/agents/runner/runner.py
index 0ec84d4..291ea60 100644
--- a/src/agents/runner/runner.py
+++ b/src/agents/runner/runner.py
@@ -10,6 +10,7 @@ from src.agents.patcher import Patcher
from src.llm import LLM
from src.state import AgentState
from src.project import ProjectManager
+from src.services.utils import retry_wrapper
PROMPT = open("src/agents/runner/prompt.jinja2", "r").read().strip()
RERUNNER_PROMPT = open("src/agents/runner/rerunner.jinja2", "r").read().strip()
@@ -87,6 +88,7 @@ class Runner:
else:
return response
+ @retry_wrapper
def run_code(
self,
commands: list,
@@ -140,16 +142,8 @@ class Runner:
valid_response = self.validate_rerunner_response(response)
- while not valid_response:
- print("Invalid response from the model, trying again...")
- return self.run_code(
- commands,
- project_path,
- project_name,
- conversation,
- code_markdown,
- system_os
- )
+ if not valid_response:
+ return False
action = valid_response["action"]
@@ -224,6 +218,7 @@ class Runner:
else:
break
+ @retry_wrapper
def execute(
self,
conversation: list,
@@ -237,13 +232,6 @@ class Runner:
valid_response = self.validate_response(response)
- while not valid_response:
- print("Invalid response from the model, trying again...")
- return self.execute(conversation, code_markdown, os_system, project_path, project_name)
-
- print("=====" * 10)
- print(valid_response)
-
self.run_code(
valid_response,
project_path,
diff --git a/src/services/utils.py b/src/services/utils.py
new file mode 100644
index 0000000..6a85c82
--- /dev/null
+++ b/src/services/utils.py
@@ -0,0 +1,23 @@
+# create wrapper function that will has retry logic of 5 times
+import sys
+import time
+from src.socket_instance import emit_agent
+
+def retry_wrapper(func):
+ def wrapper(*args, **kwargs):
+ max_tries = 5
+ tries = 0
+ while tries < max_tries:
+ result = func(*args, **kwargs)
+ if result:
+ return result
+ print("Invalid response from the model, I'm trying again...")
+ emit_agent("info", {"type": "warning", "message": "Invalid response from the model, trying again..."})
+ tries += 1
+ time.sleep(2)
+ print("Maximum 5 attempts reached. try other models")
+ emit_agent("info", {"type": "error", "message": "Maximum attempts reached. model keeps failing."})
+ sys.exit(1)
+
+ return False
+ return wrapper