Name | llama-index-llms-openrouter JSON |
Version |
0.2.1
JSON |
| download |
home_page | None |
Summary | llama-index llms openrouter integration |
upload_time | 2024-10-08 22:33:39 |
maintainer | None |
docs_url | None |
author | Your Name |
requires_python | <4.0,>=3.8.1 |
license | MIT |
keywords |
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# LlamaIndex Llms Integration: Openrouter
## Installation
To install the required packages, run:
```bash
%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:
```python
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:
```python
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:
```python
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:
```python
resp = llm.complete("Tell me a joke")
print(resp)
```
### Streaming Completion
To stream completions, use the `stream_complete` method:
```python
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:
```python
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/
Raw data
{
"_id": null,
"home_page": null,
"name": "llama-index-llms-openrouter",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.8.1",
"maintainer_email": null,
"keywords": null,
"author": "Your Name",
"author_email": "you@example.com",
"download_url": "https://files.pythonhosted.org/packages/6d/ad/7dcc6d39a3d08bc815bdcf62e40456332a004e071ac0ea88ee6d9b2023b4/llama_index_llms_openrouter-0.2.1.tar.gz",
"platform": null,
"description": "# LlamaIndex Llms Integration: Openrouter\n\n## Installation\n\nTo install the required packages, run:\n\n```bash\n%pip install llama-index-llms-openrouter\n!pip install llama-index\n```\n\n## Setup\n\n### Initialize OpenRouter\n\nYou 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:\n\n```python\nfrom llama_index.llms.openrouter import OpenRouter\nfrom llama_index.core.llms import ChatMessage\n\nllm = OpenRouter(\n api_key=\"<your-api-key>\",\n max_tokens=256,\n context_window=4096,\n model=\"gryphe/mythomax-l2-13b\",\n)\n```\n\n## Generate Chat Responses\n\nYou can generate a chat response by sending a list of `ChatMessage` instances:\n\n```python\nmessage = ChatMessage(role=\"user\", content=\"Tell me a joke\")\nresp = llm.chat([message])\nprint(resp)\n```\n\n### Streaming Responses\n\nTo stream responses, use the `stream_chat` method:\n\n```python\nmessage = ChatMessage(role=\"user\", content=\"Tell me a story in 250 words\")\nresp = llm.stream_chat([message])\nfor r in resp:\n print(r.delta, end=\"\")\n```\n\n### Complete with Prompt\n\nYou can also generate completions with a prompt using the `complete` method:\n\n```python\nresp = llm.complete(\"Tell me a joke\")\nprint(resp)\n```\n\n### Streaming Completion\n\nTo stream completions, use the `stream_complete` method:\n\n```python\nresp = llm.stream_complete(\"Tell me a story in 250 words\")\nfor r in resp:\n print(r.delta, end=\"\")\n```\n\n## Model Configuration\n\nTo use a specific model, you can specify it during initialization. For example, to use Mistral's Mixtral model, you can set it like this:\n\n```python\nllm = OpenRouter(model=\"mistralai/mixtral-8x7b-instruct\")\nresp = llm.complete(\"Write a story about a dragon who can code in Rust\")\nprint(resp)\n```\n\n### LLM Implementation example\n\nhttps://docs.llamaindex.ai/en/stable/examples/llm/openrouter/\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "llama-index llms openrouter integration",
"version": "0.2.1",
"project_urls": null,
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "961cbf8a8531dbfe3493601cb20f8570eea6e04c5abb27869ca9fb0368570e67",
"md5": "2e6e73a92f92068d336dafa99141fe92",
"sha256": "da47f3c2bf985680e65cd0b77e759b1db6568654d4fb8dbd6dc16ac7d6a548c9"
},
"downloads": -1,
"filename": "llama_index_llms_openrouter-0.2.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "2e6e73a92f92068d336dafa99141fe92",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.8.1",
"size": 3481,
"upload_time": "2024-10-08T22:33:38",
"upload_time_iso_8601": "2024-10-08T22:33:38.300270Z",
"url": "https://files.pythonhosted.org/packages/96/1c/bf8a8531dbfe3493601cb20f8570eea6e04c5abb27869ca9fb0368570e67/llama_index_llms_openrouter-0.2.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "6dad7dcc6d39a3d08bc815bdcf62e40456332a004e071ac0ea88ee6d9b2023b4",
"md5": "2ad39ce98ec35445c9b5fae316f6f8e3",
"sha256": "a45f03d34ef03e774f2e501c4200d2bd22e80504ef2c210783d953166a6973eb"
},
"downloads": -1,
"filename": "llama_index_llms_openrouter-0.2.1.tar.gz",
"has_sig": false,
"md5_digest": "2ad39ce98ec35445c9b5fae316f6f8e3",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.8.1",
"size": 3034,
"upload_time": "2024-10-08T22:33:39",
"upload_time_iso_8601": "2024-10-08T22:33:39.792969Z",
"url": "https://files.pythonhosted.org/packages/6d/ad/7dcc6d39a3d08bc815bdcf62e40456332a004e071ac0ea88ee6d9b2023b4/llama_index_llms_openrouter-0.2.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-10-08 22:33:39",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "llama-index-llms-openrouter"
}