Skip to main content

AnthropicLLM

caution

您当前正在查看文档,介绍如何使用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()

使用方法

<!--IMPORTS:[{"imported": "AnthropicLLM", "source": "langchain_anthropic", "docs": "https://python.langchain.com/api_reference/anthropic/llms/langchain_anthropic.llms.AnthropicLLM.html", "title": "AnthropicLLM"}, {"imported": "PromptTemplate", "source": "langchain_core.prompts", "docs": "https://python.langchain.com/api_reference/core/prompts/langchain_core.prompts.prompt.PromptTemplate.html", "title": "AnthropicLLM"}]-->
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.'

相关内容


Was this page helpful?


You can also leave detailed feedback on GitHub.

扫我,入群扫我,找书