Named Entity Recognition
Extract named entities from text using ControlFlow.
Named Entity Recognition (NER) is a crucial task in natural language processing, used to identify named entities (such as persons, organizations, locations) in text. This example demonstrates how to implement a simple NER system using ControlFlow and a GPT-4o mini model, showcasing two different approaches: extracting a simple list of entities and categorizing entities by type.
Code
First, let’s implement a function that extracts a simple list of entities:
We can call this function on any text to extract all named entities:
Now, let’s modify our function to categorize the entities it extracts. We do this by changing the result type to a dictionary and providing detailed instructions about the types of entities we want to extract:
Here’s how we can use this function to perform NER on some example texts:
Key concepts
This implementation showcases several important ControlFlow features that enable quick development of NLP tools:
-
Agents: We create an agent with a specific LLM model (GPT-4o mini) to perform the named entity recognition.
-
Flexible result types: We demonstrate two different result types: a simple list of strings and a dictionary of categorized entities. This flexibility allows us to adapt the output structure to our specific needs.
-
Detailed instructions: In the categorized version, we provide detailed instructions to guide the model in structuring its output. This allows us to define a specific schema for the results without changing the underlying model.
-
Context passing: The
context
parameter is used to pass the input text to the task.
By leveraging these ControlFlow features, we can create powerful NER tools with minimal code. This example demonstrates how ControlFlow simplifies the process of building and deploying NLP tools, making it easier for developers to incorporate advanced language processing capabilities into their applications.
The ability to easily switch between different output structures (list vs. categorized dictionary) showcases the flexibility of ControlFlow in adapting to various NLP task requirements.