# Set up Ollama on Windows

This guide walks you through the main steps of setting up an [Ollama server](https://ollama.com/) for [use in GPT for Word](https://autosheet.com/docs/gpt-for-word/setup/manage-models/connect-to-ollama) on Windows.

This guide assumes that you use GPT for Word on the same machine that hosts Ollama.

**Prerequisites**

- Local machine with enough processing power and memory to run LLMs (see the [Ollama documentation](https://docs.ollama.com/gpu) for hardware support information)

- Windows 10 22H2 or newer (see the [Ollama documentation](https://docs.ollama.com/windows) for detailed system requirements)

- Windows user account with administrator privileges

- Installed software:

  - [curl](https://curl.se/windows/) (ships with Windows)
  - [PowerShell 7](https://learn.microsoft.com/en-us/powershell/scripting/install/install-powershell-on-windows)

To set up an Ollama server on Windows:

1. [Install the server.](https://autosheet.com/docs/gpt-for-word/setup/manage-models/connect-to-ollama/set-up-ollama-on-windows#install-an-ollama-server)
2. [Install a model on the server.](https://autosheet.com/docs/gpt-for-word/setup/manage-models/connect-to-ollama/set-up-ollama-on-windows#install-a-model-on-the-ollama-server)
3. [Enable CORS for the server.](https://autosheet.com/docs/gpt-for-word/setup/manage-models/connect-to-ollama/set-up-ollama-on-windows#enable-cors-for-the-ollama-server)

## Install an Ollama server

1. Download and run the [Windows installer](https://ollama.com/download). Follow the on-screen instructions to complete the installation.

   The installer starts the Ollama server in the background and sets the server to start automatically on system boot. The installer also installs the Ollama desktop application for easily starting and stopping the server.

   > **Tip**
   >
   > You can manually start a new instance of the Ollama server in a terminal by running `ollama serve`. However, to avoid port conflicts, make sure no other Ollama server instances are running at the same time.

2. Open PowerShell as an administrator and verify that the server is running:

   ```shell
   curl http://localhost:11434
   ```

   Response if the server is running:

   ```
   Ollama is running
   ```

   > **Note**
   >
   > Make sure you're using curl and not an aliased command, such as `Invoke-WebRequest`:
   >
   > ```powershell
   > Remove-Item alias:curl
   > ```
   >
   > If `curl` has an alias, the command removes it for the current terminal session, ensuring that subsequent `curl` commands in this guide work as expected.

You have installed the Ollama server.

For more information about installing and managing the server on Windows, see the [Ollama documentation](https://docs.ollama.com/windows).

## Install a model on the Ollama server

1. Install a model from the [Ollama library](https://ollama.com/search):

   ```shell
   ollama pull <model-name>
   ```

   For example, to install the [Meta Llama 3.2 3B](https://ollama.com/library/llama3.2) model:

   ```shell
   ollama pull llama3.2
   ```

   After the installation completes, the model is available for prompting on the Ollama server.

2. Verify that the model works:

   ```shell
   curl http://localhost:11434/api/generate -d '{ "model": "llama3.2", "prompt": "Who are you?", "stream": false }'
   ```

   The above request asks the server to generate a response for the specified prompt with the specified model and to return the response in a single reply. If the model works, the server returns a JSON object containing the response and metadata about the response.

You have installed the model on the Ollama server. For more information about working with models, see the [Ollama documentation](https://docs.ollama.com/cli).

## Enable CORS for the Ollama server

By default, the Ollama server only accepts [same-origin requests](https://developer.mozilla.org/en-US/docs/Web/Security/Defenses/Same-origin_policy). Since GPT for Word always has a different origin from the Ollama server, you must enable [cross-origin resource sharing (CORS)](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/CORS) for the server using the `OLLAMA_ORIGINS` environment variable.

To enable CORS for the Ollama server:

1. Set `OLLAMA_ORIGINS` with the origins that are allowed to access the server:

   ```shell
   # Set a single origin
   setx OLLAMA_ORIGINS "<ORIGIN>"

   # Set multiple origins
   setx OLLAMA_ORIGINS "<ORIGIN_1>, <ORIGIN_2>, ..."
   ```

   For example:

   - Allow any origin to make requests to the server:

     ```shell
     setx OLLAMA_ORIGINS "*"
     ```

   - Allow only GPT for Word to make requests to the server:

     ```shell
     setx OLLAMA_ORIGINS "https://excel-addin.gptforwork.com"
     ```

   > **Info**
   >
   > By default, the `setx` command sets a user variable. If you want to set a system variable, which applies to all users of the current machine, run the command (as an administrator) with the `/m` parameter. For example:
   >
   > ```shell
   > setx OLLAMA_ORIGINS "*" /m
   > ```

2. Restart the server for the variable to take effect.

3. Verify that the [`/api/tags`](https://github.com/ollama/ollama/blob/main/docs/api.md#list-local-models) endpoint of the server works:

   ```shell
   curl http://localhost:11434/api/tags
   ```

   GPT for Word uses the endpoint to fetch a list of models installed on the server. If the endpoint works, the server returns a JSON object with a `models` property listing all currently installed models:

   ```
   {
     "models": [
       {
         ...
       }
     ]
   }
   ```

You have enabled CORS for the Ollama server.

> **What's next**
>
> You have completed the setup required to access the Ollama server from GPT for Word on the same machine. You can now set `http://localhost:11434` as the [endpoint URL in GPT for Word](https://autosheet.com/docs/gpt-for-word/setup/manage-models/connect-to-ollama#set-up-the-api-endpoint-for-an-ollama-server).
