Skip to content

Commit 7cdbb8b

Browse files
Add tools helper function
1 parent 33caf32 commit 7cdbb8b

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

src/axiomatic/client.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import base64
22
import requests
33
import os
4+
import time
45
from typing import Dict
56

67
from .base_client import BaseClient, AsyncBaseClient
@@ -15,6 +16,7 @@ def __init__(self, *args, **kwargs):
1516
super().__init__(*args, **kwargs)
1617

1718
self.document_helper = DocumentHelper(self)
19+
self.tools_helper = ToolsHelper(self)
1820

1921

2022
class DocumentHelper:
@@ -90,4 +92,46 @@ def load_parsed_pdf(self, path: str) -> ParseResponse:
9092
return ParseResponse(markdown=markdown, images=images)
9193

9294

95+
class ToolsHelper:
96+
_ax_client: Axiomatic
97+
98+
def __init__(self, ax_client: Axiomatic):
99+
self._ax_client = ax_client
100+
101+
def tool_exec(self, tool: str, code: str, poll_interval: int = 3, debug: bool = False) -> str:
102+
"""
103+
Helper function to schedule code execution for a specific tool and wait
104+
the execution to finish and return the output or error trace
105+
"""
106+
if not tool.strip():
107+
return "Please specify a tool"
108+
else:
109+
tool_name = tool.strip()
110+
code_string = code
111+
112+
output = self._ax_client.tools.schedule(
113+
tool_name=tool_name,
114+
code=code_string,
115+
)
116+
if output.is_success is True:
117+
result = self._ax_client.tools.status(job_id=output.job_id)
118+
if debug:
119+
print(f"job_id: {output.job_id}")
120+
while True:
121+
result = self._ax_client.tools.status(job_id=output.job_id)
122+
if result.status == "PENDING" or result.status == "RUNNING":
123+
if debug:
124+
print(f"status: {result.status}")
125+
time.sleep(poll_interval)
126+
else:
127+
if debug:
128+
print(f"status: {result.status}")
129+
if result.status == "SUCCEEDED":
130+
return result.output
131+
else:
132+
return result.error_trace
133+
else:
134+
return output.error_trace
135+
136+
93137
class AsyncAxiomatic(AsyncBaseClient): ...

0 commit comments

Comments
 (0)