Skip to content

Commit

Permalink
Bump to 0.0.3 (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
ultmaster authored Nov 2, 2023
1 parent 3688eca commit 2febf19
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 51 deletions.
8 changes: 6 additions & 2 deletions coml/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@
)
from .vis_utils import VisVerifier

_debug_mode: bool = False


def debug_messages(*messages: BaseMessage) -> None:
if not _debug_mode:
return
for message in messages:
if isinstance(message, SystemMessage):
print(colorama.Fore.BLUE + message.content + colorama.Fore.RESET + "\n")
Expand Down Expand Up @@ -113,10 +117,10 @@ def generate_code(
)
question, _ = render_generate_context(context)
messages.append(HumanMessage(content=question))
# debug_messages(*messages)
debug_messages(*messages)

response = self.llm(messages)
# debug_messages(response)
debug_messages(response)
code = parse_code(response.content)
return {**context, "answer": code}

Expand Down
122 changes: 75 additions & 47 deletions coml/magics.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
run_code_in_next_cell,
update_running_cell_metadata,
)
from .linter import lint
from .prompt_utils import (
FixContext,
GenerateContext,
Expand All @@ -33,6 +34,38 @@
filter_variables,
)

VERIFY_STYLE = """
<style>
summary {
display: list-style;
}
details :last-child {
margin-bottom: 1em;
}
.loader {
width: 1em;
height: 1em;
border: 0.1em solid;
border-bottom-color: transparent;
border-radius: 50%;
display: inline-block;
box-sizing: border-box;
animation: rotation 1s linear infinite;
margin-bottom: 0 !important;
}
@keyframes rotation {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
</style>
"""


@magics_class
class CoMLMagics(Magics):
Expand Down Expand Up @@ -116,6 +149,22 @@ def coml(self, line, cell=None):
)
return self._post_generation(generate_context["answer"], generate_context)

@no_var_expand
@line_magic
def comlset(self, line):
key, value = line.lower().strip().split()
if key == "debug":
from . import core

if value == "on":
core._debug_mode = True
elif value == "off":
core._debug_mode = False
else:
raise ValueError("Debug mode must be either on or off.")
else:
raise ValueError("Unknown setting.")

@no_var_expand
@line_magic
def comlinspire(self, line):
Expand Down Expand Up @@ -202,47 +251,11 @@ def comlverify(self, line):
generated_vis = False
if context.get("action") == "run":
error, output = parse_cell_outputs(target_cell["outputs"])
generated_vis = (
output
and "<image/svg+xml>" in output
and "request" in context
and "answer" in context
and "codes" in context
and "variables" in context
)
style = """<style>
summary {
display: list-style;
}
details :last-child {
margin-bottom: 1em;
}
.loader {
width: 1em;
height: 1em;
border: 0.1em solid;
border-bottom-color: transparent;
border-radius: 50%;
display: inline-block;
box-sizing: border-box;
animation: rotation 1s linear infinite;
margin-bottom: 0 !important;
}
@keyframes rotation {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
</style>"""
generated_vis = output and "<image/svg+xml>" in output

def display_statuses(statuses):
clear_output(wait=True)
html = style + "\n"
html = VERIFY_STYLE + "\n"
display_names = {
"lint": "PyLint",
"rubberduck": "Rubberduck",
Expand All @@ -261,21 +274,33 @@ def display_statuses(statuses):
False: "❌",
}
loading = "<span class='loader'></span>"
message_template = "<details><summary><b>{}:</b> {}</summary>\n{}</details>"
for name in display_names:
detail_message = "Still loading..."
if name in statuses:
detail_message = markdown.markdown(
statuses[name]["details"], extensions=["nl2br"]
)
html += f"""<details>
<summary><b>{display_names[name]}:</b> {loading if name not in statuses else status_icon[statuses[name]["result"]]}</summary>
{detail_message}
</details>\n"""
html += message_template.format(
display_names[name],
loading
if name not in statuses
else status_icon[statuses[name]["result"]],
detail_message,
)

display(HTML(html))

result = {}
display_statuses(result)

lint_result, lint_details = lint("\n".join(self._get_code_context()), code)
result["lint"] = {
"result": lint_result,
"details": lint_details,
}
display_statuses(result)

rubberduck_result, rubberduck_details = self.agent.static_check(code, context)
result["rubberduck"] = {
"result": rubberduck_result,
Expand All @@ -287,18 +312,20 @@ def display_statuses(statuses):
# verify generated vis
svg_string = output.replace("<image/svg+xml>", "")
request = context["request"]
new_code = context["answer"]
previous_code = "\n".join(context["codes"])
variables = context["variables"]

# Roughly judge the source of the visualization
if "plt.show()" in new_code:
source = "matplotlib"
if "plt.show()" in code:
vis_framework = "matplotlib"
(
visualization_check_result,
visualization_check_details,
) = self.agent.visualization_check(
request, previous_code, svg_string, variables, source
context["request"],
"\n".join(self._get_code_context()),
code,
context["variables"],
vis_framework,
)
details = ""
for detail in visualization_check_details:
Expand All @@ -308,6 +335,7 @@ def display_statuses(statuses):
"details": details,
}
display_statuses(result)

elif error or output:
sanity_result, sanity_details = self.agent.output_sanity_check(
code, context, error, output
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "coml",
"version": "0.0.2",
"version": "0.0.3",
"description": "JupyterLab extension for CoML.",
"keywords": [
"jupyter",
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"

[project]
name = "mlcopilot"
version = "0.0.2"
version = "0.0.3"
dependencies = [
"click",
"colorama",
Expand Down

0 comments on commit 2febf19

Please sign in to comment.