Skip to main content

Integrating with Anthropic

This tutorial shows how to use the Cape API to make an Anthropic completion request. Cape will automatically redact sensitive entities like PII, PCI, and PHI so they are not shared with the model. And then Cape will re-identify the redacted entities before returning the response to the caller.

Get a Cape API Key

This tutorial assumes you have an environment variable CAPE_API_KEY that contains an API Key. You will need to signup for a Cape account, and then you can get an API key here.

Using the Cape API to call Anthropic

Here's how to use Python to make a streaming request to the Cape API when targeting Anthropic models. Please see the API docs for more details.

import os
import requests


url = "https://api.capeprivacy.com/v1/anthropic/complete"

payload = {
"model": "claude-2",
"prompt": f"\n\nHuman: Bob is a software engineer who works at Cape Privacy! Where does Bob work?\n\nAssistant:",
"max_tokens_to_sample": 256,
"format": "redacted",
"return_entities": False,
"stream": True,
"verbose": False,
"extra_anthropic": {
"temperature": 1
}
}

headers = {
"content-type": "application/json",
"Authorization": f"Bearer {os.getenv('CAPE_API_KEY')}"
}

response = requests.post(url, json=payload, headers=headers)
for line in response.iter_lines():
print(str(line, 'utf-8'))
Output
data: {"completion": " Based", "stop_reason": null, "model": null}

data: {"completion": " on", "stop_reason": null, "model": null}

data: {"completion": " the", "stop_reason": null, "model": null}

data: {"completion": " information", "stop_reason": null, "model": null}

data: {"completion": " provided", "stop_reason": null, "model": null}

data: {"completion": ",", "stop_reason": null, "model": null}

data: {"completion": " Bob", "stop_reason": null, "model": null}

data: {"completion": " works", "stop_reason": null, "model": null}

data: {"completion": " at", "stop_reason": null, "model": null}

data: {"completion": " Cape Privacy.", "stop_reason": null, "model": null}

data: {"completion": "", "stop_reason": "stop_sequence", "model": null}

data: [DONE]

To include the actual redactions in your response, you can pass "verbose": True.

Output
data: {"completion": " Based", "stop_reason": null, "model": null}

data: {"completion": " on", "stop_reason": null, "model": null}

data: {"completion": " the", "stop_reason": null, "model": null}

data: {"completion": " information", "stop_reason": null, "model": null}

data: {"completion": " provided", "stop_reason": null, "model": null}

data: {"completion": ",", "stop_reason": null, "model": null}

data: {"completion": " [NAME_GIVEN_1|Bob]", "stop_reason": null, "model": null}

data: {"completion": " works", "stop_reason": null, "model": null}

data: {"completion": " at", "stop_reason": null, "model": null}

data: {"completion": " [ORGANIZATION_1|Cape Privacy].", "stop_reason": null, "model": null}

data: {"completion": "", "stop_reason": "stop_sequence", "model": null}

data: [DONE]

You can also have all entities returned at once using "return_entities": True.

Output
data: {"completion": " Based", "stop_reason": null, "model": null}

data: {"completion": " on", "stop_reason": null, "model": null}

data: {"completion": " the", "stop_reason": null, "model": null}

data: {"completion": " information", "stop_reason": null, "model": null}

data: {"completion": " provided", "stop_reason": null, "model": null}

data: {"completion": ",", "stop_reason": null, "model": null}

data: {"completion": " Bob", "stop_reason": null, "model": null}

data: {"completion": " works", "stop_reason": null, "model": null}

data: {"completion": " at", "stop_reason": null, "model": null}

data: {"completion": " Cape Privacy.", "stop_reason": null, "model": null}

data: {"completion": "", "stop_reason": "stop_sequence", "model": null}

data: {"entities": [{"processed_text": "NAME_GIVEN_1", "text": "Bob", "best_label": "NAME_GIVEN"}, {"processed_text": "ORGANIZATION_1", "text": "Cape Privacy", "best_label": "ORGANIZATION"}]}

data: [DONE]

That's it! To recap, Cape automatically redacted any sensitive entities so they were not sent to Anthropic (for example, Bob became [NAME_GIVEN_1]). And then Cape also re-identified the redacted entities from the Anthropic response (eg [NAME_GIVEN_1] was replaced back to Bob).