|
| 1 | +"""Question validity capability for filtering off-topic user queries. |
| 2 | +
|
| 3 | +This module implements a guardrail that classifies user questions as |
| 4 | +Kubernetes/OpenShift-related or not (It can be customized to any |
| 5 | +topic as well), using an LLM-based check before the main agent |
| 6 | +processes the request. Invalid questions are rejected with a |
| 7 | +predefined response, bypassing the primary agent entirely. |
| 8 | +""" |
| 9 | + |
| 10 | +from __future__ import annotations |
| 11 | + |
| 12 | +from collections.abc import Sequence |
| 13 | +from dataclasses import dataclass, field |
| 14 | +from string import Template |
| 15 | + |
| 16 | +from pydantic_ai import AgentRunResult, RunContext |
| 17 | +from pydantic_ai._agent_graph import GraphAgentState |
| 18 | +from pydantic_ai.capabilities import AbstractCapability, WrapRunHandler |
| 19 | +from pydantic_ai.direct import model_request |
| 20 | +from pydantic_ai.messages import ModelRequest, TextContent, UserContent |
| 21 | +from pydantic_ai.models import Model |
| 22 | +from pydantic_ai.models.openai import OpenAIResponsesModelSettings |
| 23 | + |
| 24 | +from client import AsyncLlamaStackClientHolder |
| 25 | +from log import get_logger |
| 26 | +from models.config import ( |
| 27 | + QuestionValidityConfig, |
| 28 | +) |
| 29 | +from pydantic_ai_lightspeed.llamastack import LlamaStackResponsesModel |
| 30 | +from utils.pydantic_ai import llama_stack_provider_from_client |
| 31 | + |
| 32 | +logger = get_logger(__name__) |
| 33 | + |
| 34 | +SUBJECT_REJECTED = "REJECTED" |
| 35 | +SUBJECT_ALLOWED = "ALLOWED" |
| 36 | + |
| 37 | + |
| 38 | +def _extract_message_str_from_user_content(user_content: Sequence[UserContent]) -> str: |
| 39 | + """Extract and combine all text content into a string from a UserContent sequence. |
| 40 | +
|
| 41 | + Parameters: |
| 42 | + user_content: A sequence of user content items to extract text from. |
| 43 | +
|
| 44 | + Returns: |
| 45 | + A single string with all text content joined by newlines. |
| 46 | + """ |
| 47 | + str_arr: list[str] = [] |
| 48 | + for c in user_content: |
| 49 | + match c: |
| 50 | + case str() as s: |
| 51 | + str_arr.append(s) |
| 52 | + case TextContent(content=c): |
| 53 | + str_arr.append(c) |
| 54 | + |
| 55 | + return "\n".join(str_arr) |
| 56 | + |
| 57 | + |
| 58 | +def _create_model_from_llama_stack_client(model_id: str) -> LlamaStackResponsesModel: |
| 59 | + """Create a LlamaStackResponsesModel from the shared Llama Stack client. |
| 60 | +
|
| 61 | + Parameters: |
| 62 | + model_id: The model identifier to use for the responses model. |
| 63 | +
|
| 64 | + Returns: |
| 65 | + A configured LlamaStackResponsesModel instance. |
| 66 | + """ |
| 67 | + client = AsyncLlamaStackClientHolder().get_client() |
| 68 | + provider = llama_stack_provider_from_client(client) |
| 69 | + settings = OpenAIResponsesModelSettings(openai_store=False) |
| 70 | + return LlamaStackResponsesModel(model_id, provider=provider, settings=settings) |
| 71 | + |
| 72 | + |
| 73 | +@dataclass |
| 74 | +class QuestionValidity(AbstractCapability[None]): |
| 75 | + """Block or modify user input based on a guardrail check. |
| 76 | +
|
| 77 | + The guard function receives the user prompt and returns True if safe. |
| 78 | +
|
| 79 | + Example: |
| 80 | + ```python |
| 81 | + from pydantic_ai import Agent |
| 82 | + from pydantic_ai.models.openai import OpenAIResponsesModel |
| 83 | +
|
| 84 | + model = OpenAIResponsesModel("gpt-4o-mini") |
| 85 | + agent = Agent("openai:gpt-4.1", capabilities=[QuestionValidity(model)]) |
| 86 | + ``` |
| 87 | + """ |
| 88 | + |
| 89 | + config: QuestionValidityConfig |
| 90 | + _model: Model = field(init=False) |
| 91 | + |
| 92 | + def __post_init__(self) -> None: |
| 93 | + """Initialize the model instance from the configured model ID.""" |
| 94 | + self._model = _create_model_from_llama_stack_client(self.config.model_id) |
| 95 | + |
| 96 | + def _build_prompt(self, message: str | Sequence[UserContent] | None) -> str: |
| 97 | + """Build the classification prompt from the user message. |
| 98 | +
|
| 99 | + Parameters: |
| 100 | + message: The user input as a string, sequence of user content, or None. |
| 101 | +
|
| 102 | + Returns: |
| 103 | + The rendered prompt string ready to send to the validity model. |
| 104 | + """ |
| 105 | + match message: |
| 106 | + case str() as s: |
| 107 | + _message = s |
| 108 | + case Sequence() as seq: |
| 109 | + _message = _extract_message_str_from_user_content(seq) |
| 110 | + case None: |
| 111 | + _message = "" |
| 112 | + |
| 113 | + return Template(self.config.model_prompt).substitute( |
| 114 | + message=_message, allowed=SUBJECT_ALLOWED, rejected=SUBJECT_REJECTED |
| 115 | + ) |
| 116 | + |
| 117 | + async def wrap_run( |
| 118 | + self, ctx: RunContext, *, handler: WrapRunHandler |
| 119 | + ) -> AgentRunResult: |
| 120 | + """Run the question validity check before delegating to the main agent. |
| 121 | +
|
| 122 | + Sends the user prompt to the validity model for classification. |
| 123 | + If the question is allowed, the handler proceeds normally. |
| 124 | + Otherwise, a rejection response is returned and the main agent |
| 125 | + is bypassed. |
| 126 | +
|
| 127 | + Parameters: |
| 128 | + ctx: The run context containing the user prompt and usage tracker. |
| 129 | + handler: The handler that invokes the main agent run. |
| 130 | +
|
| 131 | + Returns: |
| 132 | + The agent run result, either from the main agent or a rejection. |
| 133 | + """ |
| 134 | + prompt = self._build_prompt(ctx.prompt) |
| 135 | + |
| 136 | + result = await model_request( |
| 137 | + model=self._model, |
| 138 | + messages=[ModelRequest.user_text_prompt(prompt)], |
| 139 | + ) |
| 140 | + |
| 141 | + # Include token usage from the question validity request |
| 142 | + ctx.usage.incr(result.usage) |
| 143 | + |
| 144 | + if result.text is not None and result.text.strip() == SUBJECT_ALLOWED: |
| 145 | + return await handler() # proceed with the real run |
| 146 | + |
| 147 | + # short-circuit: return the rejection message with shield usage tracked |
| 148 | + state = GraphAgentState(usage=ctx.usage) |
| 149 | + return AgentRunResult( |
| 150 | + output=self.config.invalid_question_response, _state=state |
| 151 | + ) |
0 commit comments