Getting Started with LLMs Using Ollama and Python on Your Local Machine

Rupesh Mangal
3 min readJun 14, 2024

--

Introduction

Did you know that some of the most advanced artificial intelligence (AI) applications today are powered by Large Language Models (LLMs)? From chatbots to content generators, LLMs are revolutionizing how we interact with technology. If you’re new to the world of LLMs and eager to learn how to leverage them using Python on your local machine, you’ve come to the right place. In this post, we’ll explain what LLMs are, explore their uses, introduce some popular LLMs, and provide a step-by-step tutorial on setting up and using Ollama for building AI applications.

What are LLMs?

LLMs, or Large Language Models, are a type of artificial intelligence designed to understand and generate human language. These models are trained on vast amounts of text data, enabling them to perform tasks such as text completion, translation, summarization, and more. Essentially, they can read, understand, and write text much like a human.

Applications of LLMs

LLMs are used in various AI applications, including:

  • Chatbots and Virtual Assistants: Automating customer support and providing conversational interfaces.
  • Content Generation: Creating articles, stories, and other written content.
  • Language Translation: Translating text from one language to another.
  • Summarization: Condensing large texts into shorter summaries.
  • Sentiment Analysis: Determining the sentiment or emotion behind a piece of text.

Popular LLMs

Several LLMs are available for developers, including:

  • GPT-3 by OpenAI: One of the most well-known and powerful language models.
  • BERT by Google: Widely used for natural language understanding tasks.
  • T5 by Google: Known for its flexibility in handling various NLP tasks.
  • Ollama: A user-friendly tool for managing and deploying LLMs.

Setting Up Ollama on Your Local Machine using Python

Now, let’s dive into setting up Ollama on your local machine. This will allow you to harness the power of LLMs right from your own computer.

Step 1: Install Python

First, ensure you have Python installed on your machine. You can download Python from the official website.

Step 2: Install Ollama CLI

Ollama is a tool designed to simplify the management and deployment of LLMs. To install the Ollama CLI, open your terminal (Command Prompt for Windows, Terminal for macOS/Linux) and run:

pip install ollama

Step 3: Running and Serving Models with Ollama

Before you can interact with Ollama using Python, you need to run and serve the LLM model locally.

  1. Run the Model: To run an LLM model (e.g., llama3), use the following command in your terminal:
ollama run llama3

This command initializes the model and prepares it for serving.

2. Serve the Model: Start the Ollama server to serve the model, allowing it to handle requests:

ollama serve

Ensure that the server is running without errors. You should see an output indicating that the server is up and listening for requests.

Step 4: Using Ollama in Python

Here’s how you can start using Ollama in a Python script:

  1. Import Ollama: Start by importing the Ollama package.
import ollama
  1. Initialize the Ollama Client: Create an instance of the Ollama client.
client = ollama.Client()
  1. Load a Model: Load the desired LLM.
model = client.load_model('llama3')
  1. Perform Inference: Use the model to generate text or perform other tasks.
input_text = "Tell me a joke." result = model.predict(input_text) 
print(result)

Example Python Script

Here’s a complete example of a Python script using Ollama:

import ollama

# Initialize the Ollama client
client = ollama.Client()

# Load a model
model = client.load_model('llama3')

# Perform inference
input_text = "Tell me a joke."

result = model.predict(input_text)

print(result)

Additional Resources

For more detailed information on setting up and using Ollama, check out the following resources:

Conclusion

Getting started with LLMs using Python on your local machine is a fantastic way to explore the capabilities of AI and build innovative applications.

By following the steps outlined in this guide, you can set up Ollama and begin leveraging powerful language models in your projects. Whether you’re interested in creating chatbots, generating content, or exploring other AI applications, LLMs offer endless possibilities.

What AI application are you excited to build with LLMs? Share your thoughts in the comments below, and don’t forget to subscribe for more tutorials and AI insights!

--

--

Rupesh Mangal
Rupesh Mangal

Written by Rupesh Mangal

Experienced AI software engineer specializing in developing customer-centric models and AI applications from 0-1 https://www.linkedin.com/in/rup-cst/

Responses (4)