PineRouterDocs
GuidesModelsAPI referenceApps
SDKs and frameworks

LlamaIndex

Use LlamaIndex in Python with this API through the OpenAILike LLM.

LlamaIndex reaches this API through OpenAILike, its LLM class for OpenAI-compatible endpoints. Use it on its own, in query engines, or in agents such as FunctionAgent.

Install

pip install llama-index-core llama-index-llms-openai-like
uv add llama-index-core llama-index-llms-openai-like

Configure API keys

.env
API_KEY=sk-...

Load it into the environment before you run the code (for example with python-dotenv). Keep the key out of your code.

Run

Text

import os
from llama_index.llms.openai_like import OpenAILike

llm = OpenAILike(
    model="gpt-6-luna",
    api_base="https://pinerouter.ai/v1",
    api_key=os.environ["API_KEY"],
    context_window=128000,
    is_chat_model=True,
    is_function_calling_model=True,
)

print(llm.complete("Name three rivers in Europe."))

Stream

for token in llm.stream_complete("Write a haiku about rivers."):
    print(token.delta, end="", flush=True)

stream_chat does the same for a list of chat messages.

Tool calling (agent)

import asyncio
from llama_index.core.agent.workflow import FunctionAgent

def get_weather(city: str) -> str:
    """Current weather for a city."""
    return f"Sunny, 21 C in {city}"

agent = FunctionAgent(tools=[get_weather], llm=llm, system_prompt="You answer weather questions.")

async def main():
    print(await agent.run(user_msg="What is the weather in Paris?"))

asyncio.run(main())

Specifics

  • Set context_window to the model's real context length, from its page under Models. LlamaIndex uses it to size prompts.
  • is_chat_model=True sends calls to /v1/chat/completions. Leave it on.
  • is_function_calling_model=True tells LlamaIndex the model can call tools. Set it to False for a model without tool calling.
  • For images, video and other media call Jobs with the HTTP helper, for example as a tool of the agent.

Next