-
Notifications
You must be signed in to change notification settings - Fork 279
/
Copy pathbase.py
61 lines (47 loc) · 1.84 KB
/
base.py
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
import asyncio
import random
import sys
from typing import Any
from pydantic import BaseModel, Field
from beeai_framework.context import RunContext
from beeai_framework.emitter import Emitter
from beeai_framework.errors import FrameworkError
from beeai_framework.tools import StringToolOutput, Tool, ToolRunOptions
class RiddleToolInput(BaseModel):
riddle_number: int = Field(description="Index of riddle to retrieve.")
class RiddleTool(Tool[RiddleToolInput, ToolRunOptions, StringToolOutput]):
name = "Riddle"
description = "It selects a riddle to test your knowledge."
input_schema = RiddleToolInput
data = (
"What has hands but can't clap?",
"What has a face and two hands but no arms or legs?",
"What gets wetter the more it dries?",
"What has to be broken before you can use it?",
"What has a head, a tail, but no body?",
"The more you take, the more you leave behind. What am I?",
"What goes up but never comes down?",
)
def __init__(self, options: dict[str, Any] | None = None) -> None:
super().__init__(options)
def _create_emitter(self) -> Emitter:
return Emitter.root().child(
namespace=["tool", "example", "riddle"],
creator=self,
)
async def _run(
self, input: RiddleToolInput, options: ToolRunOptions | None, context: RunContext
) -> StringToolOutput:
index = input.riddle_number % (len(self.data))
riddle = self.data[index]
return StringToolOutput(result=riddle)
async def main() -> None:
tool = RiddleTool()
tool_input = RiddleToolInput(riddle_number=random.randint(0, len(RiddleTool.data)))
result = await tool.run(tool_input)
print(result)
if __name__ == "__main__":
try:
asyncio.run(main())
except FrameworkError as e:
sys.exit(e.explain())