The chAI API¶
chai.chAI
¶
Classes¶
ChAIError
¶
ChAITeapot
¶
Bases: BaseModel
Pydantic model for the teapot component of ChAI responses. Different fields will be populated based on the request type.
Source code in src/chai/chAI.py
chAI
¶
Source code in src/chai/chAI.py
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 | |
Functions¶
__init__(aws_profile=None, llm_region=None, llm_model=None)
¶
Initialises the chAI class with required configurations and tools.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
aws_profile
|
Optional[str]
|
Optional AWS profile name. If provided, overrides environment variable. |
None
|
llm_region
|
Optional[str]
|
Optional LLM region. If provided, overrides environment variable. |
None
|
llm_model
|
Optional[str]
|
Optional LLM model. If provided, overrides environment variable. |
None
|
Notes
- AWS profile is loaded from environment variables via Config class
- Sets up Bedrock handler and runtime
- Loads LLM model and prompt
- Sets up visualization tools and templates
- Creates agent executor
Source code in src/chai/chAI.py
_process_output(raw_output, request_type)
¶
Process the raw output based on the request type and extract structured components.
This method parses the agent's response according to the request type and extracts relevant components into a dictionary structure that matches the ChAITeapot model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw_output
|
Any
|
Raw output from the agent, can be string or dictionary |
required |
request_type
|
str
|
Type of request ("dataframe", "image", or "chart") |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dict[str, Any]: Dictionary with extracted components based on request type: - For DataFrame requests: {"suggestions": str} - For image requests: {"analysis": str, "code": str, "path": str} - For chart requests: {"code": str, "path": str} |
Source code in src/chai/chAI.py
set_agent_executor(verbose=False, handle_parse=True)
¶
Sets up the LangChain agent executor with specified tools and configurations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
verbose
|
bool
|
Enable verbose output. Defaults to False. |
False
|
handle_parse
|
bool
|
Enable parsing error handling. Defaults to True. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
AgentExecutor |
AgentExecutor
|
Configured agent executor instance. |
Raises:
| Type | Description |
|---|---|
Exception
|
If there's an error setting up the agent executor. |
Source code in src/chai/chAI.py
steep(data=None, prompt=None, image_path=None, chart_type=None, **kwargs)
¶
Processes user requests based on input type and generates appropriate visualisations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Optional[DataFrame]
|
Input data for analysis. |
None
|
prompt
|
Optional[str]
|
User instructions for visualisation. |
None
|
image_path
|
Optional[Union[str, Path]]
|
Path to image for analysis. |
None
|
chart_type
|
Optional[ChartType]
|
Specific chart type from ChartType enum. |
None
|
**kwargs
|
Any
|
Additional keyword arguments for the LLM. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
ChAITeapot |
ChAITeapot
|
A response object that: - Can be printed as a string to show the full response - Has structured components directly accessible: - For DataFrame and visualisation requests: .suggestions - For image requests: .analysis, .code, .path - For chart requests: .code, .path |
Raises:
| Type | Description |
|---|---|
ChAIError
|
If there's an error processing the request. |
ValueError
|
If no valid input is provided. |
Notes
- Handles different input types (DataFrame, image, chart type)
- Limits DataFrame processing to 100 rows
- Uses appropriate templates based on chart type specified
- Saves visualisations to specified output path
Source code in src/chai/chAI.py
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 | |