From 7d3fb21807f6eb6c12336692af9674741593859e Mon Sep 17 00:00:00 2001 From: Jiang <65766008+AlwaysBluer@users.noreply.github.com> Date: Sat, 11 Jan 2025 05:30:37 +0800 Subject: [PATCH] Add lindorm as new integration (#29123) Misoperation caused the pr close: [origin pr link](https://github.com/langchain-ai/langchain/pull/29085) --------- Co-authored-by: jiangzhijie --- .../docs/integrations/providers/lindorm.ipynb | 112 ++++++ .../integrations/text_embedding/lindorm.ipynb | 294 ++++++++++++++++ .../integrations/vectorstores/lindorm.ipynb | 327 ++++++++++++++++++ libs/packages.yml | 4 + 4 files changed, 737 insertions(+) create mode 100644 docs/docs/integrations/providers/lindorm.ipynb create mode 100644 docs/docs/integrations/text_embedding/lindorm.ipynb create mode 100644 docs/docs/integrations/vectorstores/lindorm.ipynb diff --git a/docs/docs/integrations/providers/lindorm.ipynb b/docs/docs/integrations/providers/lindorm.ipynb new file mode 100644 index 0000000000000..320097d2cd3d1 --- /dev/null +++ b/docs/docs/integrations/providers/lindorm.ipynb @@ -0,0 +1,112 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Lindorm\n", + "\n", + "Lindorm is a cloud-native multimodal database from Alibaba-Cloud, It supports unified access and integrated processing of various types of data, including wide tables, time-series, text, objects, streams, and spatial data. It is compatible with multiple standard interfaces such as SQL, HBase/Cassandra/S3, TSDB, HDFS, Solr, and Kafka, and seamlessly integrates with third-party ecosystem tools. This makes it suitable for scenarios such as logging, monitoring, billing, advertising, social networking, travel, and risk control. Lindorm is also one of the databases that support Alibaba's core businesses. \n", + "\n", + "To use the AI and vector capabilities of Lindorm, you should [get the service](https://help.aliyun.com/document_detail/174640.html?spm=a2c4g.11186623.help-menu-172543.d_0_1_0.4c6367558DN8Uq) and install `langchain-lindorm-integration` package." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "id": "y8ku6X96sebl" + }, + "outputs": [], + "source": "!pip install -U langchain-lindorm-integration" + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": [ + "## Embeddings\n", + "\n", + "To use the embedding model deployed in Lindorm AI Service, import the LindormAIEmbeddings. " + ] + }, + { + "metadata": {}, + "cell_type": "code", + "outputs": [], + "execution_count": null, + "source": "from langchain_lindorm_integration import LindormAIEmbeddings" + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": [ + "## Rerank\n", + "\n", + "The Lindorm AI Service also supports reranking." + ] + }, + { + "metadata": {}, + "cell_type": "code", + "outputs": [], + "execution_count": null, + "source": "from langchain_lindorm_integration.reranker import LindormAIRerank" + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": [ + "## Vector Store\n", + "\n", + "Lindorm also supports vector store." + ] + }, + { + "metadata": {}, + "cell_type": "code", + "outputs": [], + "execution_count": null, + "source": "from langchain_lindorm_integration import LindormVectorStore" + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": [ + "## ByteStore\n", + "\n", + "Use ByteStore from Lindorm" + ] + }, + { + "metadata": {}, + "cell_type": "code", + "outputs": [], + "execution_count": null, + "source": "from langchain_lindorm_integration import LindormByteStore" + } + ], + "metadata": { + "colab": { + "provenance": [] + }, + "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.11" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} diff --git a/docs/docs/integrations/text_embedding/lindorm.ipynb b/docs/docs/integrations/text_embedding/lindorm.ipynb new file mode 100644 index 0000000000000..78f7db243354a --- /dev/null +++ b/docs/docs/integrations/text_embedding/lindorm.ipynb @@ -0,0 +1,294 @@ +{ + "cells": [ + { + "cell_type": "raw", + "id": "afaf8039", + "metadata": {}, + "source": [ + "---\n", + "sidebar_label: Lindorm\n", + "---" + ] + }, + { + "cell_type": "markdown", + "id": "9a3d6f34", + "metadata": {}, + "source": [ + "# LindormAIEmbeddings\n", + "\n", + "This will help you get started with Lindorm embedding models using LangChain. \n", + "\n", + "## Overview\n", + "### Integration details\n", + "\n", + "| Provider | Package |\n", + "|:--------:|:---------------------------------:|\n", + "| [Lindorm](/docs/integrations/providers/lindorm/) | [langchain-lindorm-integration](https://pypi.org/project/langchain-lindorm-integration/) |\n", + "\n", + "## Setup\n", + "\n", + "\n", + "To access Lindorm embedding models you'll need to create a Lindorm account, get AK&SK, and install the `langchain-lindorm-integration` integration package.\n", + "\n", + "### Credentials\n", + "\n", + "\n", + "You can get you credentials in the [console](https://lindorm.console.aliyun.com/cn-hangzhou/clusterhou/cluster?spm=a2c4g.11186623.0.0.466534e93Xj6tt)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "36521c2a", + "metadata": { + "ExecuteTime": { + "end_time": "2025-01-08T03:09:11.911612Z", + "start_time": "2025-01-08T03:09:11.907582Z" + } + }, + "outputs": [], + "source": [ + "import os\n", + "\n", + "\n", + "class Config:\n", + " AI_LLM_ENDPOINT = os.environ.get(\"AI_ENDPOINT\", \"\")\n", + " AI_USERNAME = os.environ.get(\"AI_USERNAME\", \"root\")\n", + " AI_PWD = os.environ.get(\"AI_PASSWORD\", \"\")\n", + "\n", + " AI_DEFAULT_EMBEDDING_MODEL = \"bge_m3_model\" # set to your deployed model" + ] + }, + { + "cell_type": "markdown", + "id": "d9664366", + "metadata": {}, + "source": [ + "### Installation\n", + "\n", + "The LangChain Lindorm integration lives in the `langchain-lindorm-integration` package:" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "64853226", + "metadata": { + "ExecuteTime": { + "end_time": "2025-01-08T03:09:15.249326Z", + "start_time": "2025-01-08T03:09:13.476557Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Note: you may need to restart the kernel to use updated packages.\n" + ] + } + ], + "source": [ + "%pip install -qU langchain-lindorm-integration" + ] + }, + { + "cell_type": "markdown", + "id": "45dd1724", + "metadata": {}, + "source": [ + "## Instantiation\n", + "\n", + "Now we can instantiate our model object and generate chat completions:\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "9ea7a09b", + "metadata": { + "ExecuteTime": { + "end_time": "2025-01-08T03:09:16.950069Z", + "start_time": "2025-01-08T03:09:16.385033Z" + } + }, + "outputs": [], + "source": [ + "from langchain_lindorm_integration import LindormAIEmbeddings\n", + "\n", + "embeddings = LindormAIEmbeddings(\n", + " endpoint=Config.AI_LLM_ENDPOINT,\n", + " username=Config.AI_USERNAME,\n", + " password=Config.AI_PWD,\n", + " model_name=Config.AI_DEFAULT_EMBEDDING_MODEL,\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "77d271b6", + "metadata": {}, + "source": [ + "## Indexing and Retrieval\n", + "\n", + "Embedding models are often used in retrieval-augmented generation (RAG) flows, both as part of indexing data as well as later retrieving it. For more detailed instructions, please see our [RAG tutorials](/docs/tutorials/).\n", + "\n", + "Below, see how to index and retrieve data using the `embeddings` object we initialized above. In this example, we will index and retrieve a sample document in the `InMemoryVectorStore`." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "d817716b", + "metadata": { + "ExecuteTime": { + "end_time": "2025-01-08T03:09:18.822848Z", + "start_time": "2025-01-08T03:09:18.085150Z" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'LangChain is the framework for building context-aware reasoning applications'" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Create a vector store with a sample text\n", + "from langchain_core.vectorstores import InMemoryVectorStore\n", + "\n", + "text = \"LangChain is the framework for building context-aware reasoning applications\"\n", + "\n", + "vectorstore = InMemoryVectorStore.from_texts(\n", + " [text],\n", + " embedding=embeddings,\n", + ")\n", + "\n", + "# Use the vectorstore as a retriever\n", + "retriever = vectorstore.as_retriever()\n", + "\n", + "# Retrieve the most similar text\n", + "retrieved_documents = retriever.invoke(\"What is LangChain?\")\n", + "\n", + "# show the retrieved document's content\n", + "retrieved_documents[0].page_content" + ] + }, + { + "cell_type": "markdown", + "id": "e02b9855", + "metadata": {}, + "source": [ + "## Direct Usage\n", + "\n", + "Under the hood, the vectorstore and retriever implementations are calling `embeddings.embed_documents(...)` and `embeddings.embed_query(...)` to create embeddings for the text(s) used in `from_texts` and retrieval `invoke` operations, respectively.\n", + "\n", + "You can directly call these methods to get embeddings for your own use cases.\n", + "\n", + "### Embed single texts\n", + "\n", + "You can embed single texts or documents with `embed_query`:" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "0d2befcd", + "metadata": { + "ExecuteTime": { + "end_time": "2025-01-08T03:09:23.634046Z", + "start_time": "2025-01-08T03:09:23.432791Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[-0.016254117712378502, -0.01154549140483141, 0.0042558759450912476, -0.011416379362344742, -0.01770\n" + ] + } + ], + "source": [ + "single_vector = embeddings.embed_query(text)\n", + "print(str(single_vector)[:100]) # Show the first 100 characters of the vector" + ] + }, + { + "cell_type": "markdown", + "id": "1b5a7d03", + "metadata": {}, + "source": [ + "### Embed multiple texts\n", + "\n", + "You can embed multiple texts with `embed_documents`:" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "2f4d6e97", + "metadata": { + "ExecuteTime": { + "end_time": "2025-01-08T03:09:25.960291Z", + "start_time": "2025-01-08T03:09:25.743941Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[-0.016254086047410965, -0.011545476503670216, 0.0042558712884783745, -0.011416426859796047, -0.0177\n", + "[-0.07268096506595612, -3.236892371205613e-05, -0.0019329536007717252, -0.030644644051790237, -0.018\n" + ] + } + ], + "source": [ + "text2 = (\n", + " \"LangGraph is a library for building stateful, multi-actor applications with LLMs\"\n", + ")\n", + "two_vectors = embeddings.embed_documents([text, text2])\n", + "for vector in two_vectors:\n", + " print(str(vector)[:100]) # Show the first 100 characters of the vector" + ] + }, + { + "cell_type": "markdown", + "id": "98785c12", + "metadata": {}, + "source": [ + "## API Reference\n", + "\n", + "For detailed documentation on `LindormEmbeddings` features and configuration options, please refer to the [API reference](https://pypi.org/project/langchain-lindorm-integration/).\n" + ] + } + ], + "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.5" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/docs/docs/integrations/vectorstores/lindorm.ipynb b/docs/docs/integrations/vectorstores/lindorm.ipynb new file mode 100644 index 0000000000000..a5c49d85fabd8 --- /dev/null +++ b/docs/docs/integrations/vectorstores/lindorm.ipynb @@ -0,0 +1,327 @@ +{ + "cells": [ + { + "cell_type": "raw", + "id": "1957f5cb", + "metadata": {}, + "source": [ + "---\n", + "sidebar_label: Lindorm\n", + "---" + ] + }, + { + "cell_type": "markdown", + "id": "ef1f0986", + "metadata": {}, + "source": [ + "# LindormVectorStore\n", + "\n", + "This notebook covers how to get started with the Lindorm vector store." + ] + }, + { + "cell_type": "markdown", + "id": "36fdc060", + "metadata": {}, + "source": [ + "## Setup\n", + "\n", + "To access Lindorm vector stores you'll need to create a Lindorm account, get the ak/sk, and install the `langchain-lindorm-integration` integration package." + ] + }, + { + "metadata": {}, + "cell_type": "code", + "outputs": [], + "execution_count": null, + "source": "%pip install -qU \"langchain-lindorm-integration\"", + "id": "beaec7d673c569f2" + }, + { + "cell_type": "markdown", + "id": "9695dee7", + "metadata": {}, + "source": [ + "### Credentials\n", + "\n", + "Head to [here](https://help.aliyun.com/document_detail/2773369.html?spm=a2c4g.11186623.help-menu-172543.d_2_5_0.2a383f96gr5N3M&scm=20140722.H_2773369._.OR_help-T_cn~zh-V_1) to sign up to Lindorm and generate the ak/sk." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "894c30e4", + "metadata": { + "ExecuteTime": { + "end_time": "2025-01-08T03:05:01.792898Z", + "start_time": "2025-01-08T03:05:01.786082Z" + } + }, + "outputs": [], + "source": [ + "import os\n", + "\n", + "\n", + "class Config:\n", + " SEARCH_ENDPOINT = os.environ.get(\"SEARCH_ENDPOINT\", \"SEARCH_ENDPOINT\")\n", + " SEARCH_USERNAME = os.environ.get(\"SEARCH_USERNAME\", \"root\")\n", + " SEARCH_PWD = os.environ.get(\"SEARCH_PASSWORD\", \"\")\n", + " AI_LLM_ENDPOINT = os.environ.get(\"AI_ENDPOINT\", \"\")\n", + " AI_USERNAME = os.environ.get(\"AI_USERNAME\", \"root\")\n", + " AI_PWD = os.environ.get(\"AI_PASSWORD\", \"\")\n", + " AI_DEFAULT_EMBEDDING_MODEL = \"bge_m3_model\" # set to your model" + ] + }, + { + "cell_type": "markdown", + "id": "93df377e", + "metadata": {}, + "source": [ + "## Initialization\n", + "\n", + "here we use the embedding model deployed on Lindorm AI Service." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "dc37144c-208d-4ab3-9f3a-0407a69fe052", + "metadata": { + "ExecuteTime": { + "end_time": "2025-01-08T03:05:06.907334Z", + "start_time": "2025-01-08T03:05:02.922523Z" + }, + "tags": [] + }, + "outputs": [], + "source": [ + "from langchain_lindorm_integration.embeddings import LindormAIEmbeddings\n", + "from langchain_lindorm_integration.vectorstores import LindormVectorStore\n", + "\n", + "embeddings = LindormAIEmbeddings(\n", + " endpoint=Config.AI_LLM_ENDPOINT,\n", + " username=Config.AI_USERNAME,\n", + " password=Config.AI_PWD,\n", + " model_name=Config.AI_DEFAULT_EMBEDDING_MODEL,\n", + ")\n", + "\n", + "index = \"test_index\"\n", + "vector = embeddings.embed_query(\"hello word\")\n", + "dimension = len(vector)\n", + "vector_store = LindormVectorStore(\n", + " lindorm_search_url=Config.SEARCH_ENDPOINT,\n", + " embedding=embeddings,\n", + " http_auth=(Config.SEARCH_USERNAME, Config.SEARCH_PWD),\n", + " dimension=dimension,\n", + " embeddings=embeddings,\n", + " index_name=index,\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "ac6071d4", + "metadata": {}, + "source": [ + "## Manage vector store\n", + "\n", + "### Add items to vector store\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "17f5efc0", + "metadata": { + "ExecuteTime": { + "end_time": "2025-01-08T03:05:18.586696Z", + "start_time": "2025-01-08T03:05:10.531559Z" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "['1', '2', '3']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from langchain_core.documents import Document\n", + "\n", + "document_1 = Document(page_content=\"foo\", metadata={\"source\": \"https://example.com\"})\n", + "\n", + "document_2 = Document(page_content=\"bar\", metadata={\"source\": \"https://example.com\"})\n", + "\n", + "document_3 = Document(page_content=\"baz\", metadata={\"source\": \"https://example.com\"})\n", + "\n", + "documents = [document_1, document_2, document_3]\n", + "\n", + "vector_store.add_documents(documents=documents, ids=[\"1\", \"2\", \"3\"])" + ] + }, + { + "cell_type": "markdown", + "id": "dcf1b905", + "metadata": {}, + "source": "### Delete items from vector store\n" + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "ef61e188", + "metadata": { + "ExecuteTime": { + "end_time": "2025-01-08T03:06:14.829559Z", + "start_time": "2025-01-08T03:06:14.323751Z" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{'took': 400,\n", + " 'timed_out': False,\n", + " 'total': 1,\n", + " 'deleted': 1,\n", + " 'batches': 1,\n", + " 'version_conflicts': 0,\n", + " 'noops': 0,\n", + " 'retries': {'bulk': 0, 'search': 0},\n", + " 'throttled_millis': 0,\n", + " 'requests_per_second': -1.0,\n", + " 'throttled_until_millis': 0,\n", + " 'failures': []}" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "vector_store.delete(ids=[\"3\"])" + ] + }, + { + "cell_type": "markdown", + "id": "c3620501", + "metadata": {}, + "source": [ + "## Query vector store\n", + "\n", + "Once your vector store has been created and the relevant documents have been added you will most likely wish to query it during the running of your chain or agent. \n", + "\n", + "### Query directly\n", + "\n", + "Performing a simple similarity search can be done as follows:" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "aa0a16fa", + "metadata": { + "ExecuteTime": { + "end_time": "2025-01-08T03:07:47.248796Z", + "start_time": "2025-01-08T03:07:46.939607Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "* foo [{'source': 'https://example.com'}]\n" + ] + } + ], + "source": [ + "results = vector_store.similarity_search(query=\"thud\", k=1)\n", + "for doc in results:\n", + " print(f\"* {doc.page_content} [{doc.metadata}]\")" + ] + }, + { + "cell_type": "markdown", + "id": "3ed9d733", + "metadata": {}, + "source": "If you want to execute a similarity search and receive the corresponding scores you can run:\n" + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "5efd2eaa", + "metadata": { + "ExecuteTime": { + "end_time": "2025-01-08T03:08:02.758719Z", + "start_time": "2025-01-08T03:08:02.501683Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "* [SIM=0.671268] foo [{'source': 'https://example.com'}]\n" + ] + } + ], + "source": [ + "results = vector_store.similarity_search_with_score(query=\"thud\", k=1)\n", + "for doc, score in results:\n", + " print(f\"* [SIM={score:3f}] {doc.page_content} [{doc.metadata}]\")" + ] + }, + { + "cell_type": "markdown", + "id": "901c75dc", + "metadata": {}, + "source": [ + "## Usage for retrieval-augmented generation\n", + "\n", + "For guides on how to use this vector store for retrieval-augmented generation (RAG), see the following sections:\n", + "\n", + "- [Tutorials](/docs/tutorials/)\n", + "- [How-to: Question and answer with RAG](https://python.langchain.com/docs/how_to/#qa-with-rag)\n", + "- [Retrieval conceptual docs](https://python.langchain.com/docs/concepts/#retrieval)" + ] + }, + { + "cell_type": "markdown", + "id": "8a27244f", + "metadata": {}, + "source": [ + "## API reference\n", + "\n", + "For detailed documentation of all LindormVectorStore features and configurations head to [the API reference](https://pypi.org/project/langchain-lindorm-integration/)." + ] + } + ], + "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.12" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/libs/packages.yml b/libs/packages.yml index 393fbc84c26c8..7c8970e7ecc0d 100644 --- a/libs/packages.yml +++ b/libs/packages.yml @@ -333,3 +333,7 @@ packages: path: . repo: DS4SD/docling-langchain downloads: 0 +- name: langchain-lindorm + path: . + repo: AlwaysBluer/langchain-lindorm-integration + downloads: 0