Action utils
conatus.actions.utils.docstring
¶
Docstring utilities.
More information can be found in the documentation about
Action internals.
Backlinks¶
Some of the code for these utilities comes from Pydantic AI.
DocstringStyle
module-attribute
¶
DocstringStyle = Literal['google', 'numpy', 'sphinx']
The supported docstring styles.
generate_docstring
¶
generate_docstring(
doc: str,
sig: Signature,
style: DocstringStyle | None = None,
) -> list[DocstringSection]
Generate a structured representation of the docstring from Griffe.
| PARAMETER | DESCRIPTION |
|---|---|
doc
|
The docstring to parse.
TYPE:
|
sig
|
The function signature.
TYPE:
|
style
|
The docstring style.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[DocstringSection]
|
A structured representation of the docstring as a list of Griffe
|
Source code in conatus/actions/utils/docstring.py
info_from_docstring
¶
info_from_docstring(
func: Callable[..., ParamType],
sig: Signature,
*,
style: DocstringStyle | None = None
) -> FunctionInfoFromDocstring
Extract function and parameter descriptions from a docstring.
| PARAMETER | DESCRIPTION |
|---|---|
func
|
The function to extract the info from. |
sig
|
The function signature.
TYPE:
|
style
|
The docstring style.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
FunctionInfoFromDocstring
|
A structured representation of the function info extracted from the |
FunctionInfoFromDocstring
|
docstring. |
| RAISES | DESCRIPTION |
|---|---|
DocstringParsingError
|
If the docstring could not be parsed. |
Source code in conatus/actions/utils/docstring.py
infer_docstring_style
¶
infer_docstring_style(doc: str) -> DocstringStyle | None
Simplistic docstring style inference.
The potential styles are Sphinx, Google, and Numpy. The user is responsible for handling a None value.
| PARAMETER | DESCRIPTION |
|---|---|
doc
|
The docstring to infer the style from.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DocstringStyle | None
|
The inferred docstring style. None if no style was inferred. |
Source code in conatus/actions/utils/docstring.py
conatus.actions.utils.schema_extraction
¶
Utilities to extract schema information from a function.
info_from_signature
¶
info_from_signature(
sig: Signature,
func: FunctionType | None = None,
*,
remove_self: bool = False
) -> FunctionInfoFromSignature
Extract information from a function signature.
This method extracts the schema of a function, including its parameters and return type.
| PARAMETER | DESCRIPTION |
|---|---|
sig
|
The signature of the function.
TYPE:
|
func
|
The function to extract the schema from. (Optional, and only here as fallback for ForwardRefs.)
TYPE:
|
remove_self
|
Whether to remove the first argument if it's
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
FunctionInfoFromSignature
|
The extracted information, which includes the function's parameters and return type. |
Source code in conatus/actions/utils/schema_extraction.py
reconcile_description
¶
Reconcile the description extracted from the docstring and the decorator.
| PARAMETER | DESCRIPTION |
|---|---|
docstring
|
The description extracted from the docstring.
TYPE:
|
desc
|
The description passed in the decorator.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str | None
|
The reconciled description. |
Source code in conatus/actions/utils/schema_extraction.py
reconcile_params
¶
reconcile_params(
params_sig: OrderedDict[str, ParamInfoFromSignature],
params_doc: OrderedDict[str, ParamInfoFromDocstring],
*,
override_type_hint_for_llm: bool,
fn_name: str
) -> OrderedDict[str, ParamInfo]
Reconcile the parameters extracted from the signature and docstring.
| PARAMETER | DESCRIPTION |
|---|---|
params_sig
|
The parameters extracted from the function's signature.
TYPE:
|
params_doc
|
The parameters extracted from the function's docstring.
TYPE:
|
override_type_hint_for_llm
|
Whether to override the type hint passed to the LLM.
TYPE:
|
fn_name
|
The name of the function.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
OrderedDict[str, ParamInfo]
|
The reconciled parameters. |
Source code in conatus/actions/utils/schema_extraction.py
check_docstring_signature_tuple_compatibility
¶
check_docstring_signature_tuple_compatibility(
return_sig: ReturnInfoFromSignature,
return_doc: list[ReturnInfoFromDocstring],
) -> bool
Check that a signature and a docstring have same return (tuple) values.
If the signature of a function indicates that a tuple is returned (which means multiple values), we check that the docstring also indicates that a tuple is returned, and that the number of values is the same.
In other words:
should correspond to a docstring like this:
If that's the case, we can blend the information contained in both the signature and the docstring. Otherwise we can't.
| PARAMETER | DESCRIPTION |
|---|---|
return_sig
|
The signature of the function.
TYPE:
|
return_doc
|
The docstring of the function.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
Whether the signature and the docstring are compatible. |
Source code in conatus/actions/utils/schema_extraction.py
reconcile_return
¶
reconcile_return(
return_sig: ReturnInfoFromSignature,
return_doc: list[ReturnInfoFromDocstring],
*,
override_type_hint_for_llm: bool,
passive: bool = False
) -> ReturnInfo | list[ReturnInfo]
Reconcile the return value extracted from the signature and docstring.
| PARAMETER | DESCRIPTION |
|---|---|
return_doc
|
The return value extracted from the docstring.
TYPE:
|
return_sig
|
The return value extracted from the signature.
TYPE:
|
override_type_hint_for_llm
|
Whether to override the type hint passed to the LLM.
TYPE:
|
passive
|
Whether the function is passive. For more information, see
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ReturnInfo | list[ReturnInfo]
|
The reconciled return value. |
| RAISES | DESCRIPTION |
|---|---|
PassiveActionCannotReturnError
|
If the function is passive and the
return value is not |
Source code in conatus/actions/utils/schema_extraction.py
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 | |
reconcile_info
¶
reconcile_info(
func_info_sig: FunctionInfoFromSignature,
func_info_doc: FunctionInfoFromDocstring,
*,
func: Callable[..., ParamType],
desc: str | None,
override_type_hint_for_llm: bool,
passive: bool = False
) -> FunctionInfo
Reconcile the information extracted from the signature and docstring.
See the precise logic at the top of this file.
| PARAMETER | DESCRIPTION |
|---|---|
func_info_sig
|
The information extracted from the function's signature. |
func_info_doc
|
The information extracted from the function's docstring. |
func
|
The function to extract the schema from. |
desc
|
The description of the function as passed in
TYPE:
|
override_type_hint_for_llm
|
Whether to override the type hint passed to the LLM.
TYPE:
|
passive
|
Whether the function is passive. For more information, see
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
FunctionInfo
|
The reconciled information, which includes the function's name and |
FunctionInfo
|
parameters. |
Source code in conatus/actions/utils/schema_extraction.py
extract_function_info
¶
extract_function_info(
func: FunctionType,
*,
is_action_function: bool = False,
override_type_hint_for_llm: bool = False,
desc: str | None = None,
passive: bool = False
) -> FunctionInfo
Extract information from a function.
This method extracts the schema of a function, including its parameters and return type.
| PARAMETER | DESCRIPTION |
|---|---|
func
|
The function to extract the schema from.
TYPE:
|
is_action_function
|
Whether the function is an action function. This
will tell the schema extractor to remove
TYPE:
|
override_type_hint_for_llm
|
Whether to override the type hint passed to the LLM.
TYPE:
|
desc
|
The description of the function. Defaults to None.
TYPE:
|
passive
|
Whether the function is passive. For more information, see
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
FunctionInfo
|
The extracted information, which includes the function's name, |
FunctionInfo
|
parameters, and a Pydantic model representing the function's |
FunctionInfo
|
inputs. |
Source code in conatus/actions/utils/schema_extraction.py
get_enhanced_annotation
¶
get_enhanced_annotation(
annotation: TypeOfType, kind: ParamKind
) -> TypeOfType
Take positional and keyword-only arguments into account.
We also swap None for NoneType for Pydantic compatibility.
| PARAMETER | DESCRIPTION |
|---|---|
annotation
|
The original annotation.
TYPE:
|
kind
|
The kind of parameter.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
TypeOfType
|
The enhanced annotation. |
Source code in conatus/actions/utils/schema_extraction.py
build_parameter_schema
¶
build_parameter_schema(
parameter_name: str,
parameter_info: ParamInfo,
compatible_variables: dict[str, list[str]] | None,
*,
use_test_variables_if_none: bool = False
) -> tuple[type, FieldInfo]
Build the schema for a single parameter.
| PARAMETER | DESCRIPTION |
|---|---|
parameter_name
|
The name of the parameter.
TYPE:
|
parameter_info
|
The parameter information.
TYPE:
|
compatible_variables
|
Mapping of parameter names to variable names. |
use_test_variables_if_none
|
Whether to use the test variables if
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[type, FieldInfo]
|
A tuple of the type annotation and field info for the parameter. |
| RAISES | DESCRIPTION |
|---|---|
NoCompatibleVariablesFoundForNonJSONSerializableError
|
If no compatible variables are found for a non-JSON serializable argument. |
Source code in conatus/actions/utils/schema_extraction.py
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 | |
build_return_schema
¶
build_return_schema(
function_info: FunctionInfo,
all_variables: list[str] | None,
*,
use_test_variables_if_none: bool = False
) -> dict[str, tuple[type, FieldInfo]]
Build the return schema for the JSON model.
| PARAMETER | DESCRIPTION |
|---|---|
function_info
|
The function information.
TYPE:
|
all_variables
|
A list of all available variable names. |
use_test_variables_if_none
|
Whether to use the test variables if
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict[str, tuple[type, FieldInfo]]
|
A dictionary with the key "return" mapping to a tuple containing the type annotation and field info for the return value. |
Source code in conatus/actions/utils/schema_extraction.py
generate_pydantic_json_schema_model
¶
generate_pydantic_json_schema_model(
function_info: FunctionInfo,
compatible_variables: (
dict[str, list[str]] | None
) = None,
all_variables: list[str] | None = None,
*,
args_without_variable_references: (
list[str] | None
) = None,
use_test_variables_if_none: bool = False
) -> type[BaseModel]
Generate the Pydantic model representing the function's JSON schema.
Unlike the action's input_model
, which is used to validate the
inputs and outputs of the action function, the json_schema
is used to
display the JSON schema of the action function in the LLM. The main
difference here is that arguments whose types are not JSON serializable are
converted to string references; the role of the LLM is to choose among the
runtime variables that are compatible with the argument in question.
This function is called when the Action
is being created. It is also called after each LLM step, to update the
JSON schema model with the latest compatible variables. The variables
need to be passed to this function; otherwise, the Enums
will be filled with test values.
| PARAMETER | DESCRIPTION |
|---|---|
function_info
|
The schema of the function.
TYPE:
|
compatible_variables
|
The variables that are compatible with the
function's arguments; of the format |
all_variables
|
The names of all the available variables. |
args_without_variable_references
|
The names of the arguments for which we forbid variable references. |
use_test_variables_if_none
|
Whether to use the test variables if
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
type[BaseModel]
|
The Pydantic model representing the function's JSON schema. |
Source code in conatus/actions/utils/schema_extraction.py
generate_pydantic_models
¶
generate_pydantic_models(
function_info: FunctionInfo,
*,
use_test_variables_if_none: bool = False
) -> FunctionPydanticModels
Generate Pydantic models from a function's schema.
| PARAMETER | DESCRIPTION |
|---|---|
function_info
|
The schema of the function.
TYPE:
|
use_test_variables_if_none
|
Whether to use the test variables if
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
FunctionPydanticModels
|
The Pydantic models representing the function's inputs. |
Source code in conatus/actions/utils/schema_extraction.py
conatus.actions.utils.function_info_types
¶
Types used in the actions module.
ParamKind
¶
Bases: IntEnum
Copy of the _ParameterKind enum from the inspect module.
Since this enum is private, we don't want to take any risks.
ArgForPrint
¶
Bases: ArbitraryBaseModel
Argument for pretty printing (works for both parameters and returns).
| ATTRIBUTE | DESCRIPTION |
|---|---|
type_hint |
The type hint of the argument.
TYPE:
|
description |
The description of the argument.
TYPE:
|
name |
The name of the argument.
TYPE:
|
kind |
The kind of the argument.
TYPE:
|
default_value |
The default value of the argument.
TYPE:
|
is_required |
Whether the argument is required.
TYPE:
|
is_json_serializable |
Whether the argument is JSON serializable.
TYPE:
|
json_serializable_subtype |
The subtype of the argument that is JSON serializable.
TYPE:
|
pretty_print
¶
Returns the pretty print string of the argument.
| PARAMETER | DESCRIPTION |
|---|---|
colors
|
Whether to use colors in the output. Defaults to True.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
The pretty print of the argument. |
Source code in conatus/actions/utils/function_info_types.py
ParamInfoFromDocstring
¶
Bases: ArbitraryBaseModel
Information about a raw parameter (according to the docstring).
| ATTRIBUTE | DESCRIPTION |
|---|---|
raw_type_hint |
The type hint of the parameter.
TYPE:
|
description |
The description of the parameter.
TYPE:
|
default_value |
The default value of the parameter.
TYPE:
|
pretty_print
¶
Returns the pretty print string of the parameter.
| PARAMETER | DESCRIPTION |
|---|---|
colors
|
Whether to use colors in the output. Defaults to True.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
The pretty print of the parameter. |
Source code in conatus/actions/utils/function_info_types.py
ReturnInfoFromDocstring
¶
Bases: ArbitraryBaseModel
Information about a raw return parameter (according to the docstring).
| ATTRIBUTE | DESCRIPTION |
|---|---|
name |
The name of the return parameter.
TYPE:
|
raw_type_hint |
The type hint of the return parameter.
TYPE:
|
description |
The description of the return parameter.
TYPE:
|
pretty_print
¶
Returns the pretty print string of the return parameter.
| PARAMETER | DESCRIPTION |
|---|---|
colors
|
Whether to use colors in the output. Defaults to True.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
The pretty print of the return parameter. |
Source code in conatus/actions/utils/function_info_types.py
FunctionInfoFromDocstring
¶
Bases: ArbitraryBaseModel
Structured function info from docstring.
| ATTRIBUTE | DESCRIPTION |
|---|---|
description |
The description of the function.
TYPE:
|
parameters |
The parameters of the function.
TYPE:
|
returns |
The return parameters of the function. (Can be multiple)
TYPE:
|
pretty_print
¶
Returns the pretty print string of the function info.
| PARAMETER | DESCRIPTION |
|---|---|
colors
|
Whether to use colors in the output.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
The pretty print of the function info. |
Source code in conatus/actions/utils/function_info_types.py
ReturnInfoFromSignature
¶
Bases: ArbitraryBaseModel
Information about a raw return parameter (according to the signature).
| ATTRIBUTE | DESCRIPTION |
|---|---|
raw_type_hint |
The raw type hint of the return parameter (for Pydantic).
TYPE:
|
description |
The description of the return parameter.
TYPE:
|
pretty_print
¶
Returns the pretty print string of the function info.
| PARAMETER | DESCRIPTION |
|---|---|
colors
|
Whether to use colors in the output.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
The pretty print of the function info. |
Source code in conatus/actions/utils/function_info_types.py
ParamInfoFromSignature
¶
Bases: ArbitraryBaseModel
Information about a raw parameter (according to the signature).
| ATTRIBUTE | DESCRIPTION |
|---|---|
raw_type_hint |
The raw type hint of the parameter (for Pydantic).
TYPE:
|
kind |
The kind of the parameter.
TYPE:
|
description |
The description of the parameter.
TYPE:
|
default_value |
The default value of the parameter.
TYPE:
|
is_required |
Whether the parameter is required.
TYPE:
|
is_json_serializable |
Whether the parameter is JSON serializable.
TYPE:
|
json_serializable_subtype |
The subtype of the parameter that is JSON serializable.
TYPE:
|
pretty_print
¶
Returns the pretty print string of the parameter.
| PARAMETER | DESCRIPTION |
|---|---|
colors
|
Whether to use colors in the output.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
The pretty print of the parameter. |
Source code in conatus/actions/utils/function_info_types.py
FunctionInfoFromSignature
¶
Bases: ArbitraryBaseModel
Structured function info from signature.
| ATTRIBUTE | DESCRIPTION |
|---|---|
parameters |
The parameters of the function.
TYPE:
|
returns |
The return parameter of the function.
TYPE:
|
pretty_print
¶
Returns the pretty print string of the function info.
| PARAMETER | DESCRIPTION |
|---|---|
colors
|
Whether to use colors in the output.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
The pretty print of the function info. |
Source code in conatus/actions/utils/function_info_types.py
ReturnInfo
¶
Bases: ArbitraryBaseModel
Information about a return parameter.
| ATTRIBUTE | DESCRIPTION |
|---|---|
type_hint |
The type hint of the return parameter.
TYPE:
|
type_hint_for_llm |
The type hint of the return parameter for the LLM.
TYPE:
|
description |
The description of the return parameter.
TYPE:
|
pretty_print
¶
Returns the pretty print string of the return parameter.
| PARAMETER | DESCRIPTION |
|---|---|
colors
|
Whether to use colors in the output.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
The pretty print of the return parameter. |
Source code in conatus/actions/utils/function_info_types.py
ParamInfo
¶
Bases: ArbitraryBaseModel
Information about a parameter.
| ATTRIBUTE | DESCRIPTION |
|---|---|
type_hint |
The raw type hint of the parameter.
TYPE:
|
type_hint_for_llm |
The type hint of the parameter for the LLM.
TYPE:
|
kind |
The kind of the parameter.
TYPE:
|
description |
The description of the parameter.
TYPE:
|
default_value |
The default value of the parameter.
TYPE:
|
is_required |
Whether the parameter is required.
TYPE:
|
is_json_serializable |
Whether the parameter can be serialized to JSON.
TYPE:
|
json_serializable_subtype |
The subtype of the parameter that can be serialized to JSON.
TYPE:
|
pretty_print
¶
Returns the pretty print string of the parameter.
| PARAMETER | DESCRIPTION |
|---|---|
colors
|
Whether to use colors in the output.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
The pretty print of the parameter. |
Source code in conatus/actions/utils/function_info_types.py
FunctionInfo
¶
Bases: ArbitraryBaseModel
Structured function info.
| ATTRIBUTE | DESCRIPTION |
|---|---|
fn_name |
The name of the function.
TYPE:
|
description |
The description of the function.
TYPE:
|
parameters |
The parameters of the function.
TYPE:
|
returns |
The return parameter of the function. (Can be multiple)
TYPE:
|
pretty_print
¶
Returns the pretty print string of the function info.
| PARAMETER | DESCRIPTION |
|---|---|
colors
|
Whether to use colors in the output.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
The pretty print of the function info. |
Source code in conatus/actions/utils/function_info_types.py
FunctionPydanticModels
dataclass
¶
FunctionPydanticModels(
input_model: type[BaseModel],
output_model: type[BaseModel] | None,
json_schema: type[BaseModel],
all_fields: dict[str, tuple[type, FieldInfo]],
)
Pydantic models for the function info.
input_model
instance-attribute
¶
Pydantic model for the input of the function.
This model is used to perform type checking of the input parameters.
output_model
instance-attribute
¶
Pydantic model for the output of the function.
This model is used to perform type checking of the output parameters.
json_schema
instance-attribute
¶
Pydantic model for the JSON schema of the function.
The main difference with input_model is that this model is meant
to be regenerated at runtime depending on the runtime variables. It
is also used to generate the OpenAI JSON schema of the action.
all_fields
instance-attribute
¶
All fields of the function.
The keys are the names of the fields, and the values are tuples of the type
of the field and the FieldInfo instance. We use this internally to
create the input_model.
This is mostly useful at agent runtime, when we can build a big Pydantic model of all the parameters of all the actions, which we can then use to check the compatibility of runtime variables.
add_indentation
¶
Add indentation to a text.
If we detect a newline at the end of the text, we add a newline at the end of the result. That way we don't get an ugly last line with dangling indentation.
| RETURNS | DESCRIPTION |
|---|---|
str
|
The text with indentation added. |
Source code in conatus/actions/utils/function_info_types.py
conatus.actions.utils.type_hint_handling
¶
Utilities handling type hints.
We do so here partially for type-checking, since so many values are
Any or Unknown.
OneOfCallables
module-attribute
¶
The type of a one-of callable.
CallableType
¶
Bases: ArbitraryBaseModel
Type of a callable.
| ATTRIBUTE | DESCRIPTION |
|---|---|
params |
The parameters of the callable.
TYPE:
|
return_ |
The return type of the callable.
TYPE:
|
AnnotatedType
¶
LiteralType
¶
ClassVarType
¶
Bases: ArbitraryBaseModel
Type of a ClassVar type.
| ATTRIBUTE | DESCRIPTION |
|---|---|
type_ |
The type of the ClassVar type.
TYPE:
|
FinalType
¶
Bases: ArbitraryBaseModel
Type of a Final type.
| ATTRIBUTE | DESCRIPTION |
|---|---|
type_ |
The type of the Final type.
TYPE:
|
NotRequiredType
¶
Bases: ArbitraryBaseModel
Type of a NotRequired type.
| ATTRIBUTE | DESCRIPTION |
|---|---|
type_ |
The type of the NotRequired type.
TYPE:
|
ForwardRefType
¶
Bases: ArbitraryBaseModel
Type of a ForwardRef type.
| ATTRIBUTE | DESCRIPTION |
|---|---|
type_ |
The type of the ForwardRef type.
TYPE:
|
get_restricted_annotation
¶
get_restricted_annotation(
type_hint: TypeOfType,
) -> TypeOfTypeRestrictive
Get the restricted annotation of a type hint.
| PARAMETER | DESCRIPTION |
|---|---|
type_hint
|
The type hint to get the restricted annotation from.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
TypeOfTypeRestrictive
|
The restricted annotation of the type hint. |
Source code in conatus/actions/utils/type_hint_handling.py
get_annotation
¶
get_annotation(
param: Parameter | DocstringParameter,
func_for_forward_refs: (
Callable[..., ParamType] | None
) = None,
) -> TypeOfType
Get the annotation of a parameter.
| PARAMETER | DESCRIPTION |
|---|---|
param
|
The parameter to get the annotation from.
TYPE:
|
func_for_forward_refs
|
The function to get the annotation from. (Optional, and only here as fallback for ForwardRefs.) |
| RETURNS | DESCRIPTION |
|---|---|
TypeOfType
|
The annotation of the parameter. |
Source code in conatus/actions/utils/type_hint_handling.py
get_return_annotation
¶
get_return_annotation(sig: Signature) -> TypeOfType
Get the return annotation of a signature.
| PARAMETER | DESCRIPTION |
|---|---|
sig
|
The signature to get the return annotation from.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
TypeOfType
|
The return annotation of the signature. |
Source code in conatus/actions/utils/type_hint_handling.py
get_default_value
¶
Get the default value of a parameter.
| PARAMETER | DESCRIPTION |
|---|---|
param
|
The parameter to get the default value from.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ParamType
|
The default value of the parameter. |
Source code in conatus/actions/utils/type_hint_handling.py
get_description_from_typehint
¶
get_description_from_typehint(
type_hint: TypeOfType,
) -> str | None
Get the description from a type hint.
| PARAMETER | DESCRIPTION |
|---|---|
type_hint
|
The type hint to get the description from.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str | None
|
The description of the type hint. |
Source code in conatus/actions/utils/type_hint_handling.py
is_json_serializable_value
¶
is_json_serializable_type
¶
is_json_serializable_type(
type_hint: TypeOfType,
) -> tuple[bool, TypeOfType]
Check whether part of all of a type hint is JSON-serializable.
This function returns two values: a boolean indicating whether the type hint is JSON-serializable, and the part of the type hint that is JSON-serializable. If the type hint is never JSON-serializable, we return Ellipsis as the second value. If the type hint is always JSON-serializable, we return the type hint itself.
More information on why we need to check for JSON-serializable-ness,
and why we need to determine a JSON-serializable subtype of a type hint
can be found in the Action internals documentation.
| PARAMETER | DESCRIPTION |
|---|---|
type_hint
|
The type hint to check.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
Whether the type is SOMETIMES JSON-serializable. |
TypeOfType
|
The part of the type hint that is JSON-serializable. If the type hint is JSON-serializable, we return Ellipsis. |
Source code in conatus/actions/utils/type_hint_handling.py
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 | |
process_typehint
¶
process_typehint(
type_hint: TypeOfType,
return_type: Literal["str"],
*,
allow_qualified_names: bool = True
) -> str
process_typehint(
type_hint: TypeOfType,
return_type: Literal["set", "str"] = "str",
*,
allow_qualified_names: bool = True
) -> str | set[type | str]
Process type hints.
This function recursively unpacks type hints. It has two uses:
- To generate a type hint for the LLM. In this case, we want to return a string that is more likely to be understood by the LLM.
- To get a set of all the types/literals encountered in the type hint. In this case, we want to return a set of the types/literals. We use this to be able to know all the imports that need to be added to the code.
Examples¶
Generating a type hint for the LLM¶
from typing import Union
from conatus.actions.utils.type_hint_handling import process_typehint
assert process_typehint(Union[int, str]) == "int | str"
Get all the types/literals encountered in the type hint¶
from conatus.actions.utils.type_hint_handling import process_typehint
assert process_typehint(int | str, return_type="set") == {int, str}
| PARAMETER | DESCRIPTION |
|---|---|
type_hint
|
The original type hint.
TYPE:
|
return_type
|
The expected return type. If "set", return a set of the types/literals encountered in the type hint; if "str", return a human-readable string.
TYPE:
|
allow_qualified_names
|
In string mode, whether to allow qualified names,
e.g.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str | set[type | str]
|
Either the type hint as a string or a set of the types. |
Source code in conatus/actions/utils/type_hint_handling.py
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 | |
conatus.actions.retrievability
¶
Module defining the retrievability of actions.
MaybeRetrievableActionInfo
module-attribute
¶
MaybeRetrievableActionInfo = (
RetrievableActionInfo | NonRetrievableActionInfo
)
Type alias for a retrievable or non-retrievable action info.
We separate the two to ensure correctness for the type checker.
NonRetrievableActionInfo
dataclass
¶
NonRetrievableActionInfo(
origin_type: ActionOriginType,
recipe_retrievable: Literal[False],
reason_why_not_retrievable: ReasonWhyNotRetrievable,
origin_module: str,
origin_qualname: str,
)
Information about a non-retrievable action.
This is the case when the action is not retrievable because it's defined
in a <locals> namespace or because some changes are done to the
Action that makes it non retrievable
because some configuration values are not JSON serializable.
origin_type
instance-attribute
¶
The origin of the action.
Can be either:
- "subclass": The action was created through a subclass of Action.
- "decorator": The action was a function decorated with @action or passed
to the action function.
recipe_retrievable
instance-attribute
¶
recipe_retrievable: Literal[False]
The action is not retrievable.
For more information, see RetrievableActionInfo.recipe_retrievable
.
reason_why_not_retrievable
instance-attribute
¶
The reason why the action is not retrievable.
For more information, see
NonRetrievableActionInfo.reason_why_not_retrievable
.
origin_module
instance-attribute
¶
origin_module: str
The module where the action was originally defined.
For more information, see RetrievableActionInfo.origin_module
.
origin_qualname
instance-attribute
¶
origin_qualname: str
The fully qualified name of the action as originally defined.
For more information, see RetrievableActionInfo.origin_qualname
.
RetrievabilityInstructions
dataclass
¶
Instructions to retrieve the action.
imports
instance-attribute
¶
The imports needed to retrieve the action.
The list is structured such that you want to do
from {imports[:-2]} import {imports[-1]}
qualname
instance-attribute
¶
qualname: str
The qualified name of the action after the imports have been executed.
changes
instance-attribute
¶
changes: str
The changes to the action that need to be made.
The changes are serialized as a JSON string.
This string might be {} if no changes are needed, and might contain
values if the action is a result of a with_config
call or if the user has
changed the attributes of the action.
to_python_lines
¶
Convert the retrievability instructions to a Python string.
| RETURNS | DESCRIPTION |
|---|---|
str
|
A tuple of two strings: |
str | None
|
|
tuple[str, str | None]
|
|
Source code in conatus/actions/retrievability.py
RetrievableActionInfo
dataclass
¶
RetrievableActionInfo(
origin_type: ActionOriginType,
recipe_retrievable: Literal[True],
reason_why_not_retrievable: None,
origin_module: str,
origin_qualname: str,
)
Information about a retrievable action.
It should be the default case that Action
instances are retrievable, in that we can close the Python runtime,
re-open it, and still be able to retrieve the action by running an import
and changing its configuration parameters.
In this case:
* recipe_retrievable is True
* reason_why_not_retrievable is None
* origin_module is the module where the action was originally defined
* origin_qualname is the fully qualified name of the action as originally
defined
Sometimes, the action is not retrievable. This happens, for example, if
the action is defined in a <locals> namespace. In this case, Action
will use the [NonRetrievableActionInfo]
class to store the information about the action.
origin_type
instance-attribute
¶
The origin of the action.
Can be either:
- "subclass": The action was created through a subclass of Action.
- "decorator": The action was a function decorated with @action or passed
to the action function.
recipe_retrievable
instance-attribute
¶
recipe_retrievable: Literal[True]
Whether the action is retrievable in recipes.
This is evaluated based on whether the action can be imported like a normal
Python function or class. In other words, it will not be retrievable if:
- The action is part of a
reason_why_not_retrievable
instance-attribute
¶
The reason why the action is not retrievable in recipes.
This attribute is set if _recipe_retrievable is False, and contains
information about why the action is not retrievable. If the action is
retrievable, this attribute is None.
origin_module
instance-attribute
¶
origin_module: str
The module where the action was originally defined.
To be more precise:
- If the action was created through a subclass of
Action, this will be the module where the subclass was defined. - If the action was created through a function decorated with
@actionor passed to theactionfunction, this will be the module where the function was defined.
This attribute enables us to retrieve the original action or function when recipes are executed.
origin_qualname
instance-attribute
¶
origin_qualname: str
The fully qualified name of the action as originally defined.
To be more precise:
- If the action was created through a subclass of Action, this will be
the fully qualified name of the subclass.
- If the action was created through a function decorated with @action or
passed to the action function, this will be the fully qualified name
of the function.
This attribute enables us to retrieve the original action or function when recipes are executed.
change_to_non_retrievable
¶
change_to_non_retrievable(
reason_why_not_retrievable: ReasonWhyNotRetrievable,
) -> NonRetrievableActionInfo
Change the action to non-retrievable.
This happens, for example, if some changes are done to the [Action]
that makes it non retrievable because some configuration values are not
JSON serializable.
| PARAMETER | DESCRIPTION |
|---|---|
reason_why_not_retrievable
|
The reason why the action is not retrievable.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
NonRetrievableActionInfo
|
The non-retrievable action info. |
Source code in conatus/actions/retrievability.py
get_retrievability_instructions
¶
get_retrievability_instructions(
changes_as_json: str | None = None,
) -> RetrievabilityInstructions
Get the instructions to retrieve the action.
| PARAMETER | DESCRIPTION |
|---|---|
changes_as_json
|
The changes to the action as a JSON string.
Defaults to
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
RetrievabilityInstructions
|
The retrievability instructions. |