Code
The following code creates a function that classifies the sentiment of a given text on a scale from 0 (very negative) to 1 (very positive). It uses a GPT-4o mini model for classification and leverages ControlFlow’s task running and result validation features.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 sentiment analysis.
-
Result types: We specify
result_type=floatto ensure the sentiment score is returned as a float value. -
Result validation: The
result_validatorparameter is used with thebetween()function to ensure the result falls within the expected range. -
Context passing: The
contextparameter is used to pass the input text to the task.