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