ChatOllama
Author: Ivy Bae
Peer Review : HyeonJong Moon, sunworl
Proofread : frimer
This is a part of LangChain Open Tutorial
Overview
This tutorial covers how Ollama can be used to run open source large language models, such as Llama 3.3 locally.
Ollama Library : a list of supperted models
There are two ways to utilize Ollama models:
ollamacommand in the terminalChatOllamaclass 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-opentutorialis a package that provides a set of easy-to-use environment setup, useful functions and utilities for tutorials.You can checkout the
langchain-opentutorialfor 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.

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/modelsLinux/WSL:
/usr/share/ollama/.ollama/models
Use ollama list to view all the models you’ve downloaded.

Chat with model directly from the command line using:
ollama run <name-of-model>

Send a message to model and use available commands.

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.
more information in the Ollama Modelfile documentation.
In this tutorial, two ways to view Modelfile of llama3.2:1b.
Option 1: view a template from a model's tags page
Option 2: use
ollama showcommand to print theModelfile

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:7bollama 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 stringplt_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_base64function to convertpil_imageto a Base64 encoded string.Use the
plt_img_base64function to display the Base64-encoded string as an image.
prompt_func: takes image and text data as input and converts it toHumanMessage.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 asllava.StrOutputParser: parse the output of LLM into a stringchain: pipeline prompt_func, llm, and StrOutputParser
Last updated