Overview

You can specify the Anthropic adapter when rendering a prompt to automatically format the output for Anthropic’s API.

Anthropic does not support system messages in the messages array. PromptL will automatically move the first system message to the config object, as Anthropic recommends, but that means that:

  • You can only have system messages at the beggining of your prompt.
  • You must define non-system messages (the messages array cannot be empty).

Example

import Anthropic from '@anthropic-ai/sdk'
import { Adapters, render } from '@latitude-data/promptl'

const prompt = `
---
model: claude-3-opus-20240229
max_tokens: 1024
---

<user>
  Generate a joke about {{ topic }}.
</user>
`

const { messages, config } = await render({
  prompt,
  parameters: { topic: 'chickens' },
  adapter: Adapters.anthropic,
})

const client = new Anthropic({ apiKey: YOUR_ANTHROPIC_API_KEY })
const response = await client.messages.create({
  ...config,
  messages,
})

console.log(response.content[0].text)