Bases: BasePromptProvider
Default provider using system prompts.
Source code in src/llmling_agent/prompts/builtin_provider.py
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84 | class BuiltinPromptProvider(BasePromptProvider):
"""Default provider using system prompts."""
supports_variables = True
name = "builtin"
def __init__(self, manifest_prompts: dict[str, SystemPrompt]):
from jinja2 import Environment
from llmling_agent_functional.run import run_agent, run_agent_sync
self.prompts = manifest_prompts
self.env = Environment(autoescape=False, enable_async=True)
self.env.globals["run_agent"] = run_agent
self.env.globals["run_agent_sync"] = run_agent_sync
async def get_prompt(
self,
identifier: str,
version: str | None = None,
variables: dict[str, Any] | None = None,
) -> str:
"""Get prompt content as string."""
from jinja2 import Template, meta
if identifier not in self.prompts:
msg = f"Prompt not found: {identifier}"
raise KeyError(msg)
prompt = self.prompts[identifier]
content = prompt.content
# Parse template to find required variables
ast = self.env.parse(content)
required_vars = meta.find_undeclared_variables(ast)
if variables:
# Check for unknown variables
unknown_vars = set(variables) - required_vars
if unknown_vars:
_vars = ", ".join(unknown_vars)
msg = f"Unknown variables for prompt {identifier}: {_vars}"
raise KeyError(msg)
if required_vars:
if not variables:
req = ", ".join(required_vars)
msg = f"Prompt {identifier} requires variables: {req}"
raise KeyError(msg)
# Check for missing required variables
missing_vars = required_vars - set(variables or {})
if missing_vars:
_vars = ", ".join(missing_vars)
msg = f"Missing required variables for prompt {identifier}: {_vars}"
raise KeyError(msg)
try:
template = Template(content)
content = template.render(**variables)
except Exception as e:
msg = f"Failed to render prompt {identifier}: {e}"
raise ValueError(msg) from e
return content
async def list_prompts(self) -> list[str]:
"""List available prompt identifiers."""
return list(self.prompts.keys())
|
get_prompt
async
get_prompt(
identifier: str, version: str | None = None, variables: dict[str, Any] | None = None
) -> str
Get prompt content as string.
Source code in src/llmling_agent/prompts/builtin_provider.py
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80 | async def get_prompt(
self,
identifier: str,
version: str | None = None,
variables: dict[str, Any] | None = None,
) -> str:
"""Get prompt content as string."""
from jinja2 import Template, meta
if identifier not in self.prompts:
msg = f"Prompt not found: {identifier}"
raise KeyError(msg)
prompt = self.prompts[identifier]
content = prompt.content
# Parse template to find required variables
ast = self.env.parse(content)
required_vars = meta.find_undeclared_variables(ast)
if variables:
# Check for unknown variables
unknown_vars = set(variables) - required_vars
if unknown_vars:
_vars = ", ".join(unknown_vars)
msg = f"Unknown variables for prompt {identifier}: {_vars}"
raise KeyError(msg)
if required_vars:
if not variables:
req = ", ".join(required_vars)
msg = f"Prompt {identifier} requires variables: {req}"
raise KeyError(msg)
# Check for missing required variables
missing_vars = required_vars - set(variables or {})
if missing_vars:
_vars = ", ".join(missing_vars)
msg = f"Missing required variables for prompt {identifier}: {_vars}"
raise KeyError(msg)
try:
template = Template(content)
content = template.render(**variables)
except Exception as e:
msg = f"Failed to render prompt {identifier}: {e}"
raise ValueError(msg) from e
return content
|
list_prompts
async
List available prompt identifiers.
Source code in src/llmling_agent/prompts/builtin_provider.py
| async def list_prompts(self) -> list[str]:
"""List available prompt identifiers."""
return list(self.prompts.keys())
|