Skip to main content

Anthropic

Anthropic is a platform that aims to build frontier AI systems that are reliable, interpretable, and steerable. Anthropic's models are provided via Claude, an AI system that is capable of advanced reasoning, vision analysis, code generation, and more.

The following example demonstrates a simple chat interaction with Claude, in which a text payload from a device is used as a prompt for the Claude 3.5 Sonnet model. The resulting chat answer is delivered to LightDB Stream as a JSON object.

Reminder

Make sure to create a secret named ANTHROPIC_API_KEY with a valid API key.

filter:
path: "/assistant"
steps:
- name: embed
transformer:
type: embed-in-json
version: v1
parameters:
key: text
- name: create-payload
transformer:
type: json-patch
version: v1
parameters:
patch: |
[
{
"op": "add",
"path": "/model",
"value": "claude-3-5-sonnet-20240620"
},
{
"op": "add",
"path": "/max_tokens",
"value": 1024
},
{
"op": "add",
"path": "/messages",
"value": [
{
"role": "user",
"content": "REPLACEME"
}
]
},
{
"op": "move",
"from": "/text",
"path": "/messages/0/content"
},
{
"op": "remove",
"path": "/text"
}
]
- name: chat
transformer:
type: webhook
version: v1
parameters:
url: https://api.anthropic.com/v1/messages
headers:
x-api-key: $ANTHROPIC_API_KEY
anthropic-version: "2023-06-01"
- name: parse-payload
transformer:
type: json-patch
version: v1
parameters:
patch: |
[
{"op": "add", "path": "/answer", "value": ""},
{"op": "move", "from": "/content/0/text", "path": "/answer"},
{"op": "remove", "path": "/content"},
{"op": "remove", "path": "/id"},
{"op": "remove", "path": "/type"},
{"op": "remove", "path": "/role"},
{"op": "remove", "path": "/stop_reason"},
{"op": "remove", "path": "/stop_sequence"},
{"op": "remove", "path": "/usage"}
]
destination:
type: lightdb-stream
version: v1