How to Call Your Own Deployed LLM with Ollama API (Part 1)

This guide walks through installing Ollama, pulling a large language model, and using both the raw REST endpoint and the official Python client to generate completions, manage models, and handle streaming responses on a local machine.

Subtle Storm
Subtle Storm
Subtle Storm
How to Call Your Own Deployed LLM with Ollama API (Part 1)

Ollama is an open‑source tool for locally deploying large language models (LLMs) and exposing them via an API, eliminating the need for external services. The article first outlines the hardware requirements for running big models and then lists the commands to download, install, start the Ollama service, pull a model (e.g., llama3), list installed models, and run a quick test.

1. Install and start Ollama

Download the installer from https://ollama.com/ for your OS.

Run the installer and start the service (it usually starts automatically).

Check the service status with ollama serve.

2. Pull and verify a model

Pull a model with ollama pull llama3.

List models using ollama list.

Run a test generation with ollama run llama3.

3. Basic REST API call using requests

import requests
import json
from typing import Dict, List, Optional

class OllamaClient:
    """Simple Ollama REST API client.
    Default address: http://localhost:11434"""
    def __init__(self, base_url: str = "http://localhost:11434"):
        self.base_url = base_url
        self.session = requests.Session()
        self.timeout = 120  # long texts may need more time

    def generate_completion(self, model: str, prompt: str, system: Optional[str] = None,
                            temperature: float = 0.7, max_tokens: int = 1024,
                            stream: bool = False) -> Dict:
        """Generate a text completion.
        Parameters:
          - model: model name (e.g., "llama3")
          - prompt: user input
          - system: optional system prompt
          - temperature: 0.0‑1.0
          - max_tokens: maximum tokens to generate
          - stream: whether to stream output
        Returns:
          - dictionary with the response"""
        endpoint = f"{self.base_url}/api/generate"
        payload = {
            "model": model,
            "prompt": prompt,
            "options": {
                "temperature": temperature,
                "num_predict": max_tokens
            },
            "stream": stream
        }
        if system:
            payload["system"] = system
        try:
            if stream:
                return self._handle_stream_request(endpoint, payload)
            else:
                response = self.session.post(endpoint, json=payload, timeout=self.timeout)
                response.raise_for_status()
                return response.json()
        except requests.exceptions.RequestException as e:
            raise Exception(f"API request failed: {str(e)}")

    def _handle_stream_request(self, endpoint: str, payload: dict):
        """Handle streaming responses"""
        response = self.session.post(endpoint, json=payload, stream=True, timeout=self.timeout)
        response.raise_for_status()
        full_response = ""
        for line in response.iter_lines():
            if line:
                try:
                    json_response = json.loads(line.decode('utf-8'))
                    chunk = json_response.get("response", "")
                    full_response += chunk
                    print(chunk, end="", flush=True)  # optional real‑time output
                    if json_response.get("done", False):
                        return {
                            "response": full_response,
                            "model": json_response.get("model"),
                            "total_duration": json_response.get("total_duration")
                        }
                except json.JSONDecodeError:
                    continue
        return {"response": full_response}

# Example usage
if __name__ == "__main__":
    client = OllamaClient()
    result = client.generate_completion(
        model="llama3",
        prompt="请解释什么是人工智能?",
        system="你是一个乐于助人的AI助手,请用中文回答。",
        temperature=0.8,
        max_tokens=500
    )
    print(f"Model reply: {result.get('response')}")
    print(f"Generation time: {result.get('total_duration', 0) / 1e9:.2f} seconds")

4. Using the official ollama Python package

import ollama
from ollama import ChatResponse, GenerateResponse
import asyncio
from datetime import datetime

class OllamaPythonClient:
    """Client based on the official Ollama Python package"""
    def __init__(self, host: str = "http://localhost:11434"):
        self.client = ollama.Client(host=host)

    def list_models(self) -> List[Dict]:
        """List locally available models"""
        models = self.client.list()
        return models.get("models", [])

    def check_model(self, model_name: str) -> bool:
        """Check whether a model is available"""
        try:
            models = self.list_models()
            return any(m["name"] == model_name for m in models)
        except Exception:
            return False

    def chat_completion(self, model: str, messages: List[Dict[str, str]], **kwargs) -> ChatResponse:
        """Chat‑style completion (recommended)"""
        try:
            response = self.client.chat(model=model, messages=messages, **kwargs)
            return response
        except Exception as e:
            raise Exception(f"Chat request failed: {str(e)}")

    def generate_with_stream(self, model: str, prompt: str, callback=None) -> str:
        """Generate text with streaming output"""
        full_response = ""
        stream = self.client.generate(model=model, prompt=prompt, stream=True)
        for chunk in stream:
            token = chunk.get("response", "")
            full_response += token
            if callback and token:
                callback(token)
        return full_response

def advanced_usage_demo():
    client = OllamaPythonClient()
    if not client.check_model("llama3"):
        print("Please pull the model first: ollama pull llama3")
        return
    print("=" * 50)
    print("Demo 1: Multi‑turn conversation")
    print("=" * 50)
    conversation_history = [
        {"role": "system", "content": "You are a professional Python coding assistant."},
        {"role": "user", "content": "How to read a CSV file in Python?"}
    ]
    response1 = client.chat_completion(model="llama3", messages=conversation_history, options={"temperature": 0.7})
    print(f"AI reply: {response1['message']['content']}")
    conversation_history.append({"role": "assistant", "content": response1['message']['content']})
    conversation_history.append({"role": "user", "content": "If the CSV is huge, how to read it in chunks?"})
    response2 = client.chat_completion(model="llama3", messages=conversation_history)
    print(f"
AI reply: {response2['message']['content']}")
    print("
" + "=" * 50)
    print("Demo 2: Streaming output")
    print("=" * 50)
    def print_token(token: str):
        print(token, end="", flush=True)
        print("Generating: ", end="")
    result = client.generate_with_stream(model="llama3", prompt="Write a quick‑sort implementation in Python with detailed comments.", callback=print_token)
    print("

" + "=" * 50)
    print("Demo 3: Model management")
    print("=" * 50)
    models = client.list_models()
    for model in models:
        print(f"Model: {model['name']}")
        print(f"  Size: {model.get('size', 'N/A')}")
        print(f"  Modified at: {model.get('modified_at', 'N/A')}")

if __name__ == "__main__":
    advanced_usage_demo()

The article ends with a note that more content will follow.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonLLMAPIRESTLocal DeploymentOllama
Subtle Storm
Written by

Subtle Storm

The micro era's marvels are boundlessly subtle.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.