Skip to content

Latest commit

 

History

History
225 lines (175 loc) · 7.27 KB

anthropic.md

File metadata and controls

225 lines (175 loc) · 7.27 KB

Anthropic

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.

{% hint style="info" %} Provider Slug**: anthropic** {% endhint %}

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.

{% tabs %} {% tab title="NodeJS" %}

npm install --save portkey-ai

And if you want to use Portkey with the Anthropic SDK,

npm install @anthropic-ai/sdk

{% endtab %}

{% tab title="Python" %}

pip install portkey-ai

And if you want to use Portkey with the Anthropic SDK,

pip install anthropic

{% endtab %} {% endtabs %}

2. Initialize Portkey with the Virtual Key

Set up Portkey with your virtual key as part of the initialization configuration. You can create a virtual key for Anthropic in the UI.

{% tabs %} {% tab title="NodeJS SDK" %}

import Portkey from 'portkey-ai'
 
const portkey = new Portkey({
    apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"]
    virtualKey: "VIRTUAL_KEY" // Your Anthropic Virtual Key
})

{% endtab %}

{% tab title="Python SDK" %}

from portkey_ai import Portkey

portkey = Portkey(
    api_key="PORTKEY_API_KEY",  # Replace with your Portkey API key
    virtual_key="VIRTUAL_KEY"   # Replace with your virtual key for Anthropic
)

{% endtab %}

{% tab title="OpenAI Python SDK" %}

from openai import OpenAI
from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders

client = Portkey(
    api_key="ANTHROPIC_API_KEY",
    base_url=PORTKEY_GATEWAY_URL,
    default_headers=createHeaders(
        api_key="PORTKEY_API_KEY",
        provider="anthropic"
    )
)

{% endtab %}

{% tab title="OpenAI Node SDK" %}

import OpenAI from "openai";
import { PORTKEY_GATEWAY_URL, createHeaders } from "portkey-ai";

const client = new OpenAI({
  apiKey: "ANTHROPIC_API_KEY",
  baseURL: PORTKEY_GATEWAY_URL,
  defaultHeaders: createHeaders({
    provider: "anthropic",
    apiKey: "PORTKEY_API_KEY",
  }),
});

{% endtab %} {% endtabs %}

3. Invoke Chat Completions with Anthropic

Use the Portkey instance to send requests to Anthropic. You can also override the virtual key directly in the API call if needed.

{% tabs %} {% tab title="NodeJS SDK" %}

const chatCompletion = await 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
});

console.log(chatCompletion.choices);

{% endtab %}

{% tab title="Python SDK" %}

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(completion.choices)

{% endtab %}

{% tab title="OpenAI Python SDK" %}

message = client.chat.completions.create(
    messages = [{ "role": 'user', "content": 'Say this is a test' }],
    model = 'claude-3-opus-20240229',
    max_tokens = 250
)

print(message.content)

{% endtab %}

{% tab title="OpenAI Node SDK" %}

async function main() {
    const msg = await client.chat.completions.create({
        model: "claude-3-opus-20240229",
        max_tokens: 1024,
        messages: [{ role: "user", content: "Hello, Claude" }],
    });
    console.log(msg);
}

main();

{% endtab %} {% endtabs %}

How to Use Anthropic System Prompt

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:

{% tabs %} {% tab title="NodeJS" %}

const chatCompletion = await 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
});

console.log(chatCompletion.choices);

{% endtab %}

{% tab title="Python" %}

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)

{% endtab %} {% endtabs %}

For more, check out the chat completions and completions API reference docs.

Using Anthropic Vision Models

Portkey's multimodal Gateway fully supports Anthropic's vision models claude-3-sonnet, claude-3-haiku, and claude-3-opus

For more info, check out this guide:

{% content-ref url="../../product/ai-gateway-streamline-llm-integrations/multimodal-capabilities/vision.md" %} vision.md {% endcontent-ref %}

Managing Anthropic Prompts

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.

{% content-ref url="../../api-reference/portkey-sdk-client.md" %} portkey-sdk-client.md {% endcontent-ref %}

You'll find more information in the relevant sections:

  1. Add metadata to your requests
  2. Add gateway configs to your Anthropic requests
  3. Tracing Anthropic requests
  4. Setup a fallback from OpenAI to Anthropic's Claude APIs