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
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
File details
Details for the file llama_index_llms_openrouter-0.3.0.tar.gz
.
File metadata
- Download URL: llama_index_llms_openrouter-0.3.0.tar.gz
- Upload date:
- Size: 3.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.11.10 Darwin/22.3.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4dfa5b39f1d3bfef1b494c1c0d9341f7a05024d6b043e80cd5790bad25ceac86 |
|
MD5 | 27ab1edf79e726374c84d7bce8d6aa16 |
|
BLAKE2b-256 | 7c27210827b02edf888e5d2b8e18d20c0e407b5de7bfc370e2b596e0602dcf24 |
File details
Details for the file llama_index_llms_openrouter-0.3.0-py3-none-any.whl
.
File metadata
- Download URL: llama_index_llms_openrouter-0.3.0-py3-none-any.whl
- Upload date:
- Size: 3.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.11.10 Darwin/22.3.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2d8971b338cc86a6e3cab39a844954df9e017bf3706b0db4d1e7da6c02d94a49 |
|
MD5 | fba50a3e3ed746c722c4e61f0309696a |
|
BLAKE2b-256 | fa23c3cd899dc99b34d319764569a3efe33b30e6c7fd006a6242b54eb289a404 |