A Python client for Cloudflare Workers AI
Project description
Cloudflare AI Python Package
This package provides a simple and comprehensive way to interact with Cloudflare Workers AI from Python.
Installation
Install the package using pip:
pip install cloudflare-ai-python
Or from a local directory:
pip install -e .
Usage
Initialize the Client
from cloudflare_ai_python import CloudflareAI
# Replace with your actual values
account_id = "your_account_id"
api_token = "your_api_token"
client = CloudflareAI(account_id, api_token)
List Available Models
models = client.list_models()
for model in models['result']:
print(f"Model: {model['name']}, Description: {model.get('description', 'N/A')}")
Run a Model
response = client.run_model("@cf/meta/llama-3.1-8b-instruct", "Where did the phrase Hello World come from")
print(response['result']['response'])
Backward Compatibility
For backward compatibility, you can still use the old function:
from cloudflare_ai_python import call_cloudflare_ai
response = call_cloudflare_ai(account_id, api_token, "@cf/meta/llama-3.1-8b-instruct", "Hello World")
API Reference
CloudflareAI Class
__init__(account_id, api_token): Initialize the client.list_models(): Returns a list of available models with detailed information.run_model(model, prompt, **kwargs): Run a specific model with a prompt and optional parameters.
Parameters
account_id: Your Cloudflare Account IDapi_token: Your Cloudflare API Tokenmodel: The model identifier (e.g., "@cf/meta/llama-3.1-8b-instruct")prompt: The text prompt to send to the model**kwargs: Additional parameters like max_tokens, temperature, etc.
Getting Your API Credentials
- Go to the Cloudflare Dashboard
- Select "Use REST API"
- Click "Create a Workers AI API Token"
- Review the prefilled information and select "Create API Token"
- Copy the API Token and Account ID
Important: Ensure your API token has the following permissions:
- Workers AI - Read
- Workers AI - Edit
If you get a 401 Unauthorized error, your token may be invalid or expired. Create a new one following the steps above.
Development
Setting up PyPI Publishing
To automatically publish to PyPI when pushing to main branch, you need to configure authentication. Choose one of the following methods:
Method 1: API Token (Recommended)
- Create a PyPI account at https://pypi.org/
- Go to Account Settings → API tokens → Add API token
- Create a token with scope "Entire account (all projects)"
- Copy the token
- In your GitHub repository, go to Settings → Secrets and variables → Actions
- Add a new repository secret named
PYPI_API_TOKENwith the token value
Method 2: Trusted Publishing
- First, manually publish your package to PyPI once using Method 1 above
- Go to your project on PyPI → Publishing
- Click "Add publisher" → "GitHub"
- Configure with these settings:
- Repository name:
him6794/cloudflare-ai - Workflow name:
publish.yml - Environment name: Leave empty
- Repository name:
- The workflow will then use Trusted Publishing automatically
Running Tests Locally
pip install -e .[dev]
pytest
Manual Publishing (for testing)
If you want to test publishing manually before setting up automated publishing:
# Install twine
pip install twine
# Build the package
python -m build
# Upload to PyPI (you'll be prompted for credentials)
twine upload dist/*
Or use the provided script:
python publish_manual.py
Project details
Release history Release notifications | RSS feed
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cloudflare_ai_python-0.1.0.tar.gz.
File metadata
- Download URL: cloudflare_ai_python-0.1.0.tar.gz
- Upload date:
- Size: 5.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2c57bec9bd8281cf5b96ec6a3787072722a3311c1d2b38df2c5c4b301033a22a
|
|
| MD5 |
87b655134e6c02814d8365d5f8fd2bdf
|
|
| BLAKE2b-256 |
a443289fd5a7e7b11703d010d76e060205683cb1ca1da775f82d2e52ce148b01
|
File details
Details for the file cloudflare_ai_python-0.1.0-py3-none-any.whl.
File metadata
- Download URL: cloudflare_ai_python-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
45dd4fea2c42c610a98f67bd887641832d8a499db3dca7fde29cdff9299f0f2f
|
|
| MD5 |
703223be012cb6517822107941b393f4
|
|
| BLAKE2b-256 |
213f9342548056588c4f964e96fa21bf142ea4e85f3cbcce089ccaea9b2da5b8
|