{ "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", "NOTE: AnthropicLLM only supports legacy Claude 2 models. To use the newest Claude 3 models, please use [`ChatAnthropic`](/docs/integrations/chat/anthropic) instead.\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 an [Anthropic](https://console.anthropic.com/settings/keys) API key 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 }