ontocast.tool.llm¶
Language Model (LLM) integration tool for OntoCast.
This module provides integration with various language models through LangChain, supporting both OpenAI and Ollama providers. It enables text generation and structured data extraction capabilities with optional caching support.
Cache Usage
The LLM tool supports caching of responses to avoid redundant API calls. Caching uses a shared Cacher instance that manages cache directories for all tools. The cache directory is managed by the shared Cacher class and follows these rules:
from ontocast.tool.llm import LLMTool
from ontocast.config import LLMConfig
from ontocast.tool.cache import Cacher
# Create shared cache instance
shared_cache = Cacher()
# Create LLM tool with shared cache
llm_tool = await LLMTool.acreate(
config=LLMConfig(...),
cache=shared_cache
)
Default cache locations: - Tests: .test_cache/llm/ in the current working directory - Windows: %USERPROFILE%AppDataLocalontocastllm - Unix/Linux: ~/.cache/ontocast/llm/ (or $XDG_CACHE_HOME/ontocast/llm/)
Cache files are stored as JSON files with filenames based on SHA256 hashes of the prompt and LLM configuration. This ensures that identical prompts with the same configuration will return cached responses.
The shared Cacher automatically manages subdirectories for different tools, ensuring organized cache storage while maintaining a single cache instance.
LLMTool
¶
Bases: Tool
Tool for interacting with language models.
This class provides a unified interface for working with different language model providers (OpenAI, Ollama) through LangChain. It supports both synchronous and asynchronous operations.
Attributes:
| Name | Type | Description |
|---|---|---|
config |
LLMConfig
|
LLMConfig object containing all LLM settings. |
cache |
Any
|
Cacher instance for caching LLM responses. |
Source code in ontocast/tool/llm.py
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 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 | |
llm
property
¶
Get the underlying language model instance.
Returns:
| Name | Type | Description |
|---|---|---|
BaseChatModel |
BaseChatModel
|
The configured language model. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the LLM has not been properly initialized. |
__call__(*args, **kwds)
async
¶
Call the language model directly (asynchronous).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
Any
|
Positional arguments passed to the LLM. |
()
|
**kwds
|
Any
|
Keyword arguments passed to the LLM. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The LLM's response. |
Source code in ontocast/tool/llm.py
__init__(cache=None, budget_tracker=None, **kwargs)
¶
Initialize the LLM tool.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache
|
Cacher | None
|
Optional shared Cacher instance. If None, creates a new one. |
None
|
budget_tracker
|
Any
|
Optional budget tracker instance for usage statistics. |
None
|
**kwargs
|
Additional keyword arguments passed to the parent class. |
{}
|
Source code in ontocast/tool/llm.py
acall(*args, **kwds)
async
¶
Call the language model directly (asynchronous).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
Any
|
Positional arguments passed to the LLM. |
()
|
**kwds
|
Any
|
Keyword arguments passed to the LLM. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The LLM's response. |
Source code in ontocast/tool/llm.py
acreate(config, cache=None, budget_tracker=None, **kwargs)
async
classmethod
¶
Create a new LLM tool instance asynchronously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
LLMConfig
|
LLMConfig object containing LLM settings. |
required |
cache
|
Cacher | None
|
Optional shared Cacher instance. |
None
|
budget_tracker
|
Any
|
Optional budget tracker instance for usage statistics. |
None
|
**kwargs
|
Additional keyword arguments for initialization. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
LLMTool |
A new instance of the LLM tool. |
Source code in ontocast/tool/llm.py
complete(prompt, **kwargs)
async
¶
Generate a completion for the given prompt.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
str
|
The input prompt for generation. |
required |
**kwargs
|
Additional keyword arguments for generation. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The generated completion. |
Source code in ontocast/tool/llm.py
create(config, cache=None, budget_tracker=None, **kwargs)
classmethod
¶
Create a new LLM tool instance synchronously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
LLMConfig
|
LLMConfig object containing LLM settings. |
required |
cache
|
Cacher | None
|
Optional shared Cacher instance. |
None
|
budget_tracker
|
Any
|
Optional budget tracker instance for usage statistics. |
None
|
**kwargs
|
Additional keyword arguments for initialization. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
LLMTool |
A new instance of the LLM tool. |
Source code in ontocast/tool/llm.py
extract(prompt, output_schema, **kwargs)
async
¶
Extract structured data from the prompt according to a schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
str
|
The input prompt for extraction. |
required |
output_schema
|
Type[T]
|
The Pydantic model class defining the output structure. |
required |
**kwargs
|
Additional keyword arguments for extraction. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
T |
T
|
The extracted data conforming to the output schema. |
Source code in ontocast/tool/llm.py
setup()
async
¶
Set up the language model based on the configured provider.
Raises:
| Type | Description |
|---|---|
ValueError
|
If the provider is not supported. |
Source code in ontocast/tool/llm.py
track_llm_usage(func)
¶
Decorator to track LLM usage automatically.