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
參數,有關更多詳細資訊,請參閱文檔。
預設情況下,yandexgpt-lite
的最新版本會從參數 folder_id
或 YC_FOLDER_ID
環境變數中指定的資料夾中使用。
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.'