Skip to content

Commit 0ebcbb7

Browse files
Merge pull request #21 from Axiomatic-AI/tools-helper
Add tools helper function
2 parents 33caf32 + 8ccda0d commit 0ebcbb7

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

src/axiomatic/client.py

Lines changed: 45 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,47 @@ 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):
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+
job_id = str(output.job_id)
118+
result = self._ax_client.tools.status(job_id=job_id)
119+
if debug:
120+
print(f"job_id: {job_id}")
121+
while True:
122+
result = self._ax_client.tools.status(job_id=job_id)
123+
if result.status == "PENDING" or result.status == "RUNNING":
124+
if debug:
125+
print(f"status: {result.status}")
126+
time.sleep(poll_interval)
127+
else:
128+
if debug:
129+
print(f"status: {result.status}")
130+
if result.status == "SUCCEEDED":
131+
return result.output
132+
else:
133+
return result.error_trace
134+
else:
135+
return output.error_trace
136+
137+
93138
class AsyncAxiomatic(AsyncBaseClient): ...

0 commit comments

Comments
 (0)