-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
262 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
LATEX_TO_SYMPY_PROMPT=""" | ||
You are a helpful assistant who is an expert in writing mathematical expressions in LaTeX code and the Python package SymPy. | ||
Here is an example input LaTeX | ||
["\frac{{d S(t)}}{{d t}} = -beta * S(t) * I(t) + b - m * S(t)"] | ||
You should return this output in SymPy: | ||
``` | ||
import sympy | ||
# Define time variable | ||
t = sympy.symbols("t") | ||
# Define time-dependent variables | ||
S, I = sympy.symbols("S I", cls = sympy.Function) | ||
# Define constant parameters | ||
beta, b, m = sympy.symbols("beta b m") | ||
equation_output = [sympy.Eq(S(t).diff(t), -beta * S(t) * I(t) + b - m * S(t))] | ||
``` | ||
Now, do the same for this LaTeX input: | ||
{latex_equations} | ||
Respond with just the code and nothing else, do not surround the answer in ``` | ||
Answer: | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import sys | ||
from entities import EquationsCleanup | ||
from gollm_openai.tool_utils import latex_to_sympy | ||
|
||
from taskrunner import TaskRunnerInterface | ||
import traceback | ||
|
||
|
||
def cleanup(): | ||
pass | ||
|
||
|
||
def main(): | ||
exitCode = 0 | ||
try: | ||
taskrunner = TaskRunnerInterface(description="Converting latex to sympy") | ||
taskrunner.on_cancellation(cleanup) | ||
|
||
input_dict = taskrunner.read_input_dict_with_timeout() | ||
|
||
taskrunner.log("Sending request to OpenAI API") | ||
response = latex_to_sympy(equations=input_dict) | ||
taskrunner.log("Received response from OpenAI API") | ||
|
||
taskrunner.write_output_dict_with_timeout({"response": response}) | ||
|
||
except Exception as e: | ||
sys.stderr.write(f"Error: {str(e)}\n") | ||
sys.stderr.write(traceback.format_exc()) | ||
sys.stderr.flush() | ||
exitCode = 1 | ||
|
||
taskrunner.log("Shutting down") | ||
taskrunner.shutdown() | ||
sys.exit(exitCode) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import sys | ||
import json | ||
import traceback | ||
import sympy | ||
|
||
from taskrunner import TaskRunnerInterface | ||
from mira.sources.sympy_ode import template_model_from_sympy_odes | ||
from mira.modeling.amr.petrinet import template_model_to_petrinet_json | ||
|
||
def cleanup(): | ||
pass | ||
|
||
def main(): | ||
exitCode = 0 | ||
|
||
try: | ||
taskrunner = TaskRunnerInterface(description="Sympy to AMR") | ||
taskrunner.on_cancellation(cleanup) | ||
|
||
sympy_code = taskrunner.read_input_str_with_timeout() | ||
taskrunner.log("== input code") | ||
taskrunner.log(sympy_code) | ||
taskrunner.log("") | ||
|
||
globals = {} | ||
exec(sympy_code, globals) # output should be in placed into "equation_output" | ||
taskrunner.log("== equations") | ||
taskrunner.log(globals["equation_output"]) | ||
taskrunner.log("") | ||
|
||
# SymPy to MMT | ||
mmt = template_model_from_sympy_odes(globals["equation_output"]) | ||
|
||
# MMT to AMR | ||
amr_json = template_model_to_petrinet_json(mmt) | ||
|
||
# Gather results | ||
response = {} | ||
response["amr"] = amr_json | ||
response["sympyCode"] = sympy_code | ||
response["sympyExprs"] = list(map(lambda x: str(x), globals["equation_output"])) | ||
|
||
taskrunner.log(f"Sympy to AMR conversion succeeded") | ||
taskrunner.write_output_dict_with_timeout({"response": response }) | ||
except Exception as e: | ||
sys.stderr.write(f"Error: {str(e)}\n") | ||
sys.stderr.write(traceback.format_exc()) | ||
sys.stderr.flush() | ||
exitCode = 1 | ||
|
||
taskrunner.log("Shutting down") | ||
taskrunner.shutdown() | ||
sys.exit(exitCode) | ||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...java/software/uncharted/terarium/hmiserver/service/tasks/LatexToSympyResponseHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package software.uncharted.terarium.hmiserver.service.tasks; | ||
|
||
import lombok.Data; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Component; | ||
import software.uncharted.terarium.hmiserver.models.task.TaskResponse; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class LatexToSympyResponseHandler extends TaskResponseHandler { | ||
|
||
public static final String NAME = "gollm:latex_to_sympy"; | ||
|
||
@Override | ||
public String getName() { | ||
return NAME; | ||
} | ||
|
||
@Data | ||
public static class Response { | ||
|
||
String response; | ||
} | ||
|
||
@Override | ||
public TaskResponse onSuccess(final TaskResponse resp) { | ||
return resp; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...n/java/software/uncharted/terarium/hmiserver/service/tasks/SympyToAMRResponseHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package software.uncharted.terarium.hmiserver.service.tasks; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class SympyToAMRResponseHandler extends TaskResponseHandler { | ||
|
||
public static final String NAME = "mira_task:sympy_to_amr"; | ||
|
||
@Override | ||
public String getName() { | ||
return NAME; | ||
} | ||
} |