From 5d97ee645c0401a12aa787e74c7cbf3eda349bbf Mon Sep 17 00:00:00 2001 From: Xi Yan Date: Thu, 20 Feb 2025 09:52:37 -0800 Subject: [PATCH 01/14] api update --- llama_stack/apis/agents/agents.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/llama_stack/apis/agents/agents.py b/llama_stack/apis/agents/agents.py index 367648ded3..f0ded1087d 100644 --- a/llama_stack/apis/agents/agents.py +++ b/llama_stack/apis/agents/agents.py @@ -194,6 +194,7 @@ class AgentTurnResponseEventType(Enum): turn_start = "turn_start" turn_complete = "turn_complete" + turn_pending = "turn_pending" @json_schema_type @@ -235,6 +236,12 @@ class AgentTurnResponseTurnCompletePayload(BaseModel): turn: Turn +@json_schema_type +class AgentTurnResponseTurnPendingPayload(BaseModel): + event_type: Literal[AgentTurnResponseEventType.turn_pending.value] = AgentTurnResponseEventType.turn_pending.value + turn: Turn + + AgentTurnResponseEventPayload = register_schema( Annotated[ Union[ @@ -243,6 +250,7 @@ class AgentTurnResponseTurnCompletePayload(BaseModel): AgentTurnResponseStepCompletePayload, AgentTurnResponseTurnStartPayload, AgentTurnResponseTurnCompletePayload, + AgentTurnResponseTurnPendingPayload, ], Field(discriminator="event_type"), ], @@ -335,6 +343,18 @@ async def create_agent_turn( tool_config: Optional[ToolConfig] = None, ) -> Union[Turn, AsyncIterator[AgentTurnResponseStreamChunk]]: ... + @webmethod( + route="/agents/{agent_id}/session/{session_id}/turn/{turn_id}/update", + method="POST", + ) + async def update_agent_turn_with_tool_responses( + self, + agent_id: str, + session_id: str, + turn_id: str, + tool_messages: List[ToolResponseMessage], + ) -> Union[Turn, AsyncIterator[AgentTurnResponseStreamChunk]]: ... + @webmethod( route="/agents/{agent_id}/session/{session_id}/turn/{turn_id}", method="GET", From 7689ff2b547b6e4cf9ea6d61a92ab1f33aea9a1e Mon Sep 17 00:00:00 2001 From: Xi Yan Date: Thu, 20 Feb 2025 10:36:14 -0800 Subject: [PATCH 02/14] naming submit_tool_response_messages --- llama_stack/apis/agents/agents.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/llama_stack/apis/agents/agents.py b/llama_stack/apis/agents/agents.py index f0ded1087d..2f374b6388 100644 --- a/llama_stack/apis/agents/agents.py +++ b/llama_stack/apis/agents/agents.py @@ -344,15 +344,15 @@ async def create_agent_turn( ) -> Union[Turn, AsyncIterator[AgentTurnResponseStreamChunk]]: ... @webmethod( - route="/agents/{agent_id}/session/{session_id}/turn/{turn_id}/update", + route="/agents/{agent_id}/session/{session_id}/turn/{turn_id}/submit_tool_response_messages", method="POST", ) - async def update_agent_turn_with_tool_responses( + async def submit_tool_response_messages( self, agent_id: str, session_id: str, turn_id: str, - tool_messages: List[ToolResponseMessage], + tool_response_messages: List[ToolResponseMessage], ) -> Union[Turn, AsyncIterator[AgentTurnResponseStreamChunk]]: ... @webmethod( From e0fd19531b267882714580ba9b7684bb2de73246 Mon Sep 17 00:00:00 2001 From: Xi Yan Date: Thu, 20 Feb 2025 10:41:08 -0800 Subject: [PATCH 03/14] openapi gen --- docs/_static/llama-stack-spec.html | 102 ++++++++++++++++++++++++++++- docs/_static/llama-stack-spec.yaml | 66 +++++++++++++++++++ 2 files changed, 167 insertions(+), 1 deletion(-) diff --git a/docs/_static/llama-stack-spec.html b/docs/_static/llama-stack-spec.html index 2b6e1d11c7..829c90fc47 100644 --- a/docs/_static/llama-stack-spec.html +++ b/docs/_static/llama-stack-spec.html @@ -2558,6 +2558,67 @@ } } }, + "/v1/agents/{agent_id}/session/{session_id}/turn/{turn_id}/submit_tool_response_messages": { + "post": { + "responses": { + "200": { + "description": "A single turn in an interaction with an Agentic System. **OR** streamed agent turn completion response.", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Turn" + } + }, + "text/event-stream": { + "schema": { + "$ref": "#/components/schemas/AgentTurnResponseStreamChunk" + } + } + } + } + }, + "tags": [ + "Agents" + ], + "description": "", + "parameters": [ + { + "name": "agent_id", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + }, + { + "name": "session_id", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + }, + { + "name": "turn_id", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ], + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/SubmitToolResponseMessagesRequest" + } + } + }, + "required": true + } + } + }, "/v1/post-training/supervised-fine-tune": { "post": { "responses": { @@ -5231,6 +5292,9 @@ }, { "$ref": "#/components/schemas/AgentTurnResponseTurnCompletePayload" + }, + { + "$ref": "#/components/schemas/AgentTurnResponseTurnPendingPayload" } ], "discriminator": { @@ -5240,7 +5304,8 @@ "step_progress": "#/components/schemas/AgentTurnResponseStepProgressPayload", "step_complete": "#/components/schemas/AgentTurnResponseStepCompletePayload", "turn_start": "#/components/schemas/AgentTurnResponseTurnStartPayload", - "turn_complete": "#/components/schemas/AgentTurnResponseTurnCompletePayload" + "turn_complete": "#/components/schemas/AgentTurnResponseTurnCompletePayload", + "turn_pending": "#/components/schemas/AgentTurnResponseTurnPendingPayload" } } }, @@ -5422,6 +5487,25 @@ ], "title": "AgentTurnResponseTurnCompletePayload" }, + "AgentTurnResponseTurnPendingPayload": { + "type": "object", + "properties": { + "event_type": { + "type": "string", + "const": "turn_pending", + "default": "turn_pending" + }, + "turn": { + "$ref": "#/components/schemas/Turn" + } + }, + "additionalProperties": false, + "required": [ + "event_type", + "turn" + ], + "title": "AgentTurnResponseTurnPendingPayload" + }, "AgentTurnResponseTurnStartPayload": { "type": "object", "properties": { @@ -8239,6 +8323,22 @@ ], "title": "ScoreBatchResponse" }, + "SubmitToolResponseMessagesRequest": { + "type": "object", + "properties": { + "tool_response_messages": { + "type": "array", + "items": { + "$ref": "#/components/schemas/ToolResponseMessage" + } + } + }, + "additionalProperties": false, + "required": [ + "tool_response_messages" + ], + "title": "SubmitToolResponseMessagesRequest" + }, "AlgorithmConfig": { "oneOf": [ { diff --git a/docs/_static/llama-stack-spec.yaml b/docs/_static/llama-stack-spec.yaml index 99300fedf1..4dfaa284a5 100644 --- a/docs/_static/llama-stack-spec.yaml +++ b/docs/_static/llama-stack-spec.yaml @@ -1543,6 +1543,45 @@ paths: schema: $ref: '#/components/schemas/ScoreBatchRequest' required: true + /v1/agents/{agent_id}/session/{session_id}/turn/{turn_id}/submit_tool_response_messages: + post: + responses: + '200': + description: >- + A single turn in an interaction with an Agentic System. **OR** streamed + agent turn completion response. + content: + application/json: + schema: + $ref: '#/components/schemas/Turn' + text/event-stream: + schema: + $ref: '#/components/schemas/AgentTurnResponseStreamChunk' + tags: + - Agents + description: '' + parameters: + - name: agent_id + in: path + required: true + schema: + type: string + - name: session_id + in: path + required: true + schema: + type: string + - name: turn_id + in: path + required: true + schema: + type: string + requestBody: + content: + application/json: + schema: + $ref: '#/components/schemas/SubmitToolResponseMessagesRequest' + required: true /v1/post-training/supervised-fine-tune: post: responses: @@ -3366,6 +3405,7 @@ components: - $ref: '#/components/schemas/AgentTurnResponseStepCompletePayload' - $ref: '#/components/schemas/AgentTurnResponseTurnStartPayload' - $ref: '#/components/schemas/AgentTurnResponseTurnCompletePayload' + - $ref: '#/components/schemas/AgentTurnResponseTurnPendingPayload' discriminator: propertyName: event_type mapping: @@ -3374,6 +3414,7 @@ components: step_complete: '#/components/schemas/AgentTurnResponseStepCompletePayload' turn_start: '#/components/schemas/AgentTurnResponseTurnStartPayload' turn_complete: '#/components/schemas/AgentTurnResponseTurnCompletePayload' + turn_pending: '#/components/schemas/AgentTurnResponseTurnPendingPayload' AgentTurnResponseStepCompletePayload: type: object properties: @@ -3494,6 +3535,20 @@ components: - event_type - turn title: AgentTurnResponseTurnCompletePayload + AgentTurnResponseTurnPendingPayload: + type: object + properties: + event_type: + type: string + const: turn_pending + default: turn_pending + turn: + $ref: '#/components/schemas/Turn' + additionalProperties: false + required: + - event_type + - turn + title: AgentTurnResponseTurnPendingPayload AgentTurnResponseTurnStartPayload: type: object properties: @@ -5273,6 +5328,17 @@ components: required: - results title: ScoreBatchResponse + SubmitToolResponseMessagesRequest: + type: object + properties: + tool_response_messages: + type: array + items: + $ref: '#/components/schemas/ToolResponseMessage' + additionalProperties: false + required: + - tool_response_messages + title: SubmitToolResponseMessagesRequest AlgorithmConfig: oneOf: - $ref: '#/components/schemas/LoraFinetuningConfig' From dc1406c25a85ca8a1b6c9f89b9c4f37c7ed4de4b Mon Sep 17 00:00:00 2001 From: Xi Yan Date: Thu, 20 Feb 2025 10:51:08 -0800 Subject: [PATCH 04/14] dummy impl --- .../providers/inline/agents/meta_reference/agents.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/llama_stack/providers/inline/agents/meta_reference/agents.py b/llama_stack/providers/inline/agents/meta_reference/agents.py index e3c18d1122..87d73bf414 100644 --- a/llama_stack/providers/inline/agents/meta_reference/agents.py +++ b/llama_stack/providers/inline/agents/meta_reference/agents.py @@ -169,6 +169,15 @@ async def _create_agent_turn_streaming( async for event in agent.create_and_execute_turn(request): yield event + async def submit_tool_response_messages( + self, + agent_id: str, + session_id: str, + turn_id: str, + tool_response_messages: List[ToolResponseMessage], + ) -> Union[Turn, AsyncIterator[AgentTurnResponseStreamChunk]]: + pass + async def get_agents_turn(self, agent_id: str, session_id: str, turn_id: str) -> Turn: turn = await self.persistence_store.get(f"session:{agent_id}:{session_id}:{turn_id}") turn = json.loads(turn) From 6b6feebc72f4ddf7674d530ca2870ebaf7757b2f Mon Sep 17 00:00:00 2001 From: Xi Yan Date: Thu, 20 Feb 2025 10:53:36 -0800 Subject: [PATCH 05/14] update types --- llama_stack/providers/inline/agents/meta_reference/agents.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llama_stack/providers/inline/agents/meta_reference/agents.py b/llama_stack/providers/inline/agents/meta_reference/agents.py index 87d73bf414..c208388be8 100644 --- a/llama_stack/providers/inline/agents/meta_reference/agents.py +++ b/llama_stack/providers/inline/agents/meta_reference/agents.py @@ -175,7 +175,7 @@ async def submit_tool_response_messages( session_id: str, turn_id: str, tool_response_messages: List[ToolResponseMessage], - ) -> Union[Turn, AsyncIterator[AgentTurnResponseStreamChunk]]: + ) -> AsyncGenerator: pass async def get_agents_turn(self, agent_id: str, session_id: str, turn_id: str) -> Turn: From 82109749ea8e9728ec9cc28ae45f7bd43b48e652 Mon Sep 17 00:00:00 2001 From: Xi Yan Date: Thu, 20 Feb 2025 13:56:47 -0800 Subject: [PATCH 06/14] rename --- llama_stack/apis/agents/agents.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/llama_stack/apis/agents/agents.py b/llama_stack/apis/agents/agents.py index 2f374b6388..b88fb61465 100644 --- a/llama_stack/apis/agents/agents.py +++ b/llama_stack/apis/agents/agents.py @@ -194,7 +194,7 @@ class AgentTurnResponseEventType(Enum): turn_start = "turn_start" turn_complete = "turn_complete" - turn_pending = "turn_pending" + turn_awaiting_input = "turn_awaiting_input" @json_schema_type @@ -344,15 +344,20 @@ async def create_agent_turn( ) -> Union[Turn, AsyncIterator[AgentTurnResponseStreamChunk]]: ... @webmethod( - route="/agents/{agent_id}/session/{session_id}/turn/{turn_id}/submit_tool_response_messages", + route="/agents/{agent_id}/session/{session_id}/turn/{turn_id}/continue", method="POST", ) - async def submit_tool_response_messages( + async def continue_agent_turn( self, agent_id: str, session_id: str, turn_id: str, - tool_response_messages: List[ToolResponseMessage], + new_messages: List[ + Union[ + UserMessage, + ToolResponseMessage, + ] + ], ) -> Union[Turn, AsyncIterator[AgentTurnResponseStreamChunk]]: ... @webmethod( From ff876771023e6d379f6eb95485155028fee27d43 Mon Sep 17 00:00:00 2001 From: Xi Yan Date: Thu, 20 Feb 2025 14:00:08 -0800 Subject: [PATCH 07/14] rename --- llama_stack/apis/agents/agents.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/llama_stack/apis/agents/agents.py b/llama_stack/apis/agents/agents.py index b88fb61465..c3301d5eda 100644 --- a/llama_stack/apis/agents/agents.py +++ b/llama_stack/apis/agents/agents.py @@ -237,8 +237,10 @@ class AgentTurnResponseTurnCompletePayload(BaseModel): @json_schema_type -class AgentTurnResponseTurnPendingPayload(BaseModel): - event_type: Literal[AgentTurnResponseEventType.turn_pending.value] = AgentTurnResponseEventType.turn_pending.value +class AgentTurnResponseTurnAwaitingInputPayload(BaseModel): + event_type: Literal[AgentTurnResponseEventType.turn_awaiting_input.value] = ( + AgentTurnResponseEventType.turn_awaiting_input.value + ) turn: Turn From a44d23067698e6c92934155947ea11b1c24d084e Mon Sep 17 00:00:00 2001 From: Xi Yan Date: Thu, 20 Feb 2025 14:02:17 -0800 Subject: [PATCH 08/14] rename --- docs/_static/llama-stack-spec.html | 389 +++++++++--------- docs/_static/llama-stack-spec.yaml | 259 ++++++------ llama_stack/apis/agents/agents.py | 2 +- .../inline/agents/meta_reference/agents.py | 9 +- 4 files changed, 337 insertions(+), 322 deletions(-) diff --git a/docs/_static/llama-stack-spec.html b/docs/_static/llama-stack-spec.html index 9a38ed6927..72db82f28b 100644 --- a/docs/_static/llama-stack-spec.html +++ b/docs/_static/llama-stack-spec.html @@ -554,6 +554,67 @@ } } }, + "/v1/agents/{agent_id}/session/{session_id}/turn/{turn_id}/continue": { + "post": { + "responses": { + "200": { + "description": "A single turn in an interaction with an Agentic System. **OR** streamed agent turn completion response.", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Turn" + } + }, + "text/event-stream": { + "schema": { + "$ref": "#/components/schemas/AgentTurnResponseStreamChunk" + } + } + } + } + }, + "tags": [ + "Agents" + ], + "description": "", + "parameters": [ + { + "name": "agent_id", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + }, + { + "name": "session_id", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + }, + { + "name": "turn_id", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ], + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ContinueAgentTurnRequest" + } + } + }, + "required": true + } + } + }, "/v1/agents": { "post": { "responses": { @@ -2811,67 +2872,6 @@ } } }, - "/v1/agents/{agent_id}/session/{session_id}/turn/{turn_id}/submit_tool_response_messages": { - "post": { - "responses": { - "200": { - "description": "A single turn in an interaction with an Agentic System. **OR** streamed agent turn completion response.", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/Turn" - } - }, - "text/event-stream": { - "schema": { - "$ref": "#/components/schemas/AgentTurnResponseStreamChunk" - } - } - } - } - }, - "tags": [ - "Agents" - ], - "description": "", - "parameters": [ - { - "name": "agent_id", - "in": "path", - "required": true, - "schema": { - "type": "string" - } - }, - { - "name": "session_id", - "in": "path", - "required": true, - "schema": { - "type": "string" - } - }, - { - "name": "turn_id", - "in": "path", - "required": true, - "schema": { - "type": "string" - } - } - ], - "requestBody": { - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/SubmitToolResponseMessagesRequest" - } - } - }, - "required": true - } - } - }, "/v1/post-training/supervised-fine-tune": { "post": { "responses": { @@ -5044,62 +5044,10 @@ "title": "CompletionResponseStreamChunk", "description": "A chunk of a streamed completion response." }, - "CreateAgentRequest": { + "ContinueAgentTurnRequest": { "type": "object", "properties": { - "agent_config": { - "$ref": "#/components/schemas/AgentConfig" - } - }, - "additionalProperties": false, - "required": [ - "agent_config" - ], - "title": "CreateAgentRequest" - }, - "AgentCreateResponse": { - "type": "object", - "properties": { - "agent_id": { - "type": "string" - } - }, - "additionalProperties": false, - "required": [ - "agent_id" - ], - "title": "AgentCreateResponse" - }, - "CreateAgentSessionRequest": { - "type": "object", - "properties": { - "session_name": { - "type": "string" - } - }, - "additionalProperties": false, - "required": [ - "session_name" - ], - "title": "CreateAgentSessionRequest" - }, - "AgentSessionCreateResponse": { - "type": "object", - "properties": { - "session_id": { - "type": "string" - } - }, - "additionalProperties": false, - "required": [ - "session_id" - ], - "title": "AgentSessionCreateResponse" - }, - "CreateAgentTurnRequest": { - "type": "object", - "properties": { - "messages": { + "new_messages": { "type": "array", "items": { "oneOf": [ @@ -5111,61 +5059,13 @@ } ] } - }, - "stream": { - "type": "boolean" - }, - "documents": { - "type": "array", - "items": { - "type": "object", - "properties": { - "content": { - "oneOf": [ - { - "type": "string" - }, - { - "$ref": "#/components/schemas/InterleavedContentItem" - }, - { - "type": "array", - "items": { - "$ref": "#/components/schemas/InterleavedContentItem" - } - }, - { - "$ref": "#/components/schemas/URL" - } - ] - }, - "mime_type": { - "type": "string" - } - }, - "additionalProperties": false, - "required": [ - "content", - "mime_type" - ], - "title": "Document" - } - }, - "toolgroups": { - "type": "array", - "items": { - "$ref": "#/components/schemas/AgentTool" - } - }, - "tool_config": { - "$ref": "#/components/schemas/ToolConfig" } }, "additionalProperties": false, "required": [ - "messages" + "new_messages" ], - "title": "CreateAgentTurnRequest" + "title": "ContinueAgentTurnRequest" }, "InferenceStep": { "type": "object", @@ -5547,7 +5447,7 @@ "$ref": "#/components/schemas/AgentTurnResponseTurnCompletePayload" }, { - "$ref": "#/components/schemas/AgentTurnResponseTurnPendingPayload" + "$ref": "#/components/schemas/AgentTurnResponseTurnAwaitingInputPayload" } ], "discriminator": { @@ -5558,7 +5458,7 @@ "step_complete": "#/components/schemas/AgentTurnResponseStepCompletePayload", "turn_start": "#/components/schemas/AgentTurnResponseTurnStartPayload", "turn_complete": "#/components/schemas/AgentTurnResponseTurnCompletePayload", - "turn_pending": "#/components/schemas/AgentTurnResponseTurnPendingPayload" + "turn_awaiting_input": "#/components/schemas/AgentTurnResponseTurnAwaitingInputPayload" } } }, @@ -5721,13 +5621,13 @@ "title": "AgentTurnResponseStreamChunk", "description": "streamed agent turn completion response." }, - "AgentTurnResponseTurnCompletePayload": { + "AgentTurnResponseTurnAwaitingInputPayload": { "type": "object", "properties": { "event_type": { "type": "string", - "const": "turn_complete", - "default": "turn_complete" + "const": "turn_awaiting_input", + "default": "turn_awaiting_input" }, "turn": { "$ref": "#/components/schemas/Turn" @@ -5738,15 +5638,15 @@ "event_type", "turn" ], - "title": "AgentTurnResponseTurnCompletePayload" + "title": "AgentTurnResponseTurnAwaitingInputPayload" }, - "AgentTurnResponseTurnPendingPayload": { + "AgentTurnResponseTurnCompletePayload": { "type": "object", "properties": { "event_type": { "type": "string", - "const": "turn_pending", - "default": "turn_pending" + "const": "turn_complete", + "default": "turn_complete" }, "turn": { "$ref": "#/components/schemas/Turn" @@ -5757,7 +5657,7 @@ "event_type", "turn" ], - "title": "AgentTurnResponseTurnPendingPayload" + "title": "AgentTurnResponseTurnCompletePayload" }, "AgentTurnResponseTurnStartPayload": { "type": "object", @@ -5778,6 +5678,129 @@ ], "title": "AgentTurnResponseTurnStartPayload" }, + "CreateAgentRequest": { + "type": "object", + "properties": { + "agent_config": { + "$ref": "#/components/schemas/AgentConfig" + } + }, + "additionalProperties": false, + "required": [ + "agent_config" + ], + "title": "CreateAgentRequest" + }, + "AgentCreateResponse": { + "type": "object", + "properties": { + "agent_id": { + "type": "string" + } + }, + "additionalProperties": false, + "required": [ + "agent_id" + ], + "title": "AgentCreateResponse" + }, + "CreateAgentSessionRequest": { + "type": "object", + "properties": { + "session_name": { + "type": "string" + } + }, + "additionalProperties": false, + "required": [ + "session_name" + ], + "title": "CreateAgentSessionRequest" + }, + "AgentSessionCreateResponse": { + "type": "object", + "properties": { + "session_id": { + "type": "string" + } + }, + "additionalProperties": false, + "required": [ + "session_id" + ], + "title": "AgentSessionCreateResponse" + }, + "CreateAgentTurnRequest": { + "type": "object", + "properties": { + "messages": { + "type": "array", + "items": { + "oneOf": [ + { + "$ref": "#/components/schemas/UserMessage" + }, + { + "$ref": "#/components/schemas/ToolResponseMessage" + } + ] + } + }, + "stream": { + "type": "boolean" + }, + "documents": { + "type": "array", + "items": { + "type": "object", + "properties": { + "content": { + "oneOf": [ + { + "type": "string" + }, + { + "$ref": "#/components/schemas/InterleavedContentItem" + }, + { + "type": "array", + "items": { + "$ref": "#/components/schemas/InterleavedContentItem" + } + }, + { + "$ref": "#/components/schemas/URL" + } + ] + }, + "mime_type": { + "type": "string" + } + }, + "additionalProperties": false, + "required": [ + "content", + "mime_type" + ], + "title": "Document" + } + }, + "toolgroups": { + "type": "array", + "items": { + "$ref": "#/components/schemas/AgentTool" + } + }, + "tool_config": { + "$ref": "#/components/schemas/ToolConfig" + } + }, + "additionalProperties": false, + "required": [ + "messages" + ], + "title": "CreateAgentTurnRequest" + }, "CreateUploadSessionRequest": { "type": "object", "properties": { @@ -8724,22 +8747,6 @@ ], "title": "ScoreBatchResponse" }, - "SubmitToolResponseMessagesRequest": { - "type": "object", - "properties": { - "tool_response_messages": { - "type": "array", - "items": { - "$ref": "#/components/schemas/ToolResponseMessage" - } - } - }, - "additionalProperties": false, - "required": [ - "tool_response_messages" - ], - "title": "SubmitToolResponseMessagesRequest" - }, "AlgorithmConfig": { "oneOf": [ { diff --git a/docs/_static/llama-stack-spec.yaml b/docs/_static/llama-stack-spec.yaml index fe94e34ffa..efffb3f801 100644 --- a/docs/_static/llama-stack-spec.yaml +++ b/docs/_static/llama-stack-spec.yaml @@ -329,6 +329,45 @@ paths: schema: $ref: '#/components/schemas/CompletionRequest' required: true + /v1/agents/{agent_id}/session/{session_id}/turn/{turn_id}/continue: + post: + responses: + '200': + description: >- + A single turn in an interaction with an Agentic System. **OR** streamed + agent turn completion response. + content: + application/json: + schema: + $ref: '#/components/schemas/Turn' + text/event-stream: + schema: + $ref: '#/components/schemas/AgentTurnResponseStreamChunk' + tags: + - Agents + description: '' + parameters: + - name: agent_id + in: path + required: true + schema: + type: string + - name: session_id + in: path + required: true + schema: + type: string + - name: turn_id + in: path + required: true + schema: + type: string + requestBody: + content: + application/json: + schema: + $ref: '#/components/schemas/ContinueAgentTurnRequest' + required: true /v1/agents: post: responses: @@ -1703,45 +1742,6 @@ paths: schema: $ref: '#/components/schemas/ScoreBatchRequest' required: true - /v1/agents/{agent_id}/session/{session_id}/turn/{turn_id}/submit_tool_response_messages: - post: - responses: - '200': - description: >- - A single turn in an interaction with an Agentic System. **OR** streamed - agent turn completion response. - content: - application/json: - schema: - $ref: '#/components/schemas/Turn' - text/event-stream: - schema: - $ref: '#/components/schemas/AgentTurnResponseStreamChunk' - tags: - - Agents - description: '' - parameters: - - name: agent_id - in: path - required: true - schema: - type: string - - name: session_id - in: path - required: true - schema: - type: string - - name: turn_id - in: path - required: true - schema: - type: string - requestBody: - content: - application/json: - schema: - $ref: '#/components/schemas/SubmitToolResponseMessagesRequest' - required: true /v1/post-training/supervised-fine-tune: post: responses: @@ -3240,83 +3240,19 @@ components: title: CompletionResponseStreamChunk description: >- A chunk of a streamed completion response. - CreateAgentRequest: - type: object - properties: - agent_config: - $ref: '#/components/schemas/AgentConfig' - additionalProperties: false - required: - - agent_config - title: CreateAgentRequest - AgentCreateResponse: - type: object - properties: - agent_id: - type: string - additionalProperties: false - required: - - agent_id - title: AgentCreateResponse - CreateAgentSessionRequest: + ContinueAgentTurnRequest: type: object properties: - session_name: - type: string - additionalProperties: false - required: - - session_name - title: CreateAgentSessionRequest - AgentSessionCreateResponse: - type: object - properties: - session_id: - type: string - additionalProperties: false - required: - - session_id - title: AgentSessionCreateResponse - CreateAgentTurnRequest: - type: object - properties: - messages: + new_messages: type: array items: oneOf: - $ref: '#/components/schemas/UserMessage' - $ref: '#/components/schemas/ToolResponseMessage' - stream: - type: boolean - documents: - type: array - items: - type: object - properties: - content: - oneOf: - - type: string - - $ref: '#/components/schemas/InterleavedContentItem' - - type: array - items: - $ref: '#/components/schemas/InterleavedContentItem' - - $ref: '#/components/schemas/URL' - mime_type: - type: string - additionalProperties: false - required: - - content - - mime_type - title: Document - toolgroups: - type: array - items: - $ref: '#/components/schemas/AgentTool' - tool_config: - $ref: '#/components/schemas/ToolConfig' additionalProperties: false required: - - messages - title: CreateAgentTurnRequest + - new_messages + title: ContinueAgentTurnRequest InferenceStep: type: object properties: @@ -3565,7 +3501,7 @@ components: - $ref: '#/components/schemas/AgentTurnResponseStepCompletePayload' - $ref: '#/components/schemas/AgentTurnResponseTurnStartPayload' - $ref: '#/components/schemas/AgentTurnResponseTurnCompletePayload' - - $ref: '#/components/schemas/AgentTurnResponseTurnPendingPayload' + - $ref: '#/components/schemas/AgentTurnResponseTurnAwaitingInputPayload' discriminator: propertyName: event_type mapping: @@ -3574,7 +3510,7 @@ components: step_complete: '#/components/schemas/AgentTurnResponseStepCompletePayload' turn_start: '#/components/schemas/AgentTurnResponseTurnStartPayload' turn_complete: '#/components/schemas/AgentTurnResponseTurnCompletePayload' - turn_pending: '#/components/schemas/AgentTurnResponseTurnPendingPayload' + turn_awaiting_input: '#/components/schemas/AgentTurnResponseTurnAwaitingInputPayload' AgentTurnResponseStepCompletePayload: type: object properties: @@ -3681,34 +3617,35 @@ components: - event title: AgentTurnResponseStreamChunk description: streamed agent turn completion response. - AgentTurnResponseTurnCompletePayload: + "AgentTurnResponseTurnAwaitingInputPayload": type: object properties: event_type: type: string - const: turn_complete - default: turn_complete + const: turn_awaiting_input + default: turn_awaiting_input turn: $ref: '#/components/schemas/Turn' additionalProperties: false required: - event_type - turn - title: AgentTurnResponseTurnCompletePayload - AgentTurnResponseTurnPendingPayload: + title: >- + AgentTurnResponseTurnAwaitingInputPayload + AgentTurnResponseTurnCompletePayload: type: object properties: event_type: type: string - const: turn_pending - default: turn_pending + const: turn_complete + default: turn_complete turn: $ref: '#/components/schemas/Turn' additionalProperties: false required: - event_type - turn - title: AgentTurnResponseTurnPendingPayload + title: AgentTurnResponseTurnCompletePayload AgentTurnResponseTurnStartPayload: type: object properties: @@ -3723,6 +3660,83 @@ components: - event_type - turn_id title: AgentTurnResponseTurnStartPayload + CreateAgentRequest: + type: object + properties: + agent_config: + $ref: '#/components/schemas/AgentConfig' + additionalProperties: false + required: + - agent_config + title: CreateAgentRequest + AgentCreateResponse: + type: object + properties: + agent_id: + type: string + additionalProperties: false + required: + - agent_id + title: AgentCreateResponse + CreateAgentSessionRequest: + type: object + properties: + session_name: + type: string + additionalProperties: false + required: + - session_name + title: CreateAgentSessionRequest + AgentSessionCreateResponse: + type: object + properties: + session_id: + type: string + additionalProperties: false + required: + - session_id + title: AgentSessionCreateResponse + CreateAgentTurnRequest: + type: object + properties: + messages: + type: array + items: + oneOf: + - $ref: '#/components/schemas/UserMessage' + - $ref: '#/components/schemas/ToolResponseMessage' + stream: + type: boolean + documents: + type: array + items: + type: object + properties: + content: + oneOf: + - type: string + - $ref: '#/components/schemas/InterleavedContentItem' + - type: array + items: + $ref: '#/components/schemas/InterleavedContentItem' + - $ref: '#/components/schemas/URL' + mime_type: + type: string + additionalProperties: false + required: + - content + - mime_type + title: Document + toolgroups: + type: array + items: + $ref: '#/components/schemas/AgentTool' + tool_config: + $ref: '#/components/schemas/ToolConfig' + additionalProperties: false + required: + - messages + title: CreateAgentTurnRequest CreateUploadSessionRequest: type: object properties: @@ -5606,17 +5620,6 @@ components: required: - results title: ScoreBatchResponse - SubmitToolResponseMessagesRequest: - type: object - properties: - tool_response_messages: - type: array - items: - $ref: '#/components/schemas/ToolResponseMessage' - additionalProperties: false - required: - - tool_response_messages - title: SubmitToolResponseMessagesRequest AlgorithmConfig: oneOf: - $ref: '#/components/schemas/LoraFinetuningConfig' diff --git a/llama_stack/apis/agents/agents.py b/llama_stack/apis/agents/agents.py index c3301d5eda..05ddf587a6 100644 --- a/llama_stack/apis/agents/agents.py +++ b/llama_stack/apis/agents/agents.py @@ -252,7 +252,7 @@ class AgentTurnResponseTurnAwaitingInputPayload(BaseModel): AgentTurnResponseStepCompletePayload, AgentTurnResponseTurnStartPayload, AgentTurnResponseTurnCompletePayload, - AgentTurnResponseTurnPendingPayload, + AgentTurnResponseTurnAwaitingInputPayload, ], Field(discriminator="event_type"), ], diff --git a/llama_stack/providers/inline/agents/meta_reference/agents.py b/llama_stack/providers/inline/agents/meta_reference/agents.py index c208388be8..cb81929684 100644 --- a/llama_stack/providers/inline/agents/meta_reference/agents.py +++ b/llama_stack/providers/inline/agents/meta_reference/agents.py @@ -169,12 +169,17 @@ async def _create_agent_turn_streaming( async for event in agent.create_and_execute_turn(request): yield event - async def submit_tool_response_messages( + async def continue_agent_turn( self, agent_id: str, session_id: str, turn_id: str, - tool_response_messages: List[ToolResponseMessage], + new_messages: List[ + Union[ + UserMessage, + ToolResponseMessage, + ] + ], ) -> AsyncGenerator: pass From afee71604fe1b8b54d109236d0e2ed22c1e39082 Mon Sep 17 00:00:00 2001 From: Xi Yan Date: Thu, 20 Feb 2025 15:07:18 -0800 Subject: [PATCH 09/14] api --- docs/_static/llama-stack-spec.html | 444 ++++++++++++++--------------- docs/_static/llama-stack-spec.yaml | 274 +++++++++--------- 2 files changed, 359 insertions(+), 359 deletions(-) diff --git a/docs/_static/llama-stack-spec.html b/docs/_static/llama-stack-spec.html index 62b8c07f2b..32cbd60f55 100644 --- a/docs/_static/llama-stack-spec.html +++ b/docs/_static/llama-stack-spec.html @@ -3951,228 +3951,7 @@ "title": "CompletionResponseStreamChunk", "description": "A chunk of a streamed completion response." }, - "AgentConfig": { - "type": "object", - "properties": { - "sampling_params": { - "$ref": "#/components/schemas/SamplingParams" - }, - "input_shields": { - "type": "array", - "items": { - "type": "string" - } - }, - "output_shields": { - "type": "array", - "items": { - "type": "string" - } - }, - "toolgroups": { - "type": "array", - "items": { - "$ref": "#/components/schemas/AgentTool" - } - }, - "client_tools": { - "type": "array", - "items": { - "$ref": "#/components/schemas/ToolDef" - } - }, - "tool_choice": { - "type": "string", - "enum": [ - "auto", - "required", - "none" - ], - "title": "ToolChoice", - "description": "Whether tool use is required or automatic. This is a hint to the model which may not be followed. It depends on the Instruction Following capabilities of the model.", - "deprecated": true - }, - "tool_prompt_format": { - "type": "string", - "enum": [ - "json", - "function_tag", - "python_list" - ], - "title": "ToolPromptFormat", - "description": "Prompt format for calling custom / zero shot tools.", - "deprecated": true - }, - "tool_config": { - "$ref": "#/components/schemas/ToolConfig" - }, - "max_infer_iters": { - "type": "integer", - "default": 10 - }, - "model": { - "type": "string" - }, - "instructions": { - "type": "string" - }, - "enable_session_persistence": { - "type": "boolean", - "default": false - }, - "response_format": { - "$ref": "#/components/schemas/ResponseFormat" - } - }, - "additionalProperties": false, - "required": [ - "model", - "instructions" - ], - "title": "AgentConfig" - }, - "AgentTool": { - "oneOf": [ - { - "type": "string" - }, - { - "type": "object", - "properties": { - "name": { - "type": "string" - }, - "args": { - "type": "object", - "additionalProperties": { - "oneOf": [ - { - "type": "null" - }, - { - "type": "boolean" - }, - { - "type": "number" - }, - { - "type": "string" - }, - { - "type": "array" - }, - { - "type": "object" - } - ] - } - } - }, - "additionalProperties": false, - "required": [ - "name", - "args" - ], - "title": "AgentToolGroupWithArgs" - } - ] - }, - "ToolDef": { - "type": "object", - "properties": { - "name": { - "type": "string" - }, - "description": { - "type": "string" - }, - "parameters": { - "type": "array", - "items": { - "$ref": "#/components/schemas/ToolParameter" - } - }, - "metadata": { - "type": "object", - "additionalProperties": { - "oneOf": [ - { - "type": "null" - }, - { - "type": "boolean" - }, - { - "type": "number" - }, - { - "type": "string" - }, - { - "type": "array" - }, - { - "type": "object" - } - ] - } - } - }, - "additionalProperties": false, - "required": [ - "name" - ], - "title": "ToolDef" - }, - "ToolParameter": { - "type": "object", - "properties": { - "name": { - "type": "string" - }, - "parameter_type": { - "type": "string" - }, - "description": { - "type": "string" - }, - "required": { - "type": "boolean", - "default": true - }, - "default": { - "oneOf": [ - { - "type": "null" - }, - { - "type": "boolean" - }, - { - "type": "number" - }, - { - "type": "string" - }, - { - "type": "array" - }, - { - "type": "object" - } - ] - } - }, - "additionalProperties": false, - "required": [ - "name", - "parameter_type", - "description", - "required" - ], - "title": "ToolParameter" - }, - "CreateAgentRequest": { + "ContinueAgentTurnRequest": { "type": "object", "properties": { "new_messages": { @@ -4806,6 +4585,227 @@ ], "title": "AgentTurnResponseTurnStartPayload" }, + "AgentConfig": { + "type": "object", + "properties": { + "sampling_params": { + "$ref": "#/components/schemas/SamplingParams" + }, + "input_shields": { + "type": "array", + "items": { + "type": "string" + } + }, + "output_shields": { + "type": "array", + "items": { + "type": "string" + } + }, + "toolgroups": { + "type": "array", + "items": { + "$ref": "#/components/schemas/AgentTool" + } + }, + "client_tools": { + "type": "array", + "items": { + "$ref": "#/components/schemas/ToolDef" + } + }, + "tool_choice": { + "type": "string", + "enum": [ + "auto", + "required", + "none" + ], + "title": "ToolChoice", + "description": "Whether tool use is required or automatic. This is a hint to the model which may not be followed. It depends on the Instruction Following capabilities of the model.", + "deprecated": true + }, + "tool_prompt_format": { + "type": "string", + "enum": [ + "json", + "function_tag", + "python_list" + ], + "title": "ToolPromptFormat", + "description": "Prompt format for calling custom / zero shot tools.", + "deprecated": true + }, + "tool_config": { + "$ref": "#/components/schemas/ToolConfig" + }, + "max_infer_iters": { + "type": "integer", + "default": 10 + }, + "model": { + "type": "string" + }, + "instructions": { + "type": "string" + }, + "enable_session_persistence": { + "type": "boolean", + "default": false + }, + "response_format": { + "$ref": "#/components/schemas/ResponseFormat" + } + }, + "additionalProperties": false, + "required": [ + "model", + "instructions" + ], + "title": "AgentConfig" + }, + "AgentTool": { + "oneOf": [ + { + "type": "string" + }, + { + "type": "object", + "properties": { + "name": { + "type": "string" + }, + "args": { + "type": "object", + "additionalProperties": { + "oneOf": [ + { + "type": "null" + }, + { + "type": "boolean" + }, + { + "type": "number" + }, + { + "type": "string" + }, + { + "type": "array" + }, + { + "type": "object" + } + ] + } + } + }, + "additionalProperties": false, + "required": [ + "name", + "args" + ], + "title": "AgentToolGroupWithArgs" + } + ] + }, + "ToolDef": { + "type": "object", + "properties": { + "name": { + "type": "string" + }, + "description": { + "type": "string" + }, + "parameters": { + "type": "array", + "items": { + "$ref": "#/components/schemas/ToolParameter" + } + }, + "metadata": { + "type": "object", + "additionalProperties": { + "oneOf": [ + { + "type": "null" + }, + { + "type": "boolean" + }, + { + "type": "number" + }, + { + "type": "string" + }, + { + "type": "array" + }, + { + "type": "object" + } + ] + } + } + }, + "additionalProperties": false, + "required": [ + "name" + ], + "title": "ToolDef" + }, + "ToolParameter": { + "type": "object", + "properties": { + "name": { + "type": "string" + }, + "parameter_type": { + "type": "string" + }, + "description": { + "type": "string" + }, + "required": { + "type": "boolean", + "default": true + }, + "default": { + "oneOf": [ + { + "type": "null" + }, + { + "type": "boolean" + }, + { + "type": "number" + }, + { + "type": "string" + }, + { + "type": "array" + }, + { + "type": "object" + } + ] + } + }, + "additionalProperties": false, + "required": [ + "name", + "parameter_type", + "description", + "required" + ], + "title": "ToolParameter" + }, "CreateAgentRequest": { "type": "object", "properties": { diff --git a/docs/_static/llama-stack-spec.yaml b/docs/_static/llama-stack-spec.yaml index bf82c34f84..139f515071 100644 --- a/docs/_static/llama-stack-spec.yaml +++ b/docs/_static/llama-stack-spec.yaml @@ -2570,143 +2570,7 @@ components: title: CompletionResponseStreamChunk description: >- A chunk of a streamed completion response. - AgentConfig: - type: object - properties: - sampling_params: - $ref: '#/components/schemas/SamplingParams' - input_shields: - type: array - items: - type: string - output_shields: - type: array - items: - type: string - toolgroups: - type: array - items: - $ref: '#/components/schemas/AgentTool' - client_tools: - type: array - items: - $ref: '#/components/schemas/ToolDef' - tool_choice: - type: string - enum: - - auto - - required - - none - title: ToolChoice - description: >- - Whether tool use is required or automatic. This is a hint to the model - which may not be followed. It depends on the Instruction Following capabilities - of the model. - deprecated: true - tool_prompt_format: - type: string - enum: - - json - - function_tag - - python_list - title: ToolPromptFormat - description: >- - Prompt format for calling custom / zero shot tools. - deprecated: true - tool_config: - $ref: '#/components/schemas/ToolConfig' - max_infer_iters: - type: integer - default: 10 - model: - type: string - instructions: - type: string - enable_session_persistence: - type: boolean - default: false - response_format: - $ref: '#/components/schemas/ResponseFormat' - additionalProperties: false - required: - - model - - instructions - title: AgentConfig - AgentTool: - oneOf: - - type: string - - type: object - properties: - name: - type: string - args: - type: object - additionalProperties: - oneOf: - - type: 'null' - - type: boolean - - type: number - - type: string - - type: array - - type: object - additionalProperties: false - required: - - name - - args - title: AgentToolGroupWithArgs - ToolDef: - type: object - properties: - name: - type: string - description: - type: string - parameters: - type: array - items: - $ref: '#/components/schemas/ToolParameter' - metadata: - type: object - additionalProperties: - oneOf: - - type: 'null' - - type: boolean - - type: number - - type: string - - type: array - - type: object - additionalProperties: false - required: - - name - title: ToolDef - ToolParameter: - type: object - properties: - name: - type: string - parameter_type: - type: string - description: - type: string - required: - type: boolean - default: true - default: - oneOf: - - type: 'null' - - type: boolean - - type: number - - type: string - - type: array - - type: object - additionalProperties: false - required: - - name - - parameter_type - - description - - required - title: ToolParameter - CreateAgentRequest: + ContinueAgentTurnRequest: type: object properties: new_messages: @@ -3126,6 +2990,142 @@ components: - event_type - turn_id title: AgentTurnResponseTurnStartPayload + AgentConfig: + type: object + properties: + sampling_params: + $ref: '#/components/schemas/SamplingParams' + input_shields: + type: array + items: + type: string + output_shields: + type: array + items: + type: string + toolgroups: + type: array + items: + $ref: '#/components/schemas/AgentTool' + client_tools: + type: array + items: + $ref: '#/components/schemas/ToolDef' + tool_choice: + type: string + enum: + - auto + - required + - none + title: ToolChoice + description: >- + Whether tool use is required or automatic. This is a hint to the model + which may not be followed. It depends on the Instruction Following capabilities + of the model. + deprecated: true + tool_prompt_format: + type: string + enum: + - json + - function_tag + - python_list + title: ToolPromptFormat + description: >- + Prompt format for calling custom / zero shot tools. + deprecated: true + tool_config: + $ref: '#/components/schemas/ToolConfig' + max_infer_iters: + type: integer + default: 10 + model: + type: string + instructions: + type: string + enable_session_persistence: + type: boolean + default: false + response_format: + $ref: '#/components/schemas/ResponseFormat' + additionalProperties: false + required: + - model + - instructions + title: AgentConfig + AgentTool: + oneOf: + - type: string + - type: object + properties: + name: + type: string + args: + type: object + additionalProperties: + oneOf: + - type: 'null' + - type: boolean + - type: number + - type: string + - type: array + - type: object + additionalProperties: false + required: + - name + - args + title: AgentToolGroupWithArgs + ToolDef: + type: object + properties: + name: + type: string + description: + type: string + parameters: + type: array + items: + $ref: '#/components/schemas/ToolParameter' + metadata: + type: object + additionalProperties: + oneOf: + - type: 'null' + - type: boolean + - type: number + - type: string + - type: array + - type: object + additionalProperties: false + required: + - name + title: ToolDef + ToolParameter: + type: object + properties: + name: + type: string + parameter_type: + type: string + description: + type: string + required: + type: boolean + default: true + default: + oneOf: + - type: 'null' + - type: boolean + - type: number + - type: string + - type: array + - type: object + additionalProperties: false + required: + - name + - parameter_type + - description + - required + title: ToolParameter CreateAgentRequest: type: object properties: From 5644d10c82f98ff1e61c251824182f98db0d81f6 Mon Sep 17 00:00:00 2001 From: Xi Yan Date: Thu, 20 Feb 2025 15:26:37 -0800 Subject: [PATCH 10/14] remove usermessages --- docs/_static/llama-stack-spec.html | 13 +++---------- docs/_static/llama-stack-spec.yaml | 8 +++----- llama_stack/apis/agents/agents.py | 7 +------ .../inline/agents/meta_reference/agents.py | 7 +------ 4 files changed, 8 insertions(+), 27 deletions(-) diff --git a/docs/_static/llama-stack-spec.html b/docs/_static/llama-stack-spec.html index 32cbd60f55..d2b3529df8 100644 --- a/docs/_static/llama-stack-spec.html +++ b/docs/_static/llama-stack-spec.html @@ -3954,23 +3954,16 @@ "ContinueAgentTurnRequest": { "type": "object", "properties": { - "new_messages": { + "tool_responses": { "type": "array", "items": { - "oneOf": [ - { - "$ref": "#/components/schemas/UserMessage" - }, - { - "$ref": "#/components/schemas/ToolResponseMessage" - } - ] + "$ref": "#/components/schemas/ToolResponseMessage" } } }, "additionalProperties": false, "required": [ - "new_messages" + "tool_responses" ], "title": "ContinueAgentTurnRequest" }, diff --git a/docs/_static/llama-stack-spec.yaml b/docs/_static/llama-stack-spec.yaml index 139f515071..121098a54a 100644 --- a/docs/_static/llama-stack-spec.yaml +++ b/docs/_static/llama-stack-spec.yaml @@ -2573,15 +2573,13 @@ components: ContinueAgentTurnRequest: type: object properties: - new_messages: + tool_responses: type: array items: - oneOf: - - $ref: '#/components/schemas/UserMessage' - - $ref: '#/components/schemas/ToolResponseMessage' + $ref: '#/components/schemas/ToolResponseMessage' additionalProperties: false required: - - new_messages + - tool_responses title: ContinueAgentTurnRequest InferenceStep: type: object diff --git a/llama_stack/apis/agents/agents.py b/llama_stack/apis/agents/agents.py index 05ddf587a6..c9554098f6 100644 --- a/llama_stack/apis/agents/agents.py +++ b/llama_stack/apis/agents/agents.py @@ -354,12 +354,7 @@ async def continue_agent_turn( agent_id: str, session_id: str, turn_id: str, - new_messages: List[ - Union[ - UserMessage, - ToolResponseMessage, - ] - ], + tool_responses: List[ToolResponseMessage], ) -> Union[Turn, AsyncIterator[AgentTurnResponseStreamChunk]]: ... @webmethod( diff --git a/llama_stack/providers/inline/agents/meta_reference/agents.py b/llama_stack/providers/inline/agents/meta_reference/agents.py index cb81929684..d34fd0b57e 100644 --- a/llama_stack/providers/inline/agents/meta_reference/agents.py +++ b/llama_stack/providers/inline/agents/meta_reference/agents.py @@ -174,12 +174,7 @@ async def continue_agent_turn( agent_id: str, session_id: str, turn_id: str, - new_messages: List[ - Union[ - UserMessage, - ToolResponseMessage, - ] - ], + tool_responses: List[ToolResponseMessage], ) -> AsyncGenerator: pass From 8fe38d128d5aa3e71f7a5c75220f0ea22ca3c55e Mon Sep 17 00:00:00 2001 From: Xi Yan Date: Thu, 20 Feb 2025 16:58:45 -0800 Subject: [PATCH 11/14] streaming flag --- docs/_static/llama-stack-spec.html | 3 +++ docs/_static/llama-stack-spec.yaml | 2 ++ llama_stack/apis/agents/agents.py | 1 + llama_stack/providers/inline/agents/meta_reference/agents.py | 1 + 4 files changed, 7 insertions(+) diff --git a/docs/_static/llama-stack-spec.html b/docs/_static/llama-stack-spec.html index d2b3529df8..2627bd454a 100644 --- a/docs/_static/llama-stack-spec.html +++ b/docs/_static/llama-stack-spec.html @@ -3959,6 +3959,9 @@ "items": { "$ref": "#/components/schemas/ToolResponseMessage" } + }, + "stream": { + "type": "boolean" } }, "additionalProperties": false, diff --git a/docs/_static/llama-stack-spec.yaml b/docs/_static/llama-stack-spec.yaml index 121098a54a..7779252592 100644 --- a/docs/_static/llama-stack-spec.yaml +++ b/docs/_static/llama-stack-spec.yaml @@ -2577,6 +2577,8 @@ components: type: array items: $ref: '#/components/schemas/ToolResponseMessage' + stream: + type: boolean additionalProperties: false required: - tool_responses diff --git a/llama_stack/apis/agents/agents.py b/llama_stack/apis/agents/agents.py index c9554098f6..a83538b359 100644 --- a/llama_stack/apis/agents/agents.py +++ b/llama_stack/apis/agents/agents.py @@ -355,6 +355,7 @@ async def continue_agent_turn( session_id: str, turn_id: str, tool_responses: List[ToolResponseMessage], + stream: Optional[bool] = False, ) -> Union[Turn, AsyncIterator[AgentTurnResponseStreamChunk]]: ... @webmethod( diff --git a/llama_stack/providers/inline/agents/meta_reference/agents.py b/llama_stack/providers/inline/agents/meta_reference/agents.py index d34fd0b57e..dfbc41262c 100644 --- a/llama_stack/providers/inline/agents/meta_reference/agents.py +++ b/llama_stack/providers/inline/agents/meta_reference/agents.py @@ -175,6 +175,7 @@ async def continue_agent_turn( session_id: str, turn_id: str, tool_responses: List[ToolResponseMessage], + stream: Optional[bool] = False, ) -> AsyncGenerator: pass From 122b20c1428301f9899e44c96b9c92f0e4810994 Mon Sep 17 00:00:00 2001 From: Xi Yan Date: Thu, 20 Feb 2025 19:42:56 -0800 Subject: [PATCH 12/14] continue to resume --- docs/_static/llama-stack-spec.html | 1390 ++++++++--------- docs/_static/llama-stack-spec.yaml | 514 +++--- llama_stack/apis/agents/agents.py | 4 +- .../inline/agents/meta_reference/agents.py | 2 +- 4 files changed, 955 insertions(+), 955 deletions(-) diff --git a/docs/_static/llama-stack-spec.html b/docs/_static/llama-stack-spec.html index 2627bd454a..0a5d93d802 100644 --- a/docs/_static/llama-stack-spec.html +++ b/docs/_static/llama-stack-spec.html @@ -274,67 +274,6 @@ } } }, - "/v1/agents/{agent_id}/session/{session_id}/turn/{turn_id}/continue": { - "post": { - "responses": { - "200": { - "description": "A single turn in an interaction with an Agentic System. **OR** streamed agent turn completion response.", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/Turn" - } - }, - "text/event-stream": { - "schema": { - "$ref": "#/components/schemas/AgentTurnResponseStreamChunk" - } - } - } - } - }, - "tags": [ - "Agents" - ], - "description": "", - "parameters": [ - { - "name": "agent_id", - "in": "path", - "required": true, - "schema": { - "type": "string" - } - }, - { - "name": "session_id", - "in": "path", - "required": true, - "schema": { - "type": "string" - } - }, - { - "name": "turn_id", - "in": "path", - "required": true, - "schema": { - "type": "string" - } - } - ], - "requestBody": { - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/ContinueAgentTurnRequest" - } - } - }, - "required": true - } - } - }, "/v1/agents": { "post": { "responses": { @@ -2376,6 +2315,67 @@ } } }, + "/v1/agents/{agent_id}/session/{session_id}/turn/{turn_id}/resume": { + "post": { + "responses": { + "200": { + "description": "A single turn in an interaction with an Agentic System. **OR** streamed agent turn completion response.", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Turn" + } + }, + "text/event-stream": { + "schema": { + "$ref": "#/components/schemas/AgentTurnResponseStreamChunk" + } + } + } + } + }, + "tags": [ + "Agents" + ], + "description": "", + "parameters": [ + { + "name": "agent_id", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + }, + { + "name": "session_id", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + }, + { + "name": "turn_id", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ], + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ResumeAgentTurnRequest" + } + } + }, + "required": true + } + } + }, "/v1/eval/benchmarks/{benchmark_id}/jobs": { "post": { "responses": { @@ -3951,113 +3951,152 @@ "title": "CompletionResponseStreamChunk", "description": "A chunk of a streamed completion response." }, - "ContinueAgentTurnRequest": { + "AgentConfig": { "type": "object", "properties": { - "tool_responses": { + "sampling_params": { + "$ref": "#/components/schemas/SamplingParams" + }, + "input_shields": { "type": "array", "items": { - "$ref": "#/components/schemas/ToolResponseMessage" + "type": "string" } }, - "stream": { - "type": "boolean" - } - }, - "additionalProperties": false, - "required": [ - "tool_responses" - ], - "title": "ContinueAgentTurnRequest" - }, - "InferenceStep": { - "type": "object", - "properties": { - "turn_id": { - "type": "string" + "output_shields": { + "type": "array", + "items": { + "type": "string" + } }, - "step_id": { - "type": "string" + "toolgroups": { + "type": "array", + "items": { + "$ref": "#/components/schemas/AgentTool" + } }, - "started_at": { - "type": "string", - "format": "date-time" + "client_tools": { + "type": "array", + "items": { + "$ref": "#/components/schemas/ToolDef" + } }, - "completed_at": { + "tool_choice": { "type": "string", - "format": "date-time" + "enum": [ + "auto", + "required", + "none" + ], + "title": "ToolChoice", + "description": "Whether tool use is required or automatic. This is a hint to the model which may not be followed. It depends on the Instruction Following capabilities of the model.", + "deprecated": true }, - "step_type": { + "tool_prompt_format": { "type": "string", - "const": "inference", - "default": "inference" - }, - "model_response": { - "$ref": "#/components/schemas/CompletionMessage" - } - }, - "additionalProperties": false, - "required": [ - "turn_id", - "step_id", - "step_type", - "model_response" - ], - "title": "InferenceStep" - }, - "MemoryRetrievalStep": { - "type": "object", - "properties": { - "turn_id": { - "type": "string" - }, - "step_id": { - "type": "string" + "enum": [ + "json", + "function_tag", + "python_list" + ], + "title": "ToolPromptFormat", + "description": "Prompt format for calling custom / zero shot tools.", + "deprecated": true }, - "started_at": { - "type": "string", - "format": "date-time" + "tool_config": { + "$ref": "#/components/schemas/ToolConfig" }, - "completed_at": { - "type": "string", - "format": "date-time" + "max_infer_iters": { + "type": "integer", + "default": 10 }, - "step_type": { - "type": "string", - "const": "memory_retrieval", - "default": "memory_retrieval" + "model": { + "type": "string" }, - "vector_db_ids": { + "instructions": { "type": "string" }, - "inserted_context": { - "$ref": "#/components/schemas/InterleavedContent" + "enable_session_persistence": { + "type": "boolean", + "default": false + }, + "response_format": { + "$ref": "#/components/schemas/ResponseFormat" } }, "additionalProperties": false, "required": [ - "turn_id", - "step_id", - "step_type", - "vector_db_ids", - "inserted_context" + "model", + "instructions" ], - "title": "MemoryRetrievalStep" + "title": "AgentConfig" }, - "SafetyViolation": { - "type": "object", - "properties": { - "violation_level": { - "$ref": "#/components/schemas/ViolationLevel" - }, - "user_message": { + "AgentTool": { + "oneOf": [ + { "type": "string" }, - "metadata": { + { "type": "object", - "additionalProperties": { - "oneOf": [ - { + "properties": { + "name": { + "type": "string" + }, + "args": { + "type": "object", + "additionalProperties": { + "oneOf": [ + { + "type": "null" + }, + { + "type": "boolean" + }, + { + "type": "number" + }, + { + "type": "string" + }, + { + "type": "array" + }, + { + "type": "object" + } + ] + } + } + }, + "additionalProperties": false, + "required": [ + "name", + "args" + ], + "title": "AgentToolGroupWithArgs" + } + ] + }, + "ToolDef": { + "type": "object", + "properties": { + "name": { + "type": "string" + }, + "description": { + "type": "string" + }, + "parameters": { + "type": "array", + "items": { + "$ref": "#/components/schemas/ToolParameter" + } + }, + "metadata": { + "type": "object", + "additionalProperties": { + "oneOf": [ + { "type": "null" }, { @@ -4081,135 +4120,114 @@ }, "additionalProperties": false, "required": [ - "violation_level", - "metadata" + "name" ], - "title": "SafetyViolation" + "title": "ToolDef" }, - "ShieldCallStep": { + "ToolParameter": { "type": "object", "properties": { - "turn_id": { + "name": { "type": "string" }, - "step_id": { + "parameter_type": { "type": "string" }, - "started_at": { - "type": "string", - "format": "date-time" - }, - "completed_at": { - "type": "string", - "format": "date-time" + "description": { + "type": "string" }, - "step_type": { - "type": "string", - "const": "shield_call", - "default": "shield_call" + "required": { + "type": "boolean", + "default": true }, - "violation": { - "$ref": "#/components/schemas/SafetyViolation" + "default": { + "oneOf": [ + { + "type": "null" + }, + { + "type": "boolean" + }, + { + "type": "number" + }, + { + "type": "string" + }, + { + "type": "array" + }, + { + "type": "object" + } + ] } }, "additionalProperties": false, "required": [ - "turn_id", - "step_id", - "step_type" + "name", + "parameter_type", + "description", + "required" ], - "title": "ShieldCallStep" + "title": "ToolParameter" }, - "ToolExecutionStep": { + "CreateAgentRequest": { "type": "object", "properties": { - "turn_id": { - "type": "string" - }, - "step_id": { - "type": "string" - }, - "started_at": { - "type": "string", - "format": "date-time" - }, - "completed_at": { - "type": "string", - "format": "date-time" - }, - "step_type": { - "type": "string", - "const": "tool_execution", - "default": "tool_execution" - }, - "tool_calls": { - "type": "array", - "items": { - "$ref": "#/components/schemas/ToolCall" - } - }, - "tool_responses": { - "type": "array", - "items": { - "$ref": "#/components/schemas/ToolResponse" - } + "agent_config": { + "$ref": "#/components/schemas/AgentConfig" } }, "additionalProperties": false, "required": [ - "turn_id", - "step_id", - "step_type", - "tool_calls", - "tool_responses" + "agent_config" ], - "title": "ToolExecutionStep" + "title": "CreateAgentRequest" }, - "ToolResponse": { + "AgentCreateResponse": { "type": "object", "properties": { - "call_id": { + "agent_id": { "type": "string" - }, - "tool_name": { - "oneOf": [ - { - "type": "string", - "enum": [ - "brave_search", - "wolfram_alpha", - "photogen", - "code_interpreter" - ], - "title": "BuiltinTool" - }, - { - "type": "string" - } - ] - }, - "content": { - "$ref": "#/components/schemas/InterleavedContent" } }, "additionalProperties": false, "required": [ - "call_id", - "tool_name", - "content" + "agent_id" ], - "title": "ToolResponse" + "title": "AgentCreateResponse" }, - "Turn": { + "CreateAgentSessionRequest": { "type": "object", "properties": { - "turn_id": { + "session_name": { "type": "string" - }, + } + }, + "additionalProperties": false, + "required": [ + "session_name" + ], + "title": "CreateAgentSessionRequest" + }, + "AgentSessionCreateResponse": { + "type": "object", + "properties": { "session_id": { "type": "string" - }, - "input_messages": { + } + }, + "additionalProperties": false, + "required": [ + "session_id" + ], + "title": "AgentSessionCreateResponse" + }, + "CreateAgentTurnRequest": { + "type": "object", + "properties": { + "messages": { "type": "array", "items": { "oneOf": [ @@ -4222,38 +4240,10 @@ ] } }, - "steps": { - "type": "array", - "items": { - "oneOf": [ - { - "$ref": "#/components/schemas/InferenceStep" - }, - { - "$ref": "#/components/schemas/ToolExecutionStep" - }, - { - "$ref": "#/components/schemas/ShieldCallStep" - }, - { - "$ref": "#/components/schemas/MemoryRetrievalStep" - } - ], - "discriminator": { - "propertyName": "step_type", - "mapping": { - "inference": "#/components/schemas/InferenceStep", - "tool_execution": "#/components/schemas/ToolExecutionStep", - "shield_call": "#/components/schemas/ShieldCallStep", - "memory_retrieval": "#/components/schemas/MemoryRetrievalStep" - } - } - } - }, - "output_message": { - "$ref": "#/components/schemas/CompletionMessage" + "stream": { + "type": "boolean" }, - "output_attachments": { + "documents": { "type": "array", "items": { "type": "object", @@ -4286,194 +4276,106 @@ "content", "mime_type" ], - "title": "Attachment" + "title": "Document" } }, - "started_at": { - "type": "string", - "format": "date-time" + "toolgroups": { + "type": "array", + "items": { + "$ref": "#/components/schemas/AgentTool" + } }, - "completed_at": { - "type": "string", - "format": "date-time" + "tool_config": { + "$ref": "#/components/schemas/ToolConfig" } }, "additionalProperties": false, "required": [ - "turn_id", - "session_id", - "input_messages", - "steps", - "output_message", - "started_at" - ], - "title": "Turn", - "description": "A single turn in an interaction with an Agentic System." - }, - "ViolationLevel": { - "type": "string", - "enum": [ - "info", - "warn", - "error" + "messages" ], - "title": "ViolationLevel" + "title": "CreateAgentTurnRequest" }, - "AgentTurnResponseEvent": { + "InferenceStep": { "type": "object", "properties": { - "payload": { - "$ref": "#/components/schemas/AgentTurnResponseEventPayload" - } - }, - "additionalProperties": false, - "required": [ - "payload" - ], - "title": "AgentTurnResponseEvent" - }, - "AgentTurnResponseEventPayload": { - "oneOf": [ - { - "$ref": "#/components/schemas/AgentTurnResponseStepStartPayload" - }, - { - "$ref": "#/components/schemas/AgentTurnResponseStepProgressPayload" - }, - { - "$ref": "#/components/schemas/AgentTurnResponseStepCompletePayload" + "turn_id": { + "type": "string" }, - { - "$ref": "#/components/schemas/AgentTurnResponseTurnStartPayload" + "step_id": { + "type": "string" }, - { - "$ref": "#/components/schemas/AgentTurnResponseTurnCompletePayload" + "started_at": { + "type": "string", + "format": "date-time" }, - { - "$ref": "#/components/schemas/AgentTurnResponseTurnAwaitingInputPayload" - } - ], - "discriminator": { - "propertyName": "event_type", - "mapping": { - "step_start": "#/components/schemas/AgentTurnResponseStepStartPayload", - "step_progress": "#/components/schemas/AgentTurnResponseStepProgressPayload", - "step_complete": "#/components/schemas/AgentTurnResponseStepCompletePayload", - "turn_start": "#/components/schemas/AgentTurnResponseTurnStartPayload", - "turn_complete": "#/components/schemas/AgentTurnResponseTurnCompletePayload", - "turn_awaiting_input": "#/components/schemas/AgentTurnResponseTurnAwaitingInputPayload" - } - } - }, - "AgentTurnResponseStepCompletePayload": { - "type": "object", - "properties": { - "event_type": { + "completed_at": { "type": "string", - "const": "step_complete", - "default": "step_complete" + "format": "date-time" }, "step_type": { "type": "string", - "enum": [ - "inference", - "tool_execution", - "shield_call", - "memory_retrieval" - ], - "title": "StepType" - }, - "step_id": { - "type": "string" + "const": "inference", + "default": "inference" }, - "step_details": { - "oneOf": [ - { - "$ref": "#/components/schemas/InferenceStep" - }, - { - "$ref": "#/components/schemas/ToolExecutionStep" - }, - { - "$ref": "#/components/schemas/ShieldCallStep" - }, - { - "$ref": "#/components/schemas/MemoryRetrievalStep" - } - ], - "discriminator": { - "propertyName": "step_type", - "mapping": { - "inference": "#/components/schemas/InferenceStep", - "tool_execution": "#/components/schemas/ToolExecutionStep", - "shield_call": "#/components/schemas/ShieldCallStep", - "memory_retrieval": "#/components/schemas/MemoryRetrievalStep" - } - } + "model_response": { + "$ref": "#/components/schemas/CompletionMessage" } }, "additionalProperties": false, "required": [ - "event_type", - "step_type", + "turn_id", "step_id", - "step_details" + "step_type", + "model_response" ], - "title": "AgentTurnResponseStepCompletePayload" + "title": "InferenceStep" }, - "AgentTurnResponseStepProgressPayload": { + "MemoryRetrievalStep": { "type": "object", "properties": { - "event_type": { + "turn_id": { + "type": "string" + }, + "step_id": { + "type": "string" + }, + "started_at": { "type": "string", - "const": "step_progress", - "default": "step_progress" + "format": "date-time" + }, + "completed_at": { + "type": "string", + "format": "date-time" }, "step_type": { "type": "string", - "enum": [ - "inference", - "tool_execution", - "shield_call", - "memory_retrieval" - ], - "title": "StepType" + "const": "memory_retrieval", + "default": "memory_retrieval" }, - "step_id": { + "vector_db_ids": { "type": "string" }, - "delta": { - "$ref": "#/components/schemas/ContentDelta" + "inserted_context": { + "$ref": "#/components/schemas/InterleavedContent" } }, "additionalProperties": false, "required": [ - "event_type", - "step_type", + "turn_id", "step_id", - "delta" + "step_type", + "vector_db_ids", + "inserted_context" ], - "title": "AgentTurnResponseStepProgressPayload" + "title": "MemoryRetrievalStep" }, - "AgentTurnResponseStepStartPayload": { + "SafetyViolation": { "type": "object", "properties": { - "event_type": { - "type": "string", - "const": "step_start", - "default": "step_start" - }, - "step_type": { - "type": "string", - "enum": [ - "inference", - "tool_execution", - "shield_call", - "memory_retrieval" - ], - "title": "StepType" + "violation_level": { + "$ref": "#/components/schemas/ViolationLevel" }, - "step_id": { + "user_message": { "type": "string" }, "metadata": { @@ -4504,223 +4406,400 @@ }, "additionalProperties": false, "required": [ - "event_type", - "step_type", - "step_id" - ], - "title": "AgentTurnResponseStepStartPayload" - }, - "AgentTurnResponseStreamChunk": { - "type": "object", - "properties": { - "event": { - "$ref": "#/components/schemas/AgentTurnResponseEvent" - } - }, - "additionalProperties": false, - "required": [ - "event" + "violation_level", + "metadata" ], - "title": "AgentTurnResponseStreamChunk", - "description": "streamed agent turn completion response." + "title": "SafetyViolation" }, - "AgentTurnResponseTurnAwaitingInputPayload": { + "ShieldCallStep": { "type": "object", "properties": { - "event_type": { + "turn_id": { + "type": "string" + }, + "step_id": { + "type": "string" + }, + "started_at": { "type": "string", - "const": "turn_awaiting_input", - "default": "turn_awaiting_input" + "format": "date-time" }, - "turn": { - "$ref": "#/components/schemas/Turn" + "completed_at": { + "type": "string", + "format": "date-time" + }, + "step_type": { + "type": "string", + "const": "shield_call", + "default": "shield_call" + }, + "violation": { + "$ref": "#/components/schemas/SafetyViolation" } }, "additionalProperties": false, "required": [ - "event_type", - "turn" + "turn_id", + "step_id", + "step_type" ], - "title": "AgentTurnResponseTurnAwaitingInputPayload" + "title": "ShieldCallStep" }, - "AgentTurnResponseTurnCompletePayload": { + "ToolExecutionStep": { "type": "object", "properties": { - "event_type": { - "type": "string", - "const": "turn_complete", - "default": "turn_complete" + "turn_id": { + "type": "string" }, - "turn": { - "$ref": "#/components/schemas/Turn" + "step_id": { + "type": "string" + }, + "started_at": { + "type": "string", + "format": "date-time" + }, + "completed_at": { + "type": "string", + "format": "date-time" + }, + "step_type": { + "type": "string", + "const": "tool_execution", + "default": "tool_execution" + }, + "tool_calls": { + "type": "array", + "items": { + "$ref": "#/components/schemas/ToolCall" + } + }, + "tool_responses": { + "type": "array", + "items": { + "$ref": "#/components/schemas/ToolResponse" + } } }, "additionalProperties": false, "required": [ - "event_type", - "turn" + "turn_id", + "step_id", + "step_type", + "tool_calls", + "tool_responses" ], - "title": "AgentTurnResponseTurnCompletePayload" + "title": "ToolExecutionStep" }, - "AgentTurnResponseTurnStartPayload": { + "ToolResponse": { "type": "object", "properties": { - "event_type": { - "type": "string", - "const": "turn_start", - "default": "turn_start" - }, - "turn_id": { + "call_id": { "type": "string" + }, + "tool_name": { + "oneOf": [ + { + "type": "string", + "enum": [ + "brave_search", + "wolfram_alpha", + "photogen", + "code_interpreter" + ], + "title": "BuiltinTool" + }, + { + "type": "string" + } + ] + }, + "content": { + "$ref": "#/components/schemas/InterleavedContent" } }, "additionalProperties": false, "required": [ - "event_type", - "turn_id" + "call_id", + "tool_name", + "content" ], - "title": "AgentTurnResponseTurnStartPayload" + "title": "ToolResponse" }, - "AgentConfig": { + "Turn": { "type": "object", "properties": { - "sampling_params": { - "$ref": "#/components/schemas/SamplingParams" + "turn_id": { + "type": "string" }, - "input_shields": { - "type": "array", - "items": { - "type": "string" - } + "session_id": { + "type": "string" }, - "output_shields": { + "input_messages": { "type": "array", "items": { - "type": "string" + "oneOf": [ + { + "$ref": "#/components/schemas/UserMessage" + }, + { + "$ref": "#/components/schemas/ToolResponseMessage" + } + ] } }, - "toolgroups": { + "steps": { "type": "array", "items": { - "$ref": "#/components/schemas/AgentTool" + "oneOf": [ + { + "$ref": "#/components/schemas/InferenceStep" + }, + { + "$ref": "#/components/schemas/ToolExecutionStep" + }, + { + "$ref": "#/components/schemas/ShieldCallStep" + }, + { + "$ref": "#/components/schemas/MemoryRetrievalStep" + } + ], + "discriminator": { + "propertyName": "step_type", + "mapping": { + "inference": "#/components/schemas/InferenceStep", + "tool_execution": "#/components/schemas/ToolExecutionStep", + "shield_call": "#/components/schemas/ShieldCallStep", + "memory_retrieval": "#/components/schemas/MemoryRetrievalStep" + } + } } }, - "client_tools": { + "output_message": { + "$ref": "#/components/schemas/CompletionMessage" + }, + "output_attachments": { "type": "array", "items": { - "$ref": "#/components/schemas/ToolDef" + "type": "object", + "properties": { + "content": { + "oneOf": [ + { + "type": "string" + }, + { + "$ref": "#/components/schemas/InterleavedContentItem" + }, + { + "type": "array", + "items": { + "$ref": "#/components/schemas/InterleavedContentItem" + } + }, + { + "$ref": "#/components/schemas/URL" + } + ] + }, + "mime_type": { + "type": "string" + } + }, + "additionalProperties": false, + "required": [ + "content", + "mime_type" + ], + "title": "Attachment" } }, - "tool_choice": { + "started_at": { "type": "string", - "enum": [ - "auto", - "required", - "none" - ], - "title": "ToolChoice", - "description": "Whether tool use is required or automatic. This is a hint to the model which may not be followed. It depends on the Instruction Following capabilities of the model.", - "deprecated": true + "format": "date-time" }, - "tool_prompt_format": { + "completed_at": { "type": "string", - "enum": [ - "json", - "function_tag", - "python_list" - ], - "title": "ToolPromptFormat", - "description": "Prompt format for calling custom / zero shot tools.", - "deprecated": true - }, - "tool_config": { - "$ref": "#/components/schemas/ToolConfig" - }, - "max_infer_iters": { - "type": "integer", - "default": 10 - }, - "model": { - "type": "string" - }, - "instructions": { - "type": "string" - }, - "enable_session_persistence": { - "type": "boolean", - "default": false - }, - "response_format": { - "$ref": "#/components/schemas/ResponseFormat" + "format": "date-time" } }, "additionalProperties": false, "required": [ - "model", - "instructions" + "turn_id", + "session_id", + "input_messages", + "steps", + "output_message", + "started_at" ], - "title": "AgentConfig" + "title": "Turn", + "description": "A single turn in an interaction with an Agentic System." }, - "AgentTool": { + "ViolationLevel": { + "type": "string", + "enum": [ + "info", + "warn", + "error" + ], + "title": "ViolationLevel" + }, + "AgentTurnResponseEvent": { + "type": "object", + "properties": { + "payload": { + "$ref": "#/components/schemas/AgentTurnResponseEventPayload" + } + }, + "additionalProperties": false, + "required": [ + "payload" + ], + "title": "AgentTurnResponseEvent" + }, + "AgentTurnResponseEventPayload": { "oneOf": [ { - "type": "string" + "$ref": "#/components/schemas/AgentTurnResponseStepStartPayload" }, { - "type": "object", - "properties": { - "name": { - "type": "string" + "$ref": "#/components/schemas/AgentTurnResponseStepProgressPayload" + }, + { + "$ref": "#/components/schemas/AgentTurnResponseStepCompletePayload" + }, + { + "$ref": "#/components/schemas/AgentTurnResponseTurnStartPayload" + }, + { + "$ref": "#/components/schemas/AgentTurnResponseTurnCompletePayload" + }, + { + "$ref": "#/components/schemas/AgentTurnResponseTurnAwaitingInputPayload" + } + ], + "discriminator": { + "propertyName": "event_type", + "mapping": { + "step_start": "#/components/schemas/AgentTurnResponseStepStartPayload", + "step_progress": "#/components/schemas/AgentTurnResponseStepProgressPayload", + "step_complete": "#/components/schemas/AgentTurnResponseStepCompletePayload", + "turn_start": "#/components/schemas/AgentTurnResponseTurnStartPayload", + "turn_complete": "#/components/schemas/AgentTurnResponseTurnCompletePayload", + "turn_awaiting_input": "#/components/schemas/AgentTurnResponseTurnAwaitingInputPayload" + } + } + }, + "AgentTurnResponseStepCompletePayload": { + "type": "object", + "properties": { + "event_type": { + "type": "string", + "const": "step_complete", + "default": "step_complete" + }, + "step_type": { + "type": "string", + "enum": [ + "inference", + "tool_execution", + "shield_call", + "memory_retrieval" + ], + "title": "StepType" + }, + "step_id": { + "type": "string" + }, + "step_details": { + "oneOf": [ + { + "$ref": "#/components/schemas/InferenceStep" }, - "args": { - "type": "object", - "additionalProperties": { - "oneOf": [ - { - "type": "null" - }, - { - "type": "boolean" - }, - { - "type": "number" - }, - { - "type": "string" - }, - { - "type": "array" - }, - { - "type": "object" - } - ] - } + { + "$ref": "#/components/schemas/ToolExecutionStep" + }, + { + "$ref": "#/components/schemas/ShieldCallStep" + }, + { + "$ref": "#/components/schemas/MemoryRetrievalStep" } - }, - "additionalProperties": false, - "required": [ - "name", - "args" ], - "title": "AgentToolGroupWithArgs" + "discriminator": { + "propertyName": "step_type", + "mapping": { + "inference": "#/components/schemas/InferenceStep", + "tool_execution": "#/components/schemas/ToolExecutionStep", + "shield_call": "#/components/schemas/ShieldCallStep", + "memory_retrieval": "#/components/schemas/MemoryRetrievalStep" + } + } + } + }, + "additionalProperties": false, + "required": [ + "event_type", + "step_type", + "step_id", + "step_details" + ], + "title": "AgentTurnResponseStepCompletePayload" + }, + "AgentTurnResponseStepProgressPayload": { + "type": "object", + "properties": { + "event_type": { + "type": "string", + "const": "step_progress", + "default": "step_progress" + }, + "step_type": { + "type": "string", + "enum": [ + "inference", + "tool_execution", + "shield_call", + "memory_retrieval" + ], + "title": "StepType" + }, + "step_id": { + "type": "string" + }, + "delta": { + "$ref": "#/components/schemas/ContentDelta" } - ] + }, + "additionalProperties": false, + "required": [ + "event_type", + "step_type", + "step_id", + "delta" + ], + "title": "AgentTurnResponseStepProgressPayload" }, - "ToolDef": { + "AgentTurnResponseStepStartPayload": { "type": "object", "properties": { - "name": { - "type": "string" + "event_type": { + "type": "string", + "const": "step_start", + "default": "step_start" }, - "description": { - "type": "string" + "step_type": { + "type": "string", + "enum": [ + "inference", + "tool_execution", + "shield_call", + "memory_retrieval" + ], + "title": "StepType" }, - "parameters": { - "type": "array", - "items": { - "$ref": "#/components/schemas/ToolParameter" - } + "step_id": { + "type": "string" }, "metadata": { "type": "object", @@ -4750,180 +4829,82 @@ }, "additionalProperties": false, "required": [ - "name" - ], - "title": "ToolDef" - }, - "ToolParameter": { - "type": "object", - "properties": { - "name": { - "type": "string" - }, - "parameter_type": { - "type": "string" - }, - "description": { - "type": "string" - }, - "required": { - "type": "boolean", - "default": true - }, - "default": { - "oneOf": [ - { - "type": "null" - }, - { - "type": "boolean" - }, - { - "type": "number" - }, - { - "type": "string" - }, - { - "type": "array" - }, - { - "type": "object" - } - ] - } - }, - "additionalProperties": false, - "required": [ - "name", - "parameter_type", - "description", - "required" - ], - "title": "ToolParameter" - }, - "CreateAgentRequest": { - "type": "object", - "properties": { - "agent_config": { - "$ref": "#/components/schemas/AgentConfig" - } - }, - "additionalProperties": false, - "required": [ - "agent_config" + "event_type", + "step_type", + "step_id" ], - "title": "CreateAgentRequest" + "title": "AgentTurnResponseStepStartPayload" }, - "AgentCreateResponse": { + "AgentTurnResponseStreamChunk": { "type": "object", "properties": { - "agent_id": { - "type": "string" + "event": { + "$ref": "#/components/schemas/AgentTurnResponseEvent" } }, "additionalProperties": false, "required": [ - "agent_id" + "event" ], - "title": "AgentCreateResponse" + "title": "AgentTurnResponseStreamChunk", + "description": "streamed agent turn completion response." }, - "CreateAgentSessionRequest": { + "AgentTurnResponseTurnAwaitingInputPayload": { "type": "object", "properties": { - "session_name": { - "type": "string" + "event_type": { + "type": "string", + "const": "turn_awaiting_input", + "default": "turn_awaiting_input" + }, + "turn": { + "$ref": "#/components/schemas/Turn" } }, "additionalProperties": false, "required": [ - "session_name" + "event_type", + "turn" ], - "title": "CreateAgentSessionRequest" + "title": "AgentTurnResponseTurnAwaitingInputPayload" }, - "AgentSessionCreateResponse": { + "AgentTurnResponseTurnCompletePayload": { "type": "object", "properties": { - "session_id": { - "type": "string" + "event_type": { + "type": "string", + "const": "turn_complete", + "default": "turn_complete" + }, + "turn": { + "$ref": "#/components/schemas/Turn" } }, "additionalProperties": false, "required": [ - "session_id" + "event_type", + "turn" ], - "title": "AgentSessionCreateResponse" + "title": "AgentTurnResponseTurnCompletePayload" }, - "CreateAgentTurnRequest": { + "AgentTurnResponseTurnStartPayload": { "type": "object", "properties": { - "messages": { - "type": "array", - "items": { - "oneOf": [ - { - "$ref": "#/components/schemas/UserMessage" - }, - { - "$ref": "#/components/schemas/ToolResponseMessage" - } - ] - } - }, - "stream": { - "type": "boolean" - }, - "documents": { - "type": "array", - "items": { - "type": "object", - "properties": { - "content": { - "oneOf": [ - { - "type": "string" - }, - { - "$ref": "#/components/schemas/InterleavedContentItem" - }, - { - "type": "array", - "items": { - "$ref": "#/components/schemas/InterleavedContentItem" - } - }, - { - "$ref": "#/components/schemas/URL" - } - ] - }, - "mime_type": { - "type": "string" - } - }, - "additionalProperties": false, - "required": [ - "content", - "mime_type" - ], - "title": "Document" - } - }, - "toolgroups": { - "type": "array", - "items": { - "$ref": "#/components/schemas/AgentTool" - } + "event_type": { + "type": "string", + "const": "turn_start", + "default": "turn_start" }, - "tool_config": { - "$ref": "#/components/schemas/ToolConfig" + "turn_id": { + "type": "string" } }, "additionalProperties": false, "required": [ - "messages" + "event_type", + "turn_id" ], - "title": "CreateAgentTurnRequest" + "title": "AgentTurnResponseTurnStartPayload" }, "CreateUploadSessionRequest": { "type": "object", @@ -8118,6 +8099,25 @@ ], "title": "RegisterVectorDbRequest" }, + "ResumeAgentTurnRequest": { + "type": "object", + "properties": { + "tool_responses": { + "type": "array", + "items": { + "$ref": "#/components/schemas/ToolResponseMessage" + } + }, + "stream": { + "type": "boolean" + } + }, + "additionalProperties": false, + "required": [ + "tool_responses" + ], + "title": "ResumeAgentTurnRequest" + }, "RunEvalRequest": { "type": "object", "properties": { diff --git a/docs/_static/llama-stack-spec.yaml b/docs/_static/llama-stack-spec.yaml index 7779252592..c05eef95eb 100644 --- a/docs/_static/llama-stack-spec.yaml +++ b/docs/_static/llama-stack-spec.yaml @@ -160,45 +160,6 @@ paths: schema: $ref: '#/components/schemas/CompletionRequest' required: true - /v1/agents/{agent_id}/session/{session_id}/turn/{turn_id}/continue: - post: - responses: - '200': - description: >- - A single turn in an interaction with an Agentic System. **OR** streamed - agent turn completion response. - content: - application/json: - schema: - $ref: '#/components/schemas/Turn' - text/event-stream: - schema: - $ref: '#/components/schemas/AgentTurnResponseStreamChunk' - tags: - - Agents - description: '' - parameters: - - name: agent_id - in: path - required: true - schema: - type: string - - name: session_id - in: path - required: true - schema: - type: string - - name: turn_id - in: path - required: true - schema: - type: string - requestBody: - content: - application/json: - schema: - $ref: '#/components/schemas/ContinueAgentTurnRequest' - required: true /v1/agents: post: responses: @@ -1440,6 +1401,45 @@ paths: schema: $ref: '#/components/schemas/QueryTracesRequest' required: true + /v1/agents/{agent_id}/session/{session_id}/turn/{turn_id}/resume: + post: + responses: + '200': + description: >- + A single turn in an interaction with an Agentic System. **OR** streamed + agent turn completion response. + content: + application/json: + schema: + $ref: '#/components/schemas/Turn' + text/event-stream: + schema: + $ref: '#/components/schemas/AgentTurnResponseStreamChunk' + tags: + - Agents + description: '' + parameters: + - name: agent_id + in: path + required: true + schema: + type: string + - name: session_id + in: path + required: true + schema: + type: string + - name: turn_id + in: path + required: true + schema: + type: string + requestBody: + content: + application/json: + schema: + $ref: '#/components/schemas/ResumeAgentTurnRequest' + required: true /v1/eval/benchmarks/{benchmark_id}/jobs: post: responses: @@ -2570,19 +2570,219 @@ components: title: CompletionResponseStreamChunk description: >- A chunk of a streamed completion response. - ContinueAgentTurnRequest: + AgentConfig: type: object properties: - tool_responses: + sampling_params: + $ref: '#/components/schemas/SamplingParams' + input_shields: type: array items: - $ref: '#/components/schemas/ToolResponseMessage' + type: string + output_shields: + type: array + items: + type: string + toolgroups: + type: array + items: + $ref: '#/components/schemas/AgentTool' + client_tools: + type: array + items: + $ref: '#/components/schemas/ToolDef' + tool_choice: + type: string + enum: + - auto + - required + - none + title: ToolChoice + description: >- + Whether tool use is required or automatic. This is a hint to the model + which may not be followed. It depends on the Instruction Following capabilities + of the model. + deprecated: true + tool_prompt_format: + type: string + enum: + - json + - function_tag + - python_list + title: ToolPromptFormat + description: >- + Prompt format for calling custom / zero shot tools. + deprecated: true + tool_config: + $ref: '#/components/schemas/ToolConfig' + max_infer_iters: + type: integer + default: 10 + model: + type: string + instructions: + type: string + enable_session_persistence: + type: boolean + default: false + response_format: + $ref: '#/components/schemas/ResponseFormat' + additionalProperties: false + required: + - model + - instructions + title: AgentConfig + AgentTool: + oneOf: + - type: string + - type: object + properties: + name: + type: string + args: + type: object + additionalProperties: + oneOf: + - type: 'null' + - type: boolean + - type: number + - type: string + - type: array + - type: object + additionalProperties: false + required: + - name + - args + title: AgentToolGroupWithArgs + ToolDef: + type: object + properties: + name: + type: string + description: + type: string + parameters: + type: array + items: + $ref: '#/components/schemas/ToolParameter' + metadata: + type: object + additionalProperties: + oneOf: + - type: 'null' + - type: boolean + - type: number + - type: string + - type: array + - type: object + additionalProperties: false + required: + - name + title: ToolDef + ToolParameter: + type: object + properties: + name: + type: string + parameter_type: + type: string + description: + type: string + required: + type: boolean + default: true + default: + oneOf: + - type: 'null' + - type: boolean + - type: number + - type: string + - type: array + - type: object + additionalProperties: false + required: + - name + - parameter_type + - description + - required + title: ToolParameter + CreateAgentRequest: + type: object + properties: + agent_config: + $ref: '#/components/schemas/AgentConfig' + additionalProperties: false + required: + - agent_config + title: CreateAgentRequest + AgentCreateResponse: + type: object + properties: + agent_id: + type: string + additionalProperties: false + required: + - agent_id + title: AgentCreateResponse + CreateAgentSessionRequest: + type: object + properties: + session_name: + type: string + additionalProperties: false + required: + - session_name + title: CreateAgentSessionRequest + AgentSessionCreateResponse: + type: object + properties: + session_id: + type: string + additionalProperties: false + required: + - session_id + title: AgentSessionCreateResponse + CreateAgentTurnRequest: + type: object + properties: + messages: + type: array + items: + oneOf: + - $ref: '#/components/schemas/UserMessage' + - $ref: '#/components/schemas/ToolResponseMessage' stream: type: boolean + documents: + type: array + items: + type: object + properties: + content: + oneOf: + - type: string + - $ref: '#/components/schemas/InterleavedContentItem' + - type: array + items: + $ref: '#/components/schemas/InterleavedContentItem' + - $ref: '#/components/schemas/URL' + mime_type: + type: string + additionalProperties: false + required: + - content + - mime_type + title: Document + toolgroups: + type: array + items: + $ref: '#/components/schemas/AgentTool' + tool_config: + $ref: '#/components/schemas/ToolConfig' additionalProperties: false required: - - tool_responses - title: ContinueAgentTurnRequest + - messages + title: CreateAgentTurnRequest InferenceStep: type: object properties: @@ -2990,219 +3190,6 @@ components: - event_type - turn_id title: AgentTurnResponseTurnStartPayload - AgentConfig: - type: object - properties: - sampling_params: - $ref: '#/components/schemas/SamplingParams' - input_shields: - type: array - items: - type: string - output_shields: - type: array - items: - type: string - toolgroups: - type: array - items: - $ref: '#/components/schemas/AgentTool' - client_tools: - type: array - items: - $ref: '#/components/schemas/ToolDef' - tool_choice: - type: string - enum: - - auto - - required - - none - title: ToolChoice - description: >- - Whether tool use is required or automatic. This is a hint to the model - which may not be followed. It depends on the Instruction Following capabilities - of the model. - deprecated: true - tool_prompt_format: - type: string - enum: - - json - - function_tag - - python_list - title: ToolPromptFormat - description: >- - Prompt format for calling custom / zero shot tools. - deprecated: true - tool_config: - $ref: '#/components/schemas/ToolConfig' - max_infer_iters: - type: integer - default: 10 - model: - type: string - instructions: - type: string - enable_session_persistence: - type: boolean - default: false - response_format: - $ref: '#/components/schemas/ResponseFormat' - additionalProperties: false - required: - - model - - instructions - title: AgentConfig - AgentTool: - oneOf: - - type: string - - type: object - properties: - name: - type: string - args: - type: object - additionalProperties: - oneOf: - - type: 'null' - - type: boolean - - type: number - - type: string - - type: array - - type: object - additionalProperties: false - required: - - name - - args - title: AgentToolGroupWithArgs - ToolDef: - type: object - properties: - name: - type: string - description: - type: string - parameters: - type: array - items: - $ref: '#/components/schemas/ToolParameter' - metadata: - type: object - additionalProperties: - oneOf: - - type: 'null' - - type: boolean - - type: number - - type: string - - type: array - - type: object - additionalProperties: false - required: - - name - title: ToolDef - ToolParameter: - type: object - properties: - name: - type: string - parameter_type: - type: string - description: - type: string - required: - type: boolean - default: true - default: - oneOf: - - type: 'null' - - type: boolean - - type: number - - type: string - - type: array - - type: object - additionalProperties: false - required: - - name - - parameter_type - - description - - required - title: ToolParameter - CreateAgentRequest: - type: object - properties: - agent_config: - $ref: '#/components/schemas/AgentConfig' - additionalProperties: false - required: - - agent_config - title: CreateAgentRequest - AgentCreateResponse: - type: object - properties: - agent_id: - type: string - additionalProperties: false - required: - - agent_id - title: AgentCreateResponse - CreateAgentSessionRequest: - type: object - properties: - session_name: - type: string - additionalProperties: false - required: - - session_name - title: CreateAgentSessionRequest - AgentSessionCreateResponse: - type: object - properties: - session_id: - type: string - additionalProperties: false - required: - - session_id - title: AgentSessionCreateResponse - CreateAgentTurnRequest: - type: object - properties: - messages: - type: array - items: - oneOf: - - $ref: '#/components/schemas/UserMessage' - - $ref: '#/components/schemas/ToolResponseMessage' - stream: - type: boolean - documents: - type: array - items: - type: object - properties: - content: - oneOf: - - type: string - - $ref: '#/components/schemas/InterleavedContentItem' - - type: array - items: - $ref: '#/components/schemas/InterleavedContentItem' - - $ref: '#/components/schemas/URL' - mime_type: - type: string - additionalProperties: false - required: - - content - - mime_type - title: Document - toolgroups: - type: array - items: - $ref: '#/components/schemas/AgentTool' - tool_config: - $ref: '#/components/schemas/ToolConfig' - additionalProperties: false - required: - - messages - title: CreateAgentTurnRequest CreateUploadSessionRequest: type: object properties: @@ -5248,6 +5235,19 @@ components: - vector_db_id - embedding_model title: RegisterVectorDbRequest + ResumeAgentTurnRequest: + type: object + properties: + tool_responses: + type: array + items: + $ref: '#/components/schemas/ToolResponseMessage' + stream: + type: boolean + additionalProperties: false + required: + - tool_responses + title: ResumeAgentTurnRequest RunEvalRequest: type: object properties: diff --git a/llama_stack/apis/agents/agents.py b/llama_stack/apis/agents/agents.py index a83538b359..18f5a37085 100644 --- a/llama_stack/apis/agents/agents.py +++ b/llama_stack/apis/agents/agents.py @@ -346,10 +346,10 @@ async def create_agent_turn( ) -> Union[Turn, AsyncIterator[AgentTurnResponseStreamChunk]]: ... @webmethod( - route="/agents/{agent_id}/session/{session_id}/turn/{turn_id}/continue", + route="/agents/{agent_id}/session/{session_id}/turn/{turn_id}/resume", method="POST", ) - async def continue_agent_turn( + async def resume_agent_turn( self, agent_id: str, session_id: str, diff --git a/llama_stack/providers/inline/agents/meta_reference/agents.py b/llama_stack/providers/inline/agents/meta_reference/agents.py index dfbc41262c..acacbdfdf0 100644 --- a/llama_stack/providers/inline/agents/meta_reference/agents.py +++ b/llama_stack/providers/inline/agents/meta_reference/agents.py @@ -169,7 +169,7 @@ async def _create_agent_turn_streaming( async for event in agent.create_and_execute_turn(request): yield event - async def continue_agent_turn( + async def resume_agent_turn( self, agent_id: str, session_id: str, From fa0dfdeac2b74dd0cb278decf2754e225df4f609 Mon Sep 17 00:00:00 2001 From: Xi Yan Date: Thu, 20 Feb 2025 19:46:43 -0800 Subject: [PATCH 13/14] resume request --- llama_stack/apis/agents/agents.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/llama_stack/apis/agents/agents.py b/llama_stack/apis/agents/agents.py index 18f5a37085..8fde864e43 100644 --- a/llama_stack/apis/agents/agents.py +++ b/llama_stack/apis/agents/agents.py @@ -297,6 +297,15 @@ class AgentTurnCreateRequest(AgentConfigOverridablePerTurn): tool_config: Optional[ToolConfig] = None +@json_schema_type +class AgentTurnResumeRequest(BaseModel): + agent_id: str + session_id: str + turn_id: str + tool_responses: List[ToolResponseMessage] + stream: Optional[bool] = False + + @json_schema_type class AgentTurnResponseStreamChunk(BaseModel): """streamed agent turn completion response.""" From 36a1e8bc575a70b9a75bfef63f787904cd0d1fea Mon Sep 17 00:00:00 2001 From: Xi Yan Date: Fri, 21 Feb 2025 11:47:21 -0800 Subject: [PATCH 14/14] feat(2/n): agent return turn awaiting input for tool calls (#1187) # What does this PR do? - 1/n: https://github.com/meta-llama/llama-stack/pull/1178 This is a backward compat change. Default `allow_resume_turn=False` to work with client v0.1.3. **Expected Behaviour** 1. User sends out `agent.create_turn`. When user receives a custom client_tool_call. The last chunk should be ```python chunk = AgentTurnResponseStreamChunk( event=AgentTurnResponseEvent( payload=AgentTurnResponseTurnAwaitingInputPayload( turn=turn, ) ) ) ``` There should be a `tool_execution` step. 2. When user sees turn_pending as the event_type or if there's tool call in the response. They will execute the client tools, and submits back the tool response. E.g. ```python client.continue_turn( [ToolResponseMessage(...)] ) ``` The agent will then send back & record `tool_execution` step finish on server. and continue with the execution. [//]: # (If resolving an issue, uncomment and update the line below) [//]: # (Closes #[issue-number]) > NOTE: This PR handles (1). Next PR handle (2). ## Test Plan - Testing w/ Client SDK 0.1.3 ``` LLAMA_STACK_BASE_URL=http://localhost:8321 pytest -v tests/client-sdk/agents/test_agents.py --inference-model meta-llama/Llama-3.1-8B-Instruct ``` image [//]: # (## Documentation) --- docs/_static/llama-stack-spec.html | 16 +- docs/_static/llama-stack-spec.yaml | 19 +- llama_stack/apis/agents/agents.py | 18 +- .../agents/meta_reference/agent_instance.py | 179 ++++++++++++++++-- .../inline/agents/meta_reference/agents.py | 23 ++- .../agents/meta_reference/persistence.py | 14 +- tests/client-sdk/agents/test_agents.py | 8 +- 7 files changed, 248 insertions(+), 29 deletions(-) diff --git a/docs/_static/llama-stack-spec.html b/docs/_static/llama-stack-spec.html index 0a5d93d802..8e8b8804f8 100644 --- a/docs/_static/llama-stack-spec.html +++ b/docs/_static/llama-stack-spec.html @@ -2319,7 +2319,7 @@ "post": { "responses": { "200": { - "description": "A single turn in an interaction with an Agentic System. **OR** streamed agent turn completion response.", + "description": "A Turn object if stream is False, otherwise an AsyncIterator of AgentTurnResponseStreamChunk objects.", "content": { "application/json": { "schema": { @@ -2337,11 +2337,12 @@ "tags": [ "Agents" ], - "description": "", + "description": "Resume an agent turn with executed tool call responses.\nWhen a Turn has the status `awaiting_input` due to pending input from client side tool calls, this endpoint can be used to submit the outputs from the tool calls once they are ready.", "parameters": [ { "name": "agent_id", "in": "path", + "description": "The ID of the agent to resume.", "required": true, "schema": { "type": "string" @@ -2350,6 +2351,7 @@ { "name": "session_id", "in": "path", + "description": "The ID of the session to resume.", "required": true, "schema": { "type": "string" @@ -2358,6 +2360,7 @@ { "name": "turn_id", "in": "path", + "description": "The ID of the turn to resume.", "required": true, "schema": { "type": "string" @@ -4287,6 +4290,9 @@ }, "tool_config": { "$ref": "#/components/schemas/ToolConfig" + }, + "allow_turn_resume": { + "type": "boolean" } }, "additionalProperties": false, @@ -8106,10 +8112,12 @@ "type": "array", "items": { "$ref": "#/components/schemas/ToolResponseMessage" - } + }, + "description": "The tool call responses to resume the turn with." }, "stream": { - "type": "boolean" + "type": "boolean", + "description": "Whether to stream the response." } }, "additionalProperties": false, diff --git a/docs/_static/llama-stack-spec.yaml b/docs/_static/llama-stack-spec.yaml index c05eef95eb..c14ae3d3ad 100644 --- a/docs/_static/llama-stack-spec.yaml +++ b/docs/_static/llama-stack-spec.yaml @@ -1406,8 +1406,8 @@ paths: responses: '200': description: >- - A single turn in an interaction with an Agentic System. **OR** streamed - agent turn completion response. + A Turn object if stream is False, otherwise an AsyncIterator of AgentTurnResponseStreamChunk + objects. content: application/json: schema: @@ -1417,20 +1417,28 @@ paths: $ref: '#/components/schemas/AgentTurnResponseStreamChunk' tags: - Agents - description: '' + description: >- + Resume an agent turn with executed tool call responses. + + When a Turn has the status `awaiting_input` due to pending input from client + side tool calls, this endpoint can be used to submit the outputs from the + tool calls once they are ready. parameters: - name: agent_id in: path + description: The ID of the agent to resume. required: true schema: type: string - name: session_id in: path + description: The ID of the session to resume. required: true schema: type: string - name: turn_id in: path + description: The ID of the turn to resume. required: true schema: type: string @@ -2779,6 +2787,8 @@ components: $ref: '#/components/schemas/AgentTool' tool_config: $ref: '#/components/schemas/ToolConfig' + allow_turn_resume: + type: boolean additionalProperties: false required: - messages @@ -5242,8 +5252,11 @@ components: type: array items: $ref: '#/components/schemas/ToolResponseMessage' + description: >- + The tool call responses to resume the turn with. stream: type: boolean + description: Whether to stream the response. additionalProperties: false required: - tool_responses diff --git a/llama_stack/apis/agents/agents.py b/llama_stack/apis/agents/agents.py index 8fde864e43..c904fdbef3 100644 --- a/llama_stack/apis/agents/agents.py +++ b/llama_stack/apis/agents/agents.py @@ -296,6 +296,9 @@ class AgentTurnCreateRequest(AgentConfigOverridablePerTurn): stream: Optional[bool] = False tool_config: Optional[ToolConfig] = None + # TODO (xiyan): temporary flag, will remove for 0.1.5 + allow_turn_resume: Optional[bool] = False + @json_schema_type class AgentTurnResumeRequest(BaseModel): @@ -352,6 +355,7 @@ async def create_agent_turn( documents: Optional[List[Document]] = None, toolgroups: Optional[List[AgentToolGroup]] = None, tool_config: Optional[ToolConfig] = None, + allow_turn_resume: Optional[bool] = False, ) -> Union[Turn, AsyncIterator[AgentTurnResponseStreamChunk]]: ... @webmethod( @@ -365,7 +369,19 @@ async def resume_agent_turn( turn_id: str, tool_responses: List[ToolResponseMessage], stream: Optional[bool] = False, - ) -> Union[Turn, AsyncIterator[AgentTurnResponseStreamChunk]]: ... + ) -> Union[Turn, AsyncIterator[AgentTurnResponseStreamChunk]]: + """Resume an agent turn with executed tool call responses. + + When a Turn has the status `awaiting_input` due to pending input from client side tool calls, this endpoint can be used to submit the outputs from the tool calls once they are ready. + + :param agent_id: The ID of the agent to resume. + :param session_id: The ID of the session to resume. + :param turn_id: The ID of the turn to resume. + :param tool_responses: The tool call responses to resume the turn with. + :param stream: Whether to stream the response. + :returns: A Turn object if stream is False, otherwise an AsyncIterator of AgentTurnResponseStreamChunk objects. + """ + ... @webmethod( route="/agents/{agent_id}/session/{session_id}/turn/{turn_id}", diff --git a/llama_stack/providers/inline/agents/meta_reference/agent_instance.py b/llama_stack/providers/inline/agents/meta_reference/agent_instance.py index 1c21df57f1..edd253356f 100644 --- a/llama_stack/providers/inline/agents/meta_reference/agent_instance.py +++ b/llama_stack/providers/inline/agents/meta_reference/agent_instance.py @@ -30,8 +30,10 @@ AgentTurnResponseStepProgressPayload, AgentTurnResponseStepStartPayload, AgentTurnResponseStreamChunk, + AgentTurnResponseTurnAwaitingInputPayload, AgentTurnResponseTurnCompletePayload, AgentTurnResponseTurnStartPayload, + AgentTurnResumeRequest, Attachment, Document, InferenceStep, @@ -62,7 +64,11 @@ from llama_stack.apis.safety import Safety from llama_stack.apis.tools import RAGDocument, RAGQueryConfig, ToolGroups, ToolRuntime from llama_stack.apis.vector_io import VectorIO -from llama_stack.models.llama.datatypes import BuiltinTool, ToolCall, ToolParamDefinition +from llama_stack.models.llama.datatypes import ( + BuiltinTool, + ToolCall, + ToolParamDefinition, +) from llama_stack.providers.utils.kvstore import KVStore from llama_stack.providers.utils.memory.vector_store import concat_interleaved_content from llama_stack.providers.utils.telemetry import tracing @@ -151,6 +157,15 @@ def turn_to_messages(self, turn: Turn) -> List[Message]: async def create_session(self, name: str) -> str: return await self.storage.create_session(name) + async def get_messages_from_turns(self, turns: List[Turn]) -> List[Message]: + messages = [] + if self.agent_config.instructions != "": + messages.append(SystemMessage(content=self.agent_config.instructions)) + + for turn in turns: + messages.extend(self.turn_to_messages(turn)) + return messages + async def create_and_execute_turn(self, request: AgentTurnCreateRequest) -> AsyncGenerator: with tracing.span("create_and_execute_turn") as span: span.set_attribute("session_id", request.session_id) @@ -163,14 +178,7 @@ async def create_and_execute_turn(self, request: AgentTurnCreateRequest) -> Asyn raise ValueError(f"Session {request.session_id} not found") turns = await self.storage.get_session_turns(request.session_id) - - messages = [] - if self.agent_config.instructions != "": - messages.append(SystemMessage(content=self.agent_config.instructions)) - - for i, turn in enumerate(turns): - messages.extend(self.turn_to_messages(turn)) - + messages = await self.get_messages_from_turns(turns) messages.extend(request.messages) turn_id = str(uuid.uuid4()) @@ -222,13 +230,136 @@ async def create_and_execute_turn(self, request: AgentTurnCreateRequest) -> Asyn ) await self.storage.add_turn_to_session(request.session_id, turn) - chunk = AgentTurnResponseStreamChunk( + if output_message.tool_calls and request.allow_turn_resume: + chunk = AgentTurnResponseStreamChunk( + event=AgentTurnResponseEvent( + payload=AgentTurnResponseTurnAwaitingInputPayload( + turn=turn, + ) + ) + ) + else: + chunk = AgentTurnResponseStreamChunk( + event=AgentTurnResponseEvent( + payload=AgentTurnResponseTurnCompletePayload( + turn=turn, + ) + ) + ) + + yield chunk + + async def resume_turn(self, request: AgentTurnResumeRequest) -> AsyncGenerator: + with tracing.span("resume_turn") as span: + span.set_attribute("agent_id", self.agent_id) + span.set_attribute("session_id", request.session_id) + span.set_attribute("turn_id", request.turn_id) + span.set_attribute("request", request.model_dump_json()) + assert request.stream is True, "Non-streaming not supported" + + session_info = await self.storage.get_session_info(request.session_id) + if session_info is None: + raise ValueError(f"Session {request.session_id} not found") + + turns = await self.storage.get_session_turns(request.session_id) + messages = await self.get_messages_from_turns(turns) + messages.extend(request.tool_responses) + + last_turn_messages = [ + x for x in messages if isinstance(x, UserMessage) or isinstance(x, ToolResponseMessage) + ] + + # get the steps from the turn id + steps = [] + if len(turns) > 0: + steps = turns[-1].steps + + # mark tool execution step as complete + # if there's no tool execution in progress step (due to storage, or tool call parsing on client), + # we'll create a new tool execution step with current time + in_progress_tool_call_step = await self.storage.get_in_progress_tool_call_step( + request.session_id, request.turn_id + ) + now = datetime.now() + tool_execution_step = ToolExecutionStep( + step_id=(in_progress_tool_call_step.step_id if in_progress_tool_call_step else str(uuid.uuid4())), + turn_id=request.turn_id, + tool_calls=(in_progress_tool_call_step.tool_calls if in_progress_tool_call_step else []), + tool_responses=[ + ToolResponse( + call_id=x.call_id, + tool_name=x.tool_name, + content=x.content, + ) + for x in request.tool_responses + ], + completed_at=now, + started_at=(in_progress_tool_call_step.started_at if in_progress_tool_call_step else now), + ) + steps.append(tool_execution_step) + yield AgentTurnResponseStreamChunk( event=AgentTurnResponseEvent( - payload=AgentTurnResponseTurnCompletePayload( - turn=turn, + payload=AgentTurnResponseStepCompletePayload( + step_type=StepType.tool_execution.value, + step_id=tool_execution_step.step_id, + step_details=tool_execution_step, ) ) ) + + output_message = None + async for chunk in self.run( + session_id=request.session_id, + turn_id=request.turn_id, + input_messages=messages, + sampling_params=self.agent_config.sampling_params, + stream=request.stream, + ): + if isinstance(chunk, CompletionMessage): + output_message = chunk + continue + + assert isinstance(chunk, AgentTurnResponseStreamChunk), f"Unexpected type {type(chunk)}" + event = chunk.event + if event.payload.event_type == AgentTurnResponseEventType.step_complete.value: + steps.append(event.payload.step_details) + + yield chunk + + assert output_message is not None + + last_turn_start_time = datetime.now() + if len(turns) > 0: + last_turn_start_time = turns[-1].started_at + + turn = Turn( + turn_id=request.turn_id, + session_id=request.session_id, + input_messages=last_turn_messages, + output_message=output_message, + started_at=last_turn_start_time, + completed_at=datetime.now(), + steps=steps, + ) + await self.storage.add_turn_to_session(request.session_id, turn) + + if output_message.tool_calls: + chunk = AgentTurnResponseStreamChunk( + event=AgentTurnResponseEvent( + payload=AgentTurnResponseTurnAwaitingInputPayload( + turn=turn, + ) + ) + ) + else: + chunk = AgentTurnResponseStreamChunk( + event=AgentTurnResponseEvent( + payload=AgentTurnResponseTurnCompletePayload( + turn=turn, + ) + ) + ) + yield chunk async def run( @@ -611,11 +742,7 @@ async def _run( input_messages = input_messages + [message] else: log.info(f"{str(message)}") - tool_call = message.tool_calls[0] - if tool_call.tool_name in client_tools: - yield message - return - + # 1. Start the tool execution step and progress step_id = str(uuid.uuid4()) yield AgentTurnResponseStreamChunk( event=AgentTurnResponseEvent( @@ -625,6 +752,7 @@ async def _run( ) ) ) + tool_call = message.tool_calls[0] yield AgentTurnResponseStreamChunk( event=AgentTurnResponseEvent( payload=AgentTurnResponseStepProgressPayload( @@ -639,6 +767,23 @@ async def _run( ) ) + # If tool is a client tool, yield CompletionMessage and return + if tool_call.tool_name in client_tools: + await self.storage.set_in_progress_tool_call_step( + session_id, + turn_id, + ToolExecutionStep( + step_id=step_id, + turn_id=turn_id, + tool_calls=[tool_call], + tool_responses=[], + started_at=datetime.now(), + ), + ) + yield message + return + + # If tool is a builtin server tool, execute it tool_name = tool_call.tool_name if isinstance(tool_name, BuiltinTool): tool_name = tool_name.value diff --git a/llama_stack/providers/inline/agents/meta_reference/agents.py b/llama_stack/providers/inline/agents/meta_reference/agents.py index acacbdfdf0..8a4d912382 100644 --- a/llama_stack/providers/inline/agents/meta_reference/agents.py +++ b/llama_stack/providers/inline/agents/meta_reference/agents.py @@ -21,6 +21,7 @@ AgentStepResponse, AgentToolGroup, AgentTurnCreateRequest, + AgentTurnResumeRequest, Document, Session, Turn, @@ -146,6 +147,7 @@ async def create_agent_turn( documents: Optional[List[Document]] = None, stream: Optional[bool] = False, tool_config: Optional[ToolConfig] = None, + allow_turn_resume: Optional[bool] = False, ) -> AsyncGenerator: request = AgentTurnCreateRequest( agent_id=agent_id, @@ -155,6 +157,7 @@ async def create_agent_turn( toolgroups=toolgroups, documents=documents, tool_config=tool_config, + allow_turn_resume=allow_turn_resume, ) if stream: return self._create_agent_turn_streaming(request) @@ -177,7 +180,25 @@ async def resume_agent_turn( tool_responses: List[ToolResponseMessage], stream: Optional[bool] = False, ) -> AsyncGenerator: - pass + request = AgentTurnResumeRequest( + agent_id=agent_id, + session_id=session_id, + turn_id=turn_id, + tool_responses=tool_responses, + stream=stream, + ) + if stream: + return self._continue_agent_turn_streaming(request) + else: + raise NotImplementedError("Non-streaming agent turns not yet implemented") + + async def _continue_agent_turn_streaming( + self, + request: AgentTurnResumeRequest, + ) -> AsyncGenerator: + agent = await self.get_agent(request.agent_id) + async for event in agent.resume_turn(request): + yield event async def get_agents_turn(self, agent_id: str, session_id: str, turn_id: str) -> Turn: turn = await self.persistence_store.get(f"session:{agent_id}:{session_id}:{turn_id}") diff --git a/llama_stack/providers/inline/agents/meta_reference/persistence.py b/llama_stack/providers/inline/agents/meta_reference/persistence.py index 4b8ad6d4ad..3c3866873b 100644 --- a/llama_stack/providers/inline/agents/meta_reference/persistence.py +++ b/llama_stack/providers/inline/agents/meta_reference/persistence.py @@ -12,7 +12,7 @@ from pydantic import BaseModel -from llama_stack.apis.agents import Turn +from llama_stack.apis.agents import ToolExecutionStep, Turn from llama_stack.providers.utils.kvstore import KVStore log = logging.getLogger(__name__) @@ -84,3 +84,15 @@ async def get_session_turns(self, session_id: str) -> List[Turn]: continue turns.sort(key=lambda x: (x.completed_at or datetime.min)) return turns + + async def set_in_progress_tool_call_step(self, session_id: str, turn_id: str, step: ToolExecutionStep): + await self.kvstore.set( + key=f"in_progress_tool_call_step:{self.agent_id}:{session_id}:{turn_id}", + value=step.model_dump_json(), + ) + + async def get_in_progress_tool_call_step(self, session_id: str, turn_id: str) -> Optional[ToolExecutionStep]: + value = await self.kvstore.get( + key=f"in_progress_tool_call_step:{self.agent_id}:{session_id}:{turn_id}", + ) + return ToolExecutionStep(**json.loads(value)) if value else None diff --git a/tests/client-sdk/agents/test_agents.py b/tests/client-sdk/agents/test_agents.py index e5380d357a..781095d2b4 100644 --- a/tests/client-sdk/agents/test_agents.py +++ b/tests/client-sdk/agents/test_agents.py @@ -19,8 +19,12 @@ from llama_stack_client.types.shared_params.agent_config import AgentConfig, ToolConfig from llama_stack_client.types.tool_def_param import Parameter -from llama_stack.apis.agents.agents import AgentConfig as Server__AgentConfig -from llama_stack.apis.agents.agents import ToolChoice +from llama_stack.apis.agents.agents import ( + AgentConfig as Server__AgentConfig, +) +from llama_stack.apis.agents.agents import ( + ToolChoice, +) class TestClientTool(ClientTool):