cuery.task#
Task and Chain classes encapsulating the execution of prompts over contexts.
Attributes#
Classes#
A simple logger to count parsing errors. |
|
A simple logger to store query parameters. |
|
A task that can be executed with a prompt and a response model. |
|
Chain multiple tasks together. |
Functions#
|
Check if the model name is valid (provider/model format). |
Module Contents#
- cuery.task.AnyCfg#
- cuery.task.check_model_name(model)#
Check if the model name is valid (provider/model format).
- Parameters:
model (str)
- Return type:
None
- class cuery.task.ErrorLogger#
Bases:
listA simple logger to count parsing errors.
- log(error)#
- Parameters:
error (Exception)
- Return type:
None
- class cuery.task.QueryLogger#
Bases:
listA simple logger to store query parameters.
- log(*args, **kwargs)#
Log a query to the internal list.
- Return type:
None
- class cuery.task.Task(prompt, response, name=None, model=None, log_prompt=False, log_response=False)#
A task that can be executed with a prompt and a response model.
Tasks can be registered by name and can be called with a context to get a response. The output is always ResponseSet that contains one Reponse for each item in the iterable context.
- Parameters:
prompt (str | pathlib.Path | cuery.prompt.Prompt)
response (cuery.response.ResponseClass)
name (str | None)
model (str | None)
log_prompt (bool)
log_response (bool)
- name = None#
- response#
- prompt#
- log_prompt = False#
- log_response = False#
- errors#
- queries#
- _select_client(model=None)#
- Parameters:
model (str | None)
- Return type:
instructor.Instructor
- reset_loggers(client)#
Reset the error and query loggers.
- Parameters:
client (instructor.Instructor)
- Return type:
None
- async call(context=None, model=None, **kwds)#
Call the task with a single context item (no iteration).
- Parameters:
context (cuery.context.AnyContext | None)
model (str | None)
- Return type:
- async iter(context=None, model=None, callback=None, progress_callback=None, **kwds)#
Iterate the prompt over items in the context.
This is useful when subsequent calls depend on the previous response, and you thus cannot parallelize the calls.
The callback can be used to process each response as it is generated and to perform any additional actions, such as logging or updating the prompt for the next call.
- Parameters:
context (cuery.context.AnyContext | None)
model (str | None)
callback (collections.abc.Callable[[cuery.response.Response, cuery.prompt.Prompt, dict], None] | None)
progress_callback (collections.abc.Callable | None)
- Return type:
- async gather(context=None, model=None, n_concurrent=1, progress_callback=None, **kwds)#
Gather multiple calls to the task in parallel.
This is useful when the calls are independent and can be parallelized. The n_concurrent parameter controls how many calls can be made in parallel.
- Parameters:
context (cuery.context.AnyContext | None)
model (str | None)
n_concurrent (int)
progress_callback (collections.abc.Callable | None)
- Return type:
- async __call__(context=None, model=None, n_concurrent=1, **kwds)#
Dispatch to appropriate method (call/iter/gather) based on context and concurrency.
- Parameters:
context (cuery.context.AnyContext | None)
model (str | None)
n_concurrent (int)
- Return type:
- classmethod from_config(prompt, response)#
Create a Task from configuration.
- Parameters:
prompt (AnyCfg)
response (AnyCfg)
- Return type:
- __rich_console__(console, options)#
Render the task as a rich panel.
- Parameters:
console (cuery.pretty.Console)
options (cuery.pretty.ConsoleOptions)
- Return type:
cuery.pretty.RenderResult
- class cuery.task.Chain(*tasks)#
Chain multiple tasks together.
The output of each task is auto-converted to a DataFrame and passed to the next task as input context.
The return value of the chain is the result of successively joining each task’s output DataFrame with the previous one, using the corresponding prompt’s variables as join keys.
- Parameters:
tasks (list[Task])
- tasks = ()#
- async __call__(context=None, **kwds)#
Run the chain of tasks sequentially.
- Parameters:
context (cuery.context.AnyContext | None)
- Return type:
pandas.DataFrame