跳到主要内容
您目前正在查看的页面是关于如何使用 Anthropic 旧版 Claude 2 模型作为文本补全模型。最新和最受欢迎的 Anthropic 模型是聊天补全模型,而文本补全模型已被弃用。您可能正在寻找此页面
此示例将介绍如何使用 LangChain 与 Anthropic 模型进行交互。

安装

pip install -qU langchain-anthropic

环境设置

我们需要获取一个 Anthropic API 密钥,并设置 ANTHROPIC_API_KEY 环境变量
import os
from getpass import getpass

if "ANTHROPIC_API_KEY" not in os.environ:
    os.environ["ANTHROPIC_API_KEY"] = getpass()

用法

from langchain_anthropic import AnthropicLLM
from langchain_core.prompts import PromptTemplate

template = """Question: {question}

Answer: Let's think step by step."""

prompt = PromptTemplate.from_template(template)

model = AnthropicLLM(model="claude-2.1")

chain = prompt | model

chain.invoke({"question": "What is LangChain?"})
'\nLangChain is a decentralized blockchain network that leverages AI and machine learning to provide language translation services.'

以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.