Skip to main content

llama-index llms cloudflare ai gateway integration

Project description

LlamaIndex LLM Integration for Cloudflare AI Gateway

This package provides integration between LlamaIndex and Cloudflare AI Gateway, allowing you to use multiple AI models from different providers with automatic fallback.

Features

  • Multiple Providers: Use OpenAI, Anthropic, Mistral, Groq, and more
  • Automatic Fallback: If one provider fails, automatically tries the next
  • Streaming Support: Both chat and completion streaming
  • Async Support: Full async/await support
  • Caching: Built-in request caching with Cloudflare AI Gateway

Installation

pip install llama-index-llms-cloudflare-ai-gateway

Quick Start

from llama_index.llms.cloudflare_ai_gateway import CloudflareAIGateway
from llama_index.llms.openai import OpenAI
from llama_index.llms.anthropic import Anthropic
from llama_index.core.llms import ChatMessage

# Create LLM instances
openai_llm = OpenAI(model="gpt-4o-mini", api_key="your-openai-key")
anthropic_llm = Anthropic(
    model="claude-3-5-sonnet-20241022", api_key="your-anthropic-key"
)

# Create Cloudflare AI Gateway LLM
llm = CloudflareAIGateway(
    llms=[openai_llm, anthropic_llm],  # Try OpenAI first, then Anthropic
    account_id="your-cloudflare-account-id",
    gateway="your-ai-gateway-name",
    api_key="your-cloudflare-api-key",
)

# Use the LLM
messages = [ChatMessage(role="user", content="What is 2+2?")]
response = llm.chat(messages)
print(response.message.content)

Supported Providers

  • OpenAI
  • Anthropic
  • Mistral AI
  • Groq
  • DeepSeek
  • Perplexity
  • Replicate
  • Grok
  • Azure OpenAI

Testing

# Set environment variables
export OPENAI_API_KEY="your-key"
export ANTHROPIC_API_KEY="your-key"
export CLOUDFLARE_ACCOUNT_ID="your-id"
export CLOUDFLARE_API_KEY="your-key"
export CLOUDFLARE_GATEWAY="your-gateway"

# Run tests
uv run pytest tests/

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file llama_index_llms_cloudflare_ai_gateway-0.2.1.tar.gz.

File metadata

File hashes

Hashes for llama_index_llms_cloudflare_ai_gateway-0.2.1.tar.gz
Algorithm Hash digest
SHA256 05c575eb3f8b87f95e4e652d7baa8701c2816e1213e36b171a663feabbf2b989
MD5 61891c8acb5e32c2883b715fb801af0b
BLAKE2b-256 9f7e108b3019155aaf0835c36a57b0478349ba2b1930eb8dd0b4f094cfc80fec

See more details on using hashes here.

File details

Details for the file llama_index_llms_cloudflare_ai_gateway-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for llama_index_llms_cloudflare_ai_gateway-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 37b7131f2914963576afa50227c08f7ad4ea8c2c2affc2d7bd0b764b88144bbe
MD5 dd92d493bd57adf9970aa22073539110
BLAKE2b-256 23d5ea8c84eaf6d611538fa58c4fc4956757caba94407a8d417dbbe84f620a29

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page