Skip to content

The agent engineering platform.

docs: anthropic partner package docs (#18109)

Erick Friisf5cf6975ba92
diff --git a/docs/docs/integrations/chat/anthropic.ipynb b/docs/docs/integrations/chat/anthropic.ipynb
index 008d290fa3..649ac5ca6a 100644
--- a/docs/docs/integrations/chat/anthropic.ipynb
+++ b/docs/docs/integrations/chat/anthropic.ipynb
@@ -17,40 +17,44 @@
"source": [
"# ChatAnthropic\n",
"\n",
- "This notebook covers how to get started with Anthropic chat models."
+ "This notebook covers how to get started with Anthropic chat models.\n",
+ "\n",
+ "## Setup\n",
+ "\n",
+ "For setup instructions, please see the Installation and Environment Setup sections of the [Anthropic Platform page](/docs/integrations/platforms/anthropic.mdx)."
]
},
{
"cell_type": "code",
- "execution_count": 1,
- "id": "d4a7c55d-b235-4ca4-a579-c90cc9570da9",
- "metadata": {
- "ExecuteTime": {
- "end_time": "2024-01-19T11:25:00.590587Z",
- "start_time": "2024-01-19T11:25:00.127293Z"
- },
- "tags": []
- },
+ "execution_count": null,
+ "id": "91be2e12",
+ "metadata": {},
"outputs": [],
"source": [
- "from langchain_community.chat_models import ChatAnthropic\n",
- "from langchain_core.prompts import ChatPromptTemplate"
+ "%pip install -qU langchain-anthropic"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "584ed5ec",
+ "metadata": {},
+ "source": [
+ "## Environment Setup\n",
+ "\n",
+ "We'll need to get a [Anthropic](https://console.anthropic.com/settings/keys) and set the `ANTHROPIC_API_KEY` environment variable:"
]
},
{
"cell_type": "code",
- "execution_count": 2,
- "id": "70cf04e8-423a-4ff6-8b09-f11fb711c817",
- "metadata": {
- "ExecuteTime": {
- "end_time": "2024-01-19T11:25:04.349676Z",
- "start_time": "2024-01-19T11:25:03.964930Z"
- },
- "tags": []
- },
+ "execution_count": null,
+ "id": "01578ae3",
+ "metadata": {},
"outputs": [],
"source": [
- "chat = ChatAnthropic(temperature=0, model_name=\"claude-2\")"
+ "import os\n",
+ "from getpass import getpass\n",
+ "\n",
+ "os.environ[\"ANTHROPIC_API_KEY\"] = getpass()"
]
},
{
@@ -82,7 +86,9 @@
"outputs": [
{
"data": {
- "text/plain": "AIMessage(content=' 저는 파이썬을 좋아합니다.')"
+ "text/plain": [
+ "AIMessage(content=' 저는 파이썬을 좋아합니다.')"
+ ]
},
"execution_count": 3,
"metadata": {},
@@ -90,6 +96,11 @@
}
],
"source": [
+ "from langchain_anthropic import ChatAnthropic\n",
+ "from langchain_core.prompts import ChatPromptTemplate\n",
+ "\n",
+ "chat = ChatAnthropic(temperature=0, model_name=\"claude-2\")\n",
+ "\n",
"system = (\n",
" \"You are a helpful assistant that translates {input_language} to {output_language}.\"\n",
")\n",
@@ -128,7 +139,9 @@
"outputs": [
{
"data": {
- "text/plain": "AIMessage(content=\" Why don't bears like fast food? Because they can't catch it!\")"
+ "text/plain": [
+ "AIMessage(content=\" Why don't bears like fast food? Because they can't catch it!\")"
+ ]
},
"execution_count": 4,
"metadata": {},
@@ -189,154 +202,6 @@
"for chunk in chain.stream({}):\n",
" print(chunk.content, end=\"\", flush=True)"
]
- },
- {
- "cell_type": "markdown",
- "id": "3737fc8d",
- "metadata": {},
- "source": [
- "# ChatAnthropicMessages\n",
- "\n",
- "LangChain also offers the beta Anthropic Messages endpoint through the new `langchain-anthropic` package."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "id": "c253883f",
- "metadata": {},
- "outputs": [],
- "source": [
- "%pip install --upgrade --quiet langchain-anthropic"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 6,
- "id": "07c47c2a",
- "metadata": {
- "ExecuteTime": {
- "end_time": "2024-01-19T11:25:25.288133Z",
- "start_time": "2024-01-19T11:25:24.438968Z"
- }
- },
- "outputs": [
- {
- "data": {
- "text/plain": "AIMessage(content='파이썬을 사랑합니다.')"
- },
- "execution_count": 6,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "from langchain_anthropic import ChatAnthropicMessages\n",
- "\n",
- "chat = ChatAnthropicMessages(model_name=\"claude-instant-1.2\")\n",
- "system = (\n",
- " \"You are a helpful assistant that translates {input_language} to {output_language}.\"\n",
- ")\n",
- "human = \"{text}\"\n",
- "prompt = ChatPromptTemplate.from_messages([(\"system\", system), (\"human\", human)])\n",
- "\n",
- "chain = prompt | chat\n",
- "chain.invoke(\n",
- " {\n",
- " \"input_language\": \"English\",\n",
- " \"output_language\": \"Korean\",\n",
- " \"text\": \"I love Python\",\n",
- " }\n",
- ")"
- ]
- },
- {
- "cell_type": "markdown",
- "id": "19e53d75935143fd",
- "metadata": {
- "collapsed": false
- },
- "source": [
- "ChatAnthropicMessages also requires the anthropic_api_key argument, or the ANTHROPIC_API_KEY environment variable must be set. \n",
- "\n",
- "ChatAnthropicMessages also supports async and streaming functionality:"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 7,
- "id": "e20a139d30e3d333",
- "metadata": {
- "ExecuteTime": {
- "end_time": "2024-01-19T11:25:26.012325Z",
- "start_time": "2024-01-19T11:25:25.288358Z"
- },
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": "AIMessage(content='파이썬을 사랑합니다.')"
- },
- "execution_count": 7,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "await chain.ainvoke(\n",
- " {\n",
- " \"input_language\": \"English\",\n",
- " \"output_language\": \"Korean\",\n",
- " \"text\": \"I love Python\",\n",
- " }\n",
- ")"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 8,
- "id": "6f34f1073d7e7120",
- "metadata": {
- "ExecuteTime": {
- "end_time": "2024-01-19T11:25:28.323455Z",
- "start_time": "2024-01-19T11:25:26.012040Z"
- },
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Here are some of the most famous tourist attractions in Japan:\n",
- "\n",
- "- Tokyo Tower - A communication and observation tower in Tokyo modeled after the Eiffel Tower. It offers stunning views of the city.\n",
- "\n",
- "- Mount Fuji - Japan's highest and most famous mountain. It's a iconic symbol of Japan and a UNESCO World Heritage Site. \n",
- "\n",
- "- 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.\n",
- "\n",
- "- Himeji Castle - A UNESCO World Heritage Site famous for having withstood numerous battles without destruction to its intricate white walls and sloping, triangular roofs. \n",
- "\n",
- "- Kawaguchiko Station - Near Mount Fuji, this area is known for its scenic Fuji Five Lakes region. \n",
- "\n",
- "- Hiroshima Peace Memorial Park and Museum - Commemorates the world's first atomic bombing in Hiroshima on August 6, 1945. \n",
- "\n",
- "- Arashiyama Bamboo Grove - A renowned bamboo forest located in Kyoto that draws many visitors.\n",
- "\n",
- "- Kegon Falls - One of Japan's largest waterfalls"
- ]
- }
- ],
- "source": [
- "prompt = ChatPromptTemplate.from_messages(\n",
- " [(\"human\", \"Give me a list of famous tourist attractions in Japan\")]\n",
- ")\n",
- "chain = prompt | chat\n",
- "for chunk in chain.stream({}):\n",
- " print(chunk.content, end=\"\", flush=True)"
- ]
}
],
"metadata": {
diff --git a/docs/docs/integrations/llms/anthropic.ipynb b/docs/docs/integrations/llms/anthropic.ipynb
new file mode 100644
index 0000000000..3ee1bfb2f0
--- /dev/null
+++ b/docs/docs/integrations/llms/anthropic.ipynb
@@ -0,0 +1,143 @@
+{
+ "cells": [
+ {
+ "cell_type": "raw",
+ "id": "602a52a4",
+ "metadata": {},
+ "source": [
+ "---\n",
+ "sidebar_label: Anthropic\n",
+ "---"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "9597802c",
+ "metadata": {},
+ "source": [
+ "# AnthropicLLM\n",
+ "\n",
+ "This example goes over how to use LangChain to interact with `Anthropic` models.\n",
+ "\n",
+ "## Installation"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "59c710c4",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "%pip install -qU langchain-anthropic"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "560a2f9254963fd7",
+ "metadata": {
+ "collapsed": false
+ },
+ "source": [
+ "## Environment Setup\n",
+ "\n",
+ "We'll need to get a [Anthropic](https://console.anthropic.com/settings/keys) and set the `ANTHROPIC_API_KEY` environment variable:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 4,
+ "id": "035dea0f",
+ "metadata": {
+ "tags": []
+ },
+ "outputs": [],
+ "source": [
+ "import os\n",
+ "from getpass import getpass\n",
+ "\n",
+ "os.environ[\"ANTHROPIC_API_KEY\"] = getpass()"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "1891df96eb076e1a",
+ "metadata": {
+ "collapsed": false
+ },
+ "source": [
+ "## Usage"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 6,
+ "id": "98f70927a87e4745",
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "'\\nLangChain is a decentralized blockchain network that leverages AI and machine learning to provide language translation services.'"
+ ]
+ },
+ "execution_count": 6,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "from langchain_anthropic import AnthropicLLM\n",
+ "from langchain_core.prompts import PromptTemplate\n",
+ "\n",
+ "template = \"\"\"Question: {question}\n",
+ "\n",
+ "Answer: Let's think step by step.\"\"\"\n",
+ "\n",
+ "prompt = PromptTemplate.from_template(template)\n",
+ "\n",
+ "model = AnthropicLLM(model=\"claude-2.1\")\n",
+ "\n",
+ "chain = prompt | model\n",
+ "\n",
+ "chain.invoke({\"question\": \"What is LangChain?\"})"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "a52f765c",
+ "metadata": {},
+ "outputs": [],
+ "source": []
+ }
+ ],
+ "metadata": {
+ "kernelspec": {
+ "display_name": "Python 3.11.1 64-bit",
+ "language": "python",
+ "name": "python3"
+ },
+ "language_info": {
+ "codemirror_mode": {
+ "name": "ipython",
+ "version": 3
+ },
+ "file_extension": ".py",
+ "mimetype": "text/x-python",
+ "name": "python",
+ "nbconvert_exporter": "python",
+ "pygments_lexer": "ipython3",
+ "version": "3.11.4"
+ },
+ "vscode": {
+ "interpreter": {
+ "hash": "e971737741ff4ec9aff7dc6155a1060a59a8a6d52c757dbbe66bf8ee389494b1"
+ }
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 5
+}
diff --git a/docs/docs/integrations/platforms/anthropic.mdx b/docs/docs/integrations/platforms/anthropic.mdx
index 14b2679049..f0c60cdfa9 100644
--- a/docs/docs/integrations/platforms/anthropic.mdx
+++ b/docs/docs/integrations/platforms/anthropic.mdx
@@ -5,169 +5,39 @@ All functionality related to Anthropic models.
[Anthropic](https://www.anthropic.com/) is an AI safety and research company, and is the creator of Claude.
This page covers all integrations between Anthropic models and LangChain.
-## Prompting Overview
+## Installation
-Claude is chat-based model, meaning it is trained on conversation data.
-However, it is a text based API, meaning it takes in single string.
-It expects this string to be in a particular format.
-This means that it is up the user to ensure that is the case.
-LangChain provides several utilities and helper functions to make sure prompts that you write -
-whether formatted as a string or as a list of messages - end up formatted correctly.
-
-Specifically, Claude is trained to fill in text for the Assistant role as part of an ongoing dialogue
-between a human user (`Human:`) and an AI assistant (`Assistant:`). Prompts sent via the API must contain
-`\n\nHuman:` and `\n\nAssistant:` as the signals of who's speaking.
-The final turn must always be `\n\nAssistant:` - the input string cannot have `\n\nHuman:` as the final role.
-
-Because Claude is chat-based but accepts a string as input, it can be treated as either a LangChain `ChatModel` or `LLM`.
-This means there are two wrappers in LangChain - `ChatAnthropic` and `Anthropic`.
-It is generally recommended to use the `ChatAnthropic` wrapper, and format your prompts as `ChatMessage`s (we will show examples of this below).
-This is because it keeps your prompt in a general format that you can easily then also use with other models (should you want to).
-However, if you want more fine-grained control over the prompt, you can use the `Anthropic` wrapper - we will show and example of this as well.
-The `Anthropic` wrapper however is deprecated, as all functionality can be achieved in a more generic way using `ChatAnthropic`.
-
-## Prompting Best Practices
-
-Anthropic models have several prompting best practices compared to OpenAI models.
-
-**No System Messages**
-
-Anthropic models are not trained on the concept of a "system message".
-We have worked with the Anthropic team to handle them somewhat appropriately (a Human message with an `admin` tag)
-but this is largely a hack and it is recommended that you do not use system messages.
-
-**AI Messages Can Continue**
-
-A completion from Claude is a continuation of the last text in the string which allows you further control over Claude's output.
-For example, putting words in Claude's mouth in a prompt like this:
-
-`\n\nHuman: Tell me a joke about bears\n\nAssistant: What do you call a bear with no teeth?`
-
-This will return a completion like this `A gummy bear!` instead of a whole new assistant message with a different random bear joke.
-
-
-## `ChatAnthropic`
-
-`ChatAnthropic` is a subclass of LangChain's `ChatModel`, meaning it works best with `ChatPromptTemplate`.
-You can import this wrapper with the following code:
-
-```
-from langchain_community.chat_models import ChatAnthropic
-model = ChatAnthropic()
-```
-
-When working with ChatModels, it is preferred that you design your prompts as `ChatPromptTemplate`s.
-Here is an example below of doing that:
-
-```
-from langchain_core.prompts import ChatPromptTemplate
-
-prompt = ChatPromptTemplate.from_messages([
- ("system", "You are a helpful chatbot"),
- ("human", "Tell me a joke about {topic}"),
-])
-```
-
-You can then use this in a chain as follows:
-
-```
-chain = prompt | model
-chain.invoke({"topic": "bears"})
-```
-
-How is the prompt actually being formatted under the hood? We can see that by running the following code
-
-```
-prompt_value = prompt.format_prompt(topic="bears")
-model.convert_prompt(prompt_value)
-```
-
-This produces the following formatted string:
+To use Anthropic models, you will need to install the `langchain-anthropic` package.
+You can do this with the following command:
```
-'\n\nYou are a helpful chatbot\n\nHuman: Tell me a joke about bears\n\nAssistant:'
+pip install langchain-anthropic
```
-We can see that under the hood LangChain is not appending any prefix/suffix to `SystemMessage`'s. This is because Anthropic has no concept of `SystemMessage`.
-Anthropic requires all prompts to end with assistant messages. This means if the last message is not an assistant message, the suffix `Assistant:` will automatically be inserted.
+## Environment Setup
-If you decide instead to use a normal PromptTemplate (one that just works on a single string) let's take a look at
-what happens:
+To use Anthropic models, you will need to set the `ANTHROPIC_API_KEY` environment variable.
+You can get an Anthropic API key [here](https://console.anthropic.com/settings/keys)
-```
-from langchain.prompts import PromptTemplate
-
-prompt = PromptTemplate.from_template("Tell me a joke about {topic}")
-prompt_value = prompt.format_prompt(topic="bears")
-model.convert_prompt(prompt_value)
-```
-
-This produces the following formatted string:
-
-```
-'\n\nHuman: Tell me a joke about bears\n\nAssistant:'
-```
-
-We can see that it automatically adds the Human and Assistant tags.
-What is happening under the hood?
-First: the string gets converted to a single human message. This happens generically (because we are using a subclass of `ChatModel`).
-Then, similarly to the above example, an empty Assistant message is getting appended.
-This is Anthropic specific.
-
-## [Deprecated] `Anthropic`
-
-This `Anthropic` wrapper is subclassed from `LLM`.
-We can import it with:
-
-```
-from langchain_community.llms import Anthropic
-model = Anthropic()
-```
-
-This model class is designed to work with normal PromptTemplates. An example of that is below:
-
-```
-prompt = PromptTemplate.from_template("Tell me a joke about {topic}")
-chain = prompt | model
-chain.invoke({"topic": "bears"})
-```
-
-Let's see what is going on with the prompt templating under the hood!
-
-```
-prompt_value = prompt.format_prompt(topic="bears")
-model.convert_prompt(prompt_value)
-```
-
-This outputs the following
-
-```
-'\n\nHuman: Tell me a joke about bears\n\nAssistant: Sure, here you go:\n'
-```
-
-Notice that it adds the Human tag at the start of the string, and then finishes it with `\n\nAssistant: Sure, here you go:`.
-The extra `Sure, here you go` was added on purpose by the Anthropic team.
-
-What happens if we have those symbols in the prompt directly?
-
-```
-prompt = PromptTemplate.from_template("Human: Tell me a joke about {topic}")
-prompt_value = prompt.format_prompt(topic="bears")
-model.convert_prompt(prompt_value)
-```
+## `ChatAnthropic`
-This outputs:
+`ChatAnthropic` is a subclass of LangChain's `ChatModel`.
+You can import this wrapper with the following code:
```
-'\n\nHuman: Tell me a joke about bears'
+from langchain_anthropic import ChatAnthropic
+model = ChatAnthropic(model='claude-2.1')
```
-We can see that we detect that the user is trying to use the special tokens, and so we don't do any formatting.
+Read more in the [ChatAnthropic documentation](/docs/integrations/chat/anthropic).
-## `ChatAnthropicMessages` (Beta)
+## `AnthropicLLM`
-`ChatAnthropicMessages` uses the beta release of Anthropic's new Messages API.
+`AnthropicLLM` is a subclass of LangChain's `LLM`. It is a wrapper around Anthropic's
+text-based completion endpoints.
-You can use it from the `langchain-anthropic` package, which you can install with `pip install langchain-anthropic`.
+```python
+from langchain_anthropic import AnthropicLLM
-For more information, see the [ChatAnthropicMessages docs](../chat/anthropic#chatanthropicmessages)
\ No newline at end of file
+model = AnthropicLLM(model='claude-2.1')
+```
\ No newline at end of file