|
| 1 | + |
1 | 2 | # Ollama
|
2 | 3 |
|
3 |
| -This repository contains code to connect MATLAB® to an [Ollama™](https://ollama.com) server, running large language models (LLMs). |
| 4 | +Connect to [Ollama™](https://ollama.com/) models from MATLAB® locally or nonlocally. |
| 5 | + |
| 6 | +1. [Setup](#setup) |
| 7 | +2. [Get Started](#get-started) |
| 8 | +3. [Manage Chat History](#manage-chat-history) |
| 9 | +4. [Images](#images) |
| 10 | +5. [JSON\-Formatted and Structured Output](#json-formatted-and-structured-output) |
| 11 | + - [JSON Mode](#json-mode) |
| 12 | + - [Structured Output](#structured-output) |
| 13 | +6. [Tool Calling](#tool-calling) |
| 14 | +7. [See Also](#see-also) |
| 15 | +8. [Examples](#examples) |
| 16 | + |
| 17 | +<a id="setup"></a> |
| 18 | +# Setup |
| 19 | + |
| 20 | +Connecting to Ollama models using this add\-on requires an installed version of Ollama, as well as installed versions of the models you want to use. |
4 | 21 |
|
5 |
| -To use local models with Ollama, you will need to install and start an Ollama server, and “pull” models into it. Please follow the Ollama documentation for details. You should be familiar with the limitations and risks associated with using this technology, and you agree that you shall be solely responsible for full compliance with any terms that may apply to your use of any specific model. |
| 22 | +1. **Install Ollama.** For information on how to install Ollama, see [https://ollama.com/download](https://ollama.com/download). |
| 23 | +2. **Install Model.** If you have Ollama installed, then you can install models from the MATLAB Command Window using the `"ollama pull"` command. For example, to install Mistral, run this code. |
| 24 | +``` |
| 25 | +>> !ollama pull mistral |
| 26 | +``` |
6 | 27 |
|
7 |
| -Some of the [LLMs currently supported out of the box on Ollama](https://ollama.com/library) are: |
8 |
| -- llama2, llama2-uncensored, llama3, codellama |
9 |
| -- phi3 |
10 |
| -- aya |
11 |
| -- mistral (v0.1, v0.2, v0.3) |
12 |
| -- mixtral |
13 |
| -- gemma, codegemma |
14 |
| -- command-r |
| 28 | +<a id="get-started"></a> |
| 29 | +# Get Started |
15 | 30 |
|
16 |
| -## Establishing a connection to LLMs using Ollama |
| 31 | +Connect to Ollama using the [`ollamaChat`](functions/ollamaChat.md) function and generate text using the [`generate`](functions/generate.md) function. Optionally specify a system prompt. |
17 | 32 |
|
18 |
| -To create the chat assistant, call `ollamaChat` and specify the LLM you want to use: |
19 | 33 | ```matlab
|
20 |
| -chat = ollamaChat("mistral"); |
| 34 | +model = ollamaChat("mistral","You are a helpful assistant."); |
| 35 | +generate(model,"Who would win a footrace, a snail or a blue whale?") |
21 | 36 | ```
|
22 | 37 |
|
23 |
| -`ollamaChat` has additional options, please run `help ollamaChat` for details. |
| 38 | +```matlabTextOutput |
| 39 | +ans = " A blue whale cannot compete in a footrace as it lives and moves primarily in water, not on land. If we were to compare the speed between a snail and an animal that can move on land, like a cheetah for example, a cheetah would win hands down. Cheetahs have been recorded running at speeds up to 70 mph (112 km/h), while the maximum speed achievable by the average garden snail is about 0.03 mph (0.05 km/h)." |
| 40 | +``` |
24 | 41 |
|
25 |
| -## Simple call without preserving chat history |
| 42 | +By default, the `ollamaChat` function connects to a local server. To use a remote Ollama server, specify the server name and port number using the `Endpoint` name\-value argument. |
26 | 43 |
|
27 |
| -In some situations, you will want to use chat completion models without preserving chat history. For example, when you want to perform independent queries in a programmatic way. |
| 44 | +``` |
| 45 | +>> model = ollamaChat("mistral",Endpoint="myOllamaServer:12345"); |
| 46 | +``` |
28 | 47 |
|
29 |
| -Here's a simple example of how to use the `ollamaChat` for sentiment analysis, initialized with a few-shot prompt: |
| 48 | +For more examples of how to generate text using Ollama from MATLAB, see for instance: |
30 | 49 |
|
31 |
| -```matlab |
32 |
| -% Initialize the Ollama Chat object, passing a system prompt |
| 50 | +- [Process Generated Text in Real Time by Using Ollama in Streaming Mode](../examples/ProcessGeneratedTextinRealTimebyUsingOllamainStreamingMode.md) |
| 51 | +- [Retrieval\-Augmented Generation Using Ollama and MATLAB](../examples/RetrievalAugmentedGenerationUsingOllamaandMATLAB.md) (requires Text Analytics Toolbox™) |
33 | 52 |
|
34 |
| -% The system prompt tells the assistant how to behave, in this case, as a sentiment analyzer |
35 |
| -systemPrompt = "You are a sentiment analyser. You will look at a sentence and output"+... |
36 |
| - " a single word that classifies that sentence as either 'positive' or 'negative'."+.... |
37 |
| - newline + ... |
38 |
| - "Examples:" + newline +... |
39 |
| - "The project was a complete failure." + newline +... |
40 |
| - "negative" + newline + newline +... |
41 |
| - "The team successfully completed the project ahead of schedule." + newline +... |
42 |
| - "positive" + newline + newline +... |
43 |
| - "His attitude was terribly discouraging to the team." + newline +... |
44 |
| - "negative" + newline + newline; |
| 53 | +<a id="manage-chat-history"></a> |
| 54 | +# Manage Chat History |
45 | 55 |
|
46 |
| -chat = ollamaChat("mistral",systemPrompt); |
| 56 | +Manage and store messages in a conversation using the [`messageHistory`](functions/messageHistory.md) function. Use this to create a chatbot, use few\-shot prompting, or to facilitate workflows that require more than a single LLM call, such as tool calling. |
47 | 57 |
|
48 |
| -% Generate a response, passing a new sentence for classification |
49 |
| -txt = generate(chat,"The team is feeling very motivated") |
50 |
| -% Should output "positive" |
51 |
| -``` |
52 | 58 |
|
53 |
| -## Creating a chat system |
| 59 | +Connect to Ollama using the [`ollamaChat`](functions/ollamaChat.md) function. |
54 | 60 |
|
55 |
| -If you want to create a chat system, you will have to create a history of the conversation and pass that to the `generate` function. |
| 61 | +```matlab |
| 62 | +model = ollamaChat("mistral"); |
| 63 | +``` |
56 | 64 |
|
57 |
| -To start a conversation history, create a `messageHistory` object: |
| 65 | +Initialize the message history. |
58 | 66 |
|
59 | 67 | ```matlab
|
60 |
| -history = messageHistory; |
| 68 | +messages = messageHistory; |
61 | 69 | ```
|
62 | 70 |
|
63 |
| -Then create the chat assistant: |
| 71 | +Add a user message to the message history. |
64 | 72 |
|
65 | 73 | ```matlab
|
66 |
| -chat = ollamaChat("mistral"); |
| 74 | +messages = addUserMessage(messages,"What is the precise definition of a treble crochet stitch?"); |
67 | 75 | ```
|
68 | 76 |
|
69 |
| -Add a user message to the history and pass it to `generate`: |
| 77 | +Generate a response from the message history. |
70 | 78 |
|
71 | 79 | ```matlab
|
72 |
| -history = addUserMessage(history,"What is an eigenvalue?"); |
73 |
| -[txt, response] = generate(chat, history) |
| 80 | +[generatedText,completeOutput] = generate(model,messages) |
| 81 | +``` |
| 82 | + |
| 83 | +```matlabTextOutput |
| 84 | +generatedText = " A Treble Crochet Stitch (abbreviated as tr or trbl in patterns) is one of the basic stitches used in crocheting. It is formed by yarn-overing twice and inserting the hook under two loops on the previous row, then pulling up a loop through all six loops on the hook: one loop from each yarn over and one loop from each of the two adjacent stitches below. This combination creates a taller and looser stitch compared to a double crochet (dc) stitch. The treble crochet stitch is often used for increasing and creating textured patterns in crocheting projects." |
| 85 | +completeOutput = struct with fields: |
| 86 | + role: 'assistant' |
| 87 | + content: ' A Treble Crochet Stitch (abbreviated as tr or trbl in patterns) is one of the basic stitches used in crocheting. It is formed by yarn-overing twice and inserting the hook under two loops on the previous row, then pulling up a loop through all six loops on the hook: one loop from each yarn over and one loop from each of the two adjacent stitches below. This combination creates a taller and looser stitch compared to a double crochet (dc) stitch. The treble crochet stitch is often used for increasing and creating textured patterns in crocheting projects.' |
| 88 | +
|
74 | 89 | ```
|
75 | 90 |
|
76 |
| -The output `txt` will contain the answer and `response` will contain the full response, which you need to include in the history as follows: |
| 91 | +Add the response message to the message history. |
| 92 | + |
77 | 93 | ```matlab
|
78 |
| -history = addResponseMessage(history, response); |
| 94 | +messages = addResponseMessage(messages,completeOutput); |
79 | 95 | ```
|
80 | 96 |
|
81 |
| -You can keep interacting with the API and since we are saving the history, it will know about previous interactions. |
| 97 | +Ask a follow\-up question by adding another user message to the message history. |
| 98 | + |
82 | 99 | ```matlab
|
83 |
| -history = addUserMessage(history,"Generate MATLAB code that computes that"); |
84 |
| -[txt, response] = generate(chat,history); |
85 |
| -% Will generate code to compute the eigenvalue |
| 100 | +messages = addUserMessage(messages,"When was it first invented?"); |
86 | 101 | ```
|
87 | 102 |
|
88 |
| -## Streaming the response |
| 103 | +Generate a response from the message history. |
89 | 104 |
|
90 |
| -Streaming allows you to start receiving the output from the API as it is generated token by token, rather than wait for the entire completion to be generated. You can specifying the streaming function when you create the chat assistant. In this example, the streaming function will print the response to the command window. |
91 | 105 | ```matlab
|
92 |
| -% streaming function |
93 |
| -sf = @(x) fprintf("%s",x); |
94 |
| -chat = ollamaChat("mistral", StreamFun=sf); |
95 |
| -txt = generate(chat,"What is Model-Based Design and how is it related to Digital Twin?"); |
96 |
| -% Should stream the response token by token |
| 106 | +generate(model,messages) |
| 107 | +``` |
| 108 | + |
| 109 | +```matlabTextOutput |
| 110 | +ans = " The exact origins of crochet are unclear, but it is believed that it originated around the mid-16th century. Various types of crochet including treble stitches were developed and refined over time by different cultures such as Egyptians, Arabs, Persians, and Europeans. The modern version of crocheting using a hook and yarn became popular in Europe during the 19th century with the spread of the Industrial Revolution, which made it easier to produce thread and hooks on a mass scale. However, it is difficult to pinpoint the exact time when specific stitches like the treble crochet were first invented." |
97 | 111 | ```
|
98 | 112 |
|
99 |
| -## Understanding the content of an image |
| 113 | +For another example of how to use and manage the message history, see the [Create Simple Ollama ChatBot](../examples/CreateSimpleOllamaChatBot.md) example (requires Text Analytics Toolbox). |
| 114 | + |
| 115 | +<a id="images"></a> |
| 116 | +# Images |
100 | 117 |
|
101 |
| -You can use multimodal models like `moondream` to experiment with image understanding. |
| 118 | +You can use Ollama to generate text based on image inputs. For information on whether an Ollama model supports image inputs, check whether the model has the **`vision`** tag in [ollama.com/library](https://ollama.com/library). |
102 | 119 |
|
103 | 120 | > [!TIP]
|
104 |
| -> Many models available for Ollama allow you to include images in the prompt, even if the model does not support image inputs. In that case, the images are silently removed from the input. This can result in unexpected outputs. |
| 121 | +> Some models that do not support image inputs allow you to specify images in the prompt, but silently ignore the images from the input. |
| 122 | +
|
| 123 | + |
| 124 | +Load a sample image from Wikipedia. Use the `imread` function to read images from URLs or filenames. |
105 | 125 |
|
| 126 | +```matlab |
| 127 | +image_url = 'https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg'; |
| 128 | +im = imread(image_url); |
| 129 | +figure |
| 130 | +imshow(im) |
| 131 | +``` |
| 132 | + |
| 133 | + |
| 134 | + |
| 135 | +Set up the interface to Ollama using the model [Moondream](https://ollama.com/library/moondream). |
106 | 136 |
|
107 | 137 | ```matlab
|
108 | 138 | chat = ollamaChat("moondream");
|
109 |
| -image_path = "peppers.png"; |
| 139 | +``` |
| 140 | + |
| 141 | +Initialize the message history. Add a user prompt, along with the image, to the message history. |
| 142 | + |
| 143 | +```matlab |
110 | 144 | messages = messageHistory;
|
111 |
| -messages = addUserMessageWithImages(messages,"What is in the image?",image_path); |
112 |
| -[txt,response] = generate(chat,messages,MaxNumTokens=4096); |
113 |
| -txt |
114 |
| -% outputs a description of the image |
| 145 | +messages = addUserMessageWithImages(messages,"Please describe the image.", string(image_url)); |
115 | 146 | ```
|
116 | 147 |
|
117 |
| -## Establishing a connection to remote LLMs using Ollama |
| 148 | +Generate a response. |
118 | 149 |
|
119 |
| -To connect to a remote Ollama server, use the `Endpoint` name-value pair. Include the server name and port number. Ollama starts on 11434 by default. |
120 | 150 | ```matlab
|
121 |
| -chat = ollamaChat("mistral",Endpoint="ollamaServer:11434"); |
| 151 | +generate(chat,messages) |
122 | 152 | ```
|
| 153 | + |
| 154 | +```matlabTextOutput |
| 155 | +ans = |
| 156 | + " |
| 157 | + The image shows a long walkway or boardwalk made of wood, situated between two grass fields, likely in North America as it is close to the border between the United States and Mexico. The boards for the pathway are positioned at an angle towards the left side on both parts of the walkway. This path can provide easy access to nature, offering a relaxing stroll through the lush green field." |
| 158 | +
|
| 159 | +``` |
| 160 | + |
| 161 | +<a id="json-formatted-and-structured-output"></a> |
| 162 | +# JSON\-Formatted and Structured Output |
| 163 | + |
| 164 | +For some workflows, it is useful to generate text in a specific format. For example, a predictable output format allows you to more easily analyze the generated output. |
| 165 | + |
| 166 | + |
| 167 | +You can specify the format either by using JSON mode, or by using structured outputs, depending on what the model supports. Both generate text containing JSON code. For more information on structured output in Ollama, see [https://ollama.com/blog/structured\-outputs](https://ollama.com/blog/structured-outputs). |
| 168 | + |
| 169 | +<a id="json-mode"></a> |
| 170 | +## JSON Mode |
| 171 | + |
| 172 | +To run an LLM in JSON mode, set the `ResponseFormat` name\-value argument of [`ollamaChat`](functions/ollamaChat.md) or [`generate`](functions/generate.md) to `"json"`. To configure the format of the generated JSON code, describe the format using natural language and provide it to the model either in the system prompt or as a user message. The prompt or message describing the format must contain the word `"json"` or `"JSON"`. |
| 173 | + |
| 174 | +<a id="structured-output"></a> |
| 175 | +## Structured Output |
| 176 | + |
| 177 | +To use structured outputs, rather than describing the required format using natural language, provide the model with a valid JSON schema. |
| 178 | + |
| 179 | + |
| 180 | +In LLMs with MATLAB, you can specify the structure of the output in two different ways. |
| 181 | + |
| 182 | +- Specify a valid JSON Schema directly. |
| 183 | +- Specify an example structure array that adheres to the required output format. The software automatically generates the corresponding JSON Schema and provides this to the LLM. Then, the software automatically converts the output of the LLM back into a structure array. |
| 184 | + |
| 185 | +To do this, set the `ResponseFormat` name\-value argument of [`ollamaChat`](functions/ollamaChat.md) or [`generate`](functions/generate.md) to: |
| 186 | + |
| 187 | +- A string scalar containing a valid JSON Schema. |
| 188 | +- A structure array containing an example that adheres to the required format, for example: `ResponseFormat=struct("Name","Rudolph","NoseColor",[255 0 0])` |
| 189 | + |
| 190 | +For an example of how to use structured output with LLMs with MATLAB, see [Analyze Sentiment in Text Using ChatGPT and Structured Output](https://github.mathworks.com/development/llms-with-matlab/blob/main/examples/AnalyzeSentimentinTextUsingChatGPTwithStructuredOutput.md). |
| 191 | + |
| 192 | +<a id="tool-calling"></a> |
| 193 | +# Tool Calling |
| 194 | + |
| 195 | +Some large language models can suggest calls to a tool that you have, such as a MATLAB function, in their generated output. An LLM does not execute the tool itself. Instead, the model encodes the name of the tool and the name and value of any input arguments. You can then write scripts that automate the tool calls suggested by the LLM. |
| 196 | + |
| 197 | + |
| 198 | +To use tool calling, specify the `ToolChoice` name\-value argument of the [`ollamaChat`](functions/ollamaChat.md) function. |
| 199 | + |
| 200 | + |
| 201 | +For information on whether an Ollama model supports tool calling, check whether the model has the **`tools`** tag in [ollama.com/library](https://ollama.com/library). |
| 202 | + |
| 203 | + |
| 204 | +For an example of how to use tool calling with Ollama in LLMs with MATLAB, see [Analyze Text Data Using Parallel Function Calls with Ollama](/examples/AnalyzeTextDataUsingParallelFunctionCallwithOllama.md). |
| 205 | + |
| 206 | +<a id="see-also"></a> |
| 207 | +# See Also |
| 208 | + |
| 209 | +[`ollamaChat`](functions/ollamaChat.md) | [`generate`](functions/generate.md) | [`openAIFunction`](functions/openAIFunction.md) | [`addParameter`](functions/addParameter.md) | [`messageHistory`](functions/messageHistory.md) | [`addSystemMessage`](functions/addSystemMessage.md) | [`addUserMessage`](functions/addUserMessage.md) | [`addUserMessageWithImages`](functions/addUserMessageWithImages.md) | [`addToolMessage`](functions/addToolMessage.md) | [`addResponseMessage`](functions/addResponseMessage.md) | [`removeMessage`](functions/removeMessage.md) |
| 210 | + |
| 211 | + |
| 212 | +<a id="examples"></a> |
| 213 | +# Examples |
| 214 | + |
| 215 | +- [Process Generated Text in Real Time by Using Ollama in Streaming Mode](/examples/ProcessGeneratedTextInRealTimebyUsingOllamaInStreamingMode.md) |
| 216 | +- [Create Simple Ollama ChatBot](/examples/CreateSimpleOllamaChatBot.md) (requires Text Analytics Toolbox) |
| 217 | +- [Analyze Sentiment in Text Using ChatGPT and Structured Output](/examples/AnalyzeSentimentinTextUsingChatGPTwithStructuredOutput.md) |
| 218 | +- [Analyze Text Data Using Parallel Function Calls with Ollama](/examples/AnalyzeTextDataUsingParallelFunctionCallwithOllama.md) |
| 219 | +- [Retrieval-Augmented Generation Using Ollama and MATLAB](/examples/RetrievalAugmentedGenerationUsingOllamaAndMATLAB.md) (requires Text Analytics Toolbox) |
| 220 | + |
| 221 | +*Copyright 2024-2025 The MathWorks, Inc.* |
0 commit comments