Portkey provides a robust and secure gateway to facilitate the integration of various Large Language Models (LLMs) into your applications, including Workers AI.
With Portkey, you can take advantage of features like fast AI gateway access, observability, prompt management, and more, all while ensuring the secure management of your LLM API keys through a virtual key system.
Provider Slug. workers-ai
Portkey SDK Integration with Workers AI Models
Portkey provides a consistent API to interact with models from various providers. To integrate Workers AI with Portkey:
1. Install the Portkey SDK
Add the Portkey SDK to your application to interact with Workers AI’s API through Portkey’s gateway.
npm install --save portkey-ai
2. Initialize Portkey with the Virtual Key
To use Workers AI with Portkey, get your API key from here, then add it to Portkey to create the virtual key.
import Portkey from 'portkey-ai'
const portkey = new Portkey({
apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"]
provider:"@PROVIDER" // Your Workers AI Virtual Key
})
from portkey_ai import Portkey
portkey = Portkey(
api_key="PORTKEY_API_KEY", # Replace with your Portkey API key
provider="@PROVIDER" # Replace with your virtual key for Groq
)
3. Invoke Chat Completions with Workers AI
Use the Portkey instance to send requests to Workers AI. You can also override the virtual key directly in the API call if needed.
const chatCompletion = await portkey.chat.completions.create({
messages: [{ role: 'user', content: 'Say "this is a test"' }],
model: '@cf/meta/llama-3.2-3b-instruct',
});
console.log(chatCompletion.choices);
completion = portkey.chat.completions.create(
messages= [{ "role": 'user', "content": 'Say "this is a test"' }],
model= '@cf/meta/llama-3.2-3b-instruct'
)
print(completion)
4. Invoke Image Generation with Workers AI
Use the Portkey instance to send requests to Workers AI. You can also override the virtual key directly in the API call if needed.
Portkey supports the OpenAI signature to make text-to-image requests.
NodeJS
Python
OpenAI NodeJS
OpenAI Python
cURL
import Portkey from 'portkey-ai';
// Initialize the Portkey client
const portkey = new Portkey({
apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key
provider:"@PROVIDER"
});
async function main() {
const image = await portkey.images.generate({
model: "image_model_name",
prompt: "Lucy in the sky with diamonds"
});
console.log(image.data);
}
main();
from portkey_ai import Portkey
from IPython.display import display, Image
# Initialize the Portkey client
portkey = Portkey(
api_key="PORTKEY_API_KEY", # Replace with your Portkey API key
provider="@PROVIDER"
)
image = portkey.images.generate(
model="image_model_name",
prompt="Lucy in the sky with diamonds"
)
# Display the image
display(Image(url=image.data[0].url))
import OpenAI from 'openai'; // We're using the v4 SDK
import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai'
const openai = new OpenAI({
apiKey: 'WORKERS_AI_API_KEY', // defaults to process.env["WORKERS_AI_API_KEY"],
baseURL: PORTKEY_GATEWAY_URL,
defaultHeaders: createHeaders({
provider: "openai",
apiKey: "PORTKEY_API_KEY" // defaults to process.env["PORTKEY_API_KEY"]
})
});
async function main() {
const image = await openai.images.generate({
model: "image_model_name",
prompt: "Lucy in the sky with diamonds"
});
console.log(image.data);
}
main();
from openai import OpenAI
from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders
from IPython.display import display, Image
client = OpenAI(
api_key='WORKERS_AI_API_KEY',
base_url=PORTKEY_GATEWAY_URL,
default_headers=createHeaders(
provider="openai",
api_key="PORTKEY_API_KEY"
)
)
image = client.images.generate(
model="image_model_name",
prompt="Lucy in the sky with diamonds",
n=1,
size="1024x1024"
)
# Display the image
display(Image(url=image.data[0].url))
curl "https://api.portkey.ai/v1/images/generations" \
-H "Content-Type: application/json" \
-H "x-portkey-api-key: $PORTKEY_API_KEY" \
-H "x-portkey-provider: $WORKERS_AI_PROVIDER" \
-d '{
"model": "image_model_name",
"prompt": "Lucy in the sky with diamonds"
}'
Managing Workers AI Prompts
You can manage all prompts to Workers AI in the Prompt Library. All the current models of Workers AI are supported and you can easily start testing different prompts.
Once you’re ready with your prompt, you can use the portkey.prompts.completions.create interface to use the prompt in your application.
The complete list of features supported in the SDK are available on the link below.
You’ll find more information in the relevant sections:
- Add metadata to your requests
- Add gateway configs to your Workers requests
- Tracing Workers AI requests
- Setup a fallback from OpenAI to Workers AI APIs