processors(rtvi): add support for action responses
This commit is contained in:
@@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
|
|
||||||
from typing import Any, Awaitable, Callable, Dict, List, Literal, Optional
|
from typing import Any, Awaitable, Callable, Dict, List, Literal, Optional, Union
|
||||||
from pydantic import BaseModel, Field, PrivateAttr, ValidationError
|
from pydantic import BaseModel, Field, PrivateAttr, ValidationError
|
||||||
|
|
||||||
from pipecat.frames.frames import (
|
from pipecat.frames.frames import (
|
||||||
@@ -26,6 +26,7 @@ from loguru import logger
|
|||||||
|
|
||||||
RTVI_PROTOCOL_VERSION = "0.1"
|
RTVI_PROTOCOL_VERSION = "0.1"
|
||||||
|
|
||||||
|
ActionResult = Union[bool,int,float,str,list,dict]
|
||||||
|
|
||||||
class RTVIServiceOption(BaseModel):
|
class RTVIServiceOption(BaseModel):
|
||||||
name: str
|
name: str
|
||||||
@@ -60,7 +61,7 @@ class RTVIAction(BaseModel):
|
|||||||
service: str
|
service: str
|
||||||
action: str
|
action: str
|
||||||
arguments: List[RTVIActionArgument] = []
|
arguments: List[RTVIActionArgument] = []
|
||||||
handler: Callable[["RTVIProcessor", str, Dict[str, Any]], Awaitable[None]] = Field(exclude=True)
|
handler: Callable[["RTVIProcessor", str, Dict[str, Any]], Awaitable[ActionResult]] = Field(exclude=True)
|
||||||
_arguments_dict: Dict[str, RTVIActionArgument] = PrivateAttr(default={})
|
_arguments_dict: Dict[str, RTVIActionArgument] = PrivateAttr(default={})
|
||||||
|
|
||||||
def model_post_init(self, __context: Any) -> None:
|
def model_post_init(self, __context: Any) -> None:
|
||||||
@@ -150,14 +151,15 @@ class RTVIConfigResponse(BaseModel):
|
|||||||
data: RTVIConfig
|
data: RTVIConfig
|
||||||
|
|
||||||
|
|
||||||
class RTVILLMContextMessageData(BaseModel):
|
class RTVIActionResponseData(BaseModel):
|
||||||
messages: List[dict]
|
result: ActionResult
|
||||||
|
|
||||||
|
|
||||||
class RTVILLMContextMessage(BaseModel):
|
class RTVIActionResponse(BaseModel):
|
||||||
label: Literal["rtvi-ai"] = "rtvi-ai"
|
label: Literal["rtvi-ai"] = "rtvi-ai"
|
||||||
type: Literal["llm-context"] = "llm-context"
|
type: Literal["action-response"] = "action-response"
|
||||||
data: RTVILLMContextMessageData
|
id: str
|
||||||
|
data: RTVIActionResponseData
|
||||||
|
|
||||||
|
|
||||||
class RTVIBotReadyData(BaseModel):
|
class RTVIBotReadyData(BaseModel):
|
||||||
@@ -343,8 +345,6 @@ class RTVIProcessor(FrameProcessor):
|
|||||||
case "action":
|
case "action":
|
||||||
action = RTVIActionRun.model_validate(message.data)
|
action = RTVIActionRun.model_validate(message.data)
|
||||||
await self._handle_action(message.id, action)
|
await self._handle_action(message.id, action)
|
||||||
# case "llm-get-context":
|
|
||||||
# await self._handle_llm_get_context()
|
|
||||||
case _:
|
case _:
|
||||||
await self._send_error_response(message.id, f"Unsupported type {message.type}")
|
await self._send_error_response(message.id, f"Unsupported type {message.type}")
|
||||||
|
|
||||||
@@ -399,13 +399,10 @@ class RTVIProcessor(FrameProcessor):
|
|||||||
if data.arguments:
|
if data.arguments:
|
||||||
for arg in data.arguments:
|
for arg in data.arguments:
|
||||||
arguments[arg.name] = arg.value
|
arguments[arg.name] = arg.value
|
||||||
await action.handler(self, action.service, arguments)
|
result = await action.handler(self, action.service, arguments)
|
||||||
|
message = RTVIActionResponse(id=request_id, data=RTVIActionResponseData(result=result))
|
||||||
# async def _handle_llm_get_context(self):
|
frame = TransportMessageFrame(message=message.model_dump(exclude_none=True))
|
||||||
# data = RTVILLMContextMessageData(messages=self._tma_in.messages)
|
await self.push_frame(frame)
|
||||||
# message = RTVILLMContextMessage(data=data)
|
|
||||||
# frame = TransportMessageFrame(message=message.model_dump(exclude_none=True))
|
|
||||||
# await self.push_frame(frame)
|
|
||||||
|
|
||||||
async def _send_bot_ready(self):
|
async def _send_bot_ready(self):
|
||||||
message = RTVIBotReady(
|
message = RTVIBotReady(
|
||||||
|
|||||||
Reference in New Issue
Block a user