feat: add Prompts API to Responses API

This commit is contained in:
r3v5 2025-09-21 13:52:55 +01:00
parent 9f6c658f2a
commit bdc16ea392
No known key found for this signature in database
GPG key ID: C7611ACB4FECAD54
15 changed files with 526 additions and 4 deletions

View file

@ -59,7 +59,8 @@ async def agents_impl(config, mock_apis):
mock_apis["safety_api"],
mock_apis["tool_runtime_api"],
mock_apis["tool_groups_api"],
{},
None, # prompts_api (will be set later via set_prompts_api if needed)
[], # policy (empty list for tests)
)
await impl.initialize()
yield impl

View file

@ -38,6 +38,7 @@ from llama_stack.apis.inference import (
OpenAIResponseFormatJSONSchema,
OpenAIUserMessageParam,
)
from llama_stack.apis.prompts import Prompt
from llama_stack.apis.tools.tools import ToolDef, ToolGroups, ToolInvocationResult, ToolRuntime
from llama_stack.core.access_control.access_control import default_policy
from llama_stack.core.datatypes import ResponsesStoreConfig
@ -82,9 +83,20 @@ def mock_vector_io_api():
return vector_io_api
@pytest.fixture
def mock_prompts_api():
prompts_api = AsyncMock()
return prompts_api
@pytest.fixture
def openai_responses_impl(
mock_inference_api, mock_tool_groups_api, mock_tool_runtime_api, mock_responses_store, mock_vector_io_api
mock_inference_api,
mock_tool_groups_api,
mock_tool_runtime_api,
mock_responses_store,
mock_vector_io_api,
mock_prompts_api,
):
return OpenAIResponsesImpl(
inference_api=mock_inference_api,
@ -92,6 +104,7 @@ def openai_responses_impl(
tool_runtime_api=mock_tool_runtime_api,
responses_store=mock_responses_store,
vector_io_api=mock_vector_io_api,
prompts_api=mock_prompts_api,
)
@ -1004,3 +1017,56 @@ async def test_create_openai_response_with_invalid_text_format(openai_responses_
model=model,
text=OpenAIResponseText(format={"type": "invalid"}),
)
async def test_create_openai_response_with_prompt(openai_responses_impl, mock_inference_api, mock_prompts_api):
"""Test creating an OpenAI response with a prompt."""
input_text = "What is the capital of Ireland?"
model = "meta-llama/Llama-3.1-8B-Instruct"
prompt_id = "pmpt_1234567890abcdef1234567890abcdef1234567890abcdef"
prompt = Prompt(
prompt="You are a helpful {{ area_name }} assistant at {{ company_name }}. Always provide accurate information.",
prompt_id=prompt_id,
version=1,
variables=["area_name", "company_name"],
is_default=True,
)
from llama_stack.apis.agents.openai_responses import OpenAIResponsePromptParam
prompt_params_with_version_1 = OpenAIResponsePromptParam(
id=prompt_id, version="1", variables={"area_name": "geography", "company_name": "Dummy Company"}
)
mock_prompts_api.get_prompt.return_value = prompt
mock_inference_api.openai_chat_completion.return_value = fake_stream()
result = await openai_responses_impl.create_openai_response(
input=input_text,
model=model,
prompt=prompt_params_with_version_1,
)
mock_prompts_api.get_prompt.assert_called_with(prompt_id, 1)
mock_inference_api.openai_chat_completion.assert_called()
call_args = mock_inference_api.openai_chat_completion.call_args
sent_messages = call_args.kwargs["messages"]
assert len(sent_messages) == 2
system_messages = [msg for msg in sent_messages if msg.role == "system"]
assert len(system_messages) == 1
assert (
system_messages[0].content
== "You are a helpful geography assistant at Dummy Company. Always provide accurate information."
)
user_messages = [msg for msg in sent_messages if msg.role == "user"]
assert len(user_messages) == 1
assert user_messages[0].content == input_text
assert result.model == model
assert result.status == "completed"
assert result.prompt.prompt_id == prompt_id
assert result.prompt.variables == ["area_name", "company_name"]
assert result.prompt.version == 1
assert result.prompt.prompt == prompt.prompt