Split off into a separate function so that it can be imported in the /chat/completion proxy.
(
self,
agent_id: str,
actor: User,
# role: MessageRole,
input_messages: List[MessageCreate],
stream_steps: bool,
stream_tokens: bool,
# related to whether or not we return `LettaMessage`s or `Message`s
chat_completion_mode: bool = False,
# Support for AssistantMessage
use_assistant_message: bool = True,
assistant_message_tool_name: str = constants.DEFAULT_MESSAGE_TOOL,
assistant_message_tool_kwarg: str = constants.DEFAULT_MESSAGE_TOOL_KWARG,
metadata: Optional[dict] = None,
)
| 1531 | |
| 1532 | @trace_method |
| 1533 | async def send_message_to_agent( |
| 1534 | self, |
| 1535 | agent_id: str, |
| 1536 | actor: User, |
| 1537 | # role: MessageRole, |
| 1538 | input_messages: List[MessageCreate], |
| 1539 | stream_steps: bool, |
| 1540 | stream_tokens: bool, |
| 1541 | # related to whether or not we return `LettaMessage`s or `Message`s |
| 1542 | chat_completion_mode: bool = False, |
| 1543 | # Support for AssistantMessage |
| 1544 | use_assistant_message: bool = True, |
| 1545 | assistant_message_tool_name: str = constants.DEFAULT_MESSAGE_TOOL, |
| 1546 | assistant_message_tool_kwarg: str = constants.DEFAULT_MESSAGE_TOOL_KWARG, |
| 1547 | metadata: Optional[dict] = None, |
| 1548 | ) -> Union[StreamingResponse, LettaResponse]: |
| 1549 | """Split off into a separate function so that it can be imported in the /chat/completion proxy.""" |
| 1550 | # TODO: @charles is this the correct way to handle? |
| 1551 | include_final_message = True |
| 1552 | |
| 1553 | if not stream_steps and stream_tokens: |
| 1554 | raise HTTPException(status_code=400, detail="stream_steps must be 'true' if stream_tokens is 'true'") |
| 1555 | |
| 1556 | # For streaming response |
| 1557 | try: |
| 1558 | |
| 1559 | # TODO: move this logic into server.py |
| 1560 | |
| 1561 | # Get the generator object off of the agent's streaming interface |
| 1562 | # This will be attached to the POST SSE request used under-the-hood |
| 1563 | letta_agent = self.load_agent(agent_id=agent_id, actor=actor) |
| 1564 | |
| 1565 | # Disable token streaming if not OpenAI or Anthropic |
| 1566 | # TODO: cleanup this logic |
| 1567 | llm_config = letta_agent.agent_state.llm_config |
| 1568 | # supports_token_streaming = ["openai", "anthropic", "xai", "deepseek"] |
| 1569 | supports_token_streaming = ["openai", "anthropic", "deepseek"] # TODO re-enable xAI once streaming is patched |
| 1570 | if stream_tokens and ( |
| 1571 | llm_config.model_endpoint_type not in supports_token_streaming or "inference.memgpt.ai" in llm_config.model_endpoint |
| 1572 | ): |
| 1573 | warnings.warn( |
| 1574 | f"Token streaming is only supported for models with type {' or '.join(supports_token_streaming)} in the model_endpoint: agent has endpoint type {llm_config.model_endpoint_type} and {llm_config.model_endpoint}. Setting stream_tokens to False." |
| 1575 | ) |
| 1576 | stream_tokens = False |
| 1577 | |
| 1578 | # Create a new interface per request |
| 1579 | letta_agent.interface = StreamingServerInterface( |
| 1580 | # multi_step=True, # would we ever want to disable this? |
| 1581 | use_assistant_message=use_assistant_message, |
| 1582 | assistant_message_tool_name=assistant_message_tool_name, |
| 1583 | assistant_message_tool_kwarg=assistant_message_tool_kwarg, |
| 1584 | inner_thoughts_in_kwargs=( |
| 1585 | llm_config.put_inner_thoughts_in_kwargs if llm_config.put_inner_thoughts_in_kwargs is not None else False |
| 1586 | ), |
| 1587 | # inner_thoughts_kwarg=INNER_THOUGHTS_KWARG, |
| 1588 | ) |
| 1589 | streaming_interface = letta_agent.interface |
| 1590 | if not isinstance(streaming_interface, StreamingServerInterface): |
no test coverage detected