Back to Home

Local LLM in Python: Quick Start with Ollama and LiteLLM

Step-by-step guide to deploying and integrating a local LLM in Python. Use Ollama to run models and LiteLLM for interaction, without API keys and cloud services. Ideal for developers.

Local LLM in Python: Quick Start with Ollama and LiteLLM for Developers
Advertisement 728x90

Local LLM in Python: A Quick Start with Ollama and LiteLLM for Developers

Many developers embarking on their journey into the world of Large Language Models (LLMs) often encounter hurdles: the need to register for cloud services, manage API keys, and face continuous token costs. While functional, this approach isn't always ideal for initial experiments and local development. This article presents an effective alternative: deploying and integrating LLMs directly on your computer. We'll guide you step-by-step through setting up a local environment using Ollama to run models and LiteLLM to integrate them into Python applications, demonstrating how to get your first meaningful response from your own LLM without relying on external services.

Advantages of Local LLM Development

Local LLM deployment offers several undeniable advantages, especially for learning and prototyping. Firstly, it completely eliminates the need for API keys and associated financial costs, enabling unlimited experimentation. Secondly, it removes the dependency on an internet connection for every request, which is crucial for offline development or working in environments with unstable networks. Thirdly, this approach makes the LLM interaction process highly transparent: you can observe how your Python code communicates with LiteLLM, which then passes the request to Ollama, and finally to the local model. This isn't 'AI magic,' but rather clearly traceable software logic, significantly simplifying debugging and architectural understanding. For developers aiming to deeply grasp the mechanics of LLM integration, a local approach is the ideal starting point.

Ollama and LiteLLM: Key Architectural Components

Before diving into the installation, it's crucial to differentiate the functions of the two primary tools we'll be using: Ollama and LiteLLM.

Google AdInline article slot
  • Ollama is a powerful tool for running large language models locally. It acts as a server that manages the downloading, storage, and execution of LLMs on your computer, providing a unified API for accessing them. Essentially, Ollama is a runtime for local LLMs, making it easy to work with various models like Llama, Mistral, Qwen, and others.
  • LiteLLM is a lightweight Python library designed to unify API requests to different LLM providers. Its key feature is providing a single interface for interacting with both cloud services (OpenAI, Anthropic, Google Gemini) and local solutions like Ollama. This means that code written for a local model via LiteLLM can be easily adapted to work with cloud providers with minimal changes.

In essence, Ollama 'spins up' the local model, LiteLLM provides a standardized way to interact with it from Python, and your code builds a complete application around this.

Installing Ollama and Downloading a Local Model

To begin working with a local LLM, you need to follow a few sequential steps.

Installing Ollama

First, download and install Ollama from the official website (ollama.com) for your operating system (Windows, macOS, Linux). After installation, verify the correctness of the installation by running the following command in your terminal:

Google AdInline article slot
ollama --version

A successful output of the version number confirms Ollama is ready for use. On Windows, ensure the Ollama icon (🦙) is present in your system tray, indicating an active background service.

Downloading a Local LLM

Ollama itself is not a model, but rather a platform for running them. Now, you need to download a specific large language model. For our purposes, qwen2.5:3b is an excellent choice — it's a compact yet sufficiently powerful model that runs well on most laptops and supports the Russian language. Initiate the download via your terminal:

ollama run qwen2.5:3b

The first time you run this, the model will begin downloading and will be saved locally. Once the download is complete, you'll enter an interactive Ollama chat. Test the model's functionality by asking it a question in Russian, for example: Hello! Tell me in two sentences, what is Python. (Hello! In two sentences, tell me what Python is.) If the model responds, you've cleared the main hurdle. To exit the chat, use /bye.

Google AdInline article slot

Note: If the performance of qwen2.5:3b is insufficient for your hardware, try a lighter version: ollama run qwen2.5:1.5b. The Python code logic will remain unchanged.

Preparing Your Python Environment

To isolate project dependencies, it's recommended to use a virtual environment.

  • Create a project folder and navigate into it:

```bash

mkdir llm-chat

cd llm-chat

```

  • Initialize a virtual environment:

```bash

python -m venv venv

```

  • Activate it.

* Windows: venv\Scripts\activate

* macOS / Linux: source venv/bin/activate

Successful activation will be indicated by the (venv) prefix in your command line.

  • Install the LiteLLM library:

```bash

pip install litellm

```

Now all components are ready for coding.

Integrating an LLM in Python: Your First Request

