An LLM powered charting package
Project description
chAI
An LLM-powered charting package to enable visualisation suggestion, image review and analysis, copycat functionality to replicate a provided chart in plotly, and output of standardised plotly code for bar, histogram, scatter and line charts.
Currently the library has only been tested with compatible Anthropic models. See the AWS documentation for models that have compatible input modalities for image analysis.
Prerequisites
- An environment with Python 3.13 or greater
- Foundation model access approved within AWS for models referenced in constants.py
- OPTIONAL: A .env file with an entry for AWS_PROFILE=. This profile/user should have sufficient permissions to execute a API calls without requiring MFA authentication in e.g. a notebook. Alternatively, users can configure the chAI runtime at initialisation.
Installation
To use chAI:
- Create and activate a new virtual environment with Python 3.11 or greater
- Ensure you are in the root folder of the repo
- In the terminal type:
pip install chai-llm
You can now use chAI in your python notebook!
Usage
chAI comes with 3 LLM models for selection by default - please note the capabilities of each one when deciding which is most appropriate for your use case:
- Claude 3.5 Haiku: Claude 3.5 Haiku offers quick code suggestions and completions to accelerate development workflows.
- Claude 3.5 Sonnet: Claude 3.5 Sonnet in addition to code completion is well suited to image analysis, particularly visual reasoning and interpreting charts and graphs, and can accurately transcribe text from imperfect images.
- Claude 3.7 Sonnet: Claude 3.7 Sonnet takes the capabilities of 3.5 further by combining the ordinary LLM with a reasoning model and is generally an upgraded version of Claude 3.5.
Configuration
You can provide configuration parameters explicitly when initializing the chAI object. For convenience, we've defined Enum values in the AWSRegion and LLMModel classes that represent commonly used AWS regions and Claude model variants suitable for chAI:
from chai import chAI
from chai.constants import AWSRegion, LLMModel
chai = chAI(
aws_profile="your-aws-profile",
llm_region=AWSRegion.US_EAST_1,
llm_model=LLMModel.CLAUDE_SONNET_3_7,
)
Alternatively, you can initialise chAI with an .env file with the following variables if you wish to avoid sharing credentials information in your code:
- AWS_PROFILE=your-aws-profile
- LLM_REGION=your-aws-region (you can use the Enum here)
- LLM_MODEL=your-chosen-model (you can use the Enum here)
and then run:
from chai import chAI
chai = chAI()
Refer to the chAI_functionality_examples.ipynb notebook to see the workflows in action. A sample input chart is provided for you to experiment with.
Intelligent Visualisation Recommendations
This takes an input dataframe and reviews the first 100 rows of data to suggest potential visualisations for analysis. For each suggested chart:
- Purpose: This section outlines the goal of the visualisation. It describes what the chart is meant to show.
- Chart Type: This specifies the suitable visual you will use to present the data.
- Variables: This section defines the data being plotted on the x and y axes.
- Expected Insights: This part describes the specific insights you may gain from the visualisation.
# Example Code
sample_df = create_sample_dataset()
prompt = "Analyse this dataset and suggest visualisations to explore trends and insights."
response = chai.steep(
data=sample_df,
prompt=prompt,
)
print(response.suggestions)
Chart Analysis and Replication with chAI
This takes a stored PNG image and attempts to 1. replicate the image using plotly into an HTML file 2. analyse the image. The analytical review can be boosted by providing some additional context as to what the image might show (for example the chart relates to outcomes of a specific subset of data). This returns a JSON dictionary containing:
- analysis: A summary of what the LLM's analytical review
- code: The plotly code required to replicate the "look" of the chart (or as close as possible)
- path: The path where the HTML file has been saved
# Example Code
prompt = """
Please analyse this chart and provide a comprehensive review with the following structure:
1. Key Findings:
- What are the most significant patterns or insights?
- What are the highest and lowest values?
- Are there any notable disparities or trends?
2. Comparative Analysis:
- Compare the different outcome groups
- Identify any significant gaps or differences
- Highlight any unexpected patterns
Please provide your analysis in clear, business-friendly language suitable for stakeholders.
"""
image_analysis = chai.steep(prompt=prompt, image_path="../tests/img/satisfaction.png")
print(image_analysis.analysis) # Access the analysis
print(image_analysis.code) # View the generated Plotly code
print(image_analysis.path) # View the path to the locally generated chart
Chart Template Generation
chAI has plotly code for several chart templates in its backend:
- Bar: Set chart_type to 'bar'
- Histogram: Set chart_type to 'histogram'
- Scatter: Set chart_type to 'scatter'
- Line: Set chart_type to 'line'
chAI can take user prompts to design a specified chart and provide required plotly code.
# Example Code
chart_prompt = "I want a red chart with bold axis titles. There should also be a legend showing each distinct category"
chart_generation = chai.steep(prompt=chart_prompt, chart_type="scatter")
print(chart_generation.code) # View the generated Plotly code
Development
The project uses uv for development. To contribute:
git clone https://github.com/co-cddo/gds-ideas-chai.git
cd gds-ideas-chai
uv sync --dev
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file chai_llm-0.1.4.tar.gz.
File metadata
- Download URL: chai_llm-0.1.4.tar.gz
- Upload date:
- Size: 18.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
26c9d8ab3b3e8d9af2c44c939e4a228d8e623642c2f9ee9847aaabcc8ee41e63
|
|
| MD5 |
e98020a904237751573aee08221a3c33
|
|
| BLAKE2b-256 |
0d8ca95671bd135a7e0043718f27b8f93f5ddb89bfa868146b42062d7b1718a6
|
Provenance
The following attestation bundles were made for chai_llm-0.1.4.tar.gz:
Publisher:
publish-package.yml on co-cddo/gds-ideas-chai
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
chai_llm-0.1.4.tar.gz -
Subject digest:
26c9d8ab3b3e8d9af2c44c939e4a228d8e623642c2f9ee9847aaabcc8ee41e63 - Sigstore transparency entry: 198540882
- Sigstore integration time:
-
Permalink:
co-cddo/gds-ideas-chai@15dfabbc36c714a3d5f31405134566faee31c5af -
Branch / Tag:
refs/heads/main - Owner: https://github.com/co-cddo
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-package.yml@15dfabbc36c714a3d5f31405134566faee31c5af -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file chai_llm-0.1.4-py3-none-any.whl.
File metadata
- Download URL: chai_llm-0.1.4-py3-none-any.whl
- Upload date:
- Size: 4.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0e0efaea578e9f9fb986518894558f2468dfa4c07b14f61b597bd34dc9ed277c
|
|
| MD5 |
c41b9e7f6d7cbbcec5af0867e4bfd66f
|
|
| BLAKE2b-256 |
b4149fc1f5a12532215c7c03ef5b27b109112fd33a05736ccca8363e90f606ce
|
Provenance
The following attestation bundles were made for chai_llm-0.1.4-py3-none-any.whl:
Publisher:
publish-package.yml on co-cddo/gds-ideas-chai
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
chai_llm-0.1.4-py3-none-any.whl -
Subject digest:
0e0efaea578e9f9fb986518894558f2468dfa4c07b14f61b597bd34dc9ed277c - Sigstore transparency entry: 198540885
- Sigstore integration time:
-
Permalink:
co-cddo/gds-ideas-chai@15dfabbc36c714a3d5f31405134566faee31c5af -
Branch / Tag:
refs/heads/main - Owner: https://github.com/co-cddo
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-package.yml@15dfabbc36c714a3d5f31405134566faee31c5af -
Trigger Event:
workflow_dispatch
-
Statement type: