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_id
参数或 YC_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.'