Create a file named main.py in your project's root directory and add the following code:

from litellm import completion
 
MODEL = "ollama_chat/qwen2.5:3b"
API_BASE = "http://localhost:11434"
 
 
def ask(question: str) -> str:
    response = completion(
        model=MODEL,
        messages=[
            {"role": "user", "content": question}
        ],
        api_base=API_BASE,
        request_timeout=120,
    )
    return response.choices[0].message.content
 
 
answer = ask("Hello! Write one short sentence about Python.")
print(answer)

Run the script from your activated virtual environment:

python main.py

If a meaningful response from the model appears in your console, you have successfully integrated a local LLM into your Python code. This is a pivotal moment, demonstrating how an abstract model transforms into a functional component of your application.

Code Breakdown of main.py

Let's examine the main elements of this minimalistic yet functional script:

  • from litellm import completion: We import the core completion function from the LiteLLM library, designed for sending requests to LLMs.
  • MODEL = "ollama_chat/qwen2.5:3b": This variable defines the model we are using. The ollama_chat/ prefix tells LiteLLM that the request should be directed to the local Ollama service. Here, you should specify the name of the model you downloaded (e.g., qwen2.5:1.5b).
  • API_BASE = "http://localhost:11434": Specifies the address of the local Ollama API server. By default, Ollama runs on port 11434.
  • messages=[{"role": "user", "content": question}]: This is the fundamental structure for interacting with most modern LLM APIs. Requests are passed as a list of messages, where each message has a role (e.g., "user", "assistant", "system") and content (the message text). Even for a single question, this structure is used, laying the groundwork for future work with dialogue history and context.
  • request_timeout=120: Sets the maximum waiting time for a response from the model in seconds. Local models might require more time for their initial startup or to process complex requests.
  • response.choices[0].message.content: From the complete response object returned by the model, we extract the directly generated text. The response object contains a wealth of other useful information that we can explore in subsequent parts.

Troubleshooting Common Startup Issues

During the setup process, you might encounter various difficulties. Here are the most frequent ones and how to resolve them:

  • Issue 1: Connection refused or Failed to connect

* Cause: The Ollama service is not running or is inaccessible.

* Solution: Ensure Ollama is active. Check for the 🦙 icon in your system tray (Windows/macOS) or run ollama list in your terminal. If the command responds, the service is operational.

  • Issue 2: Model not found

* Cause: The model specified in the code has not been downloaded, or its name does not match the installed one.

* Solution: Run ollama list to see a list of available local models, and then correct the model name in the MODEL variable in main.py.

  • Issue 3: Slow response or low performance

* Cause: The first launch of a model can be slow due to loading into memory, or the model might be too demanding for your hardware.

* Solution: Wait longer. Close resource-intensive applications. Try using a lighter version of the model, for example, qwen2.5:1.5b.

  • Issue 4: ModuleNotFoundError: No module named 'litellm'

* Cause: The script was run outside the activated virtual environment where LiteLLM is installed.

* Solution: Ensure that you have activated your virtual environment (indicated by the (venv) prefix in your terminal) before running the script.

  • Issue 5: Encoding issues in PowerShell (garbled characters)

* Cause: Mismatch in terminal encoding.

* Solution: In PowerShell, execute $OutputEncoding = [System.Text.Encoding]::UTF8 before running the script.

Key Takeaways

  • Local LLM deployment with Ollama and LiteLLM helps avoid API costs and internet dependency, providing full control over the process.
  • Ollama acts as a runtime for local models, while LiteLLM unifies the API for interacting with them from Python.
  • The first step involves installing Ollama and downloading a suitable model, such as qwen2.5:3b.
  • The Python script uses litellm.completion to send requests, where MODEL points to the local model via ollama_chat/, and API_BASE specifies the Ollama address.
  • Requests to the LLM are passed as a list of messages with roles (role) and content (content), forming the foundation for building conversational systems.

Conclusion and Next Steps

At this stage, we have successfully implemented basic integration of a local LLM into a Python application. The model has transformed from an abstract technology into a manageable component capable of responding to queries directly from your code. This significant step opens the door for further experimentation and the development of more complex AI applications.

In the next part, we will transform this minimal example into a full-fledged console chat, adding a conversation loop, user input capability, and basic preparation for managing dialogue context. This will allow you to move from one-off questions to interactive engagement with your local LLM.

— Editorial Team

Advertisement 728x90

Read Next