AI21LLM
此服务已弃用。
有关更新的 ChatAI21 对象,请参阅此页面。:::
此示例介绍了如何使用 LangChain 与AI21侏罗纪模型。要使用 Jamba 模型,请改用 ChatAI21 对象。
查看 LangChain 上的 AI21 模型和工具的完整列表。
安装
!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 参考:PromptTemplate
'\nLangChain is a (database)\nLangChain is a database for storing and processing documents'
AI21 上下文答案
您可以使用 AI21 的上下文答案模型来接收文本或文档,作为上下文, 和一个 Question 一起返回答案,并返回完全基于此上下文的答案。
这意味着,如果文档中没有您问题的答案, 模型将指示它(而不是提供错误的答案)
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 参考:StrOutputParser