This is an unofficial wrapper of OpenRouter.
Project description
Introduction
Welcome to openrouter-provider, an unofficial Python wrapper for the OpenRouter API. This library lets you easily integrate with OpenRouter models, manage chat sessions, process images, and call tools within your Python application.
Features
- Simple chat interface with system, user, assistant, and tool roles
- Automatic image resizing and Base64 encoding
- Built-in tool decorator for defining custom functions
Installation
From PyPI
pip3 install openrouter-provider
From Source
git clone https://github.com/yourusername/openrouter-provider.git
cd openrouter-provider
pip3 install .
Configuration
-
Create a
.envfile in your project root. -
Add your OpenRouter API key:
OPENROUTER_API_KEY=your_api_key_here
Usage
Basic chat bot
Chat history is automatically sent, by Chatbot_manager. If you want to delete chat history, use clear_memory method.
from OpenRouterProvider.Chatbot_manager import Chat_message, Chatbot_manager
from OpenRouterProvider.LLMs import gpt_4o_mini
# Declare chat bot
ai = Chatbot_manager(system_prompt="Please answer in English.")
# Send query
query = Chat_message(text="Introduce yourself, please.")
response = ai.invoke(model=gpt_4o_mini, query=query)
print(response.text)
# Send next query. Chatbot_manager automatically handle chat history.
query = Chat_message(text="Tell me a short story.")
response = ai.invoke(model=gpt_4o_mini, query=query)
print(response.text)
# Print all chat history
ai.print_memory()
# Delete all chat history
ai.clear_memory()
Chat bot with images
You can use images in the chat.
from OpenRouterProvider.Chatbot_manager import Chat_message, Chatbot_manager
from OpenRouterProvider.LLMs import gpt_4o_mini
from PIL import Image
dog = Image.open("dog.jpg")
cat = Image.open("cat.jpg")
# Send query with images
ai = Chatbot_manager(system_prompt="Please answer in English.")
query = Chat_message(text="What can you see in the images?", images=[dog, cat])
response = ai.invoke(model=gpt_4o_mini, query=query)
print(response.text)
With tools
Use the @tool_model decorator to expose Python functions as callable tools in the chat. Tools are automatically processed by Chat_manager, so you don't need to care it.
from OpenRouterProvider.Chatbot_manager import Chat_message, Chatbot_manager
from OpenRouterProvider.LLMs import gpt_4o_mini
from OpenRouterProvider.Tool import tool_model
@tool_model
def get_user_info():
"""
Return user's personal info: name, age, and address.
"""
return "name: Alice\nage: 30\naddress: Wonderland"
ai = Chatbot_manager(system_prompt="Please answer in English.", tools=[get_user_info])
query = Chat_message(text="What is the name, age, address of the user?")
response = ai.invoke(model=gpt_4o_mini, query=query)
ai.print_memory()
Advanced Usage
Prebuilt and Custom Model Usage
You can use prebuilt models defined or declare your own custom models easily. This library provides many ready-to-use models from OpenAI, Anthropic, Google, and others.
from OpenRouterProvider.Chatbot_manager import Chat_message, Chatbot_manager
from OpenRouterProvider.LLMs import gpt_4o, claude_3_7_sonnet
# Use OpenAI GPT-4o
ai = Chatbot_manager(system_prompt="Please answer in English.")
query = Chat_message(text="Tell me a joke.")
response = ai.invoke(model=gpt_4o, query=query)
print(response.text)
# Use Anthropic Claude 3.7 Sonnet
query = Chat_message(text="Summarize the story of Hamlet.")
response = ai.invoke(model=claude_3_7_sonnet, query=query)
print(response.text)
Available prebuilt models include:
OpenAI
gpt_4ogpt_4o_minigpt_4_1gpt_4_1_minigpt_4_1_nanoo4_minio4_mini_higho3
Anthropic
claude_3_7_sonnetclaude_3_7_sonnet_thinkingclaude_3_5_haiku
gemini_2_0_flashgemini_2_0_flash_freegemini_2_5_flashgemini_2_5_flash_thinkinggemini_2_5_pro
Deepseek
deepseek_v3_freedeepseek_v3deepseek_r1_freedeepseek_r1
xAI
grok_3_minigrok_3
Microsoft
mai_ds_r1_free
Others
llama_4_maverick_freellama_4_scoutmistral_small_3_1_24B_free
All of them are instances of LLMModel, which includes cost and model name settings.
Using Custom Models
You can define and use your own custom model if it's available on OpenRouter.
from OpenRouterProvider.Chatbot_manager import Chat_message, Chatbot_manager
from OpenRouterProvider.LLMs import LLMModel
# Declare a custom model
my_model = LLMModel(
name="my-org/my-custom-model", # Model name for OpenRouter
input_cost=0.5, # Optional: cost per 1M input tokens
output_cost=2.0 # Optional: cost per 1M output tokens
)
# Use the custom model
ai = Chatbot_manager(system_prompt="Please answer in English.")
query = Chat_message(text="Explain black holes simply.")
response = ai.invoke(model=my_model, query=query)
print(response.text)
You only need to know the model name as used on OpenRouter. input_cost and output_cost are optional and currently, they are not used in this library. Please wait the future update.
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 openrouter_provider-0.0.2.tar.gz.
File metadata
- Download URL: openrouter_provider-0.0.2.tar.gz
- Upload date:
- Size: 9.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
13588435c6fd09ce279e03f6d66d231114c1626789a0add9ddd14ae5f77b6e72
|
|
| MD5 |
92920b5c162df24e8383dac64db8b6ef
|
|
| BLAKE2b-256 |
09e76b5f17606de583207bc3963ba4594341fe349489f9e9094636ea21b4db8e
|
File details
Details for the file openrouter_provider-0.0.2-py3-none-any.whl.
File metadata
- Download URL: openrouter_provider-0.0.2-py3-none-any.whl
- Upload date:
- Size: 9.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
03e550b91bc4335a2e2e708d2130ee46c02bbcf233853e231d13866c61f5b144
|
|
| MD5 |
91fc2d675bcb2338e99aa709b8a41ad9
|
|
| BLAKE2b-256 |
7ed7f28396b4e3e0e9f7de2017e83f71296203f63d682eec14225bd4309ce245
|