A professional Python library for Ollama integration with support for multiple data sources
Project description
Evolvishub Ollama Adapter
A professional Python adapter for Ollama, providing a clean and type-safe interface for interacting with Ollama models.
Features
- 🚀 Asynchronous API client with retry support
- 📝 Type-safe request/response models using Pydantic
- 💬 Chat and text generation with streaming support
- 🖼️ Image handling and encoding utilities
- 🔢 Embedding generation and similarity search
- 📊 Model management (create, copy, delete)
- 🛠️ Comprehensive utility functions
- 📚 Well-documented code with examples
- ⚙️ Flexible configuration system
Installation
# Basic installation
pip install evolvishub-ollama-adapter
# With YAML configuration support
pip install evolvishub-ollama-adapter[yaml]
Configuration
The adapter supports multiple configuration formats and provides a flexible way to manage settings.
Configuration Formats
- INI Format (Default):
[Ollama]
base_url = http://localhost:11434
timeout = 60
max_retries = 3
default_model = llama2
[Models]
llama2 = 7b
mistral = 7b
[ModelOptions]
temperature = 0.7
top_p = 0.9
- YAML Format (Optional):
ollama:
base_url: http://localhost:11434
timeout: 60
max_retries: 3
default_model: llama2
models:
llama2: 7b
mistral: 7b
model_options:
temperature: 0.7
top_p: 0.9
Configuration Options
Ollama API Settings
base_url: Ollama API base URLtimeout: Request timeout in secondsmax_retries: Maximum number of retry attemptsdefault_model: Default model to use
Model Settings
- Model versions and configurations
- Default model options (temperature, top_p, etc.)
- Context window size
- Thread count
Data Source Settings
- Text chunk size and overlap
- Markdown code extraction
- PDF image extraction
- Image processing options
File Source Settings
- File patterns for different types
- Storage paths for temp, cache, and output
- File encoding and size limits
Logging Settings
- Log level and format
- Log file location and rotation
Using Configuration
- Basic Usage:
from evolvishub_ollama_adapter.config import Config
from evolvishub_ollama_adapter.ollama import OllamaClient
# Use default configuration
client = OllamaClient()
# Use custom configuration
config = Config("path/to/config.ini")
client = OllamaClient(config)
- YAML Configuration:
from evolvishub_ollama_adapter.config import Config
from evolvishub_ollama_adapter.ollama import OllamaClient
# Use YAML configuration
config = Config("path/to/config.yaml", config_type="yaml")
client = OllamaClient(config)
- Environment Variable Override:
export OLLAMA_CONFIG=/path/to/config.ini
- Programmatic Configuration:
from evolvishub_ollama_adapter.config import Config
from evolvishub_ollama_adapter.ollama import OllamaClient
# Create and modify configuration
config = Config()
config.set("Ollama", "base_url", "http://custom:11434")
config.set("ModelOptions", "temperature", 0.8)
# Use modified configuration
client = OllamaClient(config)
Configuration Methods
The Config class provides several methods for accessing configuration values:
config = Config()
# Get values with type conversion
timeout = config.getint("Ollama", "timeout")
temperature = config.getfloat("ModelOptions", "temperature")
extract_code = config.getboolean("DataSources", "markdown_extract_code")
patterns = config.getlist("FileSources", "text_patterns")
# Get model options
options = config.get_model_options()
# Get file patterns
patterns = config.get_file_patterns()
# Get storage paths
paths = config.get_storage_paths()
Quick Start
Basic Text Generation
import asyncio
from evolvishub_ollama_adapter.ollama import OllamaClient, GenerateRequest
async def main():
# Initialize client
client = OllamaClient()
# Create generation request
request = GenerateRequest(
model="llama2",
prompt="What is the capital of France?",
options={"temperature": 0.7}
)
# Generate text
response = await client.generate(request)
print(response.response)
asyncio.run(main())
Streaming Text Generation
import asyncio
from evolvishub_ollama_adapter.ollama import OllamaClient, GenerateRequest
async def main():
client = OllamaClient()
request = GenerateRequest(
model="llama2",
prompt="Write a short story about a robot.",
stream=True
)
async for chunk in client.generate_stream(request):
print(chunk.response, end="", flush=True)
asyncio.run(main())
Chat with Images
import asyncio
from evolvishub_ollama_adapter.ollama import (
OllamaClient, ChatRequest, ChatMessage,
encode_image
)
async def main():
client = OllamaClient()
# Encode image
image_data = encode_image("path/to/image.jpg")
# Create chat request with image
request = ChatRequest(
model="llama2",
messages=[
ChatMessage(
role="user",
content="What's in this image?",
images=[image_data]
)
]
)
# Get chat response
response = await client.chat(request)
print(response.message.content)
asyncio.run(main())
Embeddings and Similarity Search
import asyncio
from evolvishub_ollama_adapter.ollama import (
OllamaClient, EmbeddingRequest,
cosine_similarity
)
async def main():
client = OllamaClient()
# Create embeddings
texts = [
"The quick brown fox jumps over the lazy dog",
"A fast orange fox leaps over a sleepy canine",
"The weather is beautiful today"
]
embeddings = []
for text in texts:
request = EmbeddingRequest(model="llama2", prompt=text)
response = await client.create_embedding(request)
embeddings.append(response.embedding)
# Calculate similarity
similarity = cosine_similarity(embeddings[0], embeddings[1])
print(f"Similarity between first two texts: {similarity:.2f}")
asyncio.run(main())
Model Management
import asyncio
from evolvishub_ollama_adapter.ollama import OllamaClient
async def main():
client = OllamaClient()
# List available models
models = await client.list_models()
print("Available models:", [model.name for model in models.models])
# Get model info
info = await client.get_model_info("llama2")
print(f"Model size: {info.size / 1024 / 1024:.2f} MB")
# Pull new model
await client.pull_model("mistral")
# Copy model
await client.copy_model("mistral", "mistral-copy")
# Delete model
await client.delete_model("mistral-copy")
asyncio.run(main())
Advanced Usage
Custom Configuration
from evolvishub_ollama_adapter.config import Config
from evolvishub_ollama_adapter.ollama import OllamaClient
# Create custom configuration
config = Config()
config.set("Ollama", "base_url", "http://localhost:11434")
config.set("Ollama", "timeout", "60")
config.set("Ollama", "max_retries", "5")
# Initialize client with custom config
client = OllamaClient(config)
Prompt Templates
from evolvishub_ollama_adapter.ollama import format_prompt
# Create template
template = """
You are a helpful AI assistant. Please help with the following task:
Task: {task}
Context: {context}
Your response:
"""
# Format prompt
prompt = format_prompt(
template,
task="Summarize this text",
context="The quick brown fox jumps over the lazy dog."
)
Image Processing
from evolvishub_ollama_adapter.ollama import resize_image, encode_image
# Resize image
image = resize_image("large_image.jpg", max_size=1024)
image.save("resized_image.jpg")
# Encode image for API
image_data = encode_image("resized_image.jpg")
API Reference
Client
OllamaClient: Main client class for interacting with Ollamagenerate(): Generate textgenerate_stream(): Stream generated textchat(): Chat with modelchat_stream(): Stream chat responsescreate_embedding(): Create embeddingslist_models(): List available modelsget_model_info(): Get model informationpull_model(): Pull model from registrypush_model(): Push model to registrydelete_model(): Delete modelcreate_model(): Create model from Modelfilecopy_model(): Copy model
Models
GenerateRequest: Text generation requestGenerateResponse: Text generation responseEmbeddingRequest: Embedding requestEmbeddingResponse: Embedding responseChatRequest: Chat requestChatResponse: Chat responseModelInfo: Model informationModelList: List of models
Utilities
encode_image(): Encode image to base64encode_image_async(): Async image encodingdecode_image(): Decode base64 imageresize_image(): Resize imagecosine_similarity(): Calculate vector similarityformat_prompt(): Format prompt templateparse_model_options(): Parse model optionsformat_chat_history(): Format chat historyparse_model_name(): Parse model name
Contributing
Contributions are welcome! Please read our Contributing Guide for details on our code of conduct and the process for submitting pull requests.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Changelog
See CHANGELOG.md for a list of changes.
Acknowledgments
- Ollama for the amazing AI model serving platform
- aiohttp for async HTTP client/server
- pydantic for data validation
- aiosqlite for async SQLite support
- asyncpg for async PostgreSQL support
- motor for async MongoDB support
- PyYAML for YAML support
About Evolvis AI
Evolvis AI is a leading provider of artificial intelligence solutions, helping businesses unlock the potential of their data through innovative AI technologies. Our mission is to make artificial intelligence accessible to companies of all sizes, enabling them to compete effectively in today's data-driven environment.
Our Approach
- Co-creation: We continuously collaborate with you in developing our solutions
- Open Source Priority: We reduce costs and develop robust tools using open-source technologies
- Transparency: We keep you informed about progress continuously
- Custom Solutions: We analyze and understand your unique business processes to provide tailored solutions
Industries We Serve
- Manufacturing
- Healthcare
- Logistics
- Construction
- Public Services
- Transportation
- Textile
- Technology
- Petrochemical
- Mining
- Metallurgy
- Automotive
- Food Industry
- Government
- Education
- Culture
Visit Evolvis AI to learn more about our solutions and how we can help transform your business.
Author
Alban Maxhuni, PhD
Email: a.maxhuni@evolvis.ai
Dr. Maxhuni is a leading expert in artificial intelligence and machine learning, with extensive experience in developing and implementing AI solutions for various industries. His work focuses on making AI accessible and practical for businesses of all sizes.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file evolvishub_ollama_adapter-0.1.0.tar.gz.
File metadata
- Download URL: evolvishub_ollama_adapter-0.1.0.tar.gz
- Upload date:
- Size: 330.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f6a66701085a4014c0d33ff332e4ebed30a9a286b451bedf4bc82d84fce6c977
|
|
| MD5 |
ea8af80172edf848d318c5b02b17965b
|
|
| BLAKE2b-256 |
51794a858d0e0ed231b18316dee4ad2e7d374887e484e1c7e66cbc7e6b6873de
|
File details
Details for the file evolvishub_ollama_adapter-0.1.0-py3-none-any.whl.
File metadata
- Download URL: evolvishub_ollama_adapter-0.1.0-py3-none-any.whl
- Upload date:
- Size: 314.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dfe6e4d5c9cfe404a94fe96becb9dc8a7872212abc2b7c7e654ba3e971cafe0f
|
|
| MD5 |
1405b4e5ca3bc96ec544c5985f1c7120
|
|
| BLAKE2b-256 |
1a654b141c1c3b498c6827a3a05e98fa485de84da4f128c37c38c842a0eba92e
|