Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/mckinsey/vizro into circlec…
Browse files Browse the repository at this point in the history
…i_to_github
  • Loading branch information
l0uden committed Jul 30, 2024
2 parents fbb6066 + 77fc1ac commit a624d80
Show file tree
Hide file tree
Showing 140 changed files with 4,963 additions and 169 deletions.
6 changes: 4 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ target-version = ["py38"]

[tool.codespell]
builtin = "clear,rare,en-GB_to_en-US"
ignore-words-list = "grey,ned,sav"
skip = "*.min.css.map,*.min.css,.vale/*"
ignore-words-list = "grey,ned,sav,Thur"
skip = "*.min.css.map,*.min.css,.vale/*, *assets/*"

[tool.mypy]
# strict checks : strict = true
Expand Down Expand Up @@ -91,3 +91,5 @@ select = [
"**/tests/**" = ["PLR2004", "S101", "TID252", "D100", "D101", "D102", "D103", "PLC1901", "RUF012"]
# Ignore import violations in all __init__.py files
"__init__.py" = ["E402", "F401"]
# Ignore missing docstrings in chart gallery examples to keep them succinct.
"vizro-core/examples/_chart-gallery/pages/examples/**" = ["D100", "D103"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<!--
A new scriv changelog fragment.
Uncomment the section that is right (remove the HTML comment wrapper).
-->

<!--
### Highlights ✨
- A bullet item for the Highlights ✨ category with a link to the relevant PR at the end of your entry, e.g. Enable feature XXX ([#1](https://github.com/mckinsey/vizro/pull/1))
-->
<!--
### Removed
- A bullet item for the Removed category with a link to the relevant PR at the end of your entry, e.g. Enable feature XXX ([#1](https://github.com/mckinsey/vizro/pull/1))
-->
<!--
### Added
- A bullet item for the Added category with a link to the relevant PR at the end of your entry, e.g. Enable feature XXX ([#1](https://github.com/mckinsey/vizro/pull/1))
-->
<!--
### Changed
- A bullet item for the Changed category with a link to the relevant PR at the end of your entry, e.g. Enable feature XXX ([#1](https://github.com/mckinsey/vizro/pull/1))
-->
<!--
### Deprecated
- A bullet item for the Deprecated category with a link to the relevant PR at the end of your entry, e.g. Enable feature XXX ([#1](https://github.com/mckinsey/vizro/pull/1))
-->
<!--
### Fixed
- A bullet item for the Fixed category with a link to the relevant PR at the end of your entry, e.g. Enable feature XXX ([#1](https://github.com/mckinsey/vizro/pull/1))
-->
<!--
### Security
- A bullet item for the Security category with a link to the relevant PR at the end of your entry, e.g. Enable feature XXX ([#1](https://github.com/mckinsey/vizro/pull/1))
-->
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<!--
A new scriv changelog fragment.
Uncomment the section that is right (remove the HTML comment wrapper).
-->

<!--
### Highlights ✨
- A bullet item for the Highlights ✨ category with a link to the relevant PR at the end of your entry, e.g. Enable feature XXX ([#1](https://github.com/mckinsey/vizro/pull/1))
-->
<!--
### Removed
- A bullet item for the Removed category with a link to the relevant PR at the end of your entry, e.g. Enable feature XXX ([#1](https://github.com/mckinsey/vizro/pull/1))
-->
<!--
### Added
- A bullet item for the Added category with a link to the relevant PR at the end of your entry, e.g. Enable feature XXX ([#1](https://github.com/mckinsey/vizro/pull/1))
-->
<!--
### Changed
- A bullet item for the Changed category with a link to the relevant PR at the end of your entry, e.g. Enable feature XXX ([#1](https://github.com/mckinsey/vizro/pull/1))
-->
<!--
### Deprecated
- A bullet item for the Deprecated category with a link to the relevant PR at the end of your entry, e.g. Enable feature XXX ([#1](https://github.com/mckinsey/vizro/pull/1))
-->
<!--
### Fixed
- A bullet item for the Fixed category with a link to the relevant PR at the end of your entry, e.g. Enable feature XXX ([#1](https://github.com/mckinsey/vizro/pull/1))
-->
<!--
### Security
- A bullet item for the Security category with a link to the relevant PR at the end of your entry, e.g. Enable feature XXX ([#1](https://github.com/mckinsey/vizro/pull/1))
-->
48 changes: 48 additions & 0 deletions vizro-ai/changelog.d/20240727_031259_lingyi_zhang_vizroai_code.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<!--
A new scriv changelog fragment.
Uncomment the section that is right (remove the HTML comment wrapper).
-->

<!--
### Highlights ✨
- A bullet item for the Highlights ✨ category with a link to the relevant PR at the end of your entry, e.g. Enable feature XXX ([#1](https://github.com/mckinsey/vizro/pull/1))
-->
<!--
### Removed
- A bullet item for the Removed category with a link to the relevant PR at the end of your entry, e.g. Enable feature XXX ([#1](https://github.com/mckinsey/vizro/pull/1))
-->
<!--
### Added
- A bullet item for the Added category with a link to the relevant PR at the end of your entry, e.g. Enable feature XXX ([#1](https://github.com/mckinsey/vizro/pull/1))
-->
<!--
### Changed
- A bullet item for the Changed category with a link to the relevant PR at the end of your entry, e.g. Enable feature XXX ([#1](https://github.com/mckinsey/vizro/pull/1))
-->
<!--
### Deprecated
- A bullet item for the Deprecated category with a link to the relevant PR at the end of your entry, e.g. Enable feature XXX ([#1](https://github.com/mckinsey/vizro/pull/1))
-->
<!--
### Fixed
- A bullet item for the Fixed category with a link to the relevant PR at the end of your entry, e.g. Enable feature XXX ([#1](https://github.com/mckinsey/vizro/pull/1))
-->
<!--
### Security
- A bullet item for the Security category with a link to the relevant PR at the end of your entry, e.g. Enable feature XXX ([#1](https://github.com/mckinsey/vizro/pull/1))
-->
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<!--
A new scriv changelog fragment.
Uncomment the section that is right (remove the HTML comment wrapper).
-->

<!--
### Highlights ✨
- A bullet item for the Highlights ✨ category with a link to the relevant PR at the end of your entry, e.g. Enable feature XXX ([#1](https://github.com/mckinsey/vizro/pull/1))
-->
<!--
### Removed
- A bullet item for the Removed category with a link to the relevant PR at the end of your entry, e.g. Enable feature XXX ([#1](https://github.com/mckinsey/vizro/pull/1))
-->
<!--
### Added
- A bullet item for the Added category with a link to the relevant PR at the end of your entry, e.g. Enable feature XXX ([#1](https://github.com/mckinsey/vizro/pull/1))
-->

### Changed

- Stabilized `plot` performance by addressing several dataframe mutation issues. ([#603](https://github.com/mckinsey/vizro/pull/603))

<!--
### Deprecated
- A bullet item for the Deprecated category with a link to the relevant PR at the end of your entry, e.g. Enable feature XXX ([#1](https://github.com/mckinsey/vizro/pull/1))
-->

<!--
### Fixed
- A bullet item for the Fixed category with a link to the relevant PR at the end of your entry, e.g. Enable feature XXX ([#1](https://github.com/mckinsey/vizro/pull/1))
-->
<!--
### Security
- A bullet item for the Security category with a link to the relevant PR at the end of your entry, e.g. Enable feature XXX ([#1](https://github.com/mckinsey/vizro/pull/1))
-->
46 changes: 30 additions & 16 deletions vizro-ai/docs/pages/user-guides/run-vizro-ai.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,36 +81,50 @@ There are two ways to integrate Vizro-AI into an application, directly and by ac
```


2. Vizro-AI's `_get_chart_code` method returns a string of Python code that manipulates the data and creates the visualization. Vizro-AI validates the code to ensure that it is executable and can be integrated.
2. When the `return_elements` argument of VizroAI's `plot` method is set to `True`, the method returns a `PlotOutputs` data class which contains all possible `VizroAI.plot()` outputs.
By setting `return_elements=True` you can access code or the figure object. Vizro-AI validates the code to ensure that it is executable and can be integrated.

!!! example "Application integration via chart code"
!!! example "Accessing outputs from PlotOutputs data class"

=== "app.py"
```py
import vizro.plotly.express as px
from vizro_ai import VizroAI

vizro_ai = VizroAI()
```py
import vizro_ai
from vizro_ai import VizroAI
import vizro.plotly.express as px
from dotenv import load_dotenv

df = px.data.gapminder()
code_string = vizro_ai._get_chart_code(df, "describe life expectancy per continent over time")
```
=== "code_string"
[![ResultCode]][ResultCode]
load_dotenv()

[ResultCode]: ../../assets/user_guides/code_string_app_integration.png
df = px.data.gapminder()
vizro_ai = VizroAI()

The returned `code_string` can be used to dynamically render charts within your application. You may have the option to encapsulate the chart within a `fig` object or convert the figure into a JSON string for further integration.
plot_outputs = vizro_ai.plot(df, "describe life expectancy per continent over time", explain=True, return_elements=True)
fig = plot_outputs.figure
code_string = plot_outputs.code
```

To use the insights or code explanation, you can use `vizro_ai._run_plot_tasks(df, ..., explain=True)`, which returns a dictionary containing the code explanation and chart insights alongside the code.

### How to use `max_debug_retry` parameter in plot function
- Default Value: 3
- Type: int
- Type: `int`
- Brief: By default, the `max_debug_retry` is set to 3, the function will try to debug errors up to three times.
If the errors are not resolved after the maximum number of retries, the function will stop further debugging retries.
For example, if you would like adjust to 5 retries, you can set `max_debug_retry = 5` in the plot function:

```py
vizro_ai.plot(df = df, user_input = "your user input", max_debug_retry= 5)
```

### How to use `return_elements` parameter in plot function
- Default Value: False
- Type: `bool`
- Brief: By default, the `return_elements` is set to `False`, and the `VizroAI.plot()` returns a `plotly.graph_objects` object. If `return_elements` is set to `True` `VizroAI.plot()` returns the `PlotOutputs` data class.
The `PlotOutputs` data class is designed to encapsulate all possible outputs generated by the `VizroAI.plot()` method.

Attributes of `PlotOutputs`:

- **`code`**: A string representing of the Python code that manipulates the data and creates the visualization.
- **`figure`**: A [`CapturedCallable`](https://vizro.readthedocs.io/en/stable/pages/API-reference/models/#vizro.models.types.CapturedCallable) object from [`Vizro`](https://vizro.readthedocs.io/en/stable/), representing the visual plot generated by `VizroAI.plot()`. This object is designed to run immediately in the Vizro dashboard, but otherwise, it behaves like a plotly `go.Figure`.
- **`business_insights`**: A string containing high-level business insights derived from the plot. `business_insights` is only available if `explain=True`.
- **`code_explanation`**: A string offering a detailed explanation of the code used to produce the plot. `code_explanation` is only available if `explain=True`.
-
File renamed without changes.
10 changes: 5 additions & 5 deletions vizro-ai/src/vizro_ai/_vizro_ai.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import plotly.graph_objects as go
from langchain_openai import ChatOpenAI

from vizro_ai.chains._llm_models import _get_llm_model
from vizro_ai.components import GetCodeExplanation, GetDebugger
from vizro_ai.task_pipeline._pipeline_manager import PipelineManager
from vizro_ai._llm_models import _get_llm_model
from vizro_ai.plot.components import GetCodeExplanation, GetDebugger
from vizro_ai.plot.task_pipeline._pipeline_manager import PipelineManager
from vizro_ai.utils.helper import (
DebugFailure,
PlotOutputs,
Expand Down Expand Up @@ -64,12 +64,12 @@ def _run_plot_tasks(
) -> PlotOutputs:
"""Task execution."""
chart_type_pipeline = self.pipeline_manager.chart_type_pipeline
chart_types = chart_type_pipeline.run(initial_args={"chain_input": user_input, "df": df})
chart_type = chart_type_pipeline.run(initial_args={"chain_input": user_input, "df": df})

# TODO update to loop through charts for multiple charts creation
plot_pipeline = self.pipeline_manager.plot_pipeline
custom_chart_code = plot_pipeline.run(
initial_args={"chain_input": user_input, "df": df, "chart_types": chart_types}
initial_args={"chain_input": user_input, "df": df, "chart_type": chart_type}
)

# TODO add debug in pipeline after getting _debug_helper logic in component
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from ._base import VizroAiComponentBase
from ._base import VizroAIComponentBase
from .chart_selection import GetChartSelection
from .code_validation import GetDebugger
from .custom_chart_wrap import GetCustomChart
Expand All @@ -7,7 +7,7 @@
from .visual_code import GetVisualCode

__all__ = [
"VizroAiComponentBase",
"VizroAIComponentBase",
"GetChartSelection",
"GetDataFrameCraft",
"GetVisualCode",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from vizro_ai.chains import FunctionCallChain


class VizroAiComponentBase(ABC):
class VizroAIComponentBase(ABC):
"""Abstract Base Class that represents a blueprint for Vizro-AI components.
Attributes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
from langchain_core.language_models.chat_models import BaseChatModel

from vizro_ai.chains._chain_utils import _log_time
from vizro_ai.components import VizroAiComponentBase
from vizro_ai.schema_manager import SchemaManager
from vizro_ai.plot.components import VizroAIComponentBase
from vizro_ai.plot.schema_manager import SchemaManager
from vizro_ai.utils.helper import _get_df_info

# initialization of schema manager, and register schema needed
# preprocess: llm kwargs for function description schema + partial vars
Expand All @@ -35,12 +36,12 @@ class ChartSelection(BaseModel):


# 2. Define prompt
chart_type_prompt = "choose a best chart types for this df info:{df_schema}, {df_head} and user question {input}?"
chart_type_prompt = "choose a best chart type for this df info:{df_schema}, {df_sample} and user question {input}?"


# 3. Define Component
class GetChartSelection(VizroAiComponentBase):
"""Get Chart Types.
class GetChartSelection(VizroAIComponentBase):
"""Get chart type.
Attributes
prompt (str): Prompt chart selection chains.
Expand All @@ -63,9 +64,9 @@ def _pre_process(self, df: pd.DataFrame, *args, **kwargs) -> Tuple[Dict, Dict]:
It should return llm_kwargs and partial_vars_map for
"""
df_schema, df_head = self._get_df_info(df)
df_schema, df_sample = _get_df_info(df)
llm_kwargs_to_use = openai_schema_manager.get_llm_kwargs("ChartSelection")
partial_vars = {"df_schema": df_schema, "df_head": df_head}
partial_vars = {"df_schema": df_schema, "df_sample": df_sample}
return llm_kwargs_to_use, partial_vars

def _post_process(self, response: Dict, *args, **kwargs) -> str:
Expand All @@ -87,14 +88,6 @@ def run(self, chain_input: str, df: pd.DataFrame = None) -> str:
"""
return super().run(chain_input=chain_input, df=df)

@staticmethod
def _get_df_info(df: pd.DataFrame) -> Tuple[str, str]:
"""Get the dataframe schema and head info as string."""
formatted_pairs = [f"{col_name}: {dtype}" for col_name, dtype in df.dtypes.items()]
schema_string = "\n".join(formatted_pairs)

return schema_string, df.head().to_markdown()

@staticmethod
def _chart_to_use(load_args) -> str:
"""Get Chart name as string or list of chart names as string."""
Expand All @@ -109,7 +102,7 @@ def _chart_to_use(load_args) -> str:
if __name__ == "__main__":
import vizro.plotly.express as px

from vizro_ai.chains._llm_models import _get_llm_model
from vizro_ai._llm_models import _get_llm_model

llm_to_use = _get_llm_model()

Expand Down
Loading

0 comments on commit a624d80

Please sign in to comment.