Skip to content

The agent engineering platform.

Add embedchain retriever (#13553)

Taranjeet Singh47451764a72b
diff --git a/docs/docs/integrations/retrievers/embedchain.ipynb b/docs/docs/integrations/retrievers/embedchain.ipynb
new file mode 100644
index 0000000000..adeddfea8c
--- /dev/null
+++ b/docs/docs/integrations/retrievers/embedchain.ipynb
@@ -0,0 +1,255 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "id": "2f0f85ac-9c49-4111-a320-e53bccc99b13",
+ "metadata": {},
+ "source": [
+ "# Embedchain\n",
+ "\n",
+ "Embedchain is a RAG framework to create data pipelines. It loads, indexes, retrieves and syncs all the data.\n",
+ "\n",
+ "It is available as an [open source package](https://github.com/embedchain/embedchain) and as a [hosted platform solution](https://app.embedchain.ai/).\n",
+ "\n",
+ "This notebook shows how to use a retriever that uses Embedchain."
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "e48de822-307b-4284-96e7-c91f11ce005b",
+ "metadata": {},
+ "source": [
+ "# Installation\n",
+ "\n",
+ "First you will need to install the [`embedchain` package](https://pypi.org/project/embedchain/). \n",
+ "\n",
+ "You can install the package by running "
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 1,
+ "id": "c690a78c-5999-4072-b4e1-2712ff73f950",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "#!pip install --upgrade embedchain"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "bc89ba12-6ebd-4cd6-8c85-7410531579ff",
+ "metadata": {},
+ "source": [
+ "# Create New Retriever\n",
+ "\n",
+ "`EmbedchainRetriever` has a static `.create()` factory method that takes the following arguments:\n",
+ "\n",
+ "* `yaml_path: string` optional -- Path to the YAML configuration file. If not provided, a default configuration is used. You can browse the [docs](https://docs.embedchain.ai/) to explore various customization options."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 2,
+ "id": "8e639bd4-2e60-487b-b7aa-f7e6b921b069",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdin",
+ "output_type": "stream",
+ "text": [
+ " ········\n"
+ ]
+ }
+ ],
+ "source": [
+ "# Setup API Key\n",
+ "\n",
+ "import os\n",
+ "from getpass import getpass\n",
+ "\n",
+ "os.environ[\"OPENAI_API_KEY\"] = getpass()"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 3,
+ "id": "223fbc76-91ad-4504-87e9-980fb0e027fc",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "from langchain.retrievers import EmbedchainRetriever\n",
+ "\n",
+ "# create retriever with default options\n",
+ "retriever = EmbedchainRetriever.create()\n",
+ "\n",
+ "# or if you want to customize, pass the yaml config path\n",
+ "# retriever = EmbedchainRetiever.create(yaml_path=\"config.yaml\")"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "536f3a1d-3491-45b5-9f25-869bd6fb6d6a",
+ "metadata": {},
+ "source": [
+ "# Add Data\n",
+ "\n",
+ "In embedchain, you can as many supported data types as possible. You can browse our [docs](https://docs.embedchain.ai/) to see the data types supported.\n",
+ "\n",
+ "Embedchain automatically deduces the types of the data. So you can add a string, URL or local file path."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 4,
+ "id": "31262be3-7d0d-42e8-9253-052160576dc7",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Inserting batches in chromadb: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 4/4 [00:08<00:00, 2.22s/it]\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Successfully saved https://en.wikipedia.org/wiki/Elon_Musk (DataType.WEB_PAGE). New chunks count: 378\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Inserting batches in chromadb: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1/1 [00:01<00:00, 1.17s/it]\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Successfully saved https://www.forbes.com/profile/elon-musk (DataType.WEB_PAGE). New chunks count: 13\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "Inserting batches in chromadb: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1/1 [00:02<00:00, 2.25s/it]"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Successfully saved https://www.youtube.com/watch?v=RcYjXbSJBN8 (DataType.YOUTUBE_VIDEO). New chunks count: 53\n"
+ ]
+ },
+ {
+ "name": "stderr",
+ "output_type": "stream",
+ "text": [
+ "\n"
+ ]
+ },
+ {
+ "data": {
+ "text/plain": [
+ "['1eab8dd1ffa92906f7fc839862871ca5',\n",
+ " '8cf46026cabf9b05394a2658bd1fe890',\n",
+ " 'da3227cdbcedb018e05c47b774d625f6']"
+ ]
+ },
+ "execution_count": 4,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "retriever.add_texts(\n",
+ " [\n",
+ " \"https://en.wikipedia.org/wiki/Elon_Musk\",\n",
+ " \"https://www.forbes.com/profile/elon-musk\",\n",
+ " \"https://www.youtube.com/watch?v=RcYjXbSJBN8\",\n",
+ " ]\n",
+ ")"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "e1f34a62-7f8e-4c03-8e10-c317ed3296aa",
+ "metadata": {},
+ "source": [
+ "# Use Retriever\n",
+ "\n",
+ "You can now use the retrieve to find relevant documents given a query"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 5,
+ "id": "6106baf9-652a-4a94-b2d7-d6a5d2917975",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "result = retriever.get_relevant_documents(\n",
+ " \"How many companies does Elon Musk run and name those?\"\n",
+ ")"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 6,
+ "id": "1deae5d0-e0fa-431d-b164-e9680ef3e69b",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "[Document(page_content='Views Filmography Companies Zip2 X.com PayPal SpaceX Starlink Tesla, Inc. Energycriticismlitigation OpenAI Neuralink The Boring Company Thud X Corp. Twitteracquisitiontenure as CEO xAI In popular culture Elon Musk (Isaacson) Elon Musk (Vance) Ludicrous Power Play \"Members Only\" \"The Platonic Permutation\" \"The Musk Who Fell to Earth\" \"One Crew over the Crewcoo\\'s Morty\" Elon Musk\\'s Crash Course Related Boring Test Tunnel Hyperloop Musk family Musk vs. Zuckerberg SolarCity Tesla Roadster in space', metadata={'source': 'https://en.wikipedia.org/wiki/Elon_Musk', 'document_id': 'c33c05d0-5028-498b-b5e3-c43a4f9e8bf8--3342161a0fbc19e91f6bf387204aa30fbb2cea05abc81882502476bde37b9392'}),\n",
+ " Document(page_content='Elon Musk PROFILEElon MuskCEO, Tesla$241.2B$508M (0.21%)Real Time Net Worthas of 11/18/23Reflects change since 5 pm ET of prior trading day. 1 in the world todayPhoto by Martin Schoeller for ForbesAbout Elon MuskElon Musk cofounded six companies, including electric car maker Tesla, rocket producer SpaceX and tunneling startup Boring Company.He owns about 21% of Tesla between stock and options, but has pledged more than half his shares as collateral for personal loans of up to $3.5', metadata={'source': 'https://www.forbes.com/profile/elon-musk', 'document_id': 'c33c05d0-5028-498b-b5e3-c43a4f9e8bf8--3c8573134c575fafc025e9211413723e1f7a725b5936e8ee297fb7fb63bdd01a'}),\n",
+ " Document(page_content='to form PayPal. In October 2002, eBay acquired PayPal for $1.5 billion, and that same year, with $100 million of the money he made, Musk founded SpaceX, a spaceflight services company. In 2004, he became an early investor in electric vehicle manufacturer Tesla Motors, Inc. (now Tesla, Inc.). He became its chairman and product architect, assuming the position of CEO in 2008. In 2006, Musk helped create SolarCity, a solar-energy company that was acquired by Tesla in 2016 and became Tesla Energy.', metadata={'source': 'https://en.wikipedia.org/wiki/Elon_Musk', 'document_id': 'c33c05d0-5028-498b-b5e3-c43a4f9e8bf8--3342161a0fbc19e91f6bf387204aa30fbb2cea05abc81882502476bde37b9392'})]"
+ ]
+ },
+ "execution_count": 6,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "result"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "b3f26c2b-048d-4588-90a0-50f5c9c35837",
+ "metadata": {},
+ "outputs": [],
+ "source": []
+ }
+ ],
+ "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.11.4"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 5
+}
diff --git a/libs/langchain/langchain/retrievers/__init__.py b/libs/langchain/langchain/retrievers/__init__.py
index 49f49d51b5..6da361501a 100644
--- a/libs/langchain/langchain/retrievers/__init__.py
+++ b/libs/langchain/langchain/retrievers/__init__.py
@@ -28,6 +28,7 @@ from langchain.retrievers.cohere_rag_retriever import CohereRagRetriever
from langchain.retrievers.contextual_compression import ContextualCompressionRetriever
from langchain.retrievers.docarray import DocArrayRetriever
from langchain.retrievers.elastic_search_bm25 import ElasticSearchBM25Retriever
+from langchain.retrievers.embedchain import EmbedchainRetriever
from langchain.retrievers.ensemble import EnsembleRetriever
from langchain.retrievers.google_cloud_documentai_warehouse import (
GoogleDocumentAIWarehouseRetriever,
@@ -78,6 +79,7 @@ __all__ = [
"ChaindeskRetriever",
"CohereRagRetriever",
"ElasticSearchBM25Retriever",
+ "EmbedchainRetriever",
"GoogleDocumentAIWarehouseRetriever",
"GoogleCloudEnterpriseSearchRetriever",
"GoogleVertexAIMultiTurnSearchRetriever",
diff --git a/libs/langchain/langchain/retrievers/embedchain.py b/libs/langchain/langchain/retrievers/embedchain.py
new file mode 100644
index 0000000000..3de589cd6e
--- /dev/null
+++ b/libs/langchain/langchain/retrievers/embedchain.py
@@ -0,0 +1,71 @@
+"""Wrapper around Embedchain Retriever."""
+
+from __future__ import annotations
+
+from typing import Any, Iterable, List, Optional
+
+from langchain.callbacks.manager import CallbackManagerForRetrieverRun
+from langchain.docstore.document import Document
+from langchain.schema import BaseRetriever
+
+
+class EmbedchainRetriever(BaseRetriever):
+ """`Embedchain` retriever."""
+
+ client: Any
+ """Embedchain Pipeline."""
+
+ @classmethod
+ def create(cls, yaml_path: Optional[str] = None) -> EmbedchainRetriever:
+ """
+ Create a EmbedchainRetriever from a YAML configuration file.
+
+ Args:
+ yaml_path: Path to the YAML configuration file. If not provided,
+ a default configuration is used.
+
+ Returns:
+ An instance of EmbedchainRetriever.
+
+ """
+ from embedchain import Pipeline
+
+ # Create an Embedchain Pipeline instance
+ if yaml_path:
+ client = Pipeline.from_config(yaml_path=yaml_path)
+ else:
+ client = Pipeline()
+ return cls(client=client)
+
+ def add_texts(
+ self,
+ texts: Iterable[str],
+ ) -> List[str]:
+ """Run more texts through the embeddings and add to the retriever.
+
+ Args:
+ texts: Iterable of strings/URLs to add to the retriever.
+
+ Returns:
+ List of ids from adding the texts into the retriever.
+ """
+ ids = []
+ for text in texts:
+ _id = self.client.add(text)
+ ids.append(_id)
+ return ids
+
+ def _get_relevant_documents(
+ self, query: str, *, run_manager: CallbackManagerForRetrieverRun
+ ) -> List[Document]:
+ res = self.client.search(query)
+
+ docs = []
+ for r in res:
+ docs.append(
+ Document(
+ page_content=r["context"],
+ metadata={"source": r["source"], "document_id": r["document_id"]},
+ )
+ )
+ return docs
diff --git a/libs/langchain/tests/integration_tests/adapters/test_openai.py b/libs/langchain/tests/integration_tests/adapters/test_openai.py
index 2e27f03650..594770742c 100644
--- a/libs/langchain/tests/integration_tests/adapters/test_openai.py
+++ b/libs/langchain/tests/integration_tests/adapters/test_openai.py
@@ -1,12 +1,13 @@
from typing import Any
-import openai
import pytest
from langchain.adapters import openai as lcopenai
def _test_no_stream(**kwargs: Any) -> None:
+ import openai
+
result = openai.ChatCompletion.create(**kwargs)
lc_result = lcopenai.ChatCompletion.create(**kwargs)
if isinstance(lc_result, dict):
@@ -18,6 +19,8 @@ def _test_no_stream(**kwargs: Any) -> None:
def _test_stream(**kwargs: Any) -> None:
+ import openai
+
result = []
for c in openai.ChatCompletion.create(**kwargs):
result.append(c["choices"][0]["delta"].to_dict_recursive())
@@ -29,6 +32,8 @@ def _test_stream(**kwargs: Any) -> None:
async def _test_async(**kwargs: Any) -> None:
+ import openai
+
result = await openai.ChatCompletion.acreate(**kwargs)
lc_result = await lcopenai.ChatCompletion.acreate(**kwargs)
if isinstance(lc_result, dict):
@@ -40,6 +45,8 @@ async def _test_async(**kwargs: Any) -> None:
async def _test_astream(**kwargs: Any) -> None:
+ import openai
+
result = []
async for c in await openai.ChatCompletion.acreate(**kwargs):
result.append(c["choices"][0]["delta"].to_dict_recursive())
diff --git a/libs/langchain/tests/integration_tests/embeddings/test_openai.py b/libs/langchain/tests/integration_tests/embeddings/test_openai.py
index f24a66597d..33dd46fa5a 100644
--- a/libs/langchain/tests/integration_tests/embeddings/test_openai.py
+++ b/libs/langchain/tests/integration_tests/embeddings/test_openai.py
@@ -1,6 +1,5 @@
"""Test openai embeddings."""
import numpy as np
-import openai
import pytest
from langchain.embeddings.openai import OpenAIEmbeddings
@@ -66,6 +65,8 @@ async def test_openai_embedding_async_query() -> None:
@pytest.mark.scheduled
def test_openai_embedding_with_empty_string() -> None:
"""Test openai embeddings with empty string."""
+ import openai
+
document = ["", "abc"]
embedding = OpenAIEmbeddings()
output = embedding.embed_documents(document)
diff --git a/libs/langchain/tests/integration_tests/retrievers/test_embedchain.py b/libs/langchain/tests/integration_tests/retrievers/test_embedchain.py
new file mode 100644
index 0000000000..fa1e6dc6bf
--- /dev/null
+++ b/libs/langchain/tests/integration_tests/retrievers/test_embedchain.py
@@ -0,0 +1,44 @@
+"""Integration test for Embedchain."""
+
+import os
+from typing import Any
+from unittest.mock import patch
+
+import pytest
+
+from langchain.retrievers.embedchain import EmbedchainRetriever
+from langchain.schema import Document
+
+try:
+ from embedchain import Pipeline
+except ImportError:
+ pytest.skip("Requires embedchain", allow_module_level=True)
+
+os.environ["OPENAI_API_KEY"] = "sk-xxxx"
+
+context_value = [
+ {
+ "context": "this document is about John",
+ "source": "source#1",
+ "document_id": 123,
+ },
+]
+
+
[email protected]("embedchain")
[email protected](Pipeline, "search", return_value=context_value)
[email protected](Pipeline, "add", return_value=123)
+def test_embedchain_retriever(mock_add: Any, mock_search: Any) -> None:
+ retriever = EmbedchainRetriever.create()
+ texts = [
+ "This document is about John",
+ ]
+ for text in texts:
+ retriever.add_texts(text)
+ docs = retriever.get_relevant_documents("doc about john")
+ assert len(docs) == 1
+ for doc in docs:
+ assert isinstance(doc, Document)
+ assert doc.page_content
+ assert doc.metadata
+ assert len(list(doc.metadata.items())) > 0
diff --git a/libs/langchain/tests/unit_tests/retrievers/test_imports.py b/libs/langchain/tests/unit_tests/retrievers/test_imports.py
index b0f27c5ee5..14253b27bd 100644
--- a/libs/langchain/tests/unit_tests/retrievers/test_imports.py
+++ b/libs/langchain/tests/unit_tests/retrievers/test_imports.py
@@ -10,6 +10,7 @@ EXPECTED_ALL = [
"ChaindeskRetriever",
"CohereRagRetriever",
"ElasticSearchBM25Retriever",
+ "EmbedchainRetriever",
"GoogleDocumentAIWarehouseRetriever",
"GoogleCloudEnterpriseSearchRetriever",
"GoogleVertexAIMultiTurnSearchRetriever",