Skip to main content

Easy access to 100s of LLMs with a few lines of code (using Openrouter).

Project description

irouter

PyPI version PyPI downloads Python Version uv Ruff

Access 100s of LLMs with 2 lines of code.

Installation

  1. Install irouter from PyPI:
pip install irouter
  1. Create an account on OpenRouter and generate an API key.

3a. (recommended!) Set the OpenRouter API key as an environment variable:

export OPENROUTER_API_KEY=your_api_key

In this way you can use irouter objects like Call and Chat without have to pass an API key.

from irouter import Call
c = Call(model="moonshotai/kimi-k2:free")

3b. Alternatively, pass api_key to irouter objects like Call and Chat.

from irouter import Call
c = Call(model="moonshotai/kimi-k2:free", api_key="your_api_key")

Usage

Call

Call is the simplest interface to call one or more LLMs.

Single LLM

from irouter import Call
c = Call(model="moonshotai/kimi-k2:free")
c("Who are you?")
# "I'm Kimi, your AI friend from Moonshot AI. I'm here to chat, answer your questions, and help you out whenever you need it."

Multiple LLMs

from irouter import Call
c = Call(model=["moonshotai/kimi-k2:free", "google/gemini-2.0-flash-exp:free"])
c("Who are you?")
# {'moonshotai/kimi-k2:free': "I'm Kimi, your AI friend from Moonshot AI. I'm here to chat, answer your questions, and help you out whenever you need it.",
#  'google/gemini-2.0-flash-exp:free': 'I am a large language model, trained by Google.\n'}

Chat

Chat is an easy way to interface with one or more LLMs, while tracking message history and token usage.

Single LLM

from irouter import Chat
c = Chat(model="moonshotai/kimi-k2:free")
c("Who are you?")
print(c.history) # {'moonshotai/kimi-k2:free': [...]}
print(c.usage) # {'moonshotai/kimi-k2:free': {'prompt_tokens': 8, 'completion_tokens': 8, 'total_tokens': 16}}

Multiple LLMs

from irouter import Chat
c = Chat(model=["moonshotai/kimi-k2:free", "google/gemini-2.0-flash-exp:free"])
c("Who are you?")
print(c.history) 
# {'moonshotai/kimi-k2:free': [...], 
# 'google/gemini-2.0-flash-exp:free': [...]}
print(c.usage) 
# {'moonshotai/kimi-k2:free': {'prompt_tokens': 8, 'completion_tokens': 8, 'total_tokens': 16}, 
# 'google/gemini-2.0-flash-exp:free': {'prompt_tokens': 8, 'completion_tokens': 10, 'total_tokens': 18}}

Image Support

Both Call and Chat support images from image URLs or local images.

Adding images is as simple as providing a list of strings with:

  • text and/or
  • image URL(s) and/or
  • image path(s)

Make sure to select an LLM that supports image input, like gpt-4o-mini.

Example image
from irouter import Chat
ic = Chat("gpt-4o-mini")
# Image URL
ic(["https://www.petlandflorida.com/wp-content/uploads/2022/04/shutterstock_1290320698-1-scaled.jpg", 
    "What is in the image?"])
# or local image
# ic(["../assets/puppy.jpg", "What is in the image?"])
# Example output:
# The image shows a cute puppy, ..., The background is blurred, 
# with green hues suggesting an outdoors setting.

# Images are tracked in history
print(ic.history)
# [{'role': 'system', 'content': 'You are a helpful assistant.'}, 
#  {'role': 'user', 'content': [{'type': 'image_url', 'image_url':
#  {'url': '...'}}, {'type': 'text', 'text': 'What is in the image?'}]}, 
#  {'role': 'assistant', 'content': 'The image shows a cute puppy...'}]

For more information on Chat, check out the chat.ipynb notebook in the nbs folder.

Misc

get_all_models

You can easily get all 300+ models available with irouter using get_all_models.

from irouter.base import get_all_models
get_all_models()
# ['llm_provider1/model1', ... 'llm_providerx/modelx']

history_to_markdown

Convert chat history to markdown for easy display in Jupyter notebooks.

from irouter.base import history_to_markdown
history_to_markdown(c.history, ipython=True)

Credits

This project is built on top of the OpenRouter API infrastructure, which provides access to LLMs through a unified interface.

This project is inspired by Answer.AI's projects like cosette and claudette.

irouter generalizes this idea to support 100s of LLMs, which includes OpenAI and Anthropic models and more, thanks to OpenRouter's infrastructure.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

irouter-0.1.1.tar.gz (12.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

irouter-0.1.1-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

Details for the file irouter-0.1.1.tar.gz.

File metadata

  • Download URL: irouter-0.1.1.tar.gz
  • Upload date:
  • Size: 12.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.11

File hashes

Hashes for irouter-0.1.1.tar.gz
Algorithm Hash digest
SHA256 2c38ab9c892a008444c2690b6c41b347d83b0fb5c2324b285c7bfcfd32a4b499
MD5 564989f8086fb71cb07b58b97fd244e8
BLAKE2b-256 2fbac29eb769a0f4828a9f9a4a5d868349c645521229689f564711b79d1e1392

See more details on using hashes here.

File details

Details for the file irouter-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: irouter-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 9.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.11

File hashes

Hashes for irouter-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 27a70039e8ececaddbf17c5e8497b9c6c398f3a4afe98c44862146088772081f
MD5 8920f716b2a1fbb779bd4741b414d528
BLAKE2b-256 5dca835d06622195db4d85781af1464cb8f54aefbd3991765de65140e879f603

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page