{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# YandexGPT\n", "\n", "This notebook goes over how to use Langchain with [YandexGPT](https://cloud.yandex.com/en/services/yandexgpt).\n", "\n", "To use, you should have the `yandexcloud` python package installed." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%pip install --upgrade --quiet yandexcloud" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First, you should [create service account](https://cloud.yandex.com/en/docs/iam/operations/sa/create) with the `ai.languageModels.user` role.\n", "\n", "Next, you have two authentication options:\n", "- [IAM token](https://cloud.yandex.com/en/docs/iam/operations/iam-token/create-for-sa).\n", " You can specify the token in a constructor parameter `iam_token` or in an environment variable `YC_IAM_TOKEN`.\n", "\n", "- [API key](https://cloud.yandex.com/en/docs/iam/operations/api-key/create)\n", " You can specify the key in a constructor parameter `api_key` or in an environment variable `YC_API_KEY`.\n", "\n", "To specify the model you can use `model_uri` parameter, see [the documentation](https://cloud.yandex.com/en/docs/yandexgpt/concepts/models#yandexgpt-generation) for more details.\n", "\n", "By default, the latest version of `yandexgpt-lite` is used from the folder specified in the parameter `folder_id` or `YC_FOLDER_ID` environment variable." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from langchain.chains import LLMChain\n", "from langchain_community.llms import YandexGPT\n", "from langchain_core.prompts import PromptTemplate" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "template = \"What is the capital of {country}?\"\n", "prompt = PromptTemplate.from_template(template)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "llm = YandexGPT()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "llm_chain = LLMChain(prompt=prompt, llm=llm)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'The capital of Russia is Moscow.'" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "country = \"Russia\"\n", "\n", "llm_chain.invoke(country)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.13" } }, "nbformat": 4, "nbformat_minor": 4 }