SemanticValidation: A Library for Semantic Checks with OpenAI
Tags: openai, csharp, dotnet, semantickernel
Published: 2023-12-26 09:02:28

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](https://github.com/mehrandvd/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:

```csharp
var result = Semantic.AreSimilar(
                "This automobile is red",
                "The car is red");
Console.WriteLine(result.IsValid); // true
```

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

```csharp
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.
```

SemanticValidation uses OpenAI and [SemanticKernel ](https://github.com/microsoft/semantic-kernel) 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](https://www.nuget.org/packages/SemanticValidation) 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:

```csharp
var semantic = new Semantic(deployment, endpoint, apiKey);
```

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.

```csharp
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
```

## 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. 🙏