Source code for swarmauri.community.tools.concrete.EntityRecognitionTool
import json
from transformers import pipeline, logging as hf_logging
from swarmauri.standard.tools.base.ToolBase import ToolBase
from swarmauri.standard.tools.concrete.Parameter import Parameter
hf_logging.set_verbosity_error()
[docs]
class EntityRecognitionTool(ToolBase):
def __init__(self):
parameters = [
Parameter("text","string","The text for entity recognition",True)
]
super().__init__(name="EntityRecognitionTool",
description="Extracts named entities from text",
parameters=parameters)
def __call__(self, text: str) -> dict:
try:
self.nlp = pipeline("ner")
entities = self.nlp(text)
organized_entities = {}
for entity in entities:
if entity['entity'] not in organized_entities:
organized_entities[entity['entity']] = []
organized_entities[entity['entity']].append(entity['word'])
return json.dumps(organized_entities)
except Exception as e:
raise e
finally:
del self.nlp