hbllmutils.model.task

This module provides an abstract base class for LLM (Large Language Model) tasks.

It defines the LLMTask class which acts as a wrapper around LLMModel and LLMHistory, providing convenient methods for asking questions and streaming responses while maintaining conversation history.

LLMTask

class hbllmutils.model.task.LLMTask(model: LLMModel, history: LLMHistory | None = None)[source]

Abstract base class for LLM tasks that manages model interactions and conversation history.

This class provides a high-level interface for interacting with language models, handling both standard and streaming responses while maintaining conversation context.

Parameters:
  • model (LLMModel) – The LLM model instance to use for generating responses.

  • history (Optional[LLMHistory]) – Optional conversation history. If not provided, a new empty history is created.

Variables:
  • model (LLMModel) – The LLM model instance.

  • history (LLMHistory) – The conversation history.

__eq__(other) bool[source]

Check equality between this LLMTask and another object.

Two LLMTask instances are considered equal if they have the same class type and the same model and history parameters.

Parameters:

other (object) – The object to compare with.

Returns:

True if the objects are equal, False otherwise.

Return type:

bool

Example::
>>> task1 = LLMTask(model, history)
>>> task2 = LLMTask(model, history)
>>> task1 == task2
True
__hash__() int[source]

Get the hash value of this LLMTask instance.

The hash is computed based on the class type and the model and history parameters.

Returns:

The hash value.

Return type:

int

Example::
>>> task = LLMTask(model, history)
>>> hash(task)
1234567890
__init__(model: LLMModel, history: LLMHistory | None = None)[source]

Initialize the LLMTask with a model and optional history.

Parameters:
  • model (LLMModel) – The LLM model instance to use for generating responses.

  • history (Optional[LLMHistory]) – Optional conversation history. If not provided, a new empty history is created.

ask(input_content: str | None = None, with_reasoning: bool = False, **params) str | Tuple[str | None, str][source]

Ask a question to the LLM model and get a response.

This method sends the current conversation history to the model and retrieves a response. The response format depends on the with_reasoning parameter.

Parameters:
  • input_content (Optional[str]) – Optional user input content to add to the history before asking. If None, uses the existing history without modification.

  • with_reasoning (bool) – If True, returns both reasoning and response as a tuple. If False, returns only the response string.

  • params (dict) – Additional parameters to pass to the model’s ask method.

Returns:

If with_reasoning is False, returns the response string. If with_reasoning is True, returns a tuple of (reasoning, response).

Return type:

Union[str, Tuple[Optional[str], str]]

Example::
>>> task = LLMTask(model)
>>> response = task.ask("What is the weather today?")
>>> print(response)
'The weather is sunny today.'
>>> reasoning, response = task.ask("Explain quantum physics", with_reasoning=True)
>>> print(f"Reasoning: {reasoning}, Response: {response}")
Reasoning: Let me break this down step by step..., Response: Quantum physics is...
ask_stream(input_content: str | None = None, with_reasoning: bool = False, **params) ResponseStream[source]

Ask a question to the LLM model and get a streaming response.

This method sends the current conversation history to the model and retrieves a streaming response, allowing for real-time processing of the model’s output.

Parameters:
  • input_content (Optional[str]) – Optional user input content to add to the history before asking. If None, uses the existing history without modification.

  • with_reasoning (bool) – If True, the stream includes reasoning information. If False, only the response is streamed.

  • params (dict) – Additional parameters to pass to the model’s ask_stream method.

Returns:

A ResponseStream object that can be iterated to receive response chunks.

Return type:

ResponseStream

Example::
>>> task = LLMTask(model)
>>> stream = task.ask_stream("Tell me a story")
>>> for chunk in stream:
...     print(chunk, end='', flush=True)
Once upon a time, there was...