|
| 1 | +import logging |
| 2 | +from typing import Dict, Any, Callable, List, Optional, Set |
| 3 | +from abc import ABC, abstractmethod |
| 4 | + |
| 5 | +# --- Tool Parser Base --- |
| 6 | + |
| 7 | +class ToolParserBase(ABC): |
| 8 | + """Abstract base class defining the interface for parsing tool calls from LLM responses. |
| 9 | + |
| 10 | + This class provides the foundational interface for parsing both complete and streaming |
| 11 | + responses from Language Models, specifically focusing on tool call extraction and processing. |
| 12 | + |
| 13 | + Attributes: |
| 14 | + None |
| 15 | + |
| 16 | + Methods: |
| 17 | + parse_response: Processes complete LLM responses |
| 18 | + parse_stream: Handles streaming response chunks |
| 19 | + """ |
| 20 | + |
| 21 | + @abstractmethod |
| 22 | + async def parse_response(self, response: Any) -> Dict[str, Any]: |
| 23 | + """Parse a complete LLM response and extract tool calls. |
| 24 | + |
| 25 | + Args: |
| 26 | + response (Any): The complete response from the LLM |
| 27 | + |
| 28 | + Returns: |
| 29 | + Dict[str, Any]: A dictionary containing: |
| 30 | + - role: The message role (usually 'assistant') |
| 31 | + - content: The text content of the response |
| 32 | + - tool_calls: List of extracted tool calls (if present) |
| 33 | + """ |
| 34 | + pass |
| 35 | + |
| 36 | + @abstractmethod |
| 37 | + async def parse_stream(self, response_chunk: Any, tool_calls_buffer: Dict[int, Dict]) -> tuple[Optional[Dict[str, Any]], bool]: |
| 38 | + """Parse a streaming response chunk and manage tool call accumulation. |
| 39 | + |
| 40 | + Args: |
| 41 | + response_chunk (Any): A single chunk from the streaming response |
| 42 | + tool_calls_buffer (Dict[int, Dict]): Buffer storing incomplete tool calls |
| 43 | + |
| 44 | + Returns: |
| 45 | + tuple[Optional[Dict[str, Any]], bool]: A tuple containing: |
| 46 | + - The parsed message if complete tool calls are found (or None) |
| 47 | + - Boolean indicating if the stream is complete |
| 48 | + """ |
| 49 | + pass |
| 50 | + |
| 51 | +# --- Tool Executor Base --- |
| 52 | + |
| 53 | +class ToolExecutorBase(ABC): |
| 54 | + """Abstract base class defining the interface for tool execution strategies. |
| 55 | + |
| 56 | + This class provides the foundation for implementing different tool execution |
| 57 | + approaches, supporting both parallel and sequential execution patterns. |
| 58 | + |
| 59 | + Attributes: |
| 60 | + None |
| 61 | + |
| 62 | + Methods: |
| 63 | + execute_tool_calls: Main entry point for tool execution |
| 64 | + _execute_parallel: Handles parallel tool execution |
| 65 | + _execute_sequential: Handles sequential tool execution |
| 66 | + """ |
| 67 | + |
| 68 | + @abstractmethod |
| 69 | + async def execute_tool_calls( |
| 70 | + self, |
| 71 | + tool_calls: List[Dict[str, Any]], |
| 72 | + available_functions: Dict[str, Callable], |
| 73 | + thread_id: str, |
| 74 | + executed_tool_calls: Optional[Set[str]] = None |
| 75 | + ) -> List[Dict[str, Any]]: |
| 76 | + """Execute a list of tool calls and return their results. |
| 77 | + |
| 78 | + Args: |
| 79 | + tool_calls: List of tool calls to execute |
| 80 | + available_functions: Dictionary of available tool functions |
| 81 | + thread_id: ID of the current conversation thread |
| 82 | + executed_tool_calls: Set of already executed tool call IDs |
| 83 | + |
| 84 | + Returns: |
| 85 | + List[Dict[str, Any]]: List of tool execution results |
| 86 | + """ |
| 87 | + pass |
| 88 | + |
| 89 | + @abstractmethod |
| 90 | + async def _execute_parallel( |
| 91 | + self, |
| 92 | + tool_calls: List[Dict[str, Any]], |
| 93 | + available_functions: Dict[str, Callable], |
| 94 | + thread_id: str, |
| 95 | + executed_tool_calls: Set[str] |
| 96 | + ) -> List[Dict[str, Any]]: |
| 97 | + """Execute tool calls in parallel.""" |
| 98 | + pass |
| 99 | + |
| 100 | + @abstractmethod |
| 101 | + async def _execute_sequential( |
| 102 | + self, |
| 103 | + tool_calls: List[Dict[str, Any]], |
| 104 | + available_functions: Dict[str, Callable], |
| 105 | + thread_id: str, |
| 106 | + executed_tool_calls: Set[str] |
| 107 | + ) -> List[Dict[str, Any]]: |
| 108 | + """Execute tool calls sequentially.""" |
| 109 | + pass |
| 110 | + |
| 111 | +# --- Results Adder Base --- |
| 112 | + |
| 113 | +class ResultsAdderBase(ABC): |
| 114 | + """Abstract base class for handling tool results and message processing.""" |
| 115 | + |
| 116 | + def __init__(self, thread_manager): |
| 117 | + """Initialize with a ThreadManager instance. |
| 118 | + |
| 119 | + Args: |
| 120 | + thread_manager: The ThreadManager instance to use for message operations |
| 121 | + """ |
| 122 | + self.add_message = thread_manager.add_message |
| 123 | + self.update_message = thread_manager._update_message |
| 124 | + self.list_messages = thread_manager.list_messages |
| 125 | + self.message_added = False |
| 126 | + |
| 127 | + @abstractmethod |
| 128 | + async def add_initial_response(self, thread_id: str, content: str, tool_calls: Optional[List[Dict[str, Any]]] = None): |
| 129 | + pass |
| 130 | + |
| 131 | + @abstractmethod |
| 132 | + async def update_response(self, thread_id: str, content: str, tool_calls: Optional[List[Dict[str, Any]]] = None): |
| 133 | + pass |
| 134 | + |
| 135 | + @abstractmethod |
| 136 | + async def add_tool_result(self, thread_id: str, result: Dict[str, Any]): |
| 137 | + pass |
0 commit comments