mirror of
https://github.com/meta-llama/llama-stack.git
synced 2025-12-13 23:42:37 +00:00
feat: Add responses and safety impl extra_body
This commit is contained in:
parent
0a96a7faa5
commit
ad4362e48d
163 changed files with 29338 additions and 141 deletions
|
|
@ -131,8 +131,20 @@ class OpenAIResponseOutputMessageContentOutputText(BaseModel):
|
|||
annotations: list[OpenAIResponseAnnotations] = Field(default_factory=list)
|
||||
|
||||
|
||||
@json_schema_type
|
||||
class OpenAIResponseContentPartRefusal(BaseModel):
|
||||
"""Refusal content within a streamed response part.
|
||||
|
||||
:param type: Content part type identifier, always "refusal"
|
||||
:param refusal: Refusal text supplied by the model
|
||||
"""
|
||||
|
||||
type: Literal["refusal"] = "refusal"
|
||||
refusal: str
|
||||
|
||||
|
||||
OpenAIResponseOutputMessageContent = Annotated[
|
||||
OpenAIResponseOutputMessageContentOutputText,
|
||||
OpenAIResponseOutputMessageContentOutputText | OpenAIResponseContentPartRefusal,
|
||||
Field(discriminator="type"),
|
||||
]
|
||||
register_schema(OpenAIResponseOutputMessageContent, name="OpenAIResponseOutputMessageContent")
|
||||
|
|
@ -878,18 +890,6 @@ class OpenAIResponseContentPartOutputText(BaseModel):
|
|||
logprobs: list[dict[str, Any]] | None = None
|
||||
|
||||
|
||||
@json_schema_type
|
||||
class OpenAIResponseContentPartRefusal(BaseModel):
|
||||
"""Refusal content within a streamed response part.
|
||||
|
||||
:param type: Content part type identifier, always "refusal"
|
||||
:param refusal: Refusal text supplied by the model
|
||||
"""
|
||||
|
||||
type: Literal["refusal"] = "refusal"
|
||||
refusal: str
|
||||
|
||||
|
||||
@json_schema_type
|
||||
class OpenAIResponseContentPartReasoningText(BaseModel):
|
||||
"""Reasoning text emitted as part of a streamed response.
|
||||
|
|
|
|||
|
|
@ -91,6 +91,7 @@ class MetaReferenceAgentsImpl(Agents):
|
|||
tool_runtime_api=self.tool_runtime_api,
|
||||
responses_store=self.responses_store,
|
||||
vector_io_api=self.vector_io_api,
|
||||
safety_api=self.safety_api,
|
||||
conversations_api=self.conversations_api,
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -15,12 +15,15 @@ from llama_stack.apis.agents.openai_responses import (
|
|||
ListOpenAIResponseInputItem,
|
||||
ListOpenAIResponseObject,
|
||||
OpenAIDeleteResponseObject,
|
||||
OpenAIResponseContentPartRefusal,
|
||||
OpenAIResponseInput,
|
||||
OpenAIResponseInputMessageContentText,
|
||||
OpenAIResponseInputTool,
|
||||
OpenAIResponseMessage,
|
||||
OpenAIResponseObject,
|
||||
OpenAIResponseObjectStream,
|
||||
OpenAIResponseObjectStreamResponseCompleted,
|
||||
OpenAIResponseObjectStreamResponseCreated,
|
||||
OpenAIResponseText,
|
||||
OpenAIResponseTextFormat,
|
||||
)
|
||||
|
|
@ -34,6 +37,7 @@ from llama_stack.apis.inference import (
|
|||
OpenAIMessageParam,
|
||||
OpenAISystemMessageParam,
|
||||
)
|
||||
from llama_stack.apis.safety import Safety
|
||||
from llama_stack.apis.tools import ToolGroups, ToolRuntime
|
||||
from llama_stack.apis.vector_io import VectorIO
|
||||
from llama_stack.log import get_logger
|
||||
|
|
@ -48,6 +52,7 @@ from .types import ChatCompletionContext, ToolContext
|
|||
from .utils import (
|
||||
convert_response_input_to_chat_messages,
|
||||
convert_response_text_to_chat_response_format,
|
||||
extract_shield_ids,
|
||||
)
|
||||
|
||||
logger = get_logger(name=__name__, category="openai_responses")
|
||||
|
|
@ -66,6 +71,7 @@ class OpenAIResponsesImpl:
|
|||
tool_runtime_api: ToolRuntime,
|
||||
responses_store: ResponsesStore,
|
||||
vector_io_api: VectorIO, # VectorIO
|
||||
safety_api: Safety,
|
||||
conversations_api: Conversations,
|
||||
):
|
||||
self.inference_api = inference_api
|
||||
|
|
@ -73,6 +79,7 @@ class OpenAIResponsesImpl:
|
|||
self.tool_runtime_api = tool_runtime_api
|
||||
self.responses_store = responses_store
|
||||
self.vector_io_api = vector_io_api
|
||||
self.safety_api = safety_api
|
||||
self.conversations_api = conversations_api
|
||||
self.tool_executor = ToolExecutor(
|
||||
tool_groups_api=tool_groups_api,
|
||||
|
|
@ -237,9 +244,7 @@ class OpenAIResponsesImpl:
|
|||
stream = bool(stream)
|
||||
text = OpenAIResponseText(format=OpenAIResponseTextFormat(type="text")) if text is None else text
|
||||
|
||||
# Shields parameter received via extra_body - not yet implemented
|
||||
if shields is not None:
|
||||
raise NotImplementedError("Shields parameter is not yet implemented in the meta-reference provider")
|
||||
shield_ids = extract_shield_ids(shields) if shields else []
|
||||
|
||||
if conversation is not None:
|
||||
if previous_response_id is not None:
|
||||
|
|
@ -261,6 +266,7 @@ class OpenAIResponsesImpl:
|
|||
text=text,
|
||||
tools=tools,
|
||||
max_infer_iters=max_infer_iters,
|
||||
shield_ids=shield_ids,
|
||||
)
|
||||
|
||||
if stream:
|
||||
|
|
@ -294,6 +300,30 @@ class OpenAIResponsesImpl:
|
|||
raise ValueError("The response stream never reached a terminal state")
|
||||
return final_response
|
||||
|
||||
async def _create_refusal_response_events(
|
||||
self, refusal_content: OpenAIResponseContentPartRefusal, response_id: str, created_at: int, model: str
|
||||
) -> AsyncIterator[OpenAIResponseObjectStream]:
|
||||
"""Create and yield refusal response events following the established streaming pattern."""
|
||||
# Create initial response and yield created event
|
||||
initial_response = OpenAIResponseObject(
|
||||
id=response_id,
|
||||
created_at=created_at,
|
||||
model=model,
|
||||
status="in_progress",
|
||||
output=[],
|
||||
)
|
||||
yield OpenAIResponseObjectStreamResponseCreated(response=initial_response)
|
||||
|
||||
# Create completed refusal response using OpenAIResponseContentPartRefusal
|
||||
refusal_response = OpenAIResponseObject(
|
||||
id=response_id,
|
||||
created_at=created_at,
|
||||
model=model,
|
||||
status="completed",
|
||||
output=[OpenAIResponseMessage(role="assistant", content=[refusal_content], type="message")],
|
||||
)
|
||||
yield OpenAIResponseObjectStreamResponseCompleted(response=refusal_response)
|
||||
|
||||
async def _create_streaming_response(
|
||||
self,
|
||||
input: str | list[OpenAIResponseInput],
|
||||
|
|
@ -306,6 +336,7 @@ class OpenAIResponsesImpl:
|
|||
text: OpenAIResponseText | None = None,
|
||||
tools: list[OpenAIResponseInputTool] | None = None,
|
||||
max_infer_iters: int | None = 10,
|
||||
shield_ids: list[str] | None = None,
|
||||
) -> AsyncIterator[OpenAIResponseObjectStream]:
|
||||
# Input preprocessing
|
||||
all_input, messages, tool_context = await self._process_input_with_previous_response(
|
||||
|
|
@ -340,8 +371,11 @@ class OpenAIResponsesImpl:
|
|||
text=text,
|
||||
max_infer_iters=max_infer_iters,
|
||||
tool_executor=self.tool_executor,
|
||||
safety_api=self.safety_api,
|
||||
shield_ids=shield_ids,
|
||||
)
|
||||
|
||||
# Output safety validation hook - delegated to streaming orchestrator for real-time validation
|
||||
# Stream the response
|
||||
final_response = None
|
||||
failed_response = None
|
||||
|
|
|
|||
|
|
@ -56,7 +56,9 @@ from llama_stack.apis.agents.openai_responses import (
|
|||
WebSearchToolTypes,
|
||||
)
|
||||
from llama_stack.apis.inference import (
|
||||
CompletionMessage,
|
||||
Inference,
|
||||
Message,
|
||||
OpenAIAssistantMessageParam,
|
||||
OpenAIChatCompletion,
|
||||
OpenAIChatCompletionChunk,
|
||||
|
|
@ -64,12 +66,18 @@ from llama_stack.apis.inference import (
|
|||
OpenAIChatCompletionToolCall,
|
||||
OpenAIChoice,
|
||||
OpenAIMessageParam,
|
||||
StopReason,
|
||||
)
|
||||
from llama_stack.log import get_logger
|
||||
from llama_stack.providers.utils.telemetry import tracing
|
||||
|
||||
from ..safety import SafetyException
|
||||
from .types import ChatCompletionContext, ChatCompletionResult
|
||||
from .utils import convert_chat_choice_to_response_message, is_function_tool_call
|
||||
from .utils import (
|
||||
convert_chat_choice_to_response_message,
|
||||
is_function_tool_call,
|
||||
run_multiple_shields,
|
||||
)
|
||||
|
||||
logger = get_logger(name=__name__, category="agents::meta_reference")
|
||||
|
||||
|
|
@ -105,6 +113,8 @@ class StreamingResponseOrchestrator:
|
|||
text: OpenAIResponseText,
|
||||
max_infer_iters: int,
|
||||
tool_executor, # Will be the tool execution logic from the main class
|
||||
safety_api,
|
||||
shield_ids: list[str] | None = None,
|
||||
):
|
||||
self.inference_api = inference_api
|
||||
self.ctx = ctx
|
||||
|
|
@ -113,6 +123,8 @@ class StreamingResponseOrchestrator:
|
|||
self.text = text
|
||||
self.max_infer_iters = max_infer_iters
|
||||
self.tool_executor = tool_executor
|
||||
self.safety_api = safety_api
|
||||
self.shield_ids = shield_ids or []
|
||||
self.sequence_number = 0
|
||||
# Store MCP tool mapping that gets built during tool processing
|
||||
self.mcp_tool_to_server: dict[str, OpenAIResponseInputToolMCP] = ctx.tool_context.previous_tools or {}
|
||||
|
|
@ -122,6 +134,60 @@ class StreamingResponseOrchestrator:
|
|||
self.citation_files: dict[str, str] = {}
|
||||
# Track accumulated usage across all inference calls
|
||||
self.accumulated_usage: OpenAIResponseUsage | None = None
|
||||
# Track if we've sent a refusal response
|
||||
self.violation_detected = False
|
||||
|
||||
async def _check_input_safety(self, messages: list[Message]) -> OpenAIResponseContentPartRefusal | None:
|
||||
"""Validate input messages against shields. Returns refusal content if violation found."""
|
||||
try:
|
||||
await run_multiple_shields(self.safety_api, messages, self.shield_ids)
|
||||
except SafetyException as e:
|
||||
logger.info(f"Input shield violation: {e.violation.user_message}")
|
||||
return OpenAIResponseContentPartRefusal(
|
||||
refusal=e.violation.user_message or "Content blocked by safety shields"
|
||||
)
|
||||
|
||||
async def _create_input_refusal_response_events(
|
||||
self, refusal_content: OpenAIResponseContentPartRefusal
|
||||
) -> AsyncIterator[OpenAIResponseObjectStream]:
|
||||
"""Create refusal response events for input safety violations."""
|
||||
# Create the refusal content part explicitly with the correct structure
|
||||
refusal_response = OpenAIResponseObject(
|
||||
id=self.response_id,
|
||||
created_at=self.created_at,
|
||||
model=self.ctx.model,
|
||||
status="completed",
|
||||
output=[OpenAIResponseMessage(role="assistant", content=[refusal_content], type="message")],
|
||||
)
|
||||
yield OpenAIResponseObjectStreamResponseCompleted(response=refusal_response)
|
||||
|
||||
async def _check_output_stream_chunk_safety(self, accumulated_text: str) -> str | None:
|
||||
"""Check accumulated streaming text content against shields. Returns violation message if blocked."""
|
||||
if not self.shield_ids or not accumulated_text:
|
||||
return None
|
||||
|
||||
messages = [CompletionMessage(content=accumulated_text, stop_reason=StopReason.end_of_turn)]
|
||||
|
||||
try:
|
||||
await run_multiple_shields(self.safety_api, messages, self.shield_ids)
|
||||
except SafetyException as e:
|
||||
logger.info(f"Output shield violation: {e.violation.user_message}")
|
||||
return e.violation.user_message or "Generated content blocked by safety shields"
|
||||
|
||||
async def _create_refusal_response(self, violation_message: str) -> OpenAIResponseObjectStream:
|
||||
"""Create a refusal response to replace streaming content."""
|
||||
refusal_content = OpenAIResponseContentPartRefusal(refusal=violation_message)
|
||||
|
||||
# Create a completed refusal response
|
||||
refusal_response = OpenAIResponseObject(
|
||||
id=self.response_id,
|
||||
created_at=self.created_at,
|
||||
model=self.ctx.model,
|
||||
status="completed",
|
||||
output=[OpenAIResponseMessage(role="assistant", content=[refusal_content], type="message")],
|
||||
)
|
||||
|
||||
return OpenAIResponseObjectStreamResponseCompleted(response=refusal_response)
|
||||
|
||||
def _clone_outputs(self, outputs: list[OpenAIResponseOutput]) -> list[OpenAIResponseOutput]:
|
||||
cloned: list[OpenAIResponseOutput] = []
|
||||
|
|
@ -166,6 +232,15 @@ class StreamingResponseOrchestrator:
|
|||
sequence_number=self.sequence_number,
|
||||
)
|
||||
|
||||
# Input safety validation - check messages before processing
|
||||
if self.shield_ids:
|
||||
input_refusal = await self._check_input_safety(self.ctx.messages)
|
||||
if input_refusal:
|
||||
# Return refusal response immediately
|
||||
async for refusal_event in self._create_input_refusal_response_events(input_refusal):
|
||||
yield refusal_event
|
||||
return
|
||||
|
||||
async for stream_event in self._process_tools(output_messages):
|
||||
yield stream_event
|
||||
|
||||
|
|
@ -201,6 +276,11 @@ class StreamingResponseOrchestrator:
|
|||
completion_result_data = stream_event_or_result
|
||||
else:
|
||||
yield stream_event_or_result
|
||||
|
||||
# If violation detected, skip the rest of processing since we already sent refusal
|
||||
if self.violation_detected:
|
||||
return
|
||||
|
||||
if not completion_result_data:
|
||||
raise ValueError("Streaming chunk processor failed to return completion data")
|
||||
last_completion_result = completion_result_data
|
||||
|
|
@ -664,6 +744,15 @@ class StreamingResponseOrchestrator:
|
|||
response_tool_call.function.arguments or ""
|
||||
) + tool_call.function.arguments
|
||||
|
||||
# Safety check after processing all choices in this chunk
|
||||
if chat_response_content:
|
||||
accumulated_text = "".join(chat_response_content)
|
||||
violation_message = await self._check_output_stream_chunk_safety(accumulated_text)
|
||||
if violation_message:
|
||||
yield await self._create_refusal_response(violation_message)
|
||||
self.violation_detected = True
|
||||
return
|
||||
|
||||
# Emit arguments.done events for completed tool calls (differentiate between MCP and function calls)
|
||||
for tool_call_index in sorted(chat_response_tool_calls.keys()):
|
||||
tool_call = chat_response_tool_calls[tool_call_index]
|
||||
|
|
|
|||
|
|
@ -4,9 +4,11 @@
|
|||
# This source code is licensed under the terms described in the LICENSE file in
|
||||
# the root directory of this source tree.
|
||||
|
||||
import asyncio
|
||||
import re
|
||||
import uuid
|
||||
|
||||
from llama_stack.apis.agents.agents import ResponseShieldSpec
|
||||
from llama_stack.apis.agents.openai_responses import (
|
||||
OpenAIResponseAnnotationFileCitation,
|
||||
OpenAIResponseInput,
|
||||
|
|
@ -26,6 +28,7 @@ from llama_stack.apis.agents.openai_responses import (
|
|||
OpenAIResponseText,
|
||||
)
|
||||
from llama_stack.apis.inference import (
|
||||
Message,
|
||||
OpenAIAssistantMessageParam,
|
||||
OpenAIChatCompletionContentPartImageParam,
|
||||
OpenAIChatCompletionContentPartParam,
|
||||
|
|
@ -45,6 +48,7 @@ from llama_stack.apis.inference import (
|
|||
OpenAIToolMessageParam,
|
||||
OpenAIUserMessageParam,
|
||||
)
|
||||
from llama_stack.apis.safety import Safety
|
||||
|
||||
|
||||
async def convert_chat_choice_to_response_message(
|
||||
|
|
@ -240,7 +244,8 @@ async def convert_response_text_to_chat_response_format(
|
|||
raise ValueError(f"Unsupported text format: {text.format}")
|
||||
|
||||
|
||||
async def get_message_type_by_role(role: str):
|
||||
async def get_message_type_by_role(role: str) -> type[OpenAIMessageParam] | None:
|
||||
"""Get the appropriate OpenAI message parameter type for a given role."""
|
||||
role_to_type = {
|
||||
"user": OpenAIUserMessageParam,
|
||||
"system": OpenAISystemMessageParam,
|
||||
|
|
@ -307,3 +312,54 @@ def is_function_tool_call(
|
|||
if t.type == "function" and t.name == tool_call.function.name:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
async def run_multiple_shields(safety_api: Safety, messages: list[Message], shield_ids: list[str]) -> None:
|
||||
"""Run multiple shields against messages and raise SafetyException for violations."""
|
||||
if not shield_ids or not messages:
|
||||
return
|
||||
shield_tasks = [
|
||||
safety_api.run_shield(shield_id=shield_id, messages=messages, params={}) for shield_id in shield_ids
|
||||
]
|
||||
|
||||
responses = await asyncio.gather(*shield_tasks)
|
||||
|
||||
for response in responses:
|
||||
if response.violation and response.violation.violation_level.name == "ERROR":
|
||||
from ..safety import SafetyException
|
||||
|
||||
raise SafetyException(response.violation)
|
||||
|
||||
|
||||
def extract_shield_ids(shields: list | None) -> list[str]:
|
||||
"""Extract shield IDs from shields parameter, handling both string IDs and ResponseShieldSpec objects."""
|
||||
if not shields:
|
||||
return []
|
||||
|
||||
shield_ids = []
|
||||
for shield in shields:
|
||||
if isinstance(shield, str):
|
||||
shield_ids.append(shield)
|
||||
elif isinstance(shield, ResponseShieldSpec):
|
||||
shield_ids.append(shield.type)
|
||||
else:
|
||||
raise ValueError(f"Unknown shield format: {shield}, expected str or ResponseShieldSpec")
|
||||
|
||||
return shield_ids
|
||||
|
||||
|
||||
def extract_text_content(content: str | list | None) -> str | None:
|
||||
"""Extract text content from OpenAI message content (string or complex structure)."""
|
||||
if isinstance(content, str):
|
||||
return content
|
||||
elif isinstance(content, list):
|
||||
# Handle complex content - extract text parts only
|
||||
text_parts = []
|
||||
for part in content:
|
||||
if hasattr(part, "text"):
|
||||
text_parts.append(part.text)
|
||||
elif hasattr(part, "type") and part.type == "refusal":
|
||||
# Skip refusal parts - don't validate them again
|
||||
continue
|
||||
return " ".join(text_parts) if text_parts else None
|
||||
return None
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue