Python Bindings for llm_api_access
This package provides a unified interface to query popular LLM providers (LlamaServer, Gemini, Anthropic, and OpenAI) directly from Python via PyO3 bindings.
1. Installation
From PyPI (Recommended for users)
pip install llm_api_access
For Local Development / Building from Source
If you are modifying the Rust core or building from source:
# Ensure maturin is installed in your virtual environment
pip install maturin
# Build and develop the package in-place with the python feature enabled
maturin develop --features python
2. Environment Variables & .env Support
Because the underlying Rust library uses dotenv, environment variables can be loaded from a .env file. When invoking from Python, it is recommended to load your .env file explicitly using python-dotenv at the very entry point of your script to ensure keys like GEMINI_API_KEY, ANTHROPIC_API_KEY, or LLAMA_SERVER_URL are available in the process environment before calling Rust code.
pip install python-dotenv
Example .env configuration:
GEMINI_API_KEY=your_gemini_api_key_here
ANTHROPIC_API_KEY=your_anthropic_api_key_here
LLAMA_SERVER_URL=http://192.168.0.91:8080
BASE64_DATA=your_base64_encoded_image_string_here
3. Usage Guide
Here is how to use the primary providers (LlamaServer and Gemini), along with multi-turn conversations and multimodal message handling in Python:
import os
from dotenv import load_dotenv
import llm_api_access
# Load environment variables from .env file first
load_dotenv()
# Configure optional parameters (temperature, max_tokens, server URLs, etc.)
config = (
llm_api_access.LlmConfig()
.with_temperature(0.7)
.with_max_tokens(150)
)
# -------------------------------------------------------------------------
# 1. LlamaServer (Local / Custom URL - Primary Target)
# -------------------------------------------------------------------------
llama_config = (
llm_api_access.LlmConfig()
.with_server_url(os.getenv("LLAMA_SERVER_URL", "http://192.168.0.91:8080"))
.with_max_tokens(100)
)
try:
llama_client = llm_api_access.LLMClient(llm_api_access.LLMProvider.LlamaServer)
response = llama_client.send_message(
prompt="Explain quantum computing in one sentence.",
model="gemma-4-26b",
config=llama_config
)
print("LlamaServer Response:", response)
except Exception as e:
print("LlamaServer Error:", e)
# -------------------------------------------------------------------------
# 2. Gemini (Secondary Target)
# -------------------------------------------------------------------------
try:
gemini_client = llm_api_access.LLMClient(llm_api_access.LLMProvider.Gemini)
response = gemini_client.send_message(
prompt="Write a haiku about coding.",
model=None,
config=config
)
print("Gemini Response:", response)
except Exception as e:
print("Gemini Error:", e)
# -------------------------------------------------------------------------
# 3. Multi-turn Conversation & Multimodal (Images via Base64)
# -------------------------------------------------------------------------
base64_image = os.getenv("BASE64_DATA")
if base64_image and base64_image != "default_base64_value":
try:
# Construct a multimodal user message with an attached base64 image
image_message = llm_api_access.Message(
role="user",
text="What is in this image? Answer briefly.",
image_base64=base64_image,
image_media_type="image/png"
)
# Follow-up message in conversation history
follow_up = llm_api_access.Message(
role="user",
text="Summarize that in 3 words."
)
conversation = [
image_message,
llm_api_access.Message(role="assistant", text="I see a graphical pattern."),
follow_up
]
client = llm_api_access.LLMClient(llm_api_access.LLMProvider.Gemini)
chat_response = client.send_chat(conversation, model=None, config=config)
print("Multimodal Chat Response:", chat_response)
except Exception as e:
print("Multimodal Error:", e)
else:
print("Skipping multimodal example: BASE64_DATA not found in environment.")
4. Troubleshooting
ModuleNotFoundError: No module named 'llm_api_access': Runmaturin develop --features pythoninside your virtual environment to ensure the dynamic library is linked correctly.- API Key Errors: Ensure
python-dotenvis installed andload_dotenv()is called at the top of your python script so environment variables are populated before Rust execution begins.
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 llm_api_access-0.1.52.tar.gz.
File metadata
- Download URL: llm_api_access-0.1.52.tar.gz
- Upload date:
- Size: 45.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
maturin/1.14.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0d855c040e8355c01758159a33919229d980e969407e096de1f931153a16c5c4
|
|
| MD5 |
aff98e0bb2c48c994251acedadb67b97
|
|
| BLAKE2b-256 |
811906d130cdb336daf919f8906f93d7dc30d2af2ecd500f79440f45050b4aa2
|
File details
Details for the file llm_api_access-0.1.52-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: llm_api_access-0.1.52-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 2.2 MB
- Tags: CPython 3.9, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via:
maturin/1.14.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3bab241dafe95371b80ed35ef63067683fa21d01a6422e4f932fb4335afd191b
|
|
| MD5 |
c7eca97c457d035025eae8829752c2a5
|
|
| BLAKE2b-256 |
91c935c1316440667501d01412ff99b7e278b8df062b4c73b15305d38992294a
|