Skip to content

Commit

Permalink
comments
Browse files Browse the repository at this point in the history
  • Loading branch information
lingyielia committed Feb 21, 2025
1 parent 2f6cc5c commit a138701
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 63 deletions.
18 changes: 13 additions & 5 deletions vizro-ai/examples/dashboard_ui/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import dash_bootstrap_components as dbc
import vizro.models as vm
from dash import dcc, get_asset_url, html
from pydantic import AfterValidator, Field
from pydantic import AfterValidator, Field, PlainSerializer
from vizro.models import Action
from vizro.models._action._actions_chain import _action_validator_factory
from vizro.models._models_utils import _log_call
Expand All @@ -27,9 +27,12 @@ class UserPromptTextArea(vm.VizroBaseModel):
"""

type: Literal["user_text_area"] = "user_text_area"
actions: list[Action] = [] # noqa: RUF012

_set_actions = _action_validator_factory("value")
actions: Annotated[
list[Action],
AfterValidator(_action_validator_factory("value")),
PlainSerializer(lambda x: x[0].actions),
Field(default=[]),
]

@_log_call
def build(self):
Expand All @@ -49,7 +52,12 @@ class UserUpload(vm.VizroBaseModel):
"""Component enabling data upload."""

type: Literal["upload"] = "upload"
actions: Annotated[list[Action], AfterValidator(_action_validator_factory("contents")), Field(default_factory=list)]
actions: Annotated[
list[Action],
AfterValidator(_action_validator_factory("contents")),
PlainSerializer(lambda x: x[0].actions),
Field(default=[]),
]

def build(self):
"""Returns the upload component for data upload."""
Expand Down
44 changes: 44 additions & 0 deletions vizro-ai/tests/unit/vizro-ai/conftest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
"""Fixtures to be shared across several tests."""

from typing import Any

import pytest
import vizro.plotly.express as px
from langchain.output_parsers import PydanticOutputParser
from langchain_community.llms.fake import FakeListLLM


@pytest.fixture
Expand All @@ -10,3 +14,43 @@ def gapminder():


# TODO add more common fixtures here


class MockStructuredOutputLLM(FakeListLLM):
def bind_tools(self, tools: list[Any]):
return super().bind(tools=tools)

def with_structured_output(self, schema, **kwargs):
llm = self
output_parser = PydanticOutputParser(pydantic_object=schema)
return llm | output_parser


@pytest.fixture
def fake_llm():
response = ['{"text":"this is a card","href":""}']
return MockStructuredOutputLLM(responses=response)


@pytest.fixture
def fake_llm_card():
response = ['{"text":"this is a card","href":""}']
return MockStructuredOutputLLM(responses=response)


@pytest.fixture
def fake_llm_layout():
response = ['{"grid":[[0,1]]}']
return MockStructuredOutputLLM(responses=response)


@pytest.fixture
def fake_llm_filter():
response = ['{"column": "a", "targets": ["bar_chart"]}']
return MockStructuredOutputLLM(responses=response)


@pytest.fixture
def fake_llm_invalid():
response = ['{"text":"this is a card", "href": "", "icon": "summary"}']
return MockStructuredOutputLLM(responses=response)
Original file line number Diff line number Diff line change
@@ -1,45 +1,13 @@
from typing import Any

import pandas as pd
import pytest
import vizro.models as vm
from langchain.output_parsers import PydanticOutputParser
from langchain_community.llms.fake import FakeListLLM

from vizro_ai.dashboard._response_models.components import ComponentPlan
from vizro_ai.dashboard._response_models.page import PagePlan
from vizro_ai.dashboard.utils import AllDfMetadata, DfMetadata
from vizro_ai.plot._response_models import ChartPlan


class MockStructuredOutputLLM(FakeListLLM):
def bind_tools(self, tools: list[Any]):
return super().bind(tools=tools)

def with_structured_output(self, schema, **kwargs):
llm = self
output_parser = PydanticOutputParser(pydantic_object=schema)
return llm | output_parser


@pytest.fixture
def fake_llm_card():
response = ['{"text":"this is a card","href":""}']
return MockStructuredOutputLLM(responses=response)


@pytest.fixture
def fake_llm_layout():
response = ['{"grid":[[0,1]]}']
return MockStructuredOutputLLM(responses=response)


@pytest.fixture
def fake_llm_filter():
response = ['{"column": "a", "targets": ["bar_chart"]}']
return MockStructuredOutputLLM(responses=response)


@pytest.fixture
def controllable_components():
return ["bar_chart"]
Expand Down
26 changes: 0 additions & 26 deletions vizro-ai/tests/unit/vizro-ai/dashboard/conftest.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,8 @@
from typing import Any

import pytest
import vizro.models as vm
from langchain.output_parsers import PydanticOutputParser
from langchain_community.llms.fake import FakeListLLM
from langchain_core.messages import HumanMessage


class MockStructuredOutputLLM(FakeListLLM):
def bind_tools(self, tools: list[Any]):
return super().bind(tools=tools)

def with_structured_output(self, schema, **kwargs):
llm = self
output_parser = PydanticOutputParser(pydantic_object=schema)
return llm | output_parser


@pytest.fixture
def fake_llm():
response = ['{"text":"this is a card","href":""}']
return MockStructuredOutputLLM(responses=response)


@pytest.fixture
def component_description():
return "This is a card"
Expand Down Expand Up @@ -50,9 +30,3 @@ def message_output_error():
"df_info": None,
"validation_error": "ValidationError",
}


@pytest.fixture
def fake_llm_invalid():
response = ['{"text":"this is a card", "href": "", "icon": "summary"}']
return MockStructuredOutputLLM(responses=response)

0 comments on commit a138701

Please sign in to comment.