forked from promptfoo/promptfoo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add Cloudflare AI Provider (promptfoo#817)
- Loading branch information
Showing
14 changed files
with
868 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Cloudflare Workers AI | ||
|
||
See the [full docs for Workers AI](https://developers.cloudflare.com/workers-ai/) and the [Cloudflare REST docs](https://developers.cloudflare.com/api/operations/workers-ai-post-run-model) for further reference on endpoints. | ||
|
||
The basic example in this repo shows the difference in outputs between the "chat" and "completion" versions of the provider. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
prompts: | ||
- Tell me a really funny joke about {{topic}}. The joke should contain the word {{topic}} | ||
|
||
providers: | ||
- id: cloudflare-ai:chat:@cf/meta/llama-3-8b-instruct | ||
config: | ||
accountId: YOUR_ACCOUNT_ID_HERE | ||
# =============== | ||
# It is not recommended to keep your API key on the config file since it is a secret value. | ||
# Use the CLOUDFLARE_API_KEY environment variable or set the apiKeyEnvar value | ||
# in the config | ||
# apiKey: YOUR_API_KEY_HERE | ||
# apiKeyEnvar: SOME_ENV_HAR_CONTAINING_THE_API_KEY | ||
# =============== | ||
# Additional model parameters that are passed through in the HTTP request body to the Cloudflare REST API call | ||
# to run the model | ||
max_tokens: 800 | ||
seed: 1 | ||
|
||
tests: | ||
- vars: | ||
topic: birds | ||
assert: | ||
- type: icontains | ||
value: "{{topic}}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
prompts: | ||
- What is the capital of the {{country}}? | ||
|
||
providers: | ||
- cloudflare-ai:completion:@cf/meta/llama-3-8b-instruct | ||
- cloudflare-ai:chat:@cf/meta/llama-3-8b-instruct | ||
|
||
tests: | ||
- vars: | ||
country: United States | ||
assert: | ||
- type: icontains | ||
value: Washington, D.C. | ||
- vars: | ||
country: England | ||
assert: | ||
- type: icontains | ||
value: London |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
prompts: | ||
- This prompt will be evaluated for embedding similarity | ||
|
||
defaultTest: | ||
options: | ||
provider: | ||
embedding: | ||
id: cloudflare-ai:embedding:@cf/baai/bge-base-en-v1.5 | ||
config: | ||
# accountId: YOUR_ACCOUNT_ID_HERE | ||
|
||
|
||
|
||
providers: | ||
- id: cloudflare-ai:chat:@cf/meta/llama-3-8b-instruct | ||
config: | ||
# accountId: YOUR_ACCOUNT_ID_HERE | ||
|
||
# =============== | ||
# It is not recommended to keep your API key on the config file since it is a secret value. | ||
# Use the CLOUDFLARE_API_KEY environment variable or set the apiKeyEnvar value | ||
# in the config | ||
# apiKey: YOUR_API_KEY_HERE | ||
# apiKeyEnvar: SOME_ENV_HAR_CONTAINING_THE_API_KEY | ||
# =============== | ||
# Additional model parameters that are passed through in the HTTP request body to the Cloudflare REST API call | ||
# to run the model | ||
# max_tokens: 800 | ||
# seed: 1 | ||
|
||
tests: | ||
- assert: | ||
- type: similar | ||
value: embedding similarity | ||
threshold: 0.6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Cloudflare Workers AI | ||
|
||
This provider supports the [models](https://developers.cloudflare.com/workers-ai/models/) provided by Cloudflare Workers AI, a serverless edge embedding and inference runtime. | ||
|
||
## Required Configuration | ||
|
||
Calling the Workers AI requires the user to supply a Cloudflare account ID and API key with sufficient permissions to invoke the Workers AI REST endpoints. | ||
|
||
```bash | ||
export CLOUDFLARE_ACCOUNT_ID=YOUR_ACCOUNT_ID_HERE | ||
export CLOUDFLARE_API_KEY=YOUR_API_KEY_HERE | ||
``` | ||
|
||
The Cloudflare account ID is not secret and therefore it is safe to put it in your `promptfoo` configuration file. The Cloudflare API key is secret, so while you can provide it in the config, this is **HIGHLY NOT RECOMMENDED** as it might lead to abuse. See below for an example safe configuration: | ||
|
||
|
||
```yaml | ||
prompts: | ||
- Tell me a really funny joke about {{topic}}. The joke should contain the word {{topic}} | ||
|
||
providers: | ||
- id: cloudflare-ai:chat:@cf/meta/llama-3-8b-instruct | ||
config: | ||
accountId: YOUR_ACCOUNT_ID_HERE | ||
# It is not recommended to keep your API key on the config file since it is a secret value. | ||
# Use the CLOUDFLARE_API_KEY environment variable or set the apiKeyEnvar value | ||
# in the config | ||
# apiKey: YOUR_API_KEY_HERE | ||
# apiKeyEnvar: SOME_ENV_HAR_CONTAINING_THE_API_KEY | ||
|
||
tests: | ||
- vars: | ||
topic: birds | ||
assert: | ||
- type: icontains | ||
value: "{{topic}}" | ||
``` | ||
In addition to `apiKeyEnvar` allowed environment variable redirection for the `CLOUDFLARE_API_KEY` value, the `accountIdEnvar` can be used to similarly redirect to a value for the `CLOUDFLARE_ACCOUNT_ID`. | ||
|
||
## Available Models and Model Parameters | ||
|
||
Cloudflare is constantly adding new models to its inventory. See their [official list of models](https://developers.cloudflare.com/workers-ai/models/) for a list of supported models. Different models support different parameters, which is supported by supplying those parameters as additional keys of the config object in the `promptfoo` config file. | ||
|
||
For an example of how advanced embedding configuration should be supplied, see `examples/cloudflare-ai/embedding_configuration.yaml` | ||
|
||
For an example of how advanced completion/chat configuration should be supplied, see `examples/cloudflare-ai/chat_advanced_configuration.yaml` | ||
|
||
Different models support different parameters. While this provider strives to be relatively flexible, it is possible that not all possible parameters have been added. If you need support for a parameter that is not yet supported, please open up a PR to add support. | ||
|
||
## Future Improvements | ||
|
||
- [ ] Allow for the pass through of all generic configuration parameters for Cloudflare REST API |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.