Skip to content

Commit

Permalink
fix: Fixed bug with invalid chart types generated
Browse files Browse the repository at this point in the history
  • Loading branch information
RamiAwar committed Nov 27, 2024
1 parent 5fb6150 commit 51282de
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
17 changes: 11 additions & 6 deletions backend/dataline/services/llm_flow/llm_calls/chart_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from enum import StrEnum

from mirascope.core import prompt_template
from pydantic import BaseModel, ValidationInfo, field_validator
from pydantic import BaseModel, model_validator


class ChartType(StrEnum):
Expand Down Expand Up @@ -201,22 +201,27 @@ class ChartType(StrEnum):


class GeneratedChart(BaseModel):
chart_type: ChartType
chartjs_json: str

@field_validator("chartjs_json", mode="before")
@model_validator(mode="before")
@classmethod
def check_alphanumeric(cls, v: str | dict, info: ValidationInfo) -> str:
def check_json(cls, data: dict):
v = data["chartjs_json"]
if isinstance(v, dict):
# check chart type - fails if not in valid types
v["type"] = ChartType[v["type"]]
v["type"] = ChartType[data["chart_type"]]
# convert to json str
v = json.dumps(v)

elif isinstance(v, str):
v_dict = json.loads(v)
# check chart type - this fails if not in valid types
v_dict["type"] = ChartType[v_dict["type"]]
return v
v_dict["type"] = ChartType[data["chart_type"]]
# convert back to json str
v = json.dumps(v_dict)
data["chartjs_json"] = v
return data


@prompt_template()
Expand Down
2 changes: 1 addition & 1 deletion backend/dataline/services/llm_flow/toolkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ def get_response( # type: ignore[misc]
base_url=state.options.openai_base_url,
),
)(
chart_type=ChartType[args["chart_type"]],
chart_type=chart_type,
request=args["request"],
chartjs_template=TEMPLATES[chart_type],
)
Expand Down

0 comments on commit 51282de

Please sign in to comment.