Heroku LLM Managed Inference
The llama-index-llms-heroku package contains LlamaIndex integrations for building applications with models on Herokuās Managed Inference platform. This integration allows you to easily connect to and use AI models deployed on Herokuās infrastructure.
Installation
Section titled āInstallationā%pip install llama-index-llms-heroku1. Create a Heroku App
Section titled ā1. Create a Heroku AppāFirst, create an app in Heroku:
heroku create $APP_NAME2. Create and Attach AI Models
Section titled ā2. Create and Attach AI ModelsāCreate and attach a chat model to your app:
heroku ai:models:create -a $APP_NAME claude-3-5-haiku3. Export Configuration Variables
Section titled ā3. Export Configuration VariablesāExport the required configuration variables:
export INFERENCE_KEY=$(heroku config:get INFERENCE_KEY -a $APP_NAME)export INFERENCE_MODEL_ID=$(heroku config:get INFERENCE_MODEL_ID -a $APP_NAME)export INFERENCE_URL=$(heroku config:get INFERENCE_URL -a $APP_NAME)Basic Usage
Section titled āBasic Usageāfrom llama_index.llms.heroku import Herokufrom llama_index.core.llms import ChatMessage, MessageRole
# Initialize the Heroku LLMllm = Heroku()
# Create chat messagesmessages = [ ChatMessage( role=MessageRole.SYSTEM, content="You are a helpful assistant." ), ChatMessage( role=MessageRole.USER, content="What are the most popular house pets in North America?", ),]
# Get responseresponse = llm.chat(messages)print(response)Using Environment Variables
Section titled āUsing Environment VariablesāThe integration automatically reads from environment variables:
import os
# Set environment variablesos.environ["INFERENCE_KEY"] = "your-inference-key"os.environ["INFERENCE_URL"] = "https://us.inference.heroku.com"os.environ["INFERENCE_MODEL_ID"] = "claude-3-5-haiku"
# Initialize without parametersllm = Heroku()Using Parameters
Section titled āUsing ParametersāYou can also pass parameters directly:
import os
llm = Heroku( model=os.getenv("INFERENCE_MODEL_ID", "claude-3-5-haiku"), api_key=os.getenv("INFERENCE_KEY", "your-inference-key"), inference_url=os.getenv( "INFERENCE_URL", "https://us.inference.heroku.com" ), max_tokens=1024,)Available Models
Section titled āAvailable ModelsāFor a complete list of available models, see the Heroku Managed Inference documentation.
Error Handling
Section titled āError HandlingāThe integration includes proper error handling for common issues:
- Missing API key
- Invalid inference URL
- Missing model configuration
Additional Information
Section titled āAdditional InformationāFor more information about Heroku Managed Inference, visit the official documentation.