Feat : better print of think process and answer
This commit is contained in:
+22
-10
@@ -1,12 +1,12 @@
|
||||
|
||||
from sources.tools import PyInterpreter, BashInterpreter
|
||||
from sources.utility import pretty_print
|
||||
from sources.agent import Agent
|
||||
from sources.agent import Agent, executorResult
|
||||
|
||||
class CoderAgent(Agent):
|
||||
def __init__(self, model, name, prompt_path, provider):
|
||||
super().__init__(model, name, prompt_path, provider)
|
||||
self._tools = {
|
||||
self.tools = {
|
||||
"bash": BashInterpreter(),
|
||||
"python": PyInterpreter()
|
||||
}
|
||||
@@ -19,6 +19,7 @@ class CoderAgent(Agent):
|
||||
lines = text.split('\n')
|
||||
post_lines = []
|
||||
in_block = False
|
||||
block_idx = 0
|
||||
for line in lines:
|
||||
if tag in line and not in_block:
|
||||
in_block = True
|
||||
@@ -27,26 +28,37 @@ class CoderAgent(Agent):
|
||||
post_lines.append(line)
|
||||
if tag in line:
|
||||
in_block = False
|
||||
post_lines.append(f"block:{block_idx}")
|
||||
block_idx += 1
|
||||
return "\n".join(post_lines)
|
||||
|
||||
def answer(self, prompt, speech_module) -> str:
|
||||
|
||||
def show_answer(self):
|
||||
lines = self.last_answer.split("\n")
|
||||
for line in lines:
|
||||
if "block:" in line:
|
||||
block_idx = int(line.split(":")[1])
|
||||
if block_idx < len(self.blocks_result):
|
||||
self.blocks_result[block_idx].show()
|
||||
else:
|
||||
pretty_print(line, color="output")
|
||||
|
||||
def process(self, prompt, speech_module) -> str:
|
||||
answer = ""
|
||||
attempt = 0
|
||||
max_attempts = 3
|
||||
self._memory.push('user', prompt)
|
||||
self.memory.push('user', prompt)
|
||||
|
||||
while attempt < max_attempts:
|
||||
pretty_print("Thinking...", color="status")
|
||||
self.wait_message(speech_module)
|
||||
answer, reasoning = self.llm_request()
|
||||
exec_success, feedback = self.execute_modules(answer)
|
||||
pretty_print(feedback, color="failure" if "failure" in feedback.lower() else "success")
|
||||
exec_success, _ = self.execute_modules(answer)
|
||||
answer = self.remove_blocks(answer)
|
||||
pretty_print(answer, color="output")
|
||||
self.last_answer = answer
|
||||
if exec_success:
|
||||
break
|
||||
self.show_answer()
|
||||
attempt += 1
|
||||
|
||||
return answer, reasoning
|
||||
|
||||
if __name__ == "__main__":
|
||||
@@ -55,5 +67,5 @@ if __name__ == "__main__":
|
||||
#local_provider = Provider("ollama", "deepseek-r1:14b", None)
|
||||
server_provider = Provider("server", "deepseek-r1:14b", "192.168.1.100:5000")
|
||||
agent = CoderAgent("deepseek-r1:14b", "jarvis", "prompts/coder_agent.txt", server_provider)
|
||||
ans = agent.answer("What is the output of 5+5 in python ?")
|
||||
ans = agent.process("What is the output of 5+5 in python ?")
|
||||
print(ans)
|
||||
Reference in New Issue
Block a user