Skip to main content

AnthropicLLM

caution

You are currently on a page documenting the use of Anthropic legacy Claude 2 models as text completion models. The latest and most popular Anthropic models are chat completion models.

You are probably looking for this page instead.

This example goes over how to use LangChain to interact with Anthropic models.

Installation

%pip install -qU langchain-anthropic

Environment Setup

We'll need to get an Anthropic API key and set the ANTHROPIC_API_KEY environment variable:

import os
from getpass import getpass

os.environ["ANTHROPIC_API_KEY"] = getpass()

Usage

from langchain_anthropic import AnthropicLLM
from langchain_core.prompts import PromptTemplate

template = """Question: {question}

Answer: Let's think step by step."""

prompt = PromptTemplate.from_template(template)

model = AnthropicLLM(model="claude-2.1")

chain = prompt | model

chain.invoke({"question": "What is LangChain?"})
'\nLangChain is a decentralized blockchain network that leverages AI and machine learning to provide language translation services.'

Was this page helpful?


You can leave detailed feedback on GitHub.