Skip to content

Instantly share code, notes, and snippets.

@thomasahle
Created May 20, 2025 14:53
Show Gist options
  • Save thomasahle/a5ed7c104d4b944cc48fa6447157d51e to your computer and use it in GitHub Desktop.
Save thomasahle/a5ed7c104d4b944cc48fa6447157d51e to your computer and use it in GitHub Desktop.
async def run_agent(
task: VerilogTask,
client: LLMClient,
tools: list[Tool],
system_prompt: str = DEFAULT_SYSTEM_PROMPT,
*,
max_turns: int = 20,
console: Optional[Union[Console, LogConsole]] = None,
) -> Tuple[Optional[VerificationResult], Dict[str, Any]]:
msgs = make_initial_messages(system_prompt, task, tools)
for msg in msgs:
console.print(f"[pink3]{msg['role']}:[/pink3] {msg['content']}")
await client.initialize()
result = None
for turn in range(1, max_turns + 1):
console.rule(f"TURN {turn}")
# Generate response from the model
start = time.time()
with console.status("Thinking..."):
response: ChatCompletion = await client.generate_response(msgs, tools)
console.print(f"[blue]Thought for {time.time() - start:.2f}s[/blue]")
assistant_message = response.choices[0].message
msgs.append({"role": "assistant", "content": assistant_message.content})
if assistant_message.content:
console.print(f"[blue]agent:[/blue] {assistant_message.content}")
tool_calls = client.get_tool_calls(assistant_message)
for tool_call in tool_calls:
if tool_call["name"] == "submit":
args = json.loads(tool_call["arguments"])
args["task_key"] = task.task_key # make sure task_key is correct
tool_call["arguments"] = json.dumps(args)
tool_result_msg = await process_tool_call(tool_call, tool_index)
if tool_call["name"] == "submit":
result = VerificationResult.model_validate_json(tool_result_msg["content"][0].text)
break
msgs.append(tool_result_msg)
# If we got a result from the tool, break out of the loop
if result is not None:
break
else:
console.print("[red]Max turns hit without success[/red]")
result = VerificationResult(
task_key=task.name,
passed=False,
error="Max turns exceeded",
)
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment