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
。
<!--IMPORTS:[{"imported": "LLMChain", "source": "langchain.chains", "docs": "https://python.langchain.com/api_reference/langchain/chains/langchain.chains.llm.LLMChain.html", "title": "YandexGPT"}, {"imported": "YandexGPT", "source": "langchain_community.llms", "docs": "https://python.langchain.com/api_reference/community/llms/langchain_community.llms.yandex.YandexGPT.html", "title": "YandexGPT"}, {"imported": "PromptTemplate", "source": "langchain_core.prompts", "docs": "https://python.langchain.com/api_reference/core/prompts/langchain_core.prompts.prompt.PromptTemplate.html", "title": "YandexGPT"}]-->
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.'