(self, config: AgentConfig = AgentConfig())
| 140 | oai_tool_id2call: Dict[str, OpenAIToolCall] = {} |
| 141 | |
| 142 | def __init__(self, config: AgentConfig = AgentConfig()): |
| 143 | self.config = config |
| 144 | self.id = ObjectRegistry.new_id() # Initialize agent ID |
| 145 | self.lock = asyncio.Lock() # for async access to update self.llm.usage_cost |
| 146 | self.dialog: List[Tuple[str, str]] = [] # seq of LLM (prompt, response) tuples |
| 147 | self.llm_tools_map: Dict[str, Type[ToolMessage]] = {} |
| 148 | self.llm_tools_handled: Set[str] = set() |
| 149 | self.llm_tools_usable: Set[str] = set() |
| 150 | self.llm_tools_known: Set[str] = set() # all known tools, handled/used or not |
| 151 | # Indicates which tool-names are allowed to be inferred when |
| 152 | # the LLM "forgets" to include the request field in its tool-call. |
| 153 | self.enabled_requests_for_inference: Optional[Set[str]] = ( |
| 154 | None # If None, we allow all |
| 155 | ) |
| 156 | self.interactive: bool = True # may be modified by Task wrapper |
| 157 | self.token_stats_str = "" |
| 158 | self.default_human_response: Optional[str] = None |
| 159 | self._indent = "" |
| 160 | self.llm = LanguageModel.create(config.llm) |
| 161 | self.vecdb = VectorStore.create(config.vecdb) if config.vecdb else None |
| 162 | self.tool_error = False |
| 163 | self.search_for_tools = { |
| 164 | SearchForTools.CONTENT.value, |
| 165 | SearchForTools.TOOLS.value, |
| 166 | SearchForTools.FUNCTIONS.value, |
| 167 | } |
| 168 | if config.parsing is not None and self.config.llm is not None: |
| 169 | # token_encoding_model is used to obtain the tokenizer, |
| 170 | # so in case it's an OpenAI model, we ensure that the tokenizer |
| 171 | # corresponding to the model is used. |
| 172 | if isinstance(self.llm, OpenAIGPT) and self.llm.is_openai_chat_model(): |
| 173 | config.parsing.token_encoding_model = self.llm.config.chat_model |
| 174 | self.parser: Optional[Parser] = ( |
| 175 | Parser(config.parsing) if config.parsing else None |
| 176 | ) |
| 177 | if config.add_to_registry: |
| 178 | ObjectRegistry.register_object(self) |
| 179 | |
| 180 | self.callbacks = SimpleNamespace( |
| 181 | start_llm_stream=lambda: noop_fn, |
| 182 | start_llm_stream_async=async_lambda_noop_fn, |
| 183 | cancel_llm_stream=noop_fn, |
| 184 | finish_llm_stream=noop_fn, |
| 185 | show_llm_response=noop_fn, |
| 186 | show_agent_response=noop_fn, |
| 187 | get_user_response=None, |
| 188 | get_user_response_async=None, |
| 189 | get_last_step=noop_fn, |
| 190 | set_parent_agent=noop_fn, |
| 191 | show_error_message=noop_fn, |
| 192 | show_start_response=noop_fn, |
| 193 | ) |
| 194 | Agent.init_state(self) |
| 195 | |
| 196 | def init_state(self) -> None: |
| 197 | """Initialize all state vars. Called by Task.run() if restart is True""" |
nothing calls this directly
no test coverage detected