Logo

dev-resources.site

for different kinds of informations.

Ollama 0.5 Is Here: Generate Structured Outputs

Published at
12/8/2024
Categories
ollama
genai
ai
tutorial
Author
busycaesar
Categories
4 categories in total
ollama
open
genai
open
ai
open
tutorial
open
Author
10 person written this
busycaesar
open
Ollama 0.5 Is Here: Generate Structured Outputs

Introduction

Whats up everyone! This is Dev. Yesterday, that is, 6th December, Ollama released 0.5 version which supports Structured Outputs. This is really exciting and problem solving.

This blog contains information on how to define the required structure and a tutorial on how to get the output. In this tutorial, I am going to use Ollama's Docker image.

Tutorial.

Step 1: docker-compose.yml

Firstly, defining the requirements for pulling the Ollama's docker image. Create a docker-compose.yml file with the following code.

services:
  ollama:
    image: ollama/ollama:latest
    restart: always
    ports:
      - "8080:11434"
Enter fullscreen mode Exit fullscreen mode

Note: If you previously pulled Ollama's image, make sure to remove the previous image using docker rmi ollama/ollama, so that it pulls the latest image when you run the docker compose file.

Finally, start the container using, docker compose up --build -d.

Step 2: Pull the model

Once the container is started, now its time to pull the required model. For that purpose, we need to get into the container using docker exec -it <Container's Name> bash. Replace the 'Container's Name' with the name of the container. This starts an interactive bash shell terminal.

From this shell, to get the required model, use the command, ollama pull <Model Name>. Replace the 'Model Name' with the name of the required model. For the tutotial, I am using llama3.2:latest.

Once the model is pulled, you can exit the shell using exit command.

Step 3: Prompt the model

Now its time to prompt the model. You can use any API testing application like Postman or Thunder Client. For the tutorial, I am using Thunder Client.

To generate the response, make a POST request on http://localhost:8080/api/generate with the following body,

{
  "model":"Model Name", // Replace this with the model that you pulled.
  "prompt":"Prompt", // Add your prompt here.
  "stream":false,
  "format": {} // Add the required structure here.
}
Enter fullscreen mode Exit fullscreen mode

Such request will generate the response and return it in the required format. Checkout the following response when I prompted "list 5 cities of India with the primary language spoken there." to "llama3.2:latest".

Request

Response

How to define the required structure?

To define the required structure, you need to start by defining the type. The type can be a string, an object or an array. Following is how a type can be defined.

{
  "type": "object" // or "string" or "array"
}
Enter fullscreen mode Exit fullscreen mode

Object

In case the type is defined as object, you also need to add properties and required keys with the type key. The properties key contains the list of values, required in the response along with its type. Lastly, the value of required key is an array of string which containers the list of all the keys defined in properties.

The explanation is confusing. Here is an example to understand it much better. For the prompt What is the capital of India and the primary language spoken there., following can be the structure.

{
  "type": "object",
  "properties": {
    "capital": {
      "type": "string"
    },
    "primary_language": {
      "type": "string"
    }
  },
  "required": ["capital", "primary_language"]
}
Enter fullscreen mode Exit fullscreen mode

The type of properties, for eg, capital's type can also be an object or array.

Array

In case the type is defined as array, you also need to add items key. The items key contains the type of the item of the array. For instance, for the prompt List 5 cities of Canada., following can be the structure.

{
  "type": "object",
  "properties": {
    "cities": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
  },
  "required": ["cities"]
}
Enter fullscreen mode Exit fullscreen mode

Again, the type of item of the array can be an object or array.

String

In case of string, you just need to define the type key.

Final Words

In conclusion, this is a great feature of generating structured responses. In my opinion it solves a big issue. I am really excited to leverage Ollama in my future projects. Follow me on Twitter and LinkedIn for more updates.

ollama Article's
30 articles in total
Favicon
What is ollama? Is it also a LLM?
Favicon
Semantic Kernel: Crea un API para Generaciรณn de Texto con Ollama y Aspire
Favicon
Local AI apps with C#, Semantic Kernel and Ollama
Favicon
Running Out of Space? Move Your Ollama Models to a Different Drive ๐Ÿš€
Favicon
Working with LLMs in .NET using Microsoft.Extensions.AI
Favicon
Building an Ollama-Powered GitHub Copilot Extension
Favicon
Step-by-Step Guide: Write Your First AI Storyteller with Ollama (llama3.2) and Semantic Kernel in C#
Favicon
Run LLMs Locally with Ollama & Semantic Kernel in .NET: A Quick Start
Favicon
How to Set Up a Local Ubuntu Server to Host Ollama Models with a WebUI
Favicon
Ollama 0.5 Is Here: Generate Structured Outputs
Favicon
Building AI-Powered Apps with SvelteKit: Managing HTTP Streams from Ollama Server
Favicon
Run Llama 3 Locally
Favicon
Building 5 AI Agents with phidata and Ollama
Favicon
Run Ollama on Intel Arc GPU (IPEX)
Favicon
Quick tip: Running OpenAI's Swarm locally using Ollama
Favicon
Langchain4J musings
Favicon
How to deploy SmolLM2 1.7B on a Virtual Machine in the Cloud with Ollama?
Favicon
Ollama - Custom Model - llama3.2
Favicon
Coding Assistants and Artificial Intelligence for the Rest of Us
Favicon
Using a Locally-Installed LLM to Fill in Client Requirement Gaps
Favicon
Create Your Own Local AI Chatbot with Ollama and LangChain
Favicon
Consuming HTTP Streams in PHP with Symfony HTTP Client and Ollama API
Favicon
Llama 3.2 Running Locally in VSCode: How to Set It Up with CodeGPT and Ollama
Favicon
Ollama Unveiled: Run LLMs Locally
Favicon
No Bullshit Guide to Youtube shorts automation in NodeJS, OpenAI, Ollama, ElevanLabs & ffmpeg
Favicon
Unloading a model from Ollama
Favicon
OLLAMA + LLAMA3 + RAG + Vector Database (Local, Open Source, Free)
Favicon
The 6 Best LLM Tools To Run Models Locally
Favicon
Demystifying AI of Your Own
Favicon
Langchain Chat Assistant using Chainlit App

Featured ones: