Skip to content

Commit 4e6eff6

Browse files
author
zeryx
committed
improved local debugging system, fleshed our docs a bit more
1 parent b60fa53 commit 4e6eff6

File tree

5 files changed

+42
-22
lines changed

5 files changed

+42
-22
lines changed

README.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# Algorithm Development Kit (ADK), Python edition
22

33
```python
4-
import from adk import ADK
4+
import Algorithmia
5+
from adk import ADK
56

67

78
# API calls will begin at the apply() method, with the request body passed as 'input'
@@ -12,7 +13,7 @@ def apply(input):
1213

1314

1415
algo = ADK(apply)
15-
algo.serve()
16+
algo.serve("Algorithmia")
1617
```
1718

1819
This document will describe the following:
@@ -38,16 +39,17 @@ Algorithm development does change with this introduction:
3839
- Primary development file has been renamed to `src/Algorithm.py` to aide in understanding around what this file actually does / why it's important
3940
- An additional import (`from adk import ADK`)
4041
- An optional `load()` function that can be implemented
41-
- this enables a dedicated function for preparing your algorithm for runtime operations, such as model loading, configuration, etc
42+
- This enables a dedicated function for preparing your algorithm for runtime operations, such as model loading, configuration, etc
4243
- A call to the handler function with your `apply` and optional` load` functions as inputs
4344
- ```python
4445
algo = ADK(apply)
45-
algo.serve()
46+
algo.serve("Algorithmia")
4647
```
47-
- converts the project into an executable, rather than a library
48-
- which will interact with the `langserver` service on Algorithmia
49-
- but is debuggable via stdin/stdout when executed locally / outside of an Algorithm container
50-
- this includes being able to step through your algorithm code in your IDE of choice! Just execute your `src/Algorithm.py` script
48+
- Converts the project into an executable, rather than a library
49+
- Which will interact with the `langserver` service on Algorithmia
50+
- But is debuggable via stdin/stdout when executed locally / outside of an Algorithm container
51+
- When a payload is provided to `serve()`, that payload will be directly provided to your algorithm when executed locally, bypassing stdin parsing and simplifying debugging!
52+
- This includes being able to step through your algorithm code in your IDE of choice! Just execute your `src/Algorithm.py` script and try stepping through your code with your favorite IDE
5153

5254
## Example workflows
5355
Check out these examples to help you get started:

adk/ADK.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,8 @@ def create_exception(self, exception):
109109
}
110110
return response
111111

112-
def serve(self):
113-
try:
114-
if self.load_func:
115-
self.load()
116-
except Exception as e:
117-
load_error = self.create_exception(e)
118-
self.write_to_pipe(load_error)
112+
def process_loop(self):
119113
response_obj = ""
120-
if self.is_local:
121-
print("waiting for input...")
122114
for line in sys.stdin:
123115
try:
124116
request = json.loads(line)
@@ -132,3 +124,20 @@ def serve(self):
132124
response_obj = self.create_exception(e)
133125
finally:
134126
self.write_to_pipe(response_obj)
127+
128+
def serve(self, local_payload=None):
129+
try:
130+
if self.load_func:
131+
self.load()
132+
except Exception as e:
133+
load_error = self.create_exception(e)
134+
self.write_to_pipe(load_error)
135+
if self.is_local and local_payload:
136+
if self.load_result:
137+
apply_result = self.apply_func(local_payload, self.load_result)
138+
else:
139+
apply_result = self.apply_func(local_payload)
140+
print(self.format_response(apply_result))
141+
142+
else:
143+
self.process_loop()

examples/hello_world/src/Algorithm.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
from adk import ADK
22

33

4+
# API calls will begin at the apply() method, with the request body passed as 'input'
5+
# For more details, see algorithmia.com/developers/algorithm-development/languages
6+
47
def apply(input):
8+
# If your apply function uses state that's loaded into memory via load, you can pass that loaded state to your apply
9+
# function by defining an additional "state" parameter in your apply function; but it's optional!
510
return "hello {}".format(str(input))
611

712

8-
algorithm = ADK(apply)
9-
algorithm.serve()
13+
# This turns your library code into an algorithm that can run on the platform.
14+
# If you intend to use loading operations, remember to pass a `load` function as a second variable.
15+
algo = ADK(apply)
16+
# The 'serve()' function actually starts the algorithm, you can follow along in the source code
17+
# to see how everything works.
18+
algo.serve("Algorithmia")

examples/loaded_state_hello_world/src/Algorithm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ def load():
2525
algo = ADK(apply, load)
2626
# The 'serve()' function actually starts the algorithm, you can follow along in the source code
2727
# to see how everything works.
28-
algo.serve()
28+
algo.serve("Algorithmia")

examples/pytorch_image_classification/src/Algorithm.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,5 +90,5 @@ def apply(input, state):
9090
raise Exception('input must be a json object')
9191

9292

93-
adk = ADK(apply_func=apply, load_func=load)
94-
adk.serve()
93+
algo = ADK(apply_func=apply, load_func=load)
94+
algo.serve({"data": "https://i.imgur.com/bXdORXl.jpeg"})

0 commit comments

Comments
 (0)