diff --git a/docs/docs/integrations/chat/anthropic.ipynb b/docs/docs/integrations/chat/anthropic.ipynb index b7c5fbce304..f8aa35142de 100644 --- a/docs/docs/integrations/chat/anthropic.ipynb +++ b/docs/docs/integrations/chat/anthropic.ipynb @@ -22,44 +22,84 @@ }, { "cell_type": "code", - "execution_count": null, + "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": [] }, "outputs": [], "source": [ - "from langchain.schema import HumanMessage\n", - "from langchain_community.chat_models import ChatAnthropic" + "from langchain_community.chat_models import ChatAnthropic\n", + "from langchain_core.prompts import ChatPromptTemplate" ] }, { "cell_type": "code", - "execution_count": null, + "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": [] }, "outputs": [], "source": [ - "chat = ChatAnthropic()" + "chat = ChatAnthropic(temperature=0, model_name=\"claude-2\")" + ] + }, + { + "cell_type": "markdown", + "id": "d1f9df276476f0bc", + "metadata": { + "collapsed": false + }, + "source": [ + "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:\n", + "```python\n", + "chat = ChatAnthropic(temperature=0, anthropic_api_key=\"YOUR_API_KEY\", model_name=\"claude-instant-1.2\")\n", + "\n", + "```\n", + "Please note that the default model is \"claude-2,\" and you can check the available models at [here](https://docs.anthropic.com/claude/reference/selecting-a-model)." ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "id": "8199ef8f-eb8b-4253-9ea0-6c24a013ca4c", "metadata": { + "ExecuteTime": { + "end_time": "2024-01-19T11:25:07.274418Z", + "start_time": "2024-01-19T11:25:05.898031Z" + }, "tags": [] }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": "AIMessage(content=' 저는 파이썬을 좋아합니다.')" + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "messages = [\n", - " HumanMessage(\n", - " content=\"Translate this sentence from English to French. I love programming.\"\n", - " )\n", - "]\n", - "chat.invoke(messages)" + "system = \"You are a helpful assistant that translates {input_language} to {output_language}.\"\n", + "human = \"{text}\"\n", + "prompt = ChatPromptTemplate.from_messages([(\"system\", system), (\"human\", human)])\n", + "\n", + "chain = prompt | chat\n", + "chain.invoke({\n", + " \"input_language\": \"English\",\n", + " \"output_language\": \"Korean\",\n", + " \"text\": \"I love Python\",\n", + "})" ] }, { @@ -72,44 +112,78 @@ }, { "cell_type": "code", - "execution_count": null, - "id": "93a21c5c-6ef9-4688-be60-b2e1f94842fb", - "metadata": { - "tags": [] - }, - "outputs": [], - "source": [ - "from langchain.callbacks.manager import CallbackManager\n", - "from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler" - ] - }, - { - "cell_type": "code", - "execution_count": null, + "execution_count": 4, "id": "c5fac0e9-05a4-4fc1-a3b3-e5bbb24b971b", "metadata": { + "ExecuteTime": { + "end_time": "2024-01-19T11:25:10.448733Z", + "start_time": "2024-01-19T11:25:08.866277Z" + }, "tags": [] }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": "AIMessage(content=\" Why don't bears like fast food? Because they can't catch it!\")" + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "await chat.ainvoke([messages])" + "chat = ChatAnthropic(temperature=0, model_name=\"claude-2\")\n", + "prompt = ChatPromptTemplate.from_messages([(\"human\", \"Tell me a joke about {topic}\")])\n", + "chain = prompt | chat\n", + "await chain.ainvoke({\"topic\": \"bear\"})" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "id": "025be980-e50d-4a68-93dc-c9c7b500ce34", "metadata": { + "ExecuteTime": { + "end_time": "2024-01-19T11:25:24.438696Z", + "start_time": "2024-01-19T11:25:14.687480Z" + }, "tags": [] }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " Here are some of the most famous tourist attractions in Japan:\n", + "\n", + "- Tokyo - Tokyo Tower, Tokyo Skytree, Imperial Palace, Sensoji Temple, Meiji Shrine, Shibuya Crossing\n", + "\n", + "- Kyoto - Kinkakuji (Golden Pavilion), Fushimi Inari Shrine, Kiyomizu-dera Temple, Arashiyama Bamboo Grove, Gion Geisha District\n", + "\n", + "- Osaka - Osaka Castle, Dotonbori, Universal Studios Japan, Osaka Aquarium Kaiyukan \n", + "\n", + "- Hiroshima - Hiroshima Peace Memorial Park and Museum, Itsukushima Shrine (Miyajima Island)\n", + "\n", + "- Mount Fuji - Iconic and famous mountain, popular for hiking and viewing from places like Hakone and Kawaguchiko Lake\n", + "\n", + "- Himeji - Himeji Castle, one of Japan's most impressive feudal castles\n", + "\n", + "- Nara - Todaiji Temple, Nara Park with its bowing deer, Horyuji Temple with some of world's oldest wooden structures \n", + "\n", + "- Nikko - Elaborate shrines and temples nestled around Nikko National Park\n", + "\n", + "- Sapporo - Snow" + ] + } + ], "source": [ - "chat = ChatAnthropic(\n", - " streaming=True,\n", - " verbose=True,\n", - " callback_manager=CallbackManager([StreamingStdOutCallbackHandler()]),\n", + "chat = ChatAnthropic(temperature=0.3, model_name=\"claude-2\")\n", + "prompt = ChatPromptTemplate.from_messages(\n", + " [(\"human\", \"Give me a list of famous tourist attractions in Japan\")]\n", ")\n", - "chat.stream(messages)" + "chain = prompt | chat\n", + "for chunk in chain.stream({}):\n", + " print(chunk.content, end=\"\", flush=True)" ] }, { @@ -134,15 +208,130 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "id": "07c47c2a", - "metadata": {}, - "outputs": [], + "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", - "chat.invoke(messages)" + "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)" ] } ],