Simple Example Agent
This commit is contained in:
@ -8,32 +8,71 @@
|
||||
# source code released under the terms of GNU Public License Version 3
|
||||
# https://www.gnu.org/licenses/gpl-3.0.txt
|
||||
|
||||
import re
|
||||
|
||||
from typing import Callable
|
||||
|
||||
from ums.agent import ExtractAudioAgent, ExtractImageAgent, ExtractTextAgent
|
||||
from ums.utils.types import RiddleData, AgentMessage
|
||||
|
||||
from ums.utils.schema import ExtractedData, ExtractedContent, ExtractedPositions
|
||||
|
||||
class SimpleExtractAudioAgent(ExtractAudioAgent):
|
||||
# here we do not have an implementation for extracting audio,
|
||||
# normally, we would not have this class, but here as example
|
||||
|
||||
def handle(self, data: RiddleData) -> RiddleData:
|
||||
print("Audio Process:", data.file_plain)
|
||||
return data
|
||||
|
||||
class SimpleExtractImageAgent(ExtractImageAgent):
|
||||
# equally, we would not have this class without implementation
|
||||
|
||||
def before_response(self, response: AgentMessage, send_it: Callable[[], None]) -> bool:
|
||||
# agents are able to prevent sending response messages to the management
|
||||
# or send this messages later via `send_it()``
|
||||
|
||||
print("The response would be:", response)
|
||||
|
||||
# just stop the response from being sent
|
||||
return False
|
||||
|
||||
def handle(self, data: RiddleData) -> RiddleData:
|
||||
print("Image Process:", data.file_plain)
|
||||
return data
|
||||
|
||||
class SimpleExtractTextAgent(ExtractTextAgent):
|
||||
|
||||
def before_response(self, response: AgentMessage, send_it: Callable[[], None]) -> bool:
|
||||
print("The response will be:", response)
|
||||
return True
|
||||
|
||||
|
||||
def handle(self, data: RiddleData) -> RiddleData:
|
||||
print("Text Process:", data.file_plain)
|
||||
|
||||
# here we extract the variables assigned with numbers
|
||||
found = False
|
||||
with open(data.file_plain) as f:
|
||||
for i, line in enumerate(f):
|
||||
if "=" in line:
|
||||
match = re.match(r"([a-z]{1})\s*=\s*(\d+)", line.strip())
|
||||
if not match is None:
|
||||
variable = match.group(1)
|
||||
value = int(match.group(2))
|
||||
found = True
|
||||
line_no = i
|
||||
if found:
|
||||
extracted = ExtractedData(
|
||||
contents=[
|
||||
ExtractedContent(type="variable",content=variable),
|
||||
ExtractedContent(type="value",content=value)
|
||||
],
|
||||
positions=[
|
||||
ExtractedPositions(type="line",position=line_no),
|
||||
ExtractedPositions(type="line",position=line_no)
|
||||
],
|
||||
other={
|
||||
"variable" : variable,
|
||||
"value" : value
|
||||
}
|
||||
)
|
||||
data.file_extracted = self.store_extracted(data, extracted)
|
||||
|
||||
return data
|
||||
|
||||
AGENT_CLASSES = [
|
||||
|
Reference in New Issue
Block a user