DEV Community

Cover image for SemanticValidation: A Library for Semantic Checks with OpenAI
Mehran Davoudi
Mehran Davoudi

Posted on

SemanticValidation: A Library for Semantic Checks with OpenAI

Have you ever wanted to use OpenAI's powerful language models to validate your data and queries? If so, you might be interested in SemanticValidation, a library that integrates OpenAI with validation systems. In this post, I will introduce SemanticValidation and show you how to use it in your .NET projects.

What is SemanticValidation?

SemanticValidation is a library that allows you to perform semantic checks on your data and queries using natural language understanding. It brings the power of OpenAI into the validation systems as easily as this:

var result = Semantic.AreSimilar(
                "This automobile is red",
                "The car is red");
Console.WriteLine(result.IsValid); // true
Enter fullscreen mode Exit fullscreen mode

The interesting part is that: When it fails, it explains why! (thanks to OpenAI)

var result = Semantic.AreSimilar(
                "This bicycle is red", 
                "The car is red");
Console.WriteLine(result.IsValid); // false
Console.WriteLine(result.Reason);
// The first text describes a red bicycle, while the second text describes a red car. They are not semantically equivalent.
Enter fullscreen mode Exit fullscreen mode

SemanticValidation uses OpenAI and SemanticKernel under the hood. SemanticKernel is a library that provides a unified interface to interact with OpenAI's language models.

How to Use SemanticValidation?

To use SemanticValidation, you need to install it as a NuGet package in your project. You also need an OpenAI API key to access the language models. Then, you need to create an instance of the Semantic class and pass your OpenAI subscription details as parameters:

var semantic = new Semantic(deployment, endpoint, apiKey);
Enter fullscreen mode Exit fullscreen mode

That's it! You are ready to use SemanticValidation in your code. 😊

SemanticValidation provides several semantic methods to perform different kinds of checks. For example, the AreSimilar method checks if two texts are semantically equivalent. The HasCondition method checks if a text meets a special condition. And again, watch how great it describes the reason for semantic validation failure.

var result = Semantic.HasCondition(
                text: "This car is red",
                condition: "It talks about trees");
Console.WriteLine(result.IsValid); // false
Console.WriteLine(result.Reason);
// The input text does not talk about trees
Enter fullscreen mode Exit fullscreen mode

Conclusion

SemanticValidation is a library that integrates OpenAI's language models with validation systems. It allows you to perform semantic checks on your data and queries using natural language understanding. It also provides explanatory feedback when a semantic check fails, thanks to OpenAI's natural language generation capabilities.

If you are interested in SemanticValidation, you can find more information and examples on its GitHub repository: https://github.com/mehrandvd/SemanticValidation

This is a new and developing project, and I would greatly appreciate your feedback to make it better. Thank you for reading this post and I hope you gained some new insights. Please share your thoughts and questions in the comments section. 🙏

Top comments (0)