ChatYandexGPT
本笔记本介绍如何使用LangChain与YandexGPT聊天模型。
要使用此功能,您需要安装yandexcloud
Python包。
%pip install --upgrade --quiet yandexcloud
首先,您应该创建服务账户,并赋予ai.languageModels.user
角色。
接下来,您有两个身份验证选项:
-
IAM令牌。 您可以在构造函数参数
iam_token
中指定令牌,或在环境变量YC_IAM_TOKEN
中指定。 -
API密钥 您可以在构造函数参数
api_key
中指定密钥,或在环境变量YC_API_KEY
中指定。
要指定模型,您可以使用model_uri
参数,更多详细信息请参见文档。
默认情况下,将使用从参数folder_id
或环境变量YC_FOLDER_ID
指定的文件夹中的最新版本yandexgpt-lite
。
<!--IMPORTS:[{"imported": "ChatYandexGPT", "source": "langchain_community.chat_models", "docs": "https://python.langchain.com/api_reference/community/chat_models/langchain_community.chat_models.yandex.ChatYandexGPT.html", "title": "ChatYandexGPT"}, {"imported": "HumanMessage", "source": "langchain_core.messages", "docs": "https://python.langchain.com/api_reference/core/messages/langchain_core.messages.human.HumanMessage.html", "title": "ChatYandexGPT"}, {"imported": "SystemMessage", "source": "langchain_core.messages", "docs": "https://python.langchain.com/api_reference/core/messages/langchain_core.messages.system.SystemMessage.html", "title": "ChatYandexGPT"}]-->
from langchain_community.chat_models import ChatYandexGPT
from langchain_core.messages import HumanMessage, SystemMessage
chat_model = ChatYandexGPT()
answer = chat_model.invoke(
[
SystemMessage(
content="You are a helpful assistant that translates English to French."
),
HumanMessage(content="I love programming."),
]
)
answer
AIMessage(content='Je adore le programmement.')