opentoken.init() instrumenta LlamaIndex: query engines, retrievers, sintetizadores y cada llamada al LLM con tokens.
1. Instala
shell
pip install "getopentoken[llamaindex,bedrock]" llama-index-llms-bedrock-converse2. Entorno
Crea una llave de ingesta en API keys (alcance Ingest) y expórtala como OT_INGEST_KEY.
.env
# OT_INGEST_KEY: an ingest key from API keys (scope: Ingest). Keep it in your secret store.
export OTEL_EXPORTER_OTLP_ENDPOINT="https://getopentoken.com/api/otel"
export OTEL_SERVICE_NAME="support-agent"3. Observa
app.py
import opentoken
opentoken.init()
from llama_index.core.llms import ChatMessage
from llama_index.llms.bedrock_converse import BedrockConverse
llm = BedrockConverse(model="us.amazon.nova-lite-v1:0", region_name="us-east-1")
with opentoken.context(feature="search"):
reply = llm.chat([ChatMessage(role="user", content="Summarize our refund policy in one sentence.")])
print(reply.message.content)Aplica presupuestos por el gateway (opcional)
llm() devuelve un modelo OpenAILike sobre el gateway con fail-open a Bedrock, utilizable en cualquier índice o query engine:
app.py
import opentoken
from llama_index.core.llms import ChatMessage
from opentoken.adapters.llamaindex import llm
opentoken.init(base_url="https://getopentoken.com/v1")
model = llm("amazon/nova-lite") # OpenAILike on the gateway, with fail-open to Bedrock
with opentoken.context(feature="search"):
reply = model.chat([ChatMessage(role="user", content="Say hello in one sentence.")])
print(reply.message.content)