Skip to content

Commit 48bb420

Browse files
authored
Merge pull request #12 from lambda-feedback/refactor_to_toolkit
Move shared request/response handling into toolkit-wolfram
2 parents 9e3fc1e + f43ddb2 commit 48bb420

7 files changed

Lines changed: 71 additions & 111 deletions

File tree

‎.gitignore‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
mathpass
1+
mathpass
2+
3+
# Local, non-Docker dev checkout of toolkit-wolfram -- see
4+
# scripts/setup-toolkit.sh and .toolkit-wolfram-version.
5+
/toolkit-wolfram/

‎Dockerfile‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
FROM ghcr.io/lambda-feedback/evaluation-function-base/wolfram:latest as base
22

3-
# Command to start the evaluation function with
4-
ENV FUNCTION_COMMAND="wolframscript"
5-
6-
# Args to start the evaluation function with
7-
ENV FUNCTION_ARGS="-f,/app/evaluation_function.wl"
8-
93
# Interface to use for the evaluation function
10-
ENV FUNCTION_INTERFACE="file"
4+
ENV FUNCTION_INTERFACE="rpc"
5+
ENV FUNCTION_RPC_TRANSPORT="tcp"
6+
ENV FUNCTION_WORKER_START_TIMEOUT="60s"
117

128
ENV LOG_LEVEL="DEBUG"
139

14-
# Copy the evaluation function to the app directory
15-
COPY ./evaluation_function.wl /app/evaluation_function.wl
10+
# The shared Wolfram evaluation-function toolkit (JSON comms layer) is
11+
# installed in the base image -- see evaluation-function-base/wolfram/Dockerfile.
12+
#
13+
# Optional local-dev override: run scripts/sync-local-toolkit.sh to populate
14+
# ./.local-toolkit from a local toolkit-wolfram checkout before building, to
15+
# test unreleased toolkit-wolfram changes here. Left empty (the default,
16+
# tracked via .local-toolkit/.gitkeep), this COPY is a no-op and the image
17+
# keeps using the toolkit-wolfram version pinned in the base image.
18+
#COPY ./.local-toolkit /opt/lambda-feedback/toolkit-wolfram
19+
1620
COPY ./evaluate.m /app/evaluate.m
1721
COPY ./preview.m /app/preview.m

‎README.md‎

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,37 @@ This repository contains an implementation of a Wolfram evaluation function that
1010

1111
You can choose between running the Wolfram evaluation function itself, ore using Shimmy to run the function.
1212

13+
This repository's JSON comms (reading the request, dispatching `eval`/`preview`, writing the response) come from the shared [`toolkit-wolfram`](https://github.com/lambda-feedback/toolkit-wolfram) paclet (`LambdaFeedback/EvaluationFunctionToolkit`) rather than being implemented in this repo — `evaluate.m`/`preview.m` here just provide `EvaluationFunction`/`PreviewFunction`, and the toolkit's `Bootstrap.wl` wires everything together (it's what the Docker image's Shimmy setup runs). To run locally, fetch the pinned toolkit version (see `.toolkit-wolfram-version`) with:
14+
15+
```bash
16+
scripts/setup-toolkit.sh
17+
```
18+
19+
This clones it into `./toolkit-wolfram` (gitignored; re-run any time to reset it). Then point `LF_TOOLKIT_PATH` at it:
20+
21+
```bash
22+
export LF_TOOLKIT_PATH=./toolkit-wolfram
23+
```
24+
1325
**Local**
1426

1527
Use the following command to run the evaluation function directly:
1628

1729
```bash
18-
wolframscript -f evaluation_function.wl request.json response.json
30+
wolframscript -f ./toolkit-wolfram/Bootstrap.wl request.json response.json
1931
```
2032

2133
This will run the evaluation function using the input data from `request.json` and write the output to `response.json`.
2234
An example `request.json` is:
2335

2436
```
2537
{
26-
"method": "eval",
38+
"command": "eval",
2739
"params": {
2840
"answer":"Sin[p x + q]",
2941
"response":"Sin[a x + b]",
3042
"params":{
31-
"comparisonType":"structure",
43+
"type":"structure",
3244
"named_variables":"{x}",
3345
"correct_response_feedback":"Your answer is correct!",
3446
"incorrect_response_feedback":"Your answer is incorrect!"
@@ -44,8 +56,7 @@ Which gives the response:
4456
"command": "eval",
4557
"result": {
4658
"is_correct": true,
47-
"feedback": "Your answer is correct!",
48-
"error": null
59+
"feedback": "Your answer is correct!"
4960
}
5061
}
5162
```
@@ -63,19 +74,20 @@ Which gives the response:
6374
build.yml # builds the public evaluation function image
6475
deploy.yml # deploys the evaluation function to Lambda Feedback
6576

66-
evaluation_function.wl # evaluation function source code
77+
evaluate.m # EvaluationFunction source code
78+
preview.m # PreviewFunction source code
6779

68-
config.json # evaluation function deployment configuration file
80+
config.json # evaluation function deployment configuration file
6981
```
7082

7183
### Development Workflow
7284

73-
In its most basic form, the development workflow consists of writing the evaluation function in the `evaluation_function.wl` file and testing it locally. As long as the evaluation function adheres to the Evaluation Function API, a development workflow which incorporates using Shimmy is not necessary.
85+
In its most basic form, the development workflow consists of writing the evaluation function in `evaluate.m`/`preview.m` and testing it locally. As long as the evaluation function adheres to the Evaluation Function API, a development workflow which incorporates using Shimmy is not necessary.
7486

75-
Testing the evaluation function can be done by running the script using the Wolfram Engine / WolframScript like so:
87+
Testing the evaluation function can be done by running the script using the Wolfram Engine / WolframScript like so (see [Run the Script](#run-the-script) above for the one-time `scripts/setup-toolkit.sh`/`LF_TOOLKIT_PATH` setup this depends on):
7688

7789
```bash
78-
wolframscript -f evaluation_function.wl request.json response.json
90+
wolframscript -f ./toolkit-wolfram/Bootstrap.wl request.json response.json
7991
```
8092

8193
> [!NOTE]
@@ -115,7 +127,7 @@ curl --location 'http://localhost:8080/wolframEvaluationFunction' \
115127
"answer":"Sin[p x + q]",
116128
"response":"Sin[a x + b]",
117129
"params":{
118-
"comparisonType":"structure",
130+
"type":"structure",
119131
"named_variables":"{x}",
120132
"correct_response_feedback":"Your answer is correct!",
121133
"incorrect_response_feedback":"Your answer is incorrect!"

‎evaluate.m‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
(* Declare package context *)
1919
BeginPackage["evaluate`"];
2020

21-
EvaluationFunction[type_, answer_, response_, params_] := Module[{result, feedback},
21+
EvaluationFunction[answer_, response_, params_] := Module[{result, feedback, type},
22+
type = params["type"];
2223
Print["Running Evaluation Function"];
2324
result = evalQ[type, answer, response, params];
2425
feedback = If[result["is_correct"],
@@ -35,15 +36,15 @@
3536

3637
Begin["`Private`"];
3738

38-
equalQNumeric[answer_, response_, params_] := Module[{tolerance},
39+
equalQNumeric[answer_, response_, params_] := Module[{tolerance, error},
3940
Print["Evaluating Equal Numeric"];
4041
tolerance = If[Lookup[params, "tolerance_is_absolute", False],
4142
Lookup[params, "tolerance", 0],
4243
Lookup[params, "tolerance", 0] * answer
4344
];
4445
error = Abs[answer - response];
4546
<|
46-
"error" -> error,
47+
"error" -> Null,
4748
"is_correct" -> TrueQ[error <= tolerance]
4849
|>
4950
]

‎evaluation_function.wl‎

Lines changed: 0 additions & 86 deletions
This file was deleted.

‎preview.m‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
(* Declare package context *)
1919
BeginPackage["preview`"];
2020

21-
PreviewFunction[response_] := Module[{latexString, wolframString, parsedResponse},
21+
PreviewFunction[response_, params_] := Module[{latexString, wolframString, parsedResponse},
2222
Print["Running Preview Function"];
2323
Print["Preview Input:", response];
2424

‎scripts/setup-toolkit.sh‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/bash
2+
# Fetches the pinned toolkit-wolfram version (see .toolkit-wolfram-version)
3+
# into ./toolkit-wolfram, for running/testing this evaluation function
4+
# locally without Docker/podman. Re-run any time to reset to the pinned
5+
# version (e.g. after bumping .toolkit-wolfram-version).
6+
#
7+
# Usage:
8+
# scripts/setup-toolkit.sh
9+
#
10+
# Then, to run the evaluation function directly:
11+
# export LF_TOOLKIT_PATH=./toolkit-wolfram
12+
# wolframscript -f ./toolkit-wolfram/Bootstrap.wl request.json response.json
13+
14+
set -euo pipefail
15+
16+
cd "$(dirname "$0")/.."
17+
18+
VERSION=$(cat .toolkit-wolfram-version)
19+
DEST="toolkit-wolfram"
20+
21+
rm -rf "$DEST"
22+
git clone --branch "$VERSION" --depth 1 \
23+
https://github.com/lambda-feedback/toolkit-wolfram.git "$DEST"
24+
25+
echo "Fetched toolkit-wolfram $VERSION -> $DEST"

0 commit comments

Comments
 (0)