ChatOllama

Open in ColabOpen in GitHub

Overview

This tutorial covers how Ollama can be used to run open source large language models, such as Llama 3.3 locally.

There are two ways to utilize Ollama models:

  • ollama command in the terminal

  • ChatOllama class of LangChain.

ChatOllama allows you to create a chain of prompt, LLM, OutputParser and run the model like other chat models such as ChatOpenAI.

  • The output format can be string, JSON, etc.

  • It also supports multimodal models.

Table of Contents

References


Environment Setup

Set up the environment. You may refer to Environment Setup for more details.

[Note]

  • langchain-opentutorial is a package that provides a set of easy-to-use environment setup, useful functions and utilities for tutorials.

  • You can checkout the langchain-opentutorial for more details.

You can alternatively set API keys such as LANGCHAIN_API_KEY in a .env file and load them.

[Note] This is not necessary if you've already set the required API keys in previous steps.

Install

Install Ollama and download models we'll use in this tutorial.

Ollama Download

Download and install Ollama available for macOS, Linux, and Windows.

After installation success, you can run ollama in your terminal.

explanation-01

Model Download using Ollama

Above ollama available commands, the pull command is used to download a model from a registry and bring it into your local environment.

Use ollama pull <name-of-model> command to get the model.

For example:

  • ollama pull llama3.2 : 3B parameters model (default)

  • ollama pull llama3.2:1b : 1B parameters model

The default tag version of the model will be downloaded to the path below.

  • Mac: ~/.ollama/models

  • Linux/WSL: /usr/share/ollama/.ollama/models

Use ollama list to view all the models you’ve downloaded.

explanation-02

Chat with model directly from the command line using:

ollama run <name-of-model>

explanation-03

Send a message to model and use available commands.

explanation-04

Model

Check the configuration information in the Modelfile of the Ollama and run the model with ChatOllama.

Modelfile

A model file is the blueprint to create and share models with Ollama.

HuggingFace support download open models(.gguf extension). Then you can create a Modelfile to define your own custom model.

In this tutorial, two ways to view Modelfile of llama3.2:1b.

explanation-05

You can check the prompt template configuration.

Ollama model

All local models are available at localhost:11434.

Streaming response is possible through the single chain created above.

  • astream() : asynchronous streaming

Output format: JSON

Use the latest version of Ollama and specify the format of the output to json.

Local models must be downloaded before they can be used.

  • ollama pull gemma2

Output the response in JSON format, even if the prompt does not include a message like response in JSON format.

Multimodal support

Ollama supports multimodal LLMs like bakllava and llava.

Download multimodal LLMs:

  • ollama pull llava:7b

  • ollama pull bakllava

Note. update Ollama to use the latest version that supports multimodal.

You can use tags to explore the full set of versions of models like llava.

Provides functions:

  • convert_to_base64 : convert a PIL image to a Base64 encoded string

  • plt_img_base64 : embed it in HTML to display the image

Example usage:

  • Open a PIL image from the specified file path and save it to pil_image.

  • Use the convert_to_base64 function to convert pil_image to a Base64 encoded string.

  • Use the plt_img_base64 function to display the Base64-encoded string as an image.

  • prompt_func : takes image and text data as input and converts it to HumanMessage.

    • image : Base64 encoded JPEG format.

    • text : plain text.

Call the chain.invoke method to pass an image and text query and generate an answer.

  • ChatOllama : Uses a multimodal LLM, such as llava.

  • StrOutputParser : parse the output of LLM into a string

  • chain : pipeline prompt_func, llm, and StrOutputParser

Last updated