Skip to content

builtin_provider

Class info

Classes

Name Children Inherits
BasePromptProvider
llmling_agent.prompts.base
Base class for external prompt providers.
BuiltinPromptProvider
llmling_agent.prompts.builtin_provider
Default provider using system prompts.
    RuntimePromptProvider
    llmling_agent.prompts.builtin_provider
    Provider for prompts from RuntimeConfig.

      🛈 DocStrings

      Builtin prompt provider.

      BuiltinPromptProvider

      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_prompts() -> list[str]
      

      List available prompt identifiers.

      Source code in src/llmling_agent/prompts/builtin_provider.py
      82
      83
      84
      async def list_prompts(self) -> list[str]:
          """List available prompt identifiers."""
          return list(self.prompts.keys())
      

      RuntimePromptProvider

      Bases: BasePromptProvider

      Provider for prompts from RuntimeConfig.

      Source code in src/llmling_agent/prompts/builtin_provider.py
       87
       88
       89
       90
       91
       92
       93
       94
       95
       96
       97
       98
       99
      100
      101
      102
      103
      104
      105
      106
      107
      108
      109
      class RuntimePromptProvider(BasePromptProvider):
          """Provider for prompts from RuntimeConfig."""
      
          name = "runtime"
          supports_variables = True
      
          def __init__(self, runtime_config: RuntimeConfig):
              self.runtime = runtime_config
      
          async def get_prompt(
              self,
              name: str,
              version: str | None = None,
              variables: dict[str, Any] | None = None,
          ) -> str:
              prompt = self.runtime.get_prompt(name)
              if variables:
                  messages = await prompt.format(variables)
              messages = await prompt.format()
              return "\n".join(m.get_text_content() for m in messages)
      
          async def list_prompts(self) -> list[str]:
              return [p.name for p in self.runtime.get_prompts() if p.name]