Monkey patches LLM client libraries to print all prompts and responses
Project description
Osmosis
A Python library that monkey patches LLM client libraries to send all prompts and responses to the Osmosis API for logging and monitoring.
Supported Libraries
- Anthropic: Logs all Claude API requests and responses (both sync and async clients)
- OpenAI: Logs all OpenAI API requests and responses (supports v1 and v2 API versions, both sync and async clients)
- LangChain: Currently supports prompt template logging (LLM and ChatModel support varies by LangChain version)
Installation
# Basic installation with minimal dependencies
pip install osmosis-ai
# Install with specific provider support
pip install "osmosis-ai[openai]" # Only OpenAI support
pip install "osmosis-ai[anthropic]" # Only Anthropic support
# Install with LangChain support
pip install "osmosis-ai[langchain]" # Base LangChain support
pip install "osmosis-ai[langchain-openai]" # LangChain + OpenAI support
pip install "osmosis-ai[langchain-anthropic]" # LangChain + Anthropic support
# Install with all dependencies
pip install "osmosis-ai[all]"
Or install from source:
git clone https://github.com/your-username/osmosis-sdk-python.git
cd osmosis-ai
pip install -e .
For development, you can install all dependencies using:
pip install -r requirements.txt
Environment Setup
osmosisrequires a OSMOSIS API key to log LLM usage. Create a .env file in your project directory:
# Copy the sample .env file
cp .env.sample .env
# Edit the .env file with your API keys
Edit the .env file to add your API keys:
# Required for logging
OSMOSIS_API_KEY=your_osmosis_api_key_here
# Optional: Only needed if you're using these services
ANTHROPIC_API_KEY=your_anthropic_key_here
OPENAI_API_KEY=your_openai_key_here
Usage
First, import and initialize osmosiswith your OSMOSIS API key:
import os
import osmosis_ai
# Initialize with your OSMOSIS API key
osmosis_ai.init("your-osmosis-api-key")
# Or load from environment variable
osmosis_api_key = os.environ.get("OSMOSIS_API_KEY")
osmosis_ai.init(osmosis_api_key)
Once you import osmosis_ai and initialize it, the library automatically patches the supported LLM clients. You can then use your LLM clients normally, and all API calls will be logged to OSMOSIS:
Anthropic Example
# Import osmosis_ai first and initialize it
import osmosis_ai
osmosis_ai.init(os.environ.get("OSMOSIS_API_KEY"))
# Then import and use Anthropic as normal
from anthropic import Anthropic
# Create and use the Anthropic client as usual - it's already patched
client = Anthropic(api_key=os.environ.get("ANTHROPIC_API_KEY"))
# All API calls will now be logged to OSMOSIS automatically
response = client.messages.create(
model="claude-3-haiku-20240307",
max_tokens=1000,
messages=[
{"role": "user", "content": "Hello, Claude!"}
]
)
# Async client is also supported and automatically patched
from anthropic import AsyncAnthropic
import asyncio
async def call_claude_async():
async_client = AsyncAnthropic(api_key=os.environ.get("ANTHROPIC_API_KEY"))
response = await async_client.messages.create(
model="claude-3-haiku-20240307",
max_tokens=1000,
messages=[
{"role": "user", "content": "Hello, async Claude!"}
]
)
return response
# All async API calls will be logged to OSMOSIS as well
asyncio.run(call_claude_async())
OpenAI Example
# Import osmosis_ai first and initialize it
import osmosis_ai
osmosis_ai.init(os.environ.get("OSMOSIS_API_KEY"))
# Then import and use OpenAI as normal
from openai import OpenAI
# Create and use the OpenAI client as usual - it's already patched
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
# All API calls will now be logged to OSMOSIS automatically
response = client.chat.completions.create(
model="gpt-4o-mini",
max_tokens=150,
messages=[
{"role": "user", "content": "Hello, GPT!"}
]
)
# Async client is also supported and automatically patched
from openai import AsyncOpenAI
import asyncio
async def call_openai_async():
async_client = AsyncOpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
response = await async_client.chat.completions.create(
model="gpt-4o-mini",
max_tokens=150,
messages=[
{"role": "user", "content": "Hello, async GPT!"}
]
)
return response
# All async API calls will be logged to OSMOSIS as well
asyncio.run(call_openai_async())
LangChain Example
# Import osmosis_ai first and initialize it
import osmosis_ai
osmosis_ai.init(os.environ.get("OSMOSIS_API_KEY"))
# Then use LangChain as normal
from langchain_core.prompts import PromptTemplate
# Use LangChain prompt templates as usual
template = PromptTemplate(
input_variables=["topic"],
template="Write a short paragraph about {topic}."
)
# Formatting the prompt will be logged to OSMOSIS automatically
formatted_prompt = template.format(topic="artificial intelligence")
print(f"Formatted prompt: {formatted_prompt}")
# Multiple prompt templates are also captured
template2 = PromptTemplate(
input_variables=["name", "profession"],
template="My name is {name} and I work as a {profession}."
)
formatted_prompt2 = template2.format(name="Alice", profession="data scientist")
print(f"Formatted prompt 2: {formatted_prompt2}")
Configuration
You can configure the behavior of the library by modifying the following variables:
import osmosis_ai
# Disable logging to OSMOSIS (default: True)
osmosis_ai.enabled = False
How it Works
This library uses monkey patching to override the LLM clients' methods that make API calls. When you import the osmosis_ai module, it automatically patches the supported LLM client libraries. When methods are called on these clients, the library intercepts the calls and sends the request parameters and response data to the OSMOSIS API for logging and monitoring.
The data sent to OSMOSIS includes:
- Timestamp (UTC)
- Request parameters
- Response data
- HTTP status code
License
MIT
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 osmosis_ai-0.1.8.tar.gz.
File metadata
- Download URL: osmosis_ai-0.1.8.tar.gz
- Upload date:
- Size: 19.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a5ca1760b59d65bd72f073841356c731f0e4a918858b01fa892d57ce0a10b000
|
|
| MD5 |
fa599df35a1a2ea33a2ee22345885fe7
|
|
| BLAKE2b-256 |
785744bad873ec209e1d437f48fb8059c358419ba249cec2ad9a98d5253b52e4
|
File details
Details for the file osmosis_ai-0.1.8-py3-none-any.whl.
File metadata
- Download URL: osmosis_ai-0.1.8-py3-none-any.whl
- Upload date:
- Size: 24.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4a190015c7759c0b79fce05f01e19871ae92d8b5e0c8455f61ac021b9f1dd3c5
|
|
| MD5 |
a3263d4537774f2aac8ea71c7d5276f4
|
|
| BLAKE2b-256 |
4d5a142b46fa336484132fe0e5c0fb2b19ae00ba292a511582242423e984bd23
|