LangChain Anthropic

1.4.0 · active · verified Sun Apr 05

LangChain Anthropic is an integration package that connects Anthropic's generative AI models (like Claude) with the LangChain framework. It allows developers to easily incorporate Anthropic's powerful chat models and (legacy) text completion models into their LangChain-based applications. As part of the broader LangChain ecosystem, it follows a frequent release cadence, often aligning with updates to `langchain-core` and the main `langchain` library. The current version is 1.4.0.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to instantiate and use the `ChatAnthropic` model to get a response from a Claude chat model. It highlights the importance of setting the `ANTHROPIC_API_KEY` environment variable.

import os
from langchain_anthropic import ChatAnthropic
from langchain_core.messages import HumanMessage, SystemMessage

# Set your Anthropic API key as an environment variable
# os.environ["ANTHROPIC_API_KEY"] = "YOUR_ANTHROPIC_API_KEY"

# Ensure the API key is set
if not os.environ.get("ANTHROPIC_API_KEY"):
    raise ValueError("ANTHROPIC_API_KEY environment variable not set.")

model = ChatAnthropic(model="claude-3-sonnet-20240229", temperature=0)

messages = [
    SystemMessage(content="You are a helpful AI assistant."),
    HumanMessage(content="What is the capital of France?"),
]

response = model.invoke(messages)
print(response.content)

view raw JSON →