ChatCloudflare
ChatCloudflare(=None,
account=None,
system_prompt=None,
model=None,
api_key=MISSING,
seed=None,
kwargs )
Chat with a model hosted on Cloudflare Workers AI.
Cloudflare Workers AI hosts a variety of open-source AI models.
Prerequisites
To use the Cloudflare API, you must have an Account ID and an Access Token, which you can obtain by following the instructions at https://developers.cloudflare.com/workers-ai/get-started/rest-api/.
Examples
import os
from chatlas import ChatCloudflare
= ChatCloudflare(
chat =os.getenv("CLOUDFLARE_API_KEY"),
api_key=os.getenv("CLOUDFLARE_ACCOUNT_ID"),
account
)"What is the capital of France?") chat.chat(
Known limitations
- Tool calling does not appear to work.
- Images don’t appear to work.
Parameters
Name | Type | Description | Default |
---|---|---|---|
account | Optional[str] | The Cloudflare account ID. You generally should not supply this directly, but instead set the CLOUDFLARE_ACCOUNT_ID environment variable. |
None |
system_prompt | Optional[str] | A system prompt to set the behavior of the assistant. | None |
model | Optional[str] | The model to use for the chat. The default, None, will pick a reasonable default, and warn you about it. We strongly recommend explicitly choosing a model for all but the most casual use. | None |
api_key | Optional[str] | The API key to use for authentication. You generally should not supply this directly, but instead set the CLOUDFLARE_API_KEY environment variable. |
None |
seed | Optional[int] | MISSING_TYPE | Optional integer seed that ChatGPT uses to try and make output more reproducible. | MISSING |
kwargs | Optional['ChatClientArgs'] | Additional arguments to pass to the openai.OpenAI() client constructor. |
None |
Returns
Name | Type | Description |
---|---|---|
Chat | A chat object that retains the state of the conversation. |
Note
This function is a lightweight wrapper around ChatOpenAI
with the defaults tweaked for Cloudflare.
Note
Pasting credentials into a chat constructor (e.g., ChatCloudflare(api_key="...", account="...")
) is the simplest way to get started, and is fine for interactive use, but is problematic for code that may be shared with others.
Instead, consider using environment variables or a configuration file to manage your credentials. One popular way to manage credentials is to use a .env
file to store your credentials, and then use the python-dotenv
package to load them into your environment.
pip install python-dotenv
# .env
CLOUDFLARE_API_KEY=...
CLOUDFLARE_ACCOUNT_ID=...
from chatlas import ChatCloudflare
from dotenv import load_dotenv
load_dotenv()= ChatCloudflare()
chat chat.console()
Another, more general, solution is to load your environment variables into the shell before starting Python (maybe in a .bashrc
, .zshrc
, etc. file):
export CLOUDFLARE_API_KEY=...
export CLOUDFLARE_ACCOUNT_ID=...