Skip to main content
Open In ColabOpen on GitHub

YandexGPT

本笔记本介绍了如何将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_idYC_FOLDER_ID环境变量指定的文件夹中使用最新版本的yandexgpt-lite

from langchain.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.'