Skip to main content
本 notebook 介绍了如何将 LangChain 与 YandexGPT 一起使用。 要使用,您应该已安装 yandexcloud Python 包。
pip install -qU  yandexcloud
首先,您应该创建具有 ai.languageModels.user 角色的服务账户。 接下来,您有两种身份验证选项:
  • IAM 令牌。 您可以在构造函数参数 iam_token 中或在环境变量 YC_IAM_TOKEN 中指定令牌。
  • API 密钥 您可以在构造函数参数 api_key 中或在环境变量 YC_API_KEY 中指定密钥。
要指定模型,您可以使用 model_uri 参数,详情请参阅文档 默认情况下,将使用参数 folder_idYC_FOLDER_ID 环境变量指定的文件夹中 yandexgpt-lite 的最新版本。
from langchain_classic.chains import LLMChain
from langchain_community.llms import YandexGPT
from langchain_core.prompts import PromptTemplate
template = "What is the capital of {country}?"
prompt = PromptTemplate.from_template(template)
llm = YandexGPT()
llm_chain = LLMChain(prompt=prompt, llm=llm)
country = "Russia"

llm_chain.invoke(country)
'The capital of Russia is Moscow.'