cuery#
Submodules#
Attributes#
Classes#
Message class for chat completions. |
|
Prompt class for chat completions. |
|
Base class for all response models. |
|
A collection of responses |
|
Chain multiple tasks together. |
|
A task that can be executed with a prompt and a response model. |
|
Base class for all tools. |
Functions#
Package Contents#
- cuery.AnyContext#
- class cuery.Message(/, **data)#
Bases:
pydantic.BaseModelMessage class for chat completions.
- Parameters:
data (Any)
- content: str#
- role: str = 'user'#
- __rich_console__(console, options)#
- Parameters:
console (cuery.pretty.Console)
options (cuery.pretty.ConsoleOptions)
- Return type:
cuery.pretty.RenderResult
- class cuery.Prompt(/, **data)#
Bases:
pydantic.BaseModelPrompt class for chat completions.
This class represents a chat prompt consisting of multiple messages. Each message can have a role (e.g., user, assistant) and content. It can be constructed manually or from a configuration file or a string. In the latter case, automatically detects the required variables used by the Jinja template, if any.
- Parameters:
data (Any)
- required: list[str] = None#
- classmethod validate_messages(messages)#
Allow init from other types.
- Return type:
list
- check_required()#
- validate_required()#
Validate that all required variables are present in the prompt.
- __iter__()#
So dict(model) works.
- substitute(**kwds)#
- render(with_roles=False, **kwds)#
Render the prompt messages into single string with the given variables.
Not usually needed as Task, Tools etc. will do this automatically.
- Parameters:
with_roles (bool)
- Return type:
str
- __rich_console__(console, options)#
- Parameters:
console (cuery.pretty.Console)
options (cuery.pretty.ConsoleOptions)
- Return type:
cuery.pretty.RenderResult
- class cuery.Response(/, **data)#
Bases:
pydantic.BaseModelBase class for all response models.
Adds functionality to cache the raw response from the API call, calculate token usage, and to create a fallback instance, which by default is an empty model with all fields set to None.
Also implements rich’s console protocol for pretty printing of the model’s fields, and allows inspection of the model’s fields to determine if it has a single multivalued field (a list) or not (which can be used to automatically “explode” items into DataFrame rows e.g.).
- Parameters:
data (Any)
- model_config#
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- _raw_response: Any | None = None#
- token_usage()#
Get the token usage from the raw response.
- Return type:
dict | None
- to_dict()#
Convert the model to a dictionary.
- Return type:
dict
- classmethod iterfield()#
Check if a pydantic model has a single field that is a list.
- Return type:
str | None
- classmethod is_multivalued()#
Check if a pydantic model has a single field that is a list.
- Return type:
bool
- static from_dict(name, fields)#
Create an instance of the model from a dictionary.
- Parameters:
name (str)
fields (dict)
- Return type:
ResponseClass
- classmethod from_config(source, *keys)#
Create an instance of the model from a configuration dictionary.
- Parameters:
source (str | pathlib.Path | dict)
keys (list)
- Return type:
ResponseClass
- __rich_console__(console, options)#
- Parameters:
console (cuery.pretty.Console)
options (cuery.pretty.ConsoleOptions)
- Return type:
cuery.pretty.RenderResult
- cuery.ResponseClass#
- class cuery.ResponseSet(responses, context, required)#
A collection of responses
This class is used to manage multiple responses, allowing iteration over them, conversion to records or DataFrame, and calculating token usage across all responses.
- Parameters:
- responses#
- context#
- required#
- iterfield#
- __iter__()#
- __len__()#
- static to_dict(item, fallback_name=None)#
Convert an item to a dictionary.
If the item is not a dict-like object, return a fallback dict if a fallback name is provided, otherwise return the item as is.
- Parameters:
item (Any)
fallback_name (str | None)
- Return type:
Any
- to_records(explode=True)#
Convert to list of dicts, optionally with original context merged in.
- Parameters:
explode (bool)
- Return type:
list[dict] | pandas.DataFrame
- to_pandas(explode=True, normalize=True, prefix=None)#
Convert list of responses to DataFrame.
- Parameters:
explode (bool)
normalize (bool)
prefix (str | None)
- Return type:
pandas.DataFrame
- usage()#
Get the token usage for all responses.
- Return type:
pandas.DataFrame
- __str__()#
- Return type:
str
- __repr__()#
- Return type:
str
- class cuery.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
- class cuery.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.Tool(/, **data)#
Bases:
cuery.utils.Configurable,abc.ABCBase class for all tools.
Subclasses need to implement prompt and response models; either statically as ClassVars, or dynamically as (executable) instance properties.
- Parameters:
data (Any)
- model_config#
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- model: str = None#
The LLM provider and model to use.
- property response_model: cuery.response.ResponseClass#
- Abstractmethod:
- Return type:
cuery.response.ResponseClass
Defines the response model for this tool (ClassVar or property).
- property prompt: cuery.prompt.Prompt#
- Abstractmethod:
- Return type:
Defines the prompt for this tool (ClassVar or property).
- property task: cuery.task.Task#
Create a Task instance for this tool.
- Return type:
- property context: cuery.context.AnyContext | None#
- Return type:
cuery.context.AnyContext | None
- async __call__(**kwargs)#
- Return type: