I talked about Structured Output before in the context of Semantic Kernel. Structured Outputs is a feature that ensures the model will always generate responses that follow a supplied JSON Schema, so you can process the responses in automated fashion without worrying about getting invalid JSON back.
Recently support for Structured Output was announced by Ollama. In this post I want to show you how you can use this in combination with OllamaSharp, the C# client for Ollama.
Using Structured Ouput in OllamaSharp
Remark: Make sure you have latest Ollama version running on your local machine before you continue.
- Add the OllamaSharp client to your project:
dotnet add package OllamaSharp
- Now let’s first define our response model:
- Afterwards we need to initiate a new OllamaSharp client instance:
- We create a new request object.
- Notice that we specify a JSON schema object based on the Recipe model we created earlier
- If we now invoke the application we get a JSON response back in the provided format:
The full example can be found here: wullemsb/SemanticKernelStructuredOutput at OllamaClient
What about using the Semantic Kernel Ollama connector?
At the moment of writing this blog post, I couldn’t get the Structured Output working when combining Semantic Kernel and Ollama.
I explicitly referenced the correct OllamaSharp version:
And used the Ollama connector:
But when calling the api, I still got a response back in an unstructured format:
More information
Semantic Kernel - Structured output
awaescher/OllamaSharp: The easiest way to use the Ollama API in .NET
Support for 'Structured outputs' · awaescher/OllamaSharp · Discussion #152