-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom_tool.py
More file actions
69 lines (57 loc) · 1.77 KB
/
Copy pathcustom_tool.py
File metadata and controls
69 lines (57 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"""Example: wire a custom tool into the agent."""
from __future__ import annotations
import asyncio
from ai_agent import (
Agent,
AgentConfig,
BaseTool,
OpenRouterLLM,
Personality,
ToolParameter,
ToolResult,
build_default_registry,
)
class EchoTool(BaseTool):
"""Trivial custom tool — uppercases input text."""
def __init__(self) -> None:
super().__init__(
name="echo",
description="Return the given text in UPPERCASE.",
parameters=[
ToolParameter(
name="text",
type="string",
description="Text to echo",
required=True,
)
],
)
async def execute(self, arguments: dict[str, object]) -> ToolResult:
text = arguments.get("text")
if not isinstance(text, str):
return ToolResult(
tool_name=self.name,
success=False,
output="",
error="text must be a string",
)
return ToolResult(tool_name=self.name, success=True, output=text.upper())
async def main() -> None:
config = AgentConfig(
model="openai/gpt-4o-mini",
system_prompt="You are a demo agent. Use tools when helpful.",
tools=["calculator", "echo"],
personality=Personality(tone="friendly", style="concise"),
greeting="Hi! I can calculate or echo text.",
)
registry = build_default_registry()
registry.register(EchoTool())
selected = registry.select(config.tools)
agent = Agent(
config=config,
llm=OpenRouterLLM(model=config.model),
registry=selected,
)
await agent.run()
if __name__ == "__main__":
asyncio.run(main())