Local AI
Local AI
Intermediate
Call Ollama from Code
Use the REST API from Python, Node.js, or curl
58 of 66
Start the API server
ollama serve
The API is available at:
http://localhost:11434
Generate with curl
curl http://localhost:11434/api/generate -d '{
"model": "llama3.2",
"prompt": "Write a Python function that reverses a string",
"stream": false
}'
Generate with Python
import requests
r = requests.post("http://localhost:11434/api/generate", json={
"model": "llama3.2",
"prompt": "Write a Python function that reverses a string",
"stream": False
})
print(r.json()["response"])
Streaming responses
Set
"stream": true
and iterate over response chunks for real-time output.
Chat endpoint
For multi-turn conversations, use the chat API:
curl http://localhost:11434/api/chat -d '{
"model": "llama3.2",
"messages": [
{"role": "user", "content": "Hello"}
],
"stream": false
}'
Connect your IDE
Both Claude Code and Cursor accept a custom OpenAI-compatible URL. Point them to:
http://localhost:11434/v1