AI21 大语言模型
该服务已弃用。
请参阅 此页面 以获取更新后的 ChatAI21 对象。 :::
此示例介绍了如何使用 LangChain 与 AI21 Jurassic 模型进行交互。如需使用 Jamba 模型,请改用ChatAI21 对象。
安装
!pip install -qU langchain-ai21
环境设置
我们将需要获取一个AI21 API密钥并设置AI21_API_KEY环境变量:
import os
from getpass import getpass
if "AI21_API_KEY" not in os.environ:
os.environ["AI21_API_KEY"] = getpass()
用法
from langchain_ai21 import AI21LLM
from langchain_core.prompts import PromptTemplate
template = """Question: {question}
Answer: Let's think step by step."""
prompt = PromptTemplate.from_template(template)
model = AI21LLM(model="j2-ultra")
chain = prompt | model
chain.invoke({"question": "What is LangChain?"})
API 参考:提示模板
'\nLangChain is a (database)\nLangChain is a database for storing and processing documents'
AI21 上下文答案
您可以使用 AI21 的上下文问答模型,接收作为上下文的文本或文档,以及一个问题,并完全基于该上下文返回答案。
这意味着如果您的问题的答案不在文档中, 模型会予以说明(而不会提供错误的答案)
from langchain_ai21 import AI21ContextualAnswers
tsm = AI21ContextualAnswers()
response = tsm.invoke(input={"context": "Your context", "question": "Your question"})
您也可以将其与链、输出解析器和向量数据库一起使用
from langchain_ai21 import AI21ContextualAnswers
from langchain_core.output_parsers import StrOutputParser
tsm = AI21ContextualAnswers()
chain = tsm | StrOutputParser()
response = chain.invoke(
{"context": "Your context", "question": "Your question"},
)
API 参考:字符串输出解析器