Package: yandex-chain
yandex-chain - LangChain-compatible integrations with YandexGPT and YandexGPT EmbeddingsThis library is community-maintained Python package that provides support forYandex GPTLLM and Embeddings forLangChain Framework.Currently, Yandex GPT is in preview stage, so this library may occasionally break. Please use it at your own risk!What's IncludedThe library includes the following two main classes:YandexLLMis a class representingYandexGPT Text Generation.YandexEmbeddingsrepresentsYandexGPT Embeddingsservice.UsageYou can useYandexLLMin the following manner:fromyandex_chainimportYandexLLMLLM=YandexLLM(folder_id="...",api_key="...")print(LLM("How are you today?"))You can useYandexEmbeddingsto compute embedding vectors:fromyandex_chainimportYandexEmbeddingsembeddings=YandexEmbeddings(...)print(embeddings("How are you today?"))AuthenticationIn order to use Yandex GPT, you need to provide one of the following authentication methods, which you can specify as parameters toYandexLLMandYandexEmbeddingsclasses:A pair offolder_idandapi_keyA pair offolder_idandiam_tokenA path toconfig.jsonfile, which may in turn contain parameters listed above in a convenient JSON format.Complete ExampleA pair of LLM and Embeddings are a good combination to create problem-oriented chatbots using Retrieval-Augmented Generation (RAG). Here is a short example of this approach, inspired bythis LangChain tutorial.To begin with, we have a set of documentsdocs(for simplicity, let's assume it is just a list of strings), which we store in vector storage. We can useYandexEmbeddingsto compute embedding vectors:fromyandex_chainimportYandexLLM,YandexEmbeddingsfromlangchain.vectorstoresimportFAISSembeddings=YandexEmbeddings(config="config.json")vectorstore=FAISS.from_texts(docs,embedding=embeddings)retriever=vectorstore.as_retriever()We can now retrieve a set of documents relevant to a query:query="Which library can be used to work with Yandex GPT?"res=retriever.get_relevant_documents(query)Now, to provide a full-text answer to the query, we can use LLM. We will prompt the LLM, giving it retrieved documents as a context, and the input query, and ask it to answer the question. This can be done using LangChainchains:fromoperatorimportitemgetterfromlangchain.promptsimportChatPromptTemplatefromlangchain.schema.output_parserimportStrOutputParserfromlangchain.schema.runnableimportRunnablePassthroughtemplate="""Answer the question based only on the following context:{context}Question:{question}"""prompt=ChatPromptTemplate.from_template(template)model=YandexLLM(config="config.json")chain=({"context":retriever,"question":RunnablePassthrough()}|prompt|model|StrOutputParser())This chain can now answer our questions:chain.invoke(query)Lite vs. Full ModelsYandexGPT model comes in two flavours - YandexGPT Lite and full YandexGPT. By default, YandexGPT Lite is used. If you want to use full YandexGPT, you need to specifyuse_lite=Falseparameter when instantiatingYandexLLMlanguage model class.TestingThis repository contains some basic unit tests. To run them, you need to place a configuration fileconfig.jsonwith your credentials intotestsfolder. Useconfig_sample.jsonas a reference. After that, please run the following at the repository root directory:python-munittestdiscover-stestsCreditsThis library has originally been developed byDmitri Soshnikov.