To call a local Ollama model from a Jupyter Notebook, you typically need to interact with the Ollama service via its API or command-line interface (CLI). Since Ollama is a platform for running large language models locally, it might provide either an HTTP API or a CLI command you can call from Python.
Here’s a general approach:
1. Using Ollama CLI commands in Jupyter Notebook
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
If Ollama exposes a CLI (command-line interface), you can invoke it from Jupyter using Python’s subprocess module.
import subprocess
# Example command to call Ollama CLI with a prompt (adjust command and arguments as needed)
prompt = "Hello, how are you?"
result = subprocess.run(['ollama', 'run', 'model_name', prompt], capture_output=True, text=True)
print(result.stdout)
Replace 'model_name' with the actual model you want to run.
Make sure the ollama CLI is in your system PATH.
2. Using Ollama HTTP API (if available)
If Ollama provides a local HTTP server or REST API, you can interact with it via Python HTTP requests.
Example using the requests library:
import requests
url = "http://localhost:port/api_endpoint" # Replace with actual endpoint
data = {
"model": "model_name",
"prompt": "Hello, how are you?"
}
response = requests.post(url, json=data)
print(response.json())
You need to check Ollama documentation for the actual API endpoint, port, and request format.
Check Ollama official docs
Since Ollama is a relatively new platform, you might want to check their official documentation for specifics about:
How to start the local Ollama server.
How to run a model via CLI.
How to interact via API.
Summary
If you have the ollama CLI tool, invoke it via subprocess.run() from Python.
If there's an HTTP API, use requests in Python to access it.
Ensure Ollama is properly installed and your models are downloaded locally.
If you provide more details about your setup or the specific tasks, I can help tailor the code snippet more precisely.