ums.example.example
1# Agenten Plattform 2# 3# (c) 2024 Magnus Bender 4# Institute of Humanities-Centered Artificial Intelligence (CHAI) 5# Universitaet Hamburg 6# https://www.chai.uni-hamburg.de/~bender 7# 8# source code released under the terms of GNU Public License Version 3 9# https://www.gnu.org/licenses/gpl-3.0.txt 10 11from typing import Callable 12from ums.agent import ExtractAudioAgent, ExtractImageAgent, ExtractTextAgent, SolveAgent, GatekeeperAgent 13 14from ums.utils.types import AgentMessage, Riddle, RiddleData, RiddleSolution, RiddleStatus 15 16""" 17 Examples for simple agents. 18 19 Each agent is represented by its own class. The handling of tasks is done by `handle()` in each agent. 20 21 Finally `AGENT_CLASSES` contains the classes of the agents in a list. Via environmental variables this list is specified to the ums.agent system. 22""" 23 24class MyExtractAudioAgent(ExtractAudioAgent): 25 26 def handle(self, data: RiddleData) -> RiddleData: 27 print("Audio Process:", data.file_plain) 28 return data 29 30class MyExtractImageAgent(ExtractImageAgent): 31 32 def handle(self, data: RiddleData) -> RiddleData: 33 print("Image Process:", data.file_plain) 34 return data 35 36class MyExtractTextAgent(ExtractTextAgent): 37 38 def before_response(self, response: AgentMessage, send_it: Callable[[], None]) -> bool: 39 print("The response will be:", response) 40 return True 41 42 def handle(self, data: RiddleData) -> RiddleData: 43 print("Text Process:", data.file_plain) 44 return data 45 46 47class MySolveAgent(SolveAgent): 48 49 def handle(self, riddle: Riddle, data: RiddleData) -> RiddleSolution: 50 51 if self.message().id == "test": 52 status = RiddleStatus() 53 status.extract.required = False 54 self.sub_riddle(riddle=Riddle(context="Haha", question="Blubber"), status=status) 55 56 return RiddleSolution(solution="Huii", explanation="Blubb") 57 58 59class MyGatekeeperAgent(GatekeeperAgent): 60 61 def handle(self, solution: RiddleSolution, riddle: Riddle) -> RiddleSolution: 62 solution.accepted = True 63 solution.review = "Ok" 64 65 return solution 66 67AGENT_CLASSES = [ 68 MyExtractAudioAgent, MyExtractImageAgent, MyExtractTextAgent, 69 MySolveAgent, 70 MyGatekeeperAgent 71]
25class MyExtractAudioAgent(ExtractAudioAgent): 26 27 def handle(self, data: RiddleData) -> RiddleData: 28 print("Audio Process:", data.file_plain) 29 return data
An extraction agent for audio, create a subclass for your agent.
31class MyExtractImageAgent(ExtractImageAgent): 32 33 def handle(self, data: RiddleData) -> RiddleData: 34 print("Image Process:", data.file_plain) 35 return data
An extraction agent for images, create a subclass for your agent.
37class MyExtractTextAgent(ExtractTextAgent): 38 39 def before_response(self, response: AgentMessage, send_it: Callable[[], None]) -> bool: 40 print("The response will be:", response) 41 return True 42 43 def handle(self, data: RiddleData) -> RiddleData: 44 print("Text Process:", data.file_plain) 45 return data
An extraction agent for text, create a subclass for your agent.
39 def before_response(self, response: AgentMessage, send_it: Callable[[], None]) -> bool: 40 print("The response will be:", response) 41 return True
This method is called before the response is sent.
If the method returns False
no response will be sent.
Thus, by overwriting this method, a response can be prevented.
The response to be sent is in response
and send_it
is a callable, which sends the response to the management if it gets called.
(Hence, one may stop sending the response and later call send_it()
to send the response.)
48class MySolveAgent(SolveAgent): 49 50 def handle(self, riddle: Riddle, data: RiddleData) -> RiddleSolution: 51 52 if self.message().id == "test": 53 status = RiddleStatus() 54 status.extract.required = False 55 self.sub_riddle(riddle=Riddle(context="Haha", question="Blubber"), status=status) 56 57 return RiddleSolution(solution="Huii", explanation="Blubb")
A solve agent, create a subclass for your agent.
50 def handle(self, riddle: Riddle, data: RiddleData) -> RiddleSolution: 51 52 if self.message().id == "test": 53 status = RiddleStatus() 54 status.extract.required = False 55 self.sub_riddle(riddle=Riddle(context="Haha", question="Blubber"), status=status) 56 57 return RiddleSolution(solution="Huii", explanation="Blubb")
Solve the riddle
using data
and return a solution.
60class MyGatekeeperAgent(GatekeeperAgent): 61 62 def handle(self, solution: RiddleSolution, riddle: Riddle) -> RiddleSolution: 63 solution.accepted = True 64 solution.review = "Ok" 65 66 return solution
A gatekeeper agent, create a subclass for your agent.
62 def handle(self, solution: RiddleSolution, riddle: Riddle) -> RiddleSolution: 63 solution.accepted = True 64 solution.review = "Ok" 65 66 return solution
Check the solution
of riddle
and return solution with populated solution.accepted
and solution.review
.