Using ollama with python


Introduction 

If you want to create applications like AI chatbots, Resume Analyzer, Content Generation or any personal AI assistance then we must need any programming language.  Python help us as a bridge for easy communication between the model and the user.  By the help of python we can integrate ollama into our program and also can develop AI-powered application using python.

Why python?

Now the question can be ask why to use python over other programming language.  so the reason is that python is most popular language for fields like Artificial Intelligence and Machine Learning, most AI tool and frameworks also supports python so, it is very common to use python with ollama.

Communication flow:

The flow which is followed with or without any programming language is:-

without any programming language: User → Terminal → ollama → Model → Response here the user have to interact with the terminal every time the want any response of there prompt. with python: User → Python programing → ollama API → Model → Response → Python program → user now here user don't have to interact with terminal every time they can directly communicate with the programing language and can get there response.

Python interact with localhost API server for execution of the model.

Required library for using ollama with python: 

pip install ollama

it loads all the dependencies and libraries of ollama in the program for response.

Interaction with ollama:

now python program can send prompt to AI models.

from ollama import chat response = chat(model='llama3', messages=[{'role': 'user', 'content': ‘What is AI?’}]) print(response['message']['content'])

read more about ollama:

previous topic: understanding ollama API
next topic: building your first AI chatbot using ollama and python

0 Comments Report