ahvn.ukf.templates.basic.experience module¶
- class ahvn.ukf.templates.basic.experience.ExperienceUKFT(*, name, notes='', short_description='', description='', type='general', version='v0.1.0', version_notes='', variant='default', variant_notes='', content='', content_resources=<factory>, content_composers=<factory>, source='unknown', parents=<factory>, creator='unknown', owner='unknown', workspace='unknown', collection='general', tags=<factory>, synonyms=<factory>, triggers=<factory>, priority=0, related=<factory>, auths=<factory>, timefluid=False, timestamp=<factory>, last_verified=<factory>, expiration=-1, inactive_mark=False, metadata=<factory>, profile=<factory>)[source]¶
Bases:
BaseUKFExperience class storing function inputs-output pairs.
UKF Type: experience Recommended Components of content_resources:
func (str): The name of the generator of this experience instance.
inputs (Dict): The inputs.
output (Any): The output.
expected (Any): The ground-truth output.
hints (List[str]): Optional hints or notes about the experience instance.
metatdata (Dict): Any extra information related to the experience instance.
- Recommended Composers:
- assert:
Examples:
` assert (add(a=1,b=2) == 3) `- instance:
Examples:
` Inputs: - a: 1 - b: 2 Output: - 3 `
- Parameters:
name (UKFMediumTextType)
notes (UKFMediumTextType)
short_description (UKFMediumTextType)
description (UKFMediumTextType)
type (UKFShortTextType)
version (UKFShortTextType)
version_notes (UKFMediumTextType)
variant (UKFShortTextType)
variant_notes (UKFMediumTextType)
content (UKFLongTextType)
content_resources (UKFJsonType)
content_composers (UKFJsonType)
source (UKFShortTextType)
parents (UKFJsonType)
creator (UKFShortTextType)
owner (UKFShortTextType)
workspace (UKFShortTextType)
collection (UKFShortTextType)
tags (UKFTagsType)
synonyms (UKFSynonymsType)
triggers (UKFJsonType)
priority (UKFIntegerType)
related (UKFRelatedType)
auths (UKFAuthsType)
timefluid (UKFBooleanType)
timestamp (UKFTimestampType)
last_verified (UKFTimestampType)
expiration (UKFDurationType)
inactive_mark (UKFBooleanType)
metadata (UKFJsonType)
profile (UKFJsonType)
- classmethod from_cache_entry(entry, name=None, **updates)[source]¶
Create an ExperienceUKFT instance from a cache entry or dictionary.
Provides convenient construction from cached function call data, automatically setting up content resources and composers for common experience management patterns.
- Parameters:
entry (Union[Dict, CacheEntry]) – Cache entry or dictionary containing experience data. CacheEntry objects are converted using to_dict().
name (str, optional) – Experience name. If None, generates name from function name and input parameters in “func(param=value)” format.
**updates – Additional keyword arguments to update the Experience instance attributes.
- Returns:
- New ExperienceUKFT instance with pre-configured composers:
default/instance: instance_prompt_composer for structured prompts
assertion: assertion_composer for test generation
- Return type:
Example
>>> cache_entry = CacheEntry(func="add", inputs={"a": 1, "b": 2}, output=3) >>> exp = ExperienceUKFT.from_cache_entry(cache_entry) >>> exp.name "add(a=1, b=2)" >>> exp.text("assertion") "assert (add(a=1, b=2) == 3)"
- to_cache_entry(**updates)[source]¶
Convert the ExperienceUKFT instance to a CacheEntry.
Extracts relevant fields from the ExperienceUKFT’s content resources to create a CacheEntry object, facilitating interoperability with caching mechanisms.
- Returns:
- A CacheEntry object populated with the ExperienceUKFT’s
function name, inputs, and output.
**updates: Additional keyword arguments to update the CacheEntry attributes.
- Return type:
Example
>>> exp = ExperienceUKFT(name="exp", content_resources={"func": "add", "inputs": {"a": 1, "b": 2}, "output": 3}) >>> cache_entry = exp.to_cache_entry() >>> cache_entry.func "add" >>> cache_entry.inputs {"a": 1, "b": 2} >>> cache_entry.output 3
- annotate(expected=..., **updates)[source]¶
Annotates the ExperienceUKFT with expected output and metadata.
Creates a clone of the ExperienceUKFT with the expected value and metadata updated in the content_resources. This allows for adding ground-truth labels or annotations to existing experience data.
- Parameters:
expected (Any) – The expected output of the function. If omitted (…), will use the actual output as annotation.
**updates – Additional keyword arguments to update the ExperienceUKFT instance attributes.
- Returns:
A new ExperienceUKFT instance with the annotation.
- Return type:
Example
>>> exp = ExperienceUKFT(content_resources={"func": "add", "inputs": {"a": 1, "b": 2}, "output": 3}) >>> annotated_exp = exp.annotate(expected=5, metadata={"verified": True}) >>> annotated_exp.content_resources["expected"] 5 >>> annotated_exp.metadata["verified"] True
- model_config = {'validate_assignment': True, 'validate_default': True}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- model_post_init(context, /)¶
This function is meant to behave like a BaseModel method to initialise private attributes.
It takes context as an argument since that’s what pydantic-core passes when calling it.
- ahvn.ukf.templates.basic.experience.assertion_composer(kl, **kwargs)[source]¶
Compose a Python-like assertion string for testing function outputs.
Generates assertion strings in the format “assert (func(args) == expected_output)” based on the knowledge object’s content resources. Useful for creating test cases and validation statements from experience data.
- Recommended Knowledge Types:
ExperienceUKFT
- Parameters:
kl (BaseUKF) – Knowledge object containing func, inputs, and output data.
**kwargs – Optional keyword arguments to override content_resources: - func (str): Function name to test. - inputs (Dict): Function input arguments. - output (Any): Expected function output. - expected (Any): Alternative expected output (takes precedence over output).
- Returns:
Formatted assertion string for testing.
- Return type:
Example
>>> kl.content_resources = {"func": "add", "inputs": {"a": 1, "b": 2}, "output": 3} >>> assertion_composer(kl) "assert (add(a=1, b=2) == 3)"
- ahvn.ukf.templates.basic.experience.instance_prompt_composer(kl, lang=None, env=None, template='default_instance.jinja', **kwargs)[source]¶
Compose dynamic prompts using Jinja2 templates with experience data.
Renders Jinja2 templates using content resources from the knowledge object, enabling dynamic prompt generation for AI interactions. Combines template rendering with experience data for context-aware content generation.
- Recommended Knowledge Types:
ExperienceUKFT
- Parameters:
kl (BaseUKF) – Knowledge object containing template context data.
lang (str, optional) – Language setting for Jinja2 environment.
env (str, optional) – Path to Jinja2 template environment. Defaults to “& prompts/experience” (in the ahvn resources folder).
template (str, optional) – Template filename. “default_instance.jinja” generates a prompt containing inputs, output, expected and hints. “correct_instance.jinja” generates a prompt containing inputs, expected as output, and hints Defaults to “default_instance.jinja”.
**kwargs – Additional context variables for template rendering, override content_resources values.
- Returns:
Rendered prompt string from the template.
- Return type:
Example
>>> kl.content_resources = {"func": "calculate", "inputs": {"x": 5}, "output": 20, "expected": 25} >>> kl.text(instance_prompt_composer, lang='en') Inputs: - x: 5 Output: - 20 Expected: - 25