Portkey provides a robust and secure gateway to facilitate the integration of various Large Language Models (LLMs) into your applications, including Anthropic's Claude APIs.
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: anthropic
Portkey SDK Integration with Anthropic
Portkey provides a consistent API to interact with models from various providers. To integrate Anthropic with Portkey:
1. Install the Portkey SDK
Add the Portkey SDK to your application to interact with Anthropic's API through Portkey's gateway.
import Portkey from'portkey-ai'constportkey=newPortkey({ apiKey:"PORTKEY_API_KEY",// defaults to process.env["PORTKEY_API_KEY"] virtualKey:"VIRTUAL_KEY"// Your Anthropic Virtual Key})
from portkey_ai import Portkeyportkey =Portkey( api_key="PORTKEY_API_KEY", # Replace with your Portkey API key virtual_key="VIRTUAL_KEY"# Replace with your virtual key for Anthropic)
Use the Portkey instance to send requests to Anthropic. You can also override the virtual key directly in the API call if needed.
constchatCompletion=awaitportkey.chat.completions.create({ messages: [{ role:'user', content:'Say this is a test' }], model:'claude-3-opus-20240229', max_tokens:250// Required field for Anthropic});console.log(chatCompletion.choices[0].message.content);
chat_completion = portkey.chat.completions.create( messages= [{ "role": 'user', "content": 'Say this is a test' }], model='claude-3-opus-20240229', max_tokens=250# Required field for Anthropic)print(chat_completion.choices[0].message.content)
chat_completion = client.chat.completions.create( messages = [{ "role": 'user', "content": 'Say this is a test' }], model ='claude-3-opus-20240229', max_tokens =250)print(chat_completion.choices[0].message.content)
With Portkey, we make Anthropic models interoperable with the OpenAI schema and SDK methods. So, instead of passing the system prompt separately, you can pass it as part of the messages body, similar to OpenAI:
constchatCompletion=awaitportkey.chat.completions.create({ messages: [ { role:'system', content:'Your system prompt' }, { role:'user', content:'Say this is a test' } ], model:'claude-3-opus-20240229', max_tokens:250});console.log(chatCompletion.choices);
completion = portkey.chat.completions.create( messages= [ { "role": 'system', "content": 'Your system prompt' }, { "role": 'user', "content": 'Say this is a test' } ], model='claude-3-opus-20240229', max_tokens=250# Required field for Anthropic)print(completion.choices)
Vision Chat Completion Usage
Portkey's multimodal Gateway fully supports Anthropic's vision models claude-3-sonnet, claude-3-haiku, claude-3-opus, and the newest claude-3.5-sonnet. Images are made available to Anthropic's model by passing the base64 encoded image directly in the request.
Here's an example using Anthropic claude-3.5-sonnet model
import base64import httpxfrom portkey_ai import Portkey# Fetch and encode the imageimage1_url ="https://upload.wikimedia.org/wikipedia/commons/a/a7/Camponotus_flavomarginatus_ant.jpg"image1_data = base64.b64encode(httpx.get(image1_url).content).decode("utf-8")// Initialize the Portkey clientconst portkey = new Portkey({ apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key virtualKey: "VIRTUAL_KEY"// Add your provider's virtual key});# Create the requestresponse = portkey.chat.completions.create( model="claude-3-5-sonnet-20240620", messages=[ {"role": "system","content": "You are a helpful assistant, who describes imagse" }, {"role": "user","content": [ {"type": "image_url","image_url": {"url": f"data:image/jpeg;base64,{image1_data}" } } ] } ], max_tokens=1400,)print(response)
import Portkey from'portkey-ai';// Initialize the Portkey clientconstportkey=newPortkey({ apiKey:"PORTKEY_API_KEY",// Replace with your Portkey API key virtualKey:"VIRTUAL_KEY"// Add your anthropic's virtual key});// Generate a chat completionasyncfunctiongetChatCompletionFunctions() {constresponse=awaitportkey.chat.completions.create({ model:"claude-3-5-sonnet-20240620", messages: [ { role:"system", content:"You are a helpful assistant who describes images." }, { role:"user", content: [ { type:"text", text:"What's in this image?" }, { type:"image_url", image_url: { url:"data:image/jpeg;base64,BASE64_IMAGE_DATA" } } ] } ], max_tokens:300 });console.log(response); }// Call the functiongetChatCompletionFunctions();
import OpenAI from'openai'; // We're using the v4 SDKimport { PORTKEY_GATEWAY_URL, createHeaders } from'portkey-ai'constopenai=newOpenAI({ apiKey:'ANTHROPIC_API_KEY',// defaults to process.env["OPENAI_API_KEY"], baseURL:PORTKEY_GATEWAY_URL, defaultHeaders:createHeaders({ provider:"openai", apiKey:"PORTKEY_API_KEY"// defaults to process.env["PORTKEY_API_KEY"] })});// Generate a chat completion with streamingasyncfunctiongetChatCompletionFunctions(){constresponse=awaitopenai.chat.completions.create({ model:"gpt-4-vision-preview", messages: [ { role:"user", content: [ { type:"text", text:"What’s in this image?" }, { type:"image_url", image_url: "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg",
}, ], }, ], });console.log(response)}awaitgetChatCompletionFunctions();
On completion, the request will get logged in the logs UI where any image inputs or outputs can be viewed. Portkey will automatically load the base64 images making for a great debugging experience with vision models.
Portkey also works with Anthropic's new prompt caching feature and helps you save time & money for all your Anthropic requests. Refer to this guide to learn how to enable it:
You can manage all prompts to Anthropic in the Prompt Library. All the current models of Anthropic 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.
Next Steps
The complete list of features supported in the SDK are available on the link below.