Pkg.AIAssistant — API for using Large Language Models¶
Overview¶
The Pkg.AIAssistant module contains the API for using Large Language Models (LLMs).
This module offers both an abstract API to process tasks using a queue system as well a way to provide AI-driven analysis for file formats.
Processing AI Tasks¶
The following code example demonstrates how to process a simple task.
from Pkg.AIAssistant import *
import time
def simpleAIQueue():
queue = AIQueue()
task = AITask(1, "Test task")
task.setInstructions("Answer the question.")
task.addInput("How are you?")
queue.addTask(task)
while not queue.process():
time.sleep(0.1)
output = queue.getOutput()
print(output)
AI Analysis¶
The package allows to install custom handlers for formats not internally supported.
In order to create a custom handler it is necessary to create an entry in the ‘ai_analysis.cfg’ configuration file.
This is the entry for the “LNK” format:
[Windows LNK]
format = LNK
file = Pkg.AIAssistant.Handlers.Lnk.pyc
handler = WindowsLnkAIAnalysisHandler
‘handler’ specifies the name of the handler class in ‘file’.
The following is the handler for the LNK format:
from Pro.Core import *
from Pkg.AIAssistant import AIAnalysisHandler, AITask
class WindowsLnkAIAnalysisHandler(AIAnalysisHandler):
def initTasks(self, *, wo=None):
t = AITask(1, "Analyze Windows link")
return [t]
def loadTask(self, tasks, task, *, wo=None):
id = task.getId()
if id == 1:
self.setDefaultInstructions(task)
out = NTTextBuffer()
self.getObject().Dump(out)
task.addInput("Analyze the provided Windows Link for potential malware.")
task.addInput(out.buffer)
return True
return False
The LNK format only requires a single analysis task to be completed. However, many file formats require several tasks to be completed and summarized. Here is an abridged example demonstrating how to summarize multiple tasks:
class ComplexAIAnalysisHandler(AIAnalysisHandler):
First_TaskId = 1
Second_TaskId = 2
# etc.
Report_TaskId = 100
def initTasks(self, *, wo=None):
l = []
l.append(AITask(self.First_TaskId, "Analyze first"))
l.append(AITask(self.Second_TaskId, "Analyze second", optional=True))
# etc.
l.append(AITask(self.Report_TaskId, "Create report", priority=1000))
return l
def loadTask(self, tasks, task, *, wo=None):
id = task.getId()
if id == self.First_TaskId:
self.setDefaultInstructions(task, mode=self.PARTIAL)
# TODO: add input
return True
elif id == self.Second_TaskId:
self.setDefaultInstructions(task, mode=self.PARTIAL)
# TODO: add input
return True
elif id == self.Report_TaskId:
self.setDefaultInstructions(task, mode=self.SUMMARY)
return self.createSummary(tasks, task)
return False
Module API¶
Pkg.AIAssistant module API.
Classes:
Base class for analysis handlers that define AI-driven task workflows for a scanned object.
Describes a registered AI analysis handler for a specific format.
AIQueue(*, user_data, manager, model, …)A queue that manages and executes
AITaskinstances using an AI model.
AIQueueView(queue, *, helper)View to display an
AIQueue.Helper interface used by
AIQueueView.This class describes an AI service provider (name, credentials, default model).
AITask(id, label, *, priority, optional, enabled)Represents a single AI task to be executed by a queue (prompt + inputs).
Functions:
Retrieves the registry of available AI analysis handlers.
getAIServiceCredentials(service)Retrieves credentials (API key) for a given AI service.
Selects an appropriate model automatically based on available services.
getAvailableAIModels(*[, service, wo])Lists available AI models, optionally filtered by service.
Returns the names of AI services that are currently usable (credentials present).
Returns the global default AI model if one is available; otherwise returns
None.
getDefaultAIModelForService(service)Retrieves the default model for a specific AI service.
Returns a list of supported AI services.
hasAIAnalysisHandler(handlers, format)Checks whether a handler exists for the specified format.
initAIAnalysisHandler(handlers, sprovider)Initializes an
AIAnalysisHandlerinstance suitable for the givenScanProvider.
isAIServiceAvailable(service)Checks whether the given AI service can be used (e.g., credentials present).
- class AIAnalysisHandler¶
Base class for analysis handlers that define AI-driven task workflows for a scanned object.
Attributes:
Partial analysis task mode (one of many contributing tasks).
Single-task mode producing a complete report.
Summary mode that aggregates outputs of partial tasks.
Methods:
addJSONInput(task, js)Adds a JSON payload as input to a task.
createSummary(tasks, task, *[, minimum_priority])Builds a summary output from a set of partial tasks into the provided summary task.
Returns the object being analyzed.
getReport(tasks, *[, wo])Produces a final report from task results.
Returns the scan provider instance.
getTasks(*[, wo])Creates the set of tasks to be executed for this analysis.
initTasks(*[, wo])Initializes and returns the base set of tasks for this handler.
loadTask(tasks, task, *[, wo])Loads data and context into a specific task before processing.
setDefaultInstructions(task, *[, mode])Applies default instructions to a task based on the chosen mode.
structToJSON(s)Serializes a
CFFStructto a JSON-serializable dictionary.
- PARTIAL¶
Partial analysis task mode (one of many contributing tasks).
- REPORT¶
Single-task mode producing a complete report.
- SUMMARY¶
Summary mode that aggregates outputs of partial tasks.
- addJSONInput(task: Pkg.AIAssistant.AITask, js: Union[Dict[Any, Any], str])¶
Adds a JSON payload as input to a task.
- Parameters
task (AITask) – The target task.
js (Union[Dict[Any, Any], str]) – The JSON-like mapping or string to attach.
See also
structToJSON().
- createSummary(tasks: List[Pkg.AIAssistant.AITask], task: Pkg.AIAssistant.AITask, *, minimum_priority: int = 0) → bool¶
Builds a summary output from a set of partial tasks into the provided summary task.
- getObject() → Pro.Core.CFFObject¶
- Returns
Returns the object being analyzed.
- Return type
See also
getScanProvider().
- getReport(tasks: List[Pkg.AIAssistant.AITask], *, wo: Optional[Pro.Core.NTIWait] = None) → Optional[str]¶
Produces a final report from task results.
Note
Most handlers can rely on the default implementation of this method, unless they need to customize the behavior.
- getScanProvider() → Pro.Core.ScanProvider¶
- Returns
Returns the scan provider instance.
- Return type
See also
getObject().
- getTasks(*, wo: Optional[Pro.Core.NTIWait] = None) → List[Pkg.AIAssistant.AITask]¶
Creates the set of tasks to be executed for this analysis.
Hint
This method internally calls
initTasks().
- Parameters
wo (Optional[NTIWait]) – Optional wait object.
- Returns
Returns a list of prepared
AITaskinstances.- Return type
List[AITask]
See also
initTasks().
- initTasks(*, wo: Optional[Pro.Core.NTIWait] = None) → List[Pkg.AIAssistant.AITask]¶
Initializes and returns the base set of tasks for this handler.
- Parameters
wo (Optional[NTIWait]) – Optional wait object.
- Returns
Returns list of newly created
AITaskinstances.- Return type
List[AITask]
See also
loadTask().
- loadTask(tasks: List[Pkg.AIAssistant.AITask], task: Pkg.AIAssistant.AITask, *, wo: Optional[Pro.Core.NTIWait] = None) → bool¶
Loads data and context into a specific task before processing.
- Parameters
- Returns
Returns
Trueif the task was prepared; otherwise returnsFalse.- Return type
bool
See also
initTasks().
- setDefaultInstructions(task: Pkg.AIAssistant.AITask, *, mode: int = REPORT)¶
Applies default instructions to a task based on the chosen mode.
- structToJSON(s: Pro.Core.CFFStruct) → Dict[str, Any]¶
Serializes a
CFFStructto a JSON-serializable dictionary.
- Parameters
s (CFFStruct) – The structure to serialize.
- Returns
Returns a dictionary suitable for JSON encoding.
- Return type
Dict[str, Any]
See also
addJSONInput().
- class AIAnalysisHandlerInfo¶
Describes a registered AI analysis handler for a specific format.
Attributes:
The file that defines the handler.
The format handled by this handler.
The fully qualified class name implementing
AIAnalysisHandler.A human-friendly handler name.
- file¶
The file that defines the handler.
- format¶
The format handled by this handler.
- handler¶
The fully qualified class name implementing
AIAnalysisHandler.
- name¶
A human-friendly handler name.
- class AIQueue(*, user_data: Optional[object] = None, manager: Optional[Pro.MP.ProManager] = None, model: Optional[object] = None, api_key: Optional[str] = None, verbose: bool = True)¶
A queue that manages and executes
AITaskinstances using an AI model.
- Parameters
user_data (Optional[object]) – Optional caller-provided context object.
manager (Optional[ProManager]) – Optional user-provided
ProManagerfor multi-processing.model (Optional[object]) – Optional model descriptor to use initially.
api_key (Optional[str]) – Optional API key override.
verbose (bool) – Enable verbose logging.
Methods:
abort()Requests cancellation of the current processing session.
addTask(task)Adds a single task the list.
addTasks(tasks)Adds multiple tasks the list.
getModel()Returns the current model descriptor.
getOutput(*[, wo])Retrieves the final output produced by the queue.
getTasks()Returns the list of tasks.
Returns the user data associated with this queue.
Returns
Trueif processing is complete; otherwise returnsFalse.Returns
Trueif processing is ongoing; otherwise returnsFalse.
prepareTasks(*[, wo, priority, max_count])Prepares tasks for processing.
process(*[, wo])Processes tasks asynchronously, honoring their priority.
reset()Resets the queue to its initial state, clearing results and statuses.
setModel(model)Sets the model descriptor to be used for subsequent processing.
- addTask(task: Pkg.AIAssistant.AITask)¶
Adds a single task the list.
- Parameters
task (AITask) – The task to add.
See also
addTasks().
- addTasks(tasks: List[Pkg.AIAssistant.AITask])¶
Adds multiple tasks the list.
- Parameters
tasks (List[AITask]) – The tasks to add.
See also
addTask().
- getModel() → object¶
- Returns
Returns the current model descriptor.
- Return type
object
See also
setModel().
- getOutput(*, wo: Optional[Pro.Core.NTIWait] = None) → Optional[str]¶
Retrieves the final output produced by the queue.
- Parameters
wo (Optional[NTIWait]) – Optional wait object.
- Returns
Returns the output if available; otherwise returns
None.- Return type
Optional[str]
See also
process().
- getTasks() → List[Pkg.AIAssistant.AITask]¶
- Returns
Returns the list of tasks.
- Return type
List[AITask]
See also
addTask()andaddTasks().
- getUserData() → object¶
- Returns
Returns the user data associated with this queue.
- Return type
object
- hasFinished() → bool¶
- Returns
Returns
Trueif processing is complete; otherwise returnsFalse.- Return type
bool
See also
process()andisProcessing().
- isProcessing() → bool¶
- Returns
Returns
Trueif processing is ongoing; otherwise returnsFalse.- Return type
bool
See also
process()andhasFinished().
- prepareTasks(*, wo: Optional[Pro.Core.NTIWait] = None, priority: int = 0, max_count: int = - 1)¶
Prepares tasks for processing.
Note
Tasks are prepared when
process()is called. This method offers the possibility to prepare some tasks before callingprocess().
- Parameters
wo (Optional[NTIWait]) – Optional wait object.
priority (int) – Minimum priority threshold to include.
max_count (int) – Maximum number of tasks to prepare, or
-1for all.
- process(*, wo: Optional[Pro.Core.NTIWait] = None) → bool¶
Processes tasks asynchronously, honoring their priority.
Hint
This method is designed to be called repeatedly in a loop until it returns
True
- Parameters
wo (Optional[NTIWait]) – Optional wait object.
- Returns
Returns
Falsewhile there is still work to do, andTrueonce all tasks have finished.- Return type
bool
See also
isProcessing(),hasFinished()andgetOutput().
- setModel(model: object)¶
Sets the model descriptor to be used for subsequent processing.
- Parameters
model (object) – The model object to configure.
See also
getModel().
- class AIQueueView(queue: Pkg.AIAssistant.AIQueue, *, helper: Optional[Pkg.AIAssistant.AIQueueViewHelper] = None)¶
View to display an
AIQueue.Initializes an
AIQueueView.
- Parameters
queue (AIQueue) – The underlying queue instance.
helper (Optional[AIQueueViewHelper]) – Optional helper to manage persistence and lifecycle events.
Methods:
setup(sdata)Binds the view to the provided
ScanViewData.
- setup(sdata: Pro.Core.ScanViewData) → bool¶
Binds the view to the provided
ScanViewData.
- Parameters
sdata (ScanViewData) – The scan view data object.
- Returns
Returns
Trueif successful; otherwise returnsFalse.- Return type
bool
- class AIQueueViewHelper¶
Helper interface used by
AIQueueView.Methods:
closing()Called when the view is closing.
fetchOutput(model)Allows the helper to provide preexisting output for the given model (if any).
saveOutput(model, output)Allows the helper to persist output produced by the queue.
- closing()¶
Called when the view is closing.
- fetchOutput(model: object) → Optional[str]¶
Allows the helper to provide preexisting output for the given model (if any).
- Parameters
model (object) – The model descriptor currently selected.
- Returns
Returns the output if available; otherwise returns
None.- Return type
Optional[str]
See also
saveOutput().
- saveOutput(model: object, output: str)¶
Allows the helper to persist output produced by the queue.
- Parameters
model (object) – The model descriptor currently selected.
output (str) – The output text to save.
See also
fetchOutput().
- class AIService¶
This class describes an AI service provider (name, credentials, default model).
Attributes:
The service’s default model identifier (if any).
The environment variable name used to retrieve the API key for this service.
The display name of the AI service (e.g.,
"OpenAI","Claude").Methods:
Retrieves the API key associated with this service (typically from an environment variable).
- default_model¶
The service’s default model identifier (if any).
- env_key_name¶
The environment variable name used to retrieve the API key for this service.
- getAPIKey() → str¶
Retrieves the API key associated with this service (typically from an environment variable).
- Returns
The API key string, or an empty string if not available.
- Return type
str
- name¶
The display name of the AI service (e.g.,
"OpenAI","Claude").
- class AITask(id: int, label: str, *, priority: int = 0, optional: bool = False, enabled: bool = True)¶
Represents a single AI task to be executed by a queue (prompt + inputs).
- Parameters
id (int) – Unique identifier for the task.
label (str) – Human-readable label for display/logging.
priority (int) – Optional priority (higher values indicate higher priority).
optional (bool) – If
True, makes the task optional.enabled (bool) – If the task is optional, it can be enabled or disabled using this parameter.
Attributes:
Input role indicating assistant-provided content.
Task failed with an error.
No status set.
Task is queued but not yet running.
Task is currently being processed.
Task was skipped (e.g., due to a load error).
Task completed successfully.
Task exceeded the configured time limit.
Input role indicating user-provided content.
Methods:
addInput(content, *[, type, role])Adds an input message to the task.
Returns the elapsed processing time for this task in milliseconds.
getError()Returns the error message, if any; otherwise returns an empty string.
getId()Returns the task id.
getLabel()Returns the task label.
Returns the task output produced by the AI model if the status is
SUCCEEDED; otherwise returns an empty string.Returns the task priority.
Returns
Trueif the task is enabled; otherwise returnsFalse.Returns
Trueif the task is optional; otherwise returnsFalse.
setEnabled(enabled)Sets whether the task is enabled or not.
setInstructions(text)Sets the system/instruction prompt for this task.
- ASSISTANT¶
Input role indicating assistant-provided content.
- FAILED¶
Task failed with an error.
- NONE¶
No status set.
- PENDING¶
Task is queued but not yet running.
- RUNNING¶
Task is currently being processed.
- SKIPPED¶
Task was skipped (e.g., due to a load error).
- SUCCEEDED¶
Task completed successfully.
- TIMEDOUT¶
Task exceeded the configured time limit.
- USER¶
Input role indicating user-provided content.
- addInput(content: str, *, type: str = 'text', role: int = USER)¶
Adds an input message to the task.
- Parameters
See also
setInstructions().
- getElapsed() → int¶
- Returns
Returns the elapsed processing time for this task in milliseconds.
- Return type
int
- getError() → str¶
- Returns
Returns the error message, if any; otherwise returns an empty string.
- Return type
str
- getId() → int¶
- Returns
Returns the task id.
- Return type
int
- getLabel() → str¶
- Returns
Returns the task label.
- Return type
str
- getOutput() → str¶
- Returns
Returns the task output produced by the AI model if the status is
SUCCEEDED; otherwise returns an empty string.- Return type
str
- getPriority() → int¶
- Returns
Returns the task priority.
- Return type
int
- getStatus() → int¶
- isEnabled() → bool¶
- Returns
Returns
Trueif the task is enabled; otherwise returnsFalse.- Return type
bool
See also
setEnabled().
- isOptional() → bool¶
- Returns
Returns
Trueif the task is optional; otherwise returnsFalse.- Return type
bool
- setEnabled(enabled: bool)¶
Sets whether the task is enabled or not.
Note
Only optional tasks can be disabled.
- Parameters
enabled (bool) – If
Trueenables the task; otherwise disables it.See also
isEnabled().
- setInstructions(text: str)¶
Sets the system/instruction prompt for this task.
- Parameters
text (str) – Instruction text guiding model behavior.
See also
addInput().
- getAIAnalysisHanlders() → Dict[str, Pkg.AIAssistant.AIAnalysisHandlerInfo]¶
Retrieves the registry of available AI analysis handlers.
- Returns
Returns a mapping of format identifiers to
AIAnalysisHandlerInfo.- Return type
Dict[str, AIAnalysisHandlerInfo]
See also
hasAIAnalysisHandler()andinitAIAnalysisHandler().
- getAIServiceCredentials(service: Union[str, Pkg.AIAssistant.AIService]) → Optional[str]¶
Retrieves credentials (API key) for a given AI service.
- Parameters
service (Union[str, AIService]) – The service name or
AIServiceinstance.- Returns
Returns the API key string if available; otherwise returns
None.- Return type
Optional[str]
See also
isAIServiceAvailable()andAIService.getAPIKey().
- getAutoAIModel() → Optional[object]¶
Selects an appropriate model automatically based on available services.
- Returns
Returns a model descriptor if a suitable model is found; otherwise returns
None.- Return type
Optional[object]
See also
getAvailableAIModels().
- getAvailableAIModels(*, service: Optional[str] = None, wo: Optional[Pro.Core.NTIWait] = None) → List[object]¶
Lists available AI models, optionally filtered by service.
- Parameters
service (Optional[str]) – Optional service name to filter by.
wo (Optional[NTIWait]) – Optional wait object.
- Returns
Returns a list of model descriptors (opaque objects).
- Return type
List[object]
See also
getDefaultAIModelForService()andgetAutoAIModel().
- getAvailableAIServices() → List[str]¶
- Returns
Returns the names of AI services that are currently usable (credentials present).
- Return type
List[str]
See also
isAIServiceAvailable()andgetSupportedAIServices().
- getDefaulAIModel() → Optional[object]¶
- Returns
Returns the global default AI model if one is available; otherwise returns
None.- Return type
Optional[object]
See also
getAutoAIModel()andgetDefaultAIModelForService().
- getDefaultAIModelForService(service: str) → Optional[object]¶
Retrieves the default model for a specific AI service.
- Parameters
service (str) – The service name.
- Returns
Returns a model descriptor if available; otherwise returns
None.- Return type
Optional[object]
- getSupportedAIServices() → List[Pkg.AIAssistant.AIService]¶
- Returns
Returns a list of supported AI services.
- Return type
List[AIService]
See also
getAvailableAIServices()andisAIServiceAvailable()
- hasAIAnalysisHandler(handlers: Dict[str, Pkg.AIAssistant.AIAnalysisHandlerInfo], format: str) → bool¶
Checks whether a handler exists for the specified format.
- Parameters
handlers (Dict[str, AIAnalysisHandlerInfo]) – The handler registry returned by
getAIAnalysisHanlders().format (str) – Format identifier to look up.
- Returns
Returns
Trueif a handler is available; otherwise returnsFalse.- Return type
bool
See also
getAIAnalysisHanlders()andinitAIAnalysisHandler().
- initAIAnalysisHandler(handlers: Dict[str, Pkg.AIAssistant.AIAnalysisHandlerInfo], sprovider: Pro.Core.ScanProvider) → Optional[Pkg.AIAssistant.AIAnalysisHandler]¶
Initializes an
AIAnalysisHandlerinstance suitable for the givenScanProvider.
- Parameters
handlers (Dict[str, AIAnalysisHandlerInfo]) – The handler registry returned by
getAIAnalysisHanlders().sprovider (ScanProvider) – The scan provider.
- Returns
Returns an initialized analysis handler if successful; otherwise returns
None.- Return type
Optional[AIAnalysisHandler]
See also
getAIAnalysisHanlders()andhasAIAnalysisHandler().
- isAIServiceAvailable(service: Union[str, Pkg.AIAssistant.AIService]) → bool¶
Checks whether the given AI service can be used (e.g., credentials present).
- Parameters
service (Union[str, AIService]) – The service name or
AIServiceinstance.- Returns
Returns
Trueif the service is available; otherwise returnsFalse.- Return type
bool
See also
getAvailableAIServices().