diff --git a/docs/docs/integrations/vectorstores/surrealdb.ipynb b/docs/docs/integrations/vectorstores/surrealdb.ipynb index 3d5a0defdc..1542ae7503 100644 --- a/docs/docs/integrations/vectorstores/surrealdb.ipynb +++ b/docs/docs/integrations/vectorstores/surrealdb.ipynb @@ -40,7 +40,7 @@ }, "outputs": [], "source": [ - "%pip install --upgrade --quiet surrealdb langchain langchain-community" + "# %pip install --upgrade --quiet surrealdb langchain langchain-community" ] }, { @@ -54,6 +54,19 @@ { "cell_type": "code", "execution_count": 1, + "id": "1c2d942d-5d90-4f9f-af96-dff976e4510f", + "metadata": {}, + "outputs": [], + "source": [ + "# add this import for running in jupyter notebook\n", + "import nest_asyncio\n", + "\n", + "nest_asyncio.apply()" + ] + }, + { + "cell_type": "code", + "execution_count": 2, "id": "e49be085-ddf1-4028-8c0c-97836ce4a873", "metadata": { "tags": [] @@ -68,7 +81,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 3, "id": "38222aee-adc5-44c2-913c-97977b394cf5", "metadata": { "tags": [] @@ -92,28 +105,28 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 4, "id": "ff9d0304-1e11-4db2-9454-1350db7907e6", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "['documents:th7j29cjsx6495wluo7e',\n", - " 'documents:qkqhhjnl7ahbhr07euky',\n", - " 'documents:8kd6xw8o7y0l171iqry0',\n", - " 'documents:33ejf42dlkmavol9si74',\n", - " 'documents:f7y4dbs7eitqz58xt1p5']" + "['documents:38hz49bv1p58f5lrvrdc',\n", + " 'documents:niayw63vzwm2vcbh6w2s',\n", + " 'documents:it1fa3ktplbuye43n0ch',\n", + " 'documents:il8f7vgbbp9tywmsn98c',\n", + " 'documents:vza4c6cqje0avqd58gal']" ] }, - "execution_count": 3, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "db = SurrealDBStore(\n", - " dburl=\"http://localhost:8000/rpc\", # url for the hosted SurrealDB database\n", + " dburl=\"ws://localhost:8000/rpc\", # url for the hosted SurrealDB database\n", " embedding_function=embeddings,\n", " db_user=\"root\", # SurrealDB credentials if needed: db username\n", " db_pass=\"root\", # SurrealDB credentials if needed: db password\n", @@ -145,7 +158,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 5, "id": "73d66563-4e1f-4edf-9e95-5fc9adcfa2cb", "metadata": {}, "outputs": [], @@ -153,7 +166,7 @@ "await db.adelete()\n", "\n", "db = await SurrealDBStore.afrom_documents(\n", - " dburl=\"http://localhost:8000/rpc\", # url for the hosted SurrealDB database\n", + " dburl=\"ws://localhost:8000/rpc\", # url for the hosted SurrealDB database\n", " embedding=embeddings,\n", " documents=docs,\n", " db_user=\"root\", # SurrealDB credentials if needed: db username\n", @@ -174,7 +187,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 6, "id": "aa28a7f8-41d0-4299-84eb-91d1576e8a63", "metadata": { "tags": [] @@ -187,7 +200,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 7, "id": "1eb16d2a-b466-456a-b412-5e74bb8523dd", "metadata": { "tags": [] @@ -229,7 +242,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 8, "id": "8e9eef05-1516-469a-ad36-880c69aef7a9", "metadata": { "tags": [] @@ -241,7 +254,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 9, "id": "bd5fb0e4-2a94-4bb4-af8a-27327ecb1a7f", "metadata": { "tags": [] @@ -250,11 +263,11 @@ { "data": { "text/plain": [ - "(Document(page_content='Tonight. I call on the Senate to: Pass the Freedom to Vote Act. Pass the John Lewis Voting Rights Act. And while you’re at it, pass the Disclose Act so Americans can know who is funding our elections. \\n\\nTonight, I’d like to honor someone who has dedicated his life to serve this country: Justice Stephen Breyer—an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court. Justice Breyer, thank you for your service. \\n\\nOne of the most serious constitutional responsibilities a President has is nominating someone to serve on the United States Supreme Court. \\n\\nAnd I did that 4 days ago, when I nominated Circuit Court of Appeals Judge Ketanji Brown Jackson. One of our nation’s top legal minds, who will continue Justice Breyer’s legacy of excellence.', metadata={'id': 'documents:639m99rzwqlm9imcwg13'}),\n", - " 0.39839545290036454)" + "(Document(page_content='Tonight. I call on the Senate to: Pass the Freedom to Vote Act. Pass the John Lewis Voting Rights Act. And while you’re at it, pass the Disclose Act so Americans can know who is funding our elections. \\n\\nTonight, I’d like to honor someone who has dedicated his life to serve this country: Justice Stephen Breyer—an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court. Justice Breyer, thank you for your service. \\n\\nOne of the most serious constitutional responsibilities a President has is nominating someone to serve on the United States Supreme Court. \\n\\nAnd I did that 4 days ago, when I nominated Circuit Court of Appeals Judge Ketanji Brown Jackson. One of our nation’s top legal minds, who will continue Justice Breyer’s legacy of excellence.', metadata={'id': 'documents:slgdlhjkfknhqo15xz0w', 'source': '../../modules/state_of_the_union.txt'}),\n", + " 0.39839531721941895)" ] }, - "execution_count": 8, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" } @@ -280,7 +293,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.1" + "version": "3.10.12" } }, "nbformat": 4, diff --git a/libs/community/langchain_community/vectorstores/surrealdb.py b/libs/community/langchain_community/vectorstores/surrealdb.py index 951ec04463..d21f5bf0e0 100644 --- a/libs/community/langchain_community/vectorstores/surrealdb.py +++ b/libs/community/langchain_community/vectorstores/surrealdb.py @@ -55,14 +55,25 @@ class SurrealDBStore(VectorStore): embedding_function: Embeddings, **kwargs: Any, ) -> None: - from surrealdb import Surreal + try: + from surrealdb import Surreal + except ImportError as e: + raise ImportError( + """Cannot import from surrealdb. + please install with `pip install surrealdb`.""" + ) from e + + self.dburl = kwargs.pop("dburl", "ws://localhost:8000/rpc") + + if self.dburl[0:2] == "ws": + self.sdb = Surreal(self.dburl) + else: + raise ValueError("Only websocket connections are supported at this time.") - self.collection = kwargs.pop("collection", "documents") self.ns = kwargs.pop("ns", "langchain") self.db = kwargs.pop("db", "database") - self.dburl = kwargs.pop("dburl", "ws://localhost:8000/rpc") + self.collection = kwargs.pop("collection", "documents") self.embedding_function = embedding_function - self.sdb = Surreal(self.dburl) self.kwargs = kwargs async def initialize(self) -> None: @@ -70,12 +81,11 @@ class SurrealDBStore(VectorStore): Initialize connection to surrealdb database and authenticate if credentials are provided """ - await self.sdb.connect(self.dburl) + await self.sdb.connect() if "db_user" in self.kwargs and "db_pass" in self.kwargs: user = self.kwargs.get("db_user") password = self.kwargs.get("db_pass") await self.sdb.signin({"user": user, "pass": password}) - await self.sdb.use(self.ns, self.db) @property