Minimal, reusable AI service handlers for Gemini and other LLMs
Project description
AI Proxy Core
A minimal Python package providing reusable AI service handlers for Gemini and other LLMs. No web framework dependencies - just the core logic.
Installation
pip install ai-proxy-core
Or install from source:
git clone https://github.com/ebowwa/ai-proxy-core.git
cd ai-proxy-core
pip install -e .
Usage
Completions Handler
from ai_proxy_core import CompletionsHandler
# Initialize handler
handler = CompletionsHandler(api_key="your-gemini-api-key")
# Create completion
response = await handler.create_completion(
messages=[
{"role": "user", "content": "Hello, how are you?"}
],
model="gemini-1.5-flash",
temperature=0.7
)
print(response["choices"][0]["message"]["content"])
Gemini Live Session
from ai_proxy_core import GeminiLiveSession
# Example 1: Basic session (no system prompt)
session = GeminiLiveSession(api_key="your-gemini-api-key")
# Example 2: Session with system prompt (simple string format)
session = GeminiLiveSession(
api_key="your-gemini-api-key",
system_instruction="You are a helpful voice assistant. Be concise and friendly."
)
# Example 3: Session with built-in tools enabled
session = GeminiLiveSession(
api_key="your-gemini-api-key",
enable_code_execution=True, # Enable Python code execution
enable_google_search=True, # Enable web search
system_instruction="You are a helpful assistant with access to code execution and web search."
)
# Example 4: Session with custom function declarations
from google.genai import types
def get_weather(location: str) -> dict:
# Your custom function implementation
return {"location": location, "temp": 72, "condition": "sunny"}
weather_function = types.FunctionDeclaration(
name="get_weather",
description="Get current weather for a location",
parameters=types.Schema(
type="OBJECT",
properties={
"location": types.Schema(type="STRING", description="City name")
},
required=["location"]
)
)
session = GeminiLiveSession(
api_key="your-gemini-api-key",
custom_tools=[types.Tool(function_declarations=[weather_function])],
system_instruction="You can help with weather information."
)
# Set up callbacks
session.on_audio = lambda data: print(f"Received audio: {len(data)} bytes")
session.on_text = lambda text: print(f"Received text: {text}")
session.on_function_call = lambda call: handle_function_call(call)
async def handle_function_call(call):
if call["name"] == "get_weather":
result = get_weather(**call["args"])
await session.send_function_result(result)
# Start session
await session.start()
# Send audio/text
await session.send_audio(audio_data)
await session.send_text("What's the weather in Boston?")
# Stop when done
await session.stop()
Integration with FastAPI
Chat Completions API
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from ai_proxy_core import CompletionsHandler
app = FastAPI()
handler = CompletionsHandler()
class CompletionRequest(BaseModel):
messages: list
model: str = "gemini-1.5-flash"
temperature: float = 0.7
@app.post("/api/chat/completions")
async def create_completion(request: CompletionRequest):
try:
response = await handler.create_completion(
messages=request.messages,
model=request.model,
temperature=request.temperature
)
return response
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
WebSocket for Gemini Live
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
from ai_proxy_core import GeminiLiveSession
app = FastAPI()
@app.websocket("/ws/gemini")
async def gemini_websocket(
websocket: WebSocket,
enable_code_execution: bool = False,
enable_google_search: bool = False
):
await websocket.accept()
# Create session with tools if requested
session = GeminiLiveSession(
enable_code_execution=enable_code_execution,
enable_google_search=enable_google_search
)
# Set up callbacks to forward to WebSocket
session.on_audio = lambda data: websocket.send_json({
"type": "audio", "data": data
})
session.on_text = lambda text: websocket.send_json({
"type": "text", "data": text
})
await session.start()
try:
async for message in websocket.iter_json():
if message["type"] == "audio":
await session.send_audio(message["data"])
elif message["type"] == "text":
await session.send_text(message["data"])
except WebSocketDisconnect:
await session.stop()
Features
- No framework dependencies - Use with FastAPI, Flask, or any Python app
- Async/await support - Modern async Python
- Type hints - Full type annotations
- Minimal surface area - Just the core logic you need
- Easy testing - Mock the handlers in your tests
- Built-in tools - Code execution and Google Search with simple flags
- Custom functions - Add your own function declarations
- Reusable design - Tools configured by consumers, not hardcoded
- WebSocket support - Real-time audio/text streaming with Gemini Live
- Callback system - Handle responses with custom callbacks
Development
Building the Package
When building the package for distribution, use setup.py directly instead of python -m build to avoid pip isolation issues:
python setup.py sdist bdist_wheel
This will create both source distribution and wheel files in the dist/ directory.
Publishing to PyPI
twine upload dist/*
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 ai_proxy_core-0.1.8.tar.gz.
File metadata
- Download URL: ai_proxy_core-0.1.8.tar.gz
- Upload date:
- Size: 8.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
12ec2149a3696d833b4f34abbf93754cea2ab57b954334fd30714d9bbfeed9de
|
|
| MD5 |
c9568254b3347f99c85a1e5f531893eb
|
|
| BLAKE2b-256 |
dbb3c4205df3531239caf327e8e5bfb94f5762235e9de27ea9476d0d53e5a7a7
|
File details
Details for the file ai_proxy_core-0.1.8-py3-none-any.whl.
File metadata
- Download URL: ai_proxy_core-0.1.8-py3-none-any.whl
- Upload date:
- Size: 9.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0ee655afdd7beca839a452cd9aea8e76c79323fde601e5d8b6e1e9895e74382e
|
|
| MD5 |
f1fc4aba731dcfdba5956f7e83402342
|
|
| BLAKE2b-256 |
7b6748e22d0ef497e9c37ba15e3e997616186b3f7e30e0aabe037e1f5b8c0bf8
|