Skip to main content

ChatAI21

这份笔记涵盖了如何开始使用 AI21 聊天模型。

安装

!pip install -qU langchain-ai21

环境设置

我们需要获取一个 AI21 API key 并设置 AI21_API_KEY 环境变量:

import os
from getpass import getpass
os.environ["AI21_API_KEY"] = getpass()

使用

from langchain_ai21 import ChatAI21
from langchain_core.prompts import ChatPromptTemplate
chat = ChatAI21(model="jamba-instruct")
prompt = ChatPromptTemplate.from_messages(
[
("system", "You are a helpful assistant that translates English to French."),
("human", "Translate this sentence from English to French. {english_text}."),
]
)
chain = prompt | chat
chain.invoke({"english_text": "Hello, how are you?"})
AIMessage(content='Bonjour, comment vas-tu?')

Was this page helpful?


You can leave detailed feedback on GitHub.