LLM Interface
A flexible Python interface for working with various Language Model providers, including OpenAI, Anthropic, and Ollama. This library provides a unified way to interact with different LLM providers while supporting features like structured outputs, tool execution, and response caching.
Features
-
Multiple Provider Support
- OpenAI (GPT models)
- Anthropic (Claude models)
- Ollama (local and remote)
- Remote Ollama via SSH
-
Advanced Capabilities
- Structured output parsing with Pydantic models
- Function/tool calling support
- Response caching
- Comprehensive logging
- JSON mode support
- System prompt handling
-
Developer-Friendly
- Type hints throughout
- Extensive test coverage
- Flexible configuration options
- Error handling and retries
Installation
Install using pip:
pip install llm-interface
Or using Poetry:
poetry add llm-interface
Basic Usage
Simple Chat Completion
from llm_interface import llm_from_config
# Create an OpenAI interface
llm = llm_from_config(
provider="openai",
model_name="gpt-4",
)
# Simple chat
response = llm.chat([
{"role": "user", "content": "What is the capital of France?"}
])
Structured Output with Pydantic
from pydantic import BaseModel
class LocationInfo(BaseModel):
city: str
country: str
population: int
response = llm.generate_pydantic(
prompt_template="Provide information about Paris",
output_schema=LocationInfo,
system="You are a helpful geography assistant"
)
Tool/Function Calling
from llm_interface.llm_tool import tool
@tool(name="get_weather")
def get_weather(location: str, units: str = "celsius") -> str:
"""Get weather information for a location.
Args:
location: City or location name
units: Temperature units (celsius/fahrenheit)
"""
# Implementation here
return f"Weather in {location}"
response = llm.chat(
messages=[{"role": "user", "content": "What's the weather in Paris?"}],
tools=[get_weather]
)
Remote Ollama Setup
llm = llm_from_config(
provider="remote_ollama",
model_name="llama2",
hostname="example.com",
username="user"
)
Configuration
The library supports various configuration options through the llm_from_config function:
llm = llm_from_config(
provider="openai", # "openai", "anthropic", "ollama", or "remote_ollama"
model_name="gpt-4", # Model name
max_tokens=4096, # Maximum tokens in response
host=None, # Local Ollama host
hostname=None, # Remote SSH hostname
username=None, # Remote SSH username
log_dir="logs", # Directory for logs
use_cache=True # Enable response caching
)
Environment Variables
Required environment variables based on provider:
- OpenAI:
OPENAI_API_KEY - Anthropic:
ANTHROPIC_API_KEY - Remote Ollama: requires an SSH key to be loaded in SSH agent
Development
This project uses Poetry for dependency management:
# Install dependencies
poetry install
# Run tests
poetry run pytest
# Format code
poetry run black .
# Run linter
poetry run flake8
License
Apache License 2.0 - See LICENSE file for details.
Metadata
Release files for llm-interface 0.2.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| llm_interface-0.2.2.tar.gz | 45.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| llm_interface-0.2.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 103.2 kB
Release files / llm_interface-0.2.2.tar.gz
| Download URL | llm_interface-0.2.2.tar.gz |
|---|---|
| Size | 45.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6fbaba8d9fd2c67cf8ef4a02687e628b6a015b4e856fa987b252b3dd0326b94a
|
|
BLAKE2b-256 checksum How to use checksums |
b203958eac09280b55dfa24e4d269bd14eda065c4d18cde09d4671d0fd1173fa
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.4.3 CPython/3.10.21 Linux/6.17.0-1022-azure
|
Release files / llm_interface-0.2.2-py3-none-any.whl
| Download URL | llm_interface-0.2.2-py3-none-any.whl |
|---|---|
| Size | 57.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
59ca1afc60b055e7bc30247f140f6063d274a4878d1a1bb761068cecb019a373
|
|
BLAKE2b-256 checksum How to use checksums |
7e4299f6f582ebc18e5318a3f7e254e2ce43cc70e2a90c2b0134c4794fc38a37
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.4.3 CPython/3.10.21 Linux/6.17.0-1022-azure
|