ChatAnthropic
This notebook covers how to get started with Anthropic chat models.
from langchain_community.chat_models import ChatAnthropic
from langchain_core.prompts import ChatPromptTemplate
chat = ChatAnthropic(temperature=0, model_name="claude-2")
The code provided assumes that your ANTHROPIC_API_KEY is set in your environment variables. If you would like to manually specify your API key and also choose a different model, you can use the following code:
chat = ChatAnthropic(temperature=0, anthropic_api_key="YOUR_API_KEY", model_name="claude-instant-1.2")
Please note that the default model is βclaude-2,β and you can check the available models at here.
system = "You are a helpful assistant that translates {input_language} to {output_language}."
human = "{text}"
prompt = ChatPromptTemplate.from_messages([("system", system), ("human", human)])
chain = prompt | chat
chain.invoke({
"input_language": "English",
"output_language": "Korean",
"text": "I love Python",
})
AIMessage(content=' μ λ νμ΄μ¬μ μ’μν©λλ€.')
ChatAnthropic
also supports async and streaming functionality:β
chat = ChatAnthropic(temperature=0, model_name="claude-2")
prompt = ChatPromptTemplate.from_messages([("human", "Tell me a joke about {topic}")])
chain = prompt | chat
await chain.ainvoke({"topic": "bear"})
AIMessage(content=" Why don't bears like fast food? Because they can't catch it!")
chat = ChatAnthropic(temperature=0.3, model_name="claude-2")
prompt = ChatPromptTemplate.from_messages(
[("human", "Give me a list of famous tourist attractions in Japan")]
)
chain = prompt | chat
for chunk in chain.stream({}):
print(chunk.content, end="", flush=True)
Here are some of the most famous tourist attractions in Japan:
- Tokyo - Tokyo Tower, Tokyo Skytree, Imperial Palace, Sensoji Temple, Meiji Shrine, Shibuya Crossing
- Kyoto - Kinkakuji (Golden Pavilion), Fushimi Inari Shrine, Kiyomizu-dera Temple, Arashiyama Bamboo Grove, Gion Geisha District
- Osaka - Osaka Castle, Dotonbori, Universal Studios Japan, Osaka Aquarium Kaiyukan
- Hiroshima - Hiroshima Peace Memorial Park and Museum, Itsukushima Shrine (Miyajima Island)
- Mount Fuji - Iconic and famous mountain, popular for hiking and viewing from places like Hakone and Kawaguchiko Lake
- Himeji - Himeji Castle, one of Japan's most impressive feudal castles
- Nara - Todaiji Temple, Nara Park with its bowing deer, Horyuji Temple with some of world's oldest wooden structures
- Nikko - Elaborate shrines and temples nestled around Nikko National Park
- Sapporo - Snow
ChatAnthropicMessages
LangChain also offers the beta Anthropic Messages endpoint through the
new langchain-anthropic
package.
%pip install --upgrade --quiet langchain-anthropic
from langchain_anthropic import ChatAnthropicMessages
chat = ChatAnthropicMessages(model_name="claude-instant-1.2")
system = (
"You are a helpful assistant that translates {input_language} to {output_language}."
)
human = "{text}"
prompt = ChatPromptTemplate.from_messages([("system", system), ("human", human)])
chain = prompt | chat
chain.invoke(
{
"input_language": "English",
"output_language": "Korean",
"text": "I love Python",
}
)
AIMessage(content='νμ΄μ¬μ μ¬λν©λλ€.')
ChatAnthropicMessages also requires the anthropic_api_key argument, or the ANTHROPIC_API_KEY environment variable must be set.
ChatAnthropicMessages also supports async and streaming functionality:
await chain.ainvoke(
{
"input_language": "English",
"output_language": "Korean",
"text": "I love Python",
}
)
AIMessage(content='νμ΄μ¬μ μ¬λν©λλ€.')
prompt = ChatPromptTemplate.from_messages(
[("human", "Give me a list of famous tourist attractions in Japan")]
)
chain = prompt | chat
for chunk in chain.stream({}):
print(chunk.content, end="", flush=True)
Here are some of the most famous tourist attractions in Japan:
- Tokyo Tower - A communication and observation tower in Tokyo modeled after the Eiffel Tower. It offers stunning views of the city.
- Mount Fuji - Japan's highest and most famous mountain. It's a iconic symbol of Japan and a UNESCO World Heritage Site.
- Itsukushima Shrine (Miyajima) - A shrine located on an island in Hiroshima prefecture, known for its "floating" torii gate that seems to float on water during high tide.
- Himeji Castle - A UNESCO World Heritage Site famous for having withstood numerous battles without destruction to its intricate white walls and sloping, triangular roofs.
- Kawaguchiko Station - Near Mount Fuji, this area is known for its scenic Fuji Five Lakes region.
- Hiroshima Peace Memorial Park and Museum - Commemorates the world's first atomic bombing in Hiroshima on August 6, 1945.
- Arashiyama Bamboo Grove - A renowned bamboo forest located in Kyoto that draws many visitors.
- Kegon Falls - One of Japan's largest waterfalls