## Install Ollama
Select the appropriate installation method based on your operating system, taking Linux CentOS as an example.
Use the command `curl -fsSL https://ollama.com/install.sh | sh` to install (requires sudo privileges). After installation, you can verify if it was successful by running `ollama –version`.
### Start Ollama
After successful installation, start the Ollama service (the startup method varies slightly between operating systems; generally, after installation, the related service startup item will be automatically configured, or you can manually enter the corresponding command in the terminal to start it, for example, on Linux, you can directly enter `ollama serve` to start the service).
### Add Ollama as a System Service (Optional)
You can add Ollama as a system service via `systemd`.
#### Example Configuration File
“` /etc/systemd/system/ollama.service
[Unit]
Description=Ollama Service
After=network-online.target
[Service]
ExecStart=/usr/local/bin/ollama serve
User=ollama
Group=ollama
Restart=always
RestartSec=3
Environment=”OLLAMA_HOST=0.0.0.0″
[Install]
WantedBy=default.target
“`
#### Service Reload and Restart
“` bash
# Reload configuration file
sudo systemctl daemon-reload
# Restart ollama service
sudo systemctl restart ollama
# Check ollama service status
sudo systemctl status ollama.service
“`
## Download Phi-4 Model
In the terminal (command prompt, PowerShell, or shell on Linux/macOS), enter the following command to download the `Phi-4` model (provided that the Ollama service is running normally).
“`bash
ollama pull phi4
“`
This process will download the `Phi-4` model to your local computer from the corresponding model source; the download time depends on the server’s network bandwidth and the size of the model.
The model is stored by default at “/usr/share/ollama/.ollama/models” (`macOS at ~/.ollama/models, Windows at C:\Users\
## Use Python to Call Ollama API (Using `requests` Library as an Example)
In the `BIClass_py310` conda environment, install the `requests` library (if not installed, you can install it using the command `pip install requests`).
### Call Ollama’s `Phi-4` Model API to Generate Text Example
“` python
import json
import requests
# Default local address and port for Ollama API, can be modified based on actual configuration
url = “http://localhost:11434/api/chat”
headers = {
“Content-Type”: “application/json”
}
# Data to be sent
data = {
“model”: “phi4”,
“messages”: [
{“role”:”system”,”content”: “You are a caring higher education scholar.”},
{“role”: “user”,”content”: “What gains or changes will students who shouldn’t attend university experience?”}
],
“stream”: False
}
# Convert dictionary to JSON formatted string
json_data = json.dumps(data)
# Send POST request
response = requests.post(url, data=json_data, headers=headers)
# Print response content
print(response.text)
“`