@@ -157,13 +157,21 @@ In the example below, we define a Main Agent responsible for delegating tasks to
157157Define Tools:
158158
159159``` py
160+ from astrbot.api import logger
161+ from astrbot.core.agent.run_context import ContextWrapper
162+ from astrbot.core.agent.tool import FunctionTool, ToolExecResult, ToolSet
163+ from astrbot.core.astr_agent_context import AstrAgentContext
164+ from pydantic import Field
165+ from pydantic.dataclasses import dataclass
166+
167+
160168@dataclass
161169class AssignAgentTool (FunctionTool[AstrAgentContext]):
162170 """ Main agent uses this tool to decide which sub-agent to delegate a task to."""
163171
164172 name: str = " assign_agent"
165173 description: str = " Assign an agent to a task based on the given query"
166- parameters: dict = field (
174+ parameters: dict = Field (
167175 default_factory = lambda : {
168176 " type" : " object" ,
169177 " properties" : {
@@ -178,7 +186,7 @@ class AssignAgentTool(FunctionTool[AstrAgentContext]):
178186
179187 async def call (
180188 self , context : ContextWrapper[AstrAgentContext], ** kwargs
181- ) -> str | CallToolResult :
189+ ) -> ToolExecResult :
182190 # Here you would implement the actual agent assignment logic.
183191 # For demonstration purposes, we'll return a dummy response.
184192 return " Based on the query, you should assign agent 1."
@@ -190,7 +198,7 @@ class WeatherTool(FunctionTool[AstrAgentContext]):
190198
191199 name: str = " weather"
192200 description: str = " Get weather information for a location"
193- parameters: dict = field (
201+ parameters: dict = Field (
194202 default_factory = lambda : {
195203 " type" : " object" ,
196204 " properties" : {
@@ -205,7 +213,7 @@ class WeatherTool(FunctionTool[AstrAgentContext]):
205213
206214 async def call (
207215 self , context : ContextWrapper[AstrAgentContext], ** kwargs
208- ) -> str | CallToolResult :
216+ ) -> ToolExecResult :
209217 city = kwargs[" city" ]
210218 # Here you would implement the actual weather fetching logic.
211219 # For demonstration purposes, we'll return a dummy response.
@@ -218,7 +226,7 @@ class SubAgent1(FunctionTool[AstrAgentContext]):
218226
219227 name: str = " subagent1_name"
220228 description: str = " subagent1_description"
221- parameters: dict = field (
229+ parameters: dict = Field (
222230 default_factory = lambda : {
223231 " type" : " object" ,
224232 " properties" : {
@@ -233,7 +241,7 @@ class SubAgent1(FunctionTool[AstrAgentContext]):
233241
234242 async def call (
235243 self , context : ContextWrapper[AstrAgentContext], ** kwargs
236- ) -> str | CallToolResult :
244+ ) -> ToolExecResult :
237245 ctx = context.context.context
238246 event = context.context.event
239247 logger.info(f " the llm context messages: { context.messages} " )
@@ -255,7 +263,7 @@ class SubAgent2(FunctionTool[AstrAgentContext]):
255263
256264 name: str = " subagent2_name"
257265 description: str = " subagent2_description"
258- parameters: dict = field (
266+ parameters: dict = Field (
259267 default_factory = lambda : {
260268 " type" : " object" ,
261269 " properties" : {
@@ -270,7 +278,7 @@ class SubAgent2(FunctionTool[AstrAgentContext]):
270278
271279 async def call (
272280 self , context : ContextWrapper[AstrAgentContext], ** kwargs
273- ) -> str | CallToolResult :
281+ ) -> ToolExecResult :
274282 return " I am useless :(, you shouldn't call me :("
275283```
276284
@@ -335,6 +343,32 @@ class Conversation:
335343
336344:::
337345
346+ ### Quickly Adding LLM Records to a Conversation ` add_message_pair `
347+
348+ ``` py
349+ from astrbot.core.agent.message import (
350+ AssistantMessageSegment,
351+ UserMessageSegment,
352+ TextPart,
353+ )
354+
355+ conv_mgr = self .context.conversation_manager
356+ provider_id = await self .context.get_current_chat_provider_id(event.unified_msg_origin)
357+ curr_cid = await conv_mgr.get_curr_conversation_id(event.unified_msg_origin)
358+ user_msg = UserMessageSegment(content = [TextPart(text = " hi" )])
359+ llm_resp = await self .context.llm_generate(
360+ chat_provider_id = provider_id, # Chat model ID
361+ contexts = [user_msg], # When prompt is not specified, contexts is used as input; if both prompt and contexts are provided, prompt is appended to the end of the LLM input
362+ )
363+ await conv_mgr.add_message_pair(
364+ cid = curr_cid,
365+ user_message = user_msg,
366+ assistant_message = AssistantMessageSegment(
367+ content = [TextPart(text = llm_resp.completion_text)]
368+ ),
369+ )
370+ ```
371+
338372### Main Methods
339373
340374#### ` new_conversation `
0 commit comments