Skip to main content

GigaChat

本笔记展示了如何将LangChain与GigaChat结合使用。 要使用它,您需要安装gigachat Python包。

%pip install --upgrade --quiet  gigachat

要获取GigaChat凭证,您需要创建账户获取API访问权限

示例

import os
from getpass import getpass

if "GIGACHAT_CREDENTIALS" not in os.environ:
os.environ["GIGACHAT_CREDENTIALS"] = getpass()
<!--IMPORTS:[{"imported": "GigaChat", "source": "langchain_community.chat_models", "docs": "https://python.langchain.com/api_reference/community/chat_models/langchain_community.chat_models.gigachat.GigaChat.html", "title": "GigaChat"}]-->
from langchain_community.chat_models import GigaChat

chat = GigaChat(verify_ssl_certs=False, scope="GIGACHAT_API_PERS")
<!--IMPORTS:[{"imported": "HumanMessage", "source": "langchain_core.messages", "docs": "https://python.langchain.com/api_reference/core/messages/langchain_core.messages.human.HumanMessage.html", "title": "GigaChat"}, {"imported": "SystemMessage", "source": "langchain_core.messages", "docs": "https://python.langchain.com/api_reference/core/messages/langchain_core.messages.system.SystemMessage.html", "title": "GigaChat"}]-->
from langchain_core.messages import HumanMessage, SystemMessage

messages = [
SystemMessage(
content="You are a helpful AI that shares everything you know. Talk in English."
),
HumanMessage(content="What is capital of Russia?"),
]

print(chat.invoke(messages).content)
The capital of Russia is Moscow.

相关


Was this page helpful?


You can also leave detailed feedback on GitHub.

扫我,入群扫我,找书