Skip to main content

llama-index llms openrouter integration

Project description

LlamaIndex Llms Integration: Openrouter

Installation

To install the required packages, run:

%pip install llama-index-llms-openrouter
!pip install llama-index

Setup

Initialize OpenRouter

You need to set either the environment variable OPENROUTER_API_KEY or pass your API key directly in the class constructor. Replace <your-api-key> with your actual API key:

from llama_index.llms.openrouter import OpenRouter
from llama_index.core.llms import ChatMessage

llm = OpenRouter(
    api_key="<your-api-key>",
    max_tokens=256,
    context_window=4096,
    model="gryphe/mythomax-l2-13b",
)

Generate Chat Responses

You can generate a chat response by sending a list of ChatMessage instances:

message = ChatMessage(role="user", content="Tell me a joke")
resp = llm.chat([message])
print(resp)

Streaming Responses

To stream responses, use the stream_chat method:

message = ChatMessage(role="user", content="Tell me a story in 250 words")
resp = llm.stream_chat([message])
for r in resp:
    print(r.delta, end="")

Complete with Prompt

You can also generate completions with a prompt using the complete method:

resp = llm.complete("Tell me a joke")
print(resp)

Streaming Completion

To stream completions, use the stream_complete method:

resp = llm.stream_complete("Tell me a story in 250 words")
for r in resp:
    print(r.delta, end="")

Model Configuration

To use a specific model, you can specify it during initialization. For example, to use Mistral's Mixtral model, you can set it like this:

llm = OpenRouter(model="mistralai/mixtral-8x7b-instruct")
resp = llm.complete("Write a story about a dragon who can code in Rust")
print(resp)

LLM Implementation example

https://docs.llamaindex.ai/en/stable/examples/llm/openrouter/

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

llama_index_llms_openrouter-0.4.2.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

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

llama_index_llms_openrouter-0.4.2-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file llama_index_llms_openrouter-0.4.2.tar.gz.

File metadata

File hashes

Hashes for llama_index_llms_openrouter-0.4.2.tar.gz
Algorithm Hash digest
SHA256 af09829b2ebc8ccb22b7afe7ad92fe8ce35ea9a3be2430ec811416bad7d7edb9
MD5 70a43ca892ef58cc8526fa775d4436c5
BLAKE2b-256 82921931afb4edf2e4a4ecef9f64bcc33143449bc70f56287a4b495d4a1043ad

See more details on using hashes here.

File details

Details for the file llama_index_llms_openrouter-0.4.2-py3-none-any.whl.

File metadata

File hashes

Hashes for llama_index_llms_openrouter-0.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a7c91ddc90b2c68c47722e81a72641a423045499773d2c38ed74b622be45c18a
MD5 ab3c658f98e5060cb7b907a704468aba
BLAKE2b-256 1f6b80c7259de8020e1f64e0c746e3f8cde3bc48288f842bbba8f8ab9810ae24

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