The ollama python library provides the easiest way to integrate your python project with Ollama
This requires a python version of 3.9 or higher
pip install ollama-pythonThe python package splits the functionality into three core endpoints
- Model Management Endpoints: This includes the ability to create, delete, pull, push and list models amongst others
- Generate Endpoint: This includes the generate and chat endpoints in Ollama
- Embedding Endpoint: This includes the ability to generate embeddings for a given text
Pydantic is used to verify user input and Responses from the server are parsed into pydantic models
from ollama_python.endpoints import GenerateAPI
api = GenerateAPI(base_url="http://localhost:8000", model="mistral")
result = api.generate(prompt="Hello World", options=dict(num_tokens=10), format="json")from ollama_python.endpoints import GenerateAPI
api = GenerateAPI(base_url="http://localhost:8000", model="mistral")
for res in api.generate(prompt="Hello World", options=dict(num_tokens=10), format="json", stream=True):
print(res.response)from ollama_python.endpoints import GenerateAPI
api = GenerateAPI(base_url="http://localhost:8000", model="mistral")
messages = [{'role': 'user', 'content': 'Why is the sky blue?'}]
result = api.generate_chat_completion(messages=messages, options=dict(num_tokens=10), format="json")from ollama_python.endpoints import GenerateAPI
api = GenerateAPI(base_url="http://localhost:8000", model="mistral")
messages = [{'role': 'user', 'content': 'Why is the sky blue?'}]
for res in api.generate_chat_completion(messages=messages, options=dict(num_tokens=10), format="json", stream=True):
print(res.message)from ollama_python.endpoints import GenerateAPI
api = GenerateAPI(base_url="http://localhost:8000", model="llava")
messages = [{'role': 'user', 'content': 'What is in this image', 'image': 'iVBORw0KGgoAAAANSUhEUgAAAG0AAABmCAYAAADBPx+VAAAACXBIWXMAAAsTAAALEwEAmp'}]
result = api.generate_chat_completion(messages=messages, options=dict(num_tokens=10), format="json")
print(result.message)from ollama_python.endpoints import EmbeddingAPI
api = EmbeddingAPI(base_url="http://localhost:8000", model="mistral")
result = api.get_embedding(prompt="Hello World", options=dict(seed=10))from ollama_python.endpoints import ModelManagementAPI
api = ModelManagementAPI(base_url="http://localhost:8000")
result = api.create(name="test_model", model_file="random model_file")from ollama_python.endpoints import ModelManagementAPI
api = ModelManagementAPI(base_url="http://localhost:8000")
for res in api.create(name="test_model", model_file="random model_file", stream=True):
print(res.status)from ollama_python.endpoints import ModelManagementAPI
api = ModelManagementAPI(base_url="http://localhost:8000")
result = api.check_blob_exists(digest="sha256:29fdb92e57cf0827ded04ae6461b5931d01fa595843f55d36f5b275a52087dd2")from ollama_python.endpoints import ModelManagementAPI
api = ModelManagementAPI(base_url="http://localhost:8000")
result = api.create_blob(digest="sha256:29fdb92e57cf0827ded04ae6461b5931d01fa595843f55d36f5b275a52087dd2")from ollama_python.endpoints import ModelManagementAPI
api = ModelManagementAPI(base_url="http://localhost:8000")
result = api.list_local_models()
print(result.models)from ollama_python.endpoints import ModelManagementAPI
api = ModelManagementAPI(base_url="http://localhost:8000")
result = api.show(name="mistral")
print(result.details)from ollama_python.endpoints import ModelManagementAPI
api = ModelManagementAPI(base_url="http://localhost:8000")
result = api.copy(source="mistral", destination="mistral_copy")from ollama_python.endpoints import ModelManagementAPI
api = ModelManagementAPI(base_url="http://localhost:8000")
api.delete(name="mistral_copy")from ollama_python.endpoints import ModelManagementAPI
api = ModelManagementAPI(base_url="http://localhost:8000")
result = api.pull(name="mistral")
print(result.status)from ollama_python.endpoints import ModelManagementAPI
api = ModelManagementAPI(base_url="http://localhost:8000")
for res in api.pull(name="mistral", stream=True):
print(res.status)from ollama_python.endpoints import ModelManagementAPI
api = ModelManagementAPI(base_url="http://localhost:8000")
result = api.push(name="mistral")
print(result.status)from ollama_python.endpoints import ModelManagementAPI
api = ModelManagementAPI(base_url="http://localhost:8000")
for res in api.push(name="mistral", stream=True):
print(res.status)Add support for Asynchronous version of the library
- Clone the repo
- Run
poetry install - Run
pre-commit install
Then you're ready to contribute to the repo
