Skip to main content

LlamaIndex LLM Integration: OpenLLM

Installation

To install the required packages, run:

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

Setup

Initialize OpenLLM

First, import the necessary libraries and set up your OpenLLM instance. Replace my-model, https://hostname.com/v1, and na with your model name, API base URL, and API key, respectively:

import os
from typing import List, Optional
from llama_index.llms.openllm import OpenLLM
from llama_index.core.llms import ChatMessage

llm = OpenLLM(
    model="my-model", api_base="https://hostname.com/v1", api_key="na"
)

Generate Completions

To generate a completion, use the complete method:

completion_response = llm.complete("To infinity, and")
print(completion_response)

Stream Completions

You can also stream completions using the stream_complete method:

async for it in llm.stream_complete(
    "The meaning of time is", max_new_tokens=128
):
    print(it, end="", flush=True)

Chat Functionality

OpenLLM supports chat APIs, allowing you to handle conversation-like interactions. Here’s how to use it:

Synchronous Chat

You can perform a synchronous chat by constructing a list of ChatMessage instances:

from llama_index.core.llms import ChatMessage

chat_messages = [
    ChatMessage(role="system", content="You are acting as Ernest Hemmingway."),
    ChatMessage(role="user", content="Hi there!"),
    ChatMessage(role="assistant", content="Yes?"),
    ChatMessage(role="user", content="What is the meaning of life?"),
]

for it in llm.chat(chat_messages):
    print(it.message.content, flush=True, end="")

Asynchronous Chat

To perform an asynchronous chat, use the astream_chat method:

async for it in llm.astream_chat(chat_messages):
    print(it.message.content, flush=True, end="")

LLM Implementation example

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

Metadata

Release files for llama-index-llms-openllm 0.4.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for llama-index-llms-openllm 0.4.2
File Size Uploaded Provenance Actions
llama_index_llms_openllm-0.4.2.tar.gz 4.0 kB Without provenance Details for llama_index_llms_openllm-0.4.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for llama-index-llms-openllm 0.4.2
File Interpreter ABI Platform Actions
llama_index_llms_openllm-0.4.2-py3-none-any.whl Python 3 none any Details for llama_index_llms_openllm-0.4.2-py3-none-any.whl

Total release size: 7.5 kB

Release files / llama_index_llms_openllm-0.4.2.tar.gz

Download URL llama_index_llms_openllm-0.4.2.tar.gz
Size 4.0 kB
Tags Source
SHA-256 checksum
How to use checksums
9197d21a8135e244b02cee99298fbcda96d52a7e89496f21a48e0bc32145bf75
BLAKE2b-256 checksum
How to use checksums
db63e3c8b13d45487fde5f154786507e1cf1e040ed2b15b13f8e6afa9cc1606e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.17

Release files / llama_index_llms_openllm-0.4.2-py3-none-any.whl

Download URL llama_index_llms_openllm-0.4.2-py3-none-any.whl
Size 3.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9406f6c77bd2460ba5cd4d54c08b226b3486a52406da65d5eb8bde61495638a2
BLAKE2b-256 checksum
How to use checksums
bb607145d7ce0e0556b0de6d5ecc4722af0110751b606fb530c3229a4bff9d9b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.17

Security for llama-index-llms-openllm 0.4.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page