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>)[源代码]¶
基类:
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 `
- 参数:
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)[源代码]¶
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.
- 参数:
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.
- 返回:
- New ExperienceUKFT instance with pre-configured composers:
default/instance: instance_prompt_composer for structured prompts
assertion: assertion_composer for test generation
- 返回类型:
示例
>>> 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)[源代码]¶
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.
- 返回:
- A CacheEntry object populated with the ExperienceUKFT's
function name, inputs, and output.
**updates: Additional keyword arguments to update the CacheEntry attributes.
- 返回类型:
示例
>>> 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)[源代码]¶
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.
- 参数:
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.
- 返回:
A new ExperienceUKFT instance with the annotation.
- 返回类型:
示例
>>> 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].
- ahvn.ukf.templates.basic.experience.assertion_composer(kl, **kwargs)[源代码]¶
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
- 参数:
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).
- 返回:
Formatted assertion string for testing.
- 返回类型:
示例
>>> 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)[源代码]¶
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
- 参数:
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.
- 返回:
Rendered prompt string from the template.
- 返回类型:
示例
>>> 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