LlamaIndex OpenAI-Like LLM Integration

0.7.1 · active · verified Thu Apr 16

This package provides an integration for LlamaIndex to use OpenAI-compatible Large Language Models (LLMs). It acts as a thin wrapper, allowing LlamaIndex applications to interact with any API that mimics the OpenAI API, making it flexible for various third-party LLM providers. The current version is 0.7.1, with releases typically tied to the broader LlamaIndex ecosystem updates.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to initialize and use the `OpenAILike` LLM to complete a prompt with an OpenAI-compatible API endpoint. Environment variables are used for API configuration, with fallbacks for local testing.

import os
from llama_index.llms.openai_like import OpenAILike

# Replace with your actual model name and API base URL
# If your API doesn't require an API key, set it to a dummy string like 'fake'
# Set is_chat_model and context_window to match your model's capabilities
llm = OpenAILike(
    model="my-openai-compatible-model",
    api_base=os.environ.get("OPENAI_COMPATIBLE_API_BASE", "http://localhost:8000/v1"),
    api_key=os.environ.get("OPENAI_COMPATIBLE_API_KEY", "fake-api-key"),
    is_chat_model=True,
    context_window=4096 # Adjust based on your model's context window
)

response = llm.complete("Tell me a short story about a brave knight.")
print(response.text)

view raw JSON →