diff --git a/docs/vectorstores.ipynb b/docs/vectorstores.ipynb index ff0300f..33941b0 100644 --- a/docs/vectorstores.ipynb +++ b/docs/vectorstores.ipynb @@ -39,44 +39,42 @@ "name": "stdout", "output_type": "stream", "text": [ - "Requirement already satisfied: langchain-weaviate in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (0.0.1rc1)\n", - "Requirement already satisfied: langchain-core>=0.0.12 in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from langchain-weaviate) (0.1.16)\n", + "Requirement already satisfied: langchain-weaviate in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (0.0.1.post1)\n", + "Requirement already satisfied: langchain-core<0.2.0,>=0.1.33 in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from langchain-weaviate) (0.1.40)\n", "Requirement already satisfied: numpy<2.0.0,>=1.26.2 in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from langchain-weaviate) (1.26.3)\n", - "Requirement already satisfied: simsimd<4.0.0,>=3.6.1 in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from langchain-weaviate) (3.7.2)\n", - "Requirement already satisfied: weaviate-client<5.0.0,>=4.0.0b in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from langchain-weaviate) (4.4b9)\n", - "Requirement already satisfied: PyYAML>=5.3 in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from langchain-core>=0.0.12->langchain-weaviate) (6.0.1)\n", - "Requirement already satisfied: anyio<5,>=3 in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from langchain-core>=0.0.12->langchain-weaviate) (4.2.0)\n", - "Requirement already satisfied: jsonpatch<2.0,>=1.33 in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from langchain-core>=0.0.12->langchain-weaviate) (1.33)\n", - "Requirement already satisfied: langsmith<0.1,>=0.0.83 in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from langchain-core>=0.0.12->langchain-weaviate) (0.0.84)\n", - "Requirement already satisfied: packaging<24.0,>=23.2 in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from langchain-core>=0.0.12->langchain-weaviate) (23.2)\n", - "Requirement already satisfied: pydantic<3,>=1 in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from langchain-core>=0.0.12->langchain-weaviate) (2.5.3)\n", - "Requirement already satisfied: requests<3,>=2 in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from langchain-core>=0.0.12->langchain-weaviate) (2.31.0)\n", - "Requirement already satisfied: tenacity<9.0.0,>=8.1.0 in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from langchain-core>=0.0.12->langchain-weaviate) (8.2.3)\n", - "Requirement already satisfied: httpx==0.26.0 in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from weaviate-client<5.0.0,>=4.0.0b->langchain-weaviate) (0.26.0)\n", - "Requirement already satisfied: validators==0.22.0 in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from weaviate-client<5.0.0,>=4.0.0b->langchain-weaviate) (0.22.0)\n", - "Requirement already satisfied: authlib<2.0.0,>=1.2.1 in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from weaviate-client<5.0.0,>=4.0.0b->langchain-weaviate) (1.3.0)\n", - "Requirement already satisfied: grpcio<2.0.0,>=1.57.0 in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from weaviate-client<5.0.0,>=4.0.0b->langchain-weaviate) (1.60.0)\n", - "Requirement already satisfied: grpcio-tools<2.0.0,>=1.57.0 in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from weaviate-client<5.0.0,>=4.0.0b->langchain-weaviate) (1.60.0)\n", - "Requirement already satisfied: grpcio-health-checking<2.0.0,>=1.57.0 in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from weaviate-client<5.0.0,>=4.0.0b->langchain-weaviate) (1.60.0)\n", - "Requirement already satisfied: certifi in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from httpx==0.26.0->weaviate-client<5.0.0,>=4.0.0b->langchain-weaviate) (2023.11.17)\n", - "Requirement already satisfied: httpcore==1.* in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from httpx==0.26.0->weaviate-client<5.0.0,>=4.0.0b->langchain-weaviate) (1.0.2)\n", - "Requirement already satisfied: idna in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from httpx==0.26.0->weaviate-client<5.0.0,>=4.0.0b->langchain-weaviate) (3.6)\n", - "Requirement already satisfied: sniffio in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from httpx==0.26.0->weaviate-client<5.0.0,>=4.0.0b->langchain-weaviate) (1.3.0)\n", - "Requirement already satisfied: h11<0.15,>=0.13 in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from httpcore==1.*->httpx==0.26.0->weaviate-client<5.0.0,>=4.0.0b->langchain-weaviate) (0.14.0)\n", - "Requirement already satisfied: cryptography in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from authlib<2.0.0,>=1.2.1->weaviate-client<5.0.0,>=4.0.0b->langchain-weaviate) (42.0.1)\n", - "Requirement already satisfied: protobuf>=4.21.6 in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from grpcio-health-checking<2.0.0,>=1.57.0->weaviate-client<5.0.0,>=4.0.0b->langchain-weaviate) (4.25.2)\n", - "Requirement already satisfied: setuptools in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from grpcio-tools<2.0.0,>=1.57.0->weaviate-client<5.0.0,>=4.0.0b->langchain-weaviate) (69.0.3)\n", - "Requirement already satisfied: jsonpointer>=1.9 in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from jsonpatch<2.0,>=1.33->langchain-core>=0.0.12->langchain-weaviate) (2.4)\n", - "Requirement already satisfied: annotated-types>=0.4.0 in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from pydantic<3,>=1->langchain-core>=0.0.12->langchain-weaviate) (0.6.0)\n", - "Requirement already satisfied: pydantic-core==2.14.6 in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from pydantic<3,>=1->langchain-core>=0.0.12->langchain-weaviate) (2.14.6)\n", - "Requirement already satisfied: typing-extensions>=4.6.1 in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from pydantic<3,>=1->langchain-core>=0.0.12->langchain-weaviate) (4.9.0)\n", - "Requirement already satisfied: charset-normalizer<4,>=2 in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from requests<3,>=2->langchain-core>=0.0.12->langchain-weaviate) (3.3.2)\n", - "Requirement already satisfied: urllib3<3,>=1.21.1 in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from requests<3,>=2->langchain-core>=0.0.12->langchain-weaviate) (2.1.0)\n", - "Requirement already satisfied: cffi>=1.12 in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from cryptography->authlib<2.0.0,>=1.2.1->weaviate-client<5.0.0,>=4.0.0b->langchain-weaviate) (1.16.0)\n", - "Requirement already satisfied: pycparser in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from cffi>=1.12->cryptography->authlib<2.0.0,>=1.2.1->weaviate-client<5.0.0,>=4.0.0b->langchain-weaviate) (2.21)\n", - "\n", - "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m23.3.1\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m23.3.2\u001b[0m\n", - "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n", + "Requirement already satisfied: simsimd<5.0.0,>=3.6.1 in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from langchain-weaviate) (3.7.4)\n", + "Requirement already satisfied: weaviate-client<5.0.0,>=4.0.0 in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from langchain-weaviate) (4.4rc1)\n", + "Requirement already satisfied: PyYAML>=5.3 in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from langchain-core<0.2.0,>=0.1.33->langchain-weaviate) (6.0.1)\n", + "Requirement already satisfied: jsonpatch<2.0,>=1.33 in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from langchain-core<0.2.0,>=0.1.33->langchain-weaviate) (1.33)\n", + "Requirement already satisfied: langsmith<0.2.0,>=0.1.0 in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from langchain-core<0.2.0,>=0.1.33->langchain-weaviate) (0.1.40)\n", + "Requirement already satisfied: packaging<24.0,>=23.2 in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from langchain-core<0.2.0,>=0.1.33->langchain-weaviate) (23.2)\n", + "Requirement already satisfied: pydantic<3,>=1 in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from langchain-core<0.2.0,>=0.1.33->langchain-weaviate) (2.6.0)\n", + "Requirement already satisfied: tenacity<9.0.0,>=8.1.0 in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from langchain-core<0.2.0,>=0.1.33->langchain-weaviate) (8.2.3)\n", + "Requirement already satisfied: requests<3.0.0,>=2.30.0 in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from weaviate-client<5.0.0,>=4.0.0->langchain-weaviate) (2.31.0)\n", + "Requirement already satisfied: httpx==0.26.0 in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from weaviate-client<5.0.0,>=4.0.0->langchain-weaviate) (0.26.0)\n", + "Requirement already satisfied: validators==0.22.0 in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from weaviate-client<5.0.0,>=4.0.0->langchain-weaviate) (0.22.0)\n", + "Requirement already satisfied: authlib<2.0.0,>=1.2.1 in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from weaviate-client<5.0.0,>=4.0.0->langchain-weaviate) (1.3.0)\n", + "Requirement already satisfied: grpcio<2.0.0,>=1.57.0 in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from weaviate-client<5.0.0,>=4.0.0->langchain-weaviate) (1.60.0)\n", + "Requirement already satisfied: grpcio-tools<2.0.0,>=1.57.0 in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from weaviate-client<5.0.0,>=4.0.0->langchain-weaviate) (1.60.0)\n", + "Requirement already satisfied: grpcio-health-checking<2.0.0,>=1.57.0 in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from weaviate-client<5.0.0,>=4.0.0->langchain-weaviate) (1.60.0)\n", + "Requirement already satisfied: anyio in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from httpx==0.26.0->weaviate-client<5.0.0,>=4.0.0->langchain-weaviate) (4.2.0)\n", + "Requirement already satisfied: certifi in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from httpx==0.26.0->weaviate-client<5.0.0,>=4.0.0->langchain-weaviate) (2023.11.17)\n", + "Requirement already satisfied: httpcore==1.* in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from httpx==0.26.0->weaviate-client<5.0.0,>=4.0.0->langchain-weaviate) (1.0.2)\n", + "Requirement already satisfied: idna in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from httpx==0.26.0->weaviate-client<5.0.0,>=4.0.0->langchain-weaviate) (3.6)\n", + "Requirement already satisfied: sniffio in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from httpx==0.26.0->weaviate-client<5.0.0,>=4.0.0->langchain-weaviate) (1.3.0)\n", + "Requirement already satisfied: h11<0.15,>=0.13 in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from httpcore==1.*->httpx==0.26.0->weaviate-client<5.0.0,>=4.0.0->langchain-weaviate) (0.14.0)\n", + "Requirement already satisfied: cryptography in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from authlib<2.0.0,>=1.2.1->weaviate-client<5.0.0,>=4.0.0->langchain-weaviate) (42.0.2)\n", + "Requirement already satisfied: protobuf>=4.21.6 in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from grpcio-health-checking<2.0.0,>=1.57.0->weaviate-client<5.0.0,>=4.0.0->langchain-weaviate) (4.25.2)\n", + "Requirement already satisfied: setuptools in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from grpcio-tools<2.0.0,>=1.57.0->weaviate-client<5.0.0,>=4.0.0->langchain-weaviate) (69.0.3)\n", + "Requirement already satisfied: jsonpointer>=1.9 in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from jsonpatch<2.0,>=1.33->langchain-core<0.2.0,>=0.1.33->langchain-weaviate) (2.4)\n", + "Requirement already satisfied: orjson<4.0.0,>=3.9.14 in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from langsmith<0.2.0,>=0.1.0->langchain-core<0.2.0,>=0.1.33->langchain-weaviate) (3.10.0)\n", + "Requirement already satisfied: annotated-types>=0.4.0 in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from pydantic<3,>=1->langchain-core<0.2.0,>=0.1.33->langchain-weaviate) (0.6.0)\n", + "Requirement already satisfied: pydantic-core==2.16.1 in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from pydantic<3,>=1->langchain-core<0.2.0,>=0.1.33->langchain-weaviate) (2.16.1)\n", + "Requirement already satisfied: typing-extensions>=4.6.1 in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from pydantic<3,>=1->langchain-core<0.2.0,>=0.1.33->langchain-weaviate) (4.9.0)\n", + "Requirement already satisfied: charset-normalizer<4,>=2 in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from requests<3.0.0,>=2.30.0->weaviate-client<5.0.0,>=4.0.0->langchain-weaviate) (3.3.2)\n", + "Requirement already satisfied: urllib3<3,>=1.21.1 in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from requests<3.0.0,>=2.30.0->weaviate-client<5.0.0,>=4.0.0->langchain-weaviate) (2.2.0)\n", + "Requirement already satisfied: cffi>=1.12 in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from cryptography->authlib<2.0.0,>=1.2.1->weaviate-client<5.0.0,>=4.0.0->langchain-weaviate) (1.16.0)\n", + "Requirement already satisfied: pycparser in /workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages (from cffi>=1.12->cryptography->authlib<2.0.0,>=1.2.1->weaviate-client<5.0.0,>=4.0.0->langchain-weaviate) (2.21)\n", "Note: you may need to restart the kernel to use updated packages.\n" ] } @@ -133,7 +131,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 1, "id": "9d0ab00c", "metadata": {}, "outputs": [], @@ -147,7 +145,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 2, "id": "4618779d", "metadata": {}, "outputs": [ @@ -171,7 +169,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 3, "id": "e06f64b7", "metadata": {}, "outputs": [ @@ -179,12 +177,8 @@ "name": "stderr", "output_type": "stream", "text": [ - "/workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages/langchain_community/embeddings/openai.py:500: PydanticDeprecatedSince20: The `dict` method is deprecated; use `model_dump` instead. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.5/migration/\n", - " response = response.dict()\n", - "/workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages/pydantic/main.py:979: PydanticDeprecatedSince20: The `dict` method is deprecated; use `model_dump` instead. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.5/migration/\n", - " warnings.warn('The `dict` method is deprecated; use `model_dump` instead.', DeprecationWarning)\n", - "/workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages/weaviate/warnings.py:215: DeprecationWarning: Dep015: You are creating a batch using client.batch or collection.batch, which is deprecated. Use X.batch._batch_mode_().\n", - " warnings.warn(\n" + "/workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages/pydantic/main.py:1024: PydanticDeprecatedSince20: The `dict` method is deprecated; use `model_dump` instead. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.6/migration/\n", + " warnings.warn('The `dict` method is deprecated; use `model_dump` instead.', category=PydanticDeprecatedSince20)\n" ] } ], @@ -195,13 +189,28 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 4, "id": "ebc3aa1e", "metadata": {}, - "outputs": [], + "outputs": [ + { + "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={'source': 'state_of_the_union.txt'}),\n", + " Document(page_content='And so many families are living paycheck to paycheck, struggling to keep up with the rising cost of food, gas, housing, and so much more. \\n\\nI understand. \\n\\nI remember when my Dad had to leave our home in Scranton, Pennsylvania to find work. I grew up in a family where if the price of food went up, you felt it. \\n\\nThat’s why one of the first things I did as President was fight to pass the American Rescue Plan. \\n\\nBecause people were hurting. We needed to act, and we did. \\n\\nFew pieces of legislation have done more in a critical moment in our history to lift us out of crisis. \\n\\nIt fueled our efforts to vaccinate the nation and combat COVID-19. It delivered immediate economic relief for tens of millions of Americans. \\n\\nHelped put food on their table, keep a roof over their heads, and cut the cost of health insurance. \\n\\nAnd as my Dad used to say, it gave people a little breathing room.', metadata={'source': 'state_of_the_union.txt'}),\n", + " Document(page_content='Vice President Harris and I ran for office with a new economic vision for America. \\n\\nInvest in America. Educate Americans. Grow the workforce. Build the economy from the bottom up \\nand the middle out, not from the top down. \\n\\nBecause we know that when the middle class grows, the poor have a ladder up and the wealthy do very well. \\n\\nAmerica used to have the best roads, bridges, and airports on Earth. \\n\\nNow our infrastructure is ranked 13th in the world. \\n\\nWe won’t be able to compete for the jobs of the 21st Century if we don’t fix that. \\n\\nThat’s why it was so important to pass the Bipartisan Infrastructure Law—the most sweeping investment to rebuild America in history. \\n\\nThis was a bipartisan effort, and I want to thank the members of both parties who worked to make it happen. \\n\\nWe’re done talking about infrastructure weeks. \\n\\nWe’re going to have an infrastructure decade.', metadata={'source': 'state_of_the_union.txt'}),\n", + " Document(page_content='A former top litigator in private practice. A former federal public defender. And from a family of public school educators and police officers. A consensus builder. Since she’s been nominated, she’s received a broad range of support—from the Fraternal Order of Police to former judges appointed by Democrats and Republicans. \\n\\nAnd if we are to advance liberty and justice, we need to secure the Border and fix the immigration system. \\n\\nWe can do both. At our border, we’ve installed new technology like cutting-edge scanners to better detect drug smuggling. \\n\\nWe’ve set up joint patrols with Mexico and Guatemala to catch more human traffickers. \\n\\nWe’re putting in place dedicated immigration judges so families fleeing persecution and violence can have their cases heard faster. \\n\\nWe’re securing commitments and supporting partners in South and Central America to host more refugees and secure their own borders.', metadata={'source': 'state_of_the_union.txt'})]" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "query = \"What did the president say about Ketanji Brown Jackson\"\n", - "docs = db.similarity_search(query)" + "docs = db.similarity_search(query)\n", + "docs" ] }, { @@ -214,24 +223,24 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 5, "id": "d1210f90", "metadata": {}, "outputs": [], "source": [ - "search_filter = weaviate.classes.Filter.by_property(\"source\").equal(\"blah.txt\")\n", + "search_filter = weaviate.classes.query.Filter.by_property(\"source\").equal(\"blah.txt\")\n", "filtered_search_results = db.similarity_search(query, filters=search_filter)\n", "assert len(filtered_search_results) == 0" ] }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 6, "id": "6e53d7d5", "metadata": {}, "outputs": [], "source": [ - "search_filter = weaviate.classes.Filter.by_property(\"source\").equal(\n", + "search_filter = weaviate.classes.query.Filter.by_property(\"source\").equal(\n", " \"state_of_the_union.txt\"\n", ")\n", "filtered_search_results = db.similarity_search(query, filters=search_filter, k=3)\n", @@ -281,7 +290,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 7, "id": "b3b4a2f4", "metadata": {}, "outputs": [ @@ -289,10 +298,10 @@ "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={'source': 'state_of_the_union.txt'}),\n", - " 0.016393441706895828)" + " 1.0)" ] }, - "execution_count": 8, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } @@ -324,7 +333,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 8, "id": "74a7bae0", "metadata": {}, "outputs": [ @@ -332,8 +341,8 @@ "name": "stderr", "output_type": "stream", "text": [ - "/tmp/ipykernel_82492/2919976745.py:7: ResourceWarning: unclosed \n", - " db = WeaviateVectorStore.from_documents(docs, embeddings, client = weaviate_client)\n", + "/tmp/ipykernel_5986/1667841775.py:7: ResourceWarning: unclosed \n", + " db = WeaviateVectorStore.from_documents(docs, embeddings, client=weaviate_client)\n", "ResourceWarning: Enable tracemalloc to get the object allocation traceback\n" ] }, @@ -343,7 +352,7 @@ "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={'source': 'state_of_the_union.txt'})" ] }, - "execution_count": 9, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } @@ -395,7 +404,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 9, "id": "8d365855", "metadata": {}, "outputs": [ @@ -403,7 +412,7 @@ "name": "stderr", "output_type": "stream", "text": [ - "2024-Jan-30 09:51 AM - langchain_weaviate.vectorstores - INFO - Tenant Foo does not exist in index LangChain_a1b81358def44ba9b0f167698e09ca2c. Creating tenant.\n" + "2024-Apr-05 12:55 PM - langchain_weaviate.vectorstores - INFO - Tenant Foo does not exist in index LangChain_ac00d9821c9840f6b8a0a809070888e1. Creating tenant.\n" ] } ], @@ -415,7 +424,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 10, "id": "49659eb3", "metadata": {}, "outputs": [ @@ -424,11 +433,11 @@ "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={'source': 'state_of_the_union.txt'}),\n", " Document(page_content='And so many families are living paycheck to paycheck, struggling to keep up with the rising cost of food, gas, housing, and so much more. \\n\\nI understand. \\n\\nI remember when my Dad had to leave our home in Scranton, Pennsylvania to find work. I grew up in a family where if the price of food went up, you felt it. \\n\\nThat’s why one of the first things I did as President was fight to pass the American Rescue Plan. \\n\\nBecause people were hurting. We needed to act, and we did. \\n\\nFew pieces of legislation have done more in a critical moment in our history to lift us out of crisis. \\n\\nIt fueled our efforts to vaccinate the nation and combat COVID-19. It delivered immediate economic relief for tens of millions of Americans. \\n\\nHelped put food on their table, keep a roof over their heads, and cut the cost of health insurance. \\n\\nAnd as my Dad used to say, it gave people a little breathing room.', metadata={'source': 'state_of_the_union.txt'}),\n", - " Document(page_content='Putin’s latest attack on Ukraine was premeditated and unprovoked. \\n\\nHe rejected repeated efforts at diplomacy. \\n\\nHe thought the West and NATO wouldn’t respond. And he thought he could divide us at home. Putin was wrong. We were ready. Here is what we did. \\n\\nWe prepared extensively and carefully. \\n\\nWe spent months building a coalition of other freedom-loving nations from Europe and the Americas to Asia and Africa to confront Putin. \\n\\nI spent countless hours unifying our European allies. We shared with the world in advance what we knew Putin was planning and precisely how he would try to falsely justify his aggression. \\n\\nWe countered Russia’s lies with truth. \\n\\nAnd now that he has acted the free world is holding him accountable. \\n\\nAlong with twenty-seven members of the European Union including France, Germany, Italy, as well as countries like the United Kingdom, Canada, Japan, Korea, Australia, New Zealand, and many others, even Switzerland.', metadata={'source': 'state_of_the_union.txt'}),\n", - " Document(page_content='He and his Dad both have Type 1 diabetes, which means they need insulin every day. Insulin costs about $10 a vial to make. \\n\\nBut drug companies charge families like Joshua and his Dad up to 30 times more. I spoke with Joshua’s mom. \\n\\nImagine what it’s like to look at your child who needs insulin and have no idea how you’re going to pay for it. \\n\\nWhat it does to your dignity, your ability to look your child in the eye, to be the parent you expect to be. \\n\\nJoshua is here with us tonight. Yesterday was his birthday. Happy birthday, buddy. \\n\\nFor Joshua, and for the 200,000 other young people with Type 1 diabetes, let’s cap the cost of insulin at $35 a month so everyone can afford it. \\n\\nDrug companies will still do very well. And while we’re at it let Medicare negotiate lower prices for prescription drugs, like the VA already does.', metadata={'source': 'state_of_the_union.txt'})]" + " Document(page_content='He and his Dad both have Type 1 diabetes, which means they need insulin every day. Insulin costs about $10 a vial to make. \\n\\nBut drug companies charge families like Joshua and his Dad up to 30 times more. I spoke with Joshua’s mom. \\n\\nImagine what it’s like to look at your child who needs insulin and have no idea how you’re going to pay for it. \\n\\nWhat it does to your dignity, your ability to look your child in the eye, to be the parent you expect to be. \\n\\nJoshua is here with us tonight. Yesterday was his birthday. Happy birthday, buddy. \\n\\nFor Joshua, and for the 200,000 other young people with Type 1 diabetes, let’s cap the cost of insulin at $35 a month so everyone can afford it. \\n\\nDrug companies will still do very well. And while we’re at it let Medicare negotiate lower prices for prescription drugs, like the VA already does.', metadata={'source': 'state_of_the_union.txt'}),\n", + " Document(page_content='Putin’s latest attack on Ukraine was premeditated and unprovoked. \\n\\nHe rejected repeated efforts at diplomacy. \\n\\nHe thought the West and NATO wouldn’t respond. And he thought he could divide us at home. Putin was wrong. We were ready. Here is what we did. \\n\\nWe prepared extensively and carefully. \\n\\nWe spent months building a coalition of other freedom-loving nations from Europe and the Americas to Asia and Africa to confront Putin. \\n\\nI spent countless hours unifying our European allies. We shared with the world in advance what we knew Putin was planning and precisely how he would try to falsely justify his aggression. \\n\\nWe countered Russia’s lies with truth. \\n\\nAnd now that he has acted the free world is holding him accountable. \\n\\nAlong with twenty-seven members of the European Union including France, Germany, Italy, as well as countries like the United Kingdom, Canada, Japan, Korea, Australia, New Zealand, and many others, even Switzerland.', metadata={'source': 'state_of_the_union.txt'})]" ] }, - "execution_count": 11, + "execution_count": 10, "metadata": {}, "output_type": "execute_result" } @@ -471,7 +480,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 11, "id": "cb302651", "metadata": {}, "outputs": [ @@ -479,10 +488,8 @@ "name": "stderr", "output_type": "stream", "text": [ - "/workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages/langchain_community/embeddings/openai.py:500: PydanticDeprecatedSince20: The `dict` method is deprecated; use `model_dump` instead. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.5/migration/\n", - " response = response.dict()\n", - "/workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages/pydantic/main.py:979: PydanticDeprecatedSince20: The `dict` method is deprecated; use `model_dump` instead. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.5/migration/\n", - " warnings.warn('The `dict` method is deprecated; use `model_dump` instead.', DeprecationWarning)\n" + "/workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages/pydantic/main.py:1024: PydanticDeprecatedSince20: The `dict` method is deprecated; use `model_dump` instead. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.6/migration/\n", + " warnings.warn('The `dict` method is deprecated; use `model_dump` instead.', category=PydanticDeprecatedSince20)\n" ] }, { @@ -491,7 +498,7 @@ "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={'source': 'state_of_the_union.txt'})" ] }, - "execution_count": 12, + "execution_count": 11, "metadata": {}, "output_type": "execute_result" } @@ -519,7 +526,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 12, "id": "f74e20d8", "metadata": {}, "outputs": [ @@ -531,19 +538,17 @@ " warn_deprecated(\n", "/workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages/langchain_core/_api/deprecation.py:117: LangChainDeprecationWarning: The function `predict` was deprecated in LangChain 0.1.7 and will be removed in 0.2.0. Use invoke instead.\n", " warn_deprecated(\n", - "/workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages/langchain_community/chat_models/openai.py:458: PydanticDeprecatedSince20: The `dict` method is deprecated; use `model_dump` instead. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.5/migration/\n", - " response = response.dict()\n", - "/workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages/pydantic/main.py:979: PydanticDeprecatedSince20: The `dict` method is deprecated; use `model_dump` instead. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.5/migration/\n", - " warnings.warn('The `dict` method is deprecated; use `model_dump` instead.', DeprecationWarning)\n" + "/workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages/pydantic/main.py:1024: PydanticDeprecatedSince20: The `dict` method is deprecated; use `model_dump` instead. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.6/migration/\n", + " warnings.warn('The `dict` method is deprecated; use `model_dump` instead.', category=PydanticDeprecatedSince20)\n" ] }, { "data": { "text/plain": [ - "\"As an AI language model, I don't have real-time information or the ability to browse the internet. Therefore, I cannot provide you with the most recent statements made by the president about Justice Breyer. However, it's worth noting that the president's opinions on Justice Breyer may vary depending on the specific context and time period you are referring to.\"" + "\"I'm sorry, but I cannot provide real-time information as my responses are generated based on a mixture of licensed data, data created by human trainers, and publicly available data.\"" ] }, - "execution_count": 13, + "execution_count": 12, "metadata": {}, "output_type": "execute_result" } @@ -573,7 +578,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 13, "id": "ad91ded1", "metadata": {}, "outputs": [], @@ -584,7 +589,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 14, "id": "2438d702", "metadata": {}, "outputs": [], @@ -597,21 +602,10 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 15, "id": "b0e106ab", "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages/langchain_community/embeddings/openai.py:500: PydanticDeprecatedSince20: The `dict` method is deprecated; use `model_dump` instead. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.5/migration/\n", - " response = response.dict()\n", - "/workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages/weaviate/warnings.py:215: DeprecationWarning: Dep015: You are creating a batch using client.batch or collection.batch, which is deprecated. Use X.batch._batch_mode_().\n", - " warnings.warn(\n" - ] - } - ], + "outputs": [], "source": [ "docsearch = WeaviateVectorStore.from_texts(\n", " texts,\n", @@ -623,7 +617,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 16, "id": "86bbb953", "metadata": {}, "outputs": [ @@ -644,7 +638,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 17, "id": "5c38cc39", "metadata": {}, "outputs": [ @@ -652,14 +646,10 @@ "name": "stderr", "output_type": "stream", "text": [ - "/workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages/langchain_community/embeddings/openai.py:500: PydanticDeprecatedSince20: The `dict` method is deprecated; use `model_dump` instead. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.5/migration/\n", - " response = response.dict()\n", - "/workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages/pydantic/main.py:979: PydanticDeprecatedSince20: The `dict` method is deprecated; use `model_dump` instead. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.5/migration/\n", - " warnings.warn('The `dict` method is deprecated; use `model_dump` instead.', DeprecationWarning)\n", - "/workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages/langchain_community/llms/openai.py:466: PydanticDeprecatedSince20: The `dict` method is deprecated; use `model_dump` instead. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.5/migration/\n", - " response = response.dict()\n", - "/workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages/pydantic/main.py:979: PydanticDeprecatedSince20: The `dict` method is deprecated; use `model_dump` instead. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.5/migration/\n", - " warnings.warn('The `dict` method is deprecated; use `model_dump` instead.', DeprecationWarning)\n" + "/workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages/pydantic/main.py:1024: PydanticDeprecatedSince20: The `dict` method is deprecated; use `model_dump` instead. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.6/migration/\n", + " warnings.warn('The `dict` method is deprecated; use `model_dump` instead.', category=PydanticDeprecatedSince20)\n", + "/workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages/pydantic/main.py:1024: PydanticDeprecatedSince20: The `dict` method is deprecated; use `model_dump` instead. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.6/migration/\n", + " warnings.warn('The `dict` method is deprecated; use `model_dump` instead.', category=PydanticDeprecatedSince20)\n" ] }, { @@ -669,7 +659,7 @@ " 'sources': '31-pl'}" ] }, - "execution_count": 18, + "execution_count": 17, "metadata": {}, "output_type": "execute_result" } @@ -691,7 +681,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 18, "id": "33b0a9d3", "metadata": {}, "outputs": [], @@ -704,18 +694,136 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 19, "id": "d2ade6ae", "metadata": {}, + "outputs": [], + "source": [ + "docsearch = WeaviateVectorStore.from_texts(\n", + " texts,\n", + " embeddings,\n", + " client=weaviate_client,\n", + " metadatas=[{\"source\": f\"{i}-pl\"} for i in range(len(texts))],\n", + ")\n", + "\n", + "retriever = docsearch.as_retriever()" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "578570b8", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "input_variables=['context', 'question'] messages=[HumanMessagePromptTemplate(prompt=PromptTemplate(input_variables=['context', 'question'], template=\"You are an assistant for question-answering tasks. Use the following pieces of retrieved context to answer the question. If you don't know the answer, just say that you don't know. Use three sentences maximum and keep the answer concise.\\nQuestion: {question} \\nContext: {context} \\nAnswer:\\n\"))]\n" + ] + } + ], + "source": [ + "from langchain_core.prompts import ChatPromptTemplate\n", + "\n", + "template = \"\"\"You are an assistant for question-answering tasks. Use the following pieces of retrieved context to answer the question. If you don't know the answer, just say that you don't know. Use three sentences maximum and keep the answer concise.\n", + "Question: {question} \n", + "Context: {context} \n", + "Answer:\n", + "\"\"\"\n", + "prompt = ChatPromptTemplate.from_template(template)\n", + "\n", + "print(prompt)" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "74982155", + "metadata": {}, + "outputs": [], + "source": [ + "from langchain_community.chat_models import ChatOpenAI\n", + "\n", + "llm = ChatOpenAI(model_name=\"gpt-3.5-turbo\", temperature=0)" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "fe129bdd", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages/pydantic/main.py:1024: PydanticDeprecatedSince20: The `dict` method is deprecated; use `model_dump` instead. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.6/migration/\n", + " warnings.warn('The `dict` method is deprecated; use `model_dump` instead.', category=PydanticDeprecatedSince20)\n", + "/workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages/pydantic/main.py:1024: PydanticDeprecatedSince20: The `dict` method is deprecated; use `model_dump` instead. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.6/migration/\n", + " warnings.warn('The `dict` method is deprecated; use `model_dump` instead.', category=PydanticDeprecatedSince20)\n" + ] + }, + { + "data": { + "text/plain": [ + "\"The president honored Justice Breyer for his service to the country as an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court. The president also mentioned nominating Judge Ketanji Brown Jackson to continue Justice Breyer's legacy of excellence. The president highlighted the importance of nominating someone to serve on the United States Supreme Court as one of the most serious constitutional responsibilities.\"" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from langchain_core.output_parsers import StrOutputParser\n", + "from langchain_core.runnables import RunnablePassthrough\n", + "\n", + "rag_chain = (\n", + " {\"context\": retriever, \"question\": RunnablePassthrough()}\n", + " | prompt\n", + " | llm\n", + " | StrOutputParser()\n", + ")\n", + "\n", + "rag_chain.invoke(\"What did the president say about Justice Breyer\")" + ] + }, + { + "cell_type": "markdown", + "id": "6073d733", + "metadata": {}, + "source": [ + "### Retrieval-Augmented Generation on a specific set of metadata\n", + "\n", + "To perform a query on a specific set of metadata, include filters as an argument in the `as_retriever()` method, specifying the desired search criteria within the `search_kwargs` parameter. Here's an example below" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "id": "c1125a21", + "metadata": {}, + "outputs": [], + "source": [ + "with open(\"state_of_the_union.txt\") as f:\n", + " state_of_the_union = f.read()\n", + "text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)\n", + "texts = text_splitter.split_text(state_of_the_union)" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "id": "798524e4", + "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ - "/workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages/langchain_community/embeddings/openai.py:500: PydanticDeprecatedSince20: The `dict` method is deprecated; use `model_dump` instead. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.5/migration/\n", - " response = response.dict()\n", - "/workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages/weaviate/warnings.py:215: DeprecationWarning: Dep015: You are creating a batch using client.batch or collection.batch, which is deprecated. Use X.batch._batch_mode_().\n", - " warnings.warn(\n" + "/workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages/pydantic/main.py:1024: PydanticDeprecatedSince20: The `dict` method is deprecated; use `model_dump` instead. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.6/migration/\n", + " warnings.warn('The `dict` method is deprecated; use `model_dump` instead.', category=PydanticDeprecatedSince20)\n" ] } ], @@ -727,13 +835,14 @@ " metadatas=[{\"source\": f\"{i}-pl\"} for i in range(len(texts))],\n", ")\n", "\n", - "retriever = docsearch.as_retriever()" + "search_filter = weaviate.classes.query.Filter.by_property(\"source\").equal(\"0-pl\")\n", + "retriever = docsearch.as_retriever(search_kwargs={\"filters\": search_filter})\n" ] }, { "cell_type": "code", - "execution_count": 21, - "id": "578570b8", + "execution_count": 25, + "id": "83edb690", "metadata": {}, "outputs": [ { @@ -759,51 +868,130 @@ }, { "cell_type": "code", - "execution_count": 22, - "id": "74982155", + "execution_count": 26, + "id": "2c8ff70b", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages/pydantic/main.py:1024: PydanticDeprecatedSince20: The `dict` method is deprecated; use `model_dump` instead. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.6/migration/\n", + " warnings.warn('The `dict` method is deprecated; use `model_dump` instead.', category=PydanticDeprecatedSince20)\n", + "/workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages/pydantic/main.py:1024: PydanticDeprecatedSince20: The `dict` method is deprecated; use `model_dump` instead. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.6/migration/\n", + " warnings.warn('The `dict` method is deprecated; use `model_dump` instead.', category=PydanticDeprecatedSince20)\n" + ] + }, + { + "data": { + "text/plain": [ + "\"The text is about a speech given by a political figure addressing a diverse audience, emphasizing unity as Americans and the importance of upholding the Constitution. It also highlights the recent events involving Russia's invasion of Ukraine and praises the bravery and determination of the Ukrainian people.\"" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "from langchain_community.chat_models import ChatOpenAI\n", + "from langchain_core.output_parsers import StrOutputParser\n", + "from langchain_core.runnables import RunnablePassthrough\n", "\n", - "llm = ChatOpenAI(model_name=\"gpt-3.5-turbo\", temperature=0)" + "\n", + "llm = ChatOpenAI(model_name=\"gpt-3.5-turbo\", temperature=0)\n", + "\n", + "rag_chain = (\n", + " {\"context\": retriever, \"question\": RunnablePassthrough()}\n", + " | prompt\n", + " | llm\n", + " | StrOutputParser()\n", + ")\n", + "\n", + "rag_chain.invoke(\"What is this text about?\")" + ] + }, + { + "cell_type": "markdown", + "id": "477229eb", + "metadata": {}, + "source": [ + " Let's try another filter. " ] }, { "cell_type": "code", - "execution_count": 23, - "id": "fe129bdd", + "execution_count": 27, + "id": "9681423d", + "metadata": {}, + "outputs": [], + "source": [ + "search_filter = weaviate.classes.query.Filter.by_property(\"source\").equal(\"15-pl\")\n", + "retriever = docsearch.as_retriever(search_kwargs={\"filters\": search_filter})" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "8153c605", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "input_variables=['context', 'question'] messages=[HumanMessagePromptTemplate(prompt=PromptTemplate(input_variables=['context', 'question'], template=\"You are an assistant for question-answering tasks. Use the following pieces of retrieved context to answer the question. If you don't know the answer, just say that you don't know. Use three sentences maximum and keep the answer concise.\\nQuestion: {question} \\nContext: {context} \\nAnswer:\\n\"))]\n" + ] + } + ], + "source": [ + "from langchain_core.prompts import ChatPromptTemplate\n", + "\n", + "template = \"\"\"You are an assistant for question-answering tasks. Use the following pieces of retrieved context to answer the question. If you don't know the answer, just say that you don't know. Use three sentences maximum and keep the answer concise.\n", + "Question: {question} \n", + "Context: {context} \n", + "Answer:\n", + "\"\"\"\n", + "prompt = ChatPromptTemplate.from_template(template)\n", + "\n", + "print(prompt)" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "id": "3fe9e493", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ - "/workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages/langchain_community/embeddings/openai.py:500: PydanticDeprecatedSince20: The `dict` method is deprecated; use `model_dump` instead. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.5/migration/\n", - " response = response.dict()\n", - "/workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages/pydantic/main.py:979: PydanticDeprecatedSince20: The `dict` method is deprecated; use `model_dump` instead. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.5/migration/\n", - " warnings.warn('The `dict` method is deprecated; use `model_dump` instead.', DeprecationWarning)\n", - "/workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages/langchain_community/chat_models/openai.py:458: PydanticDeprecatedSince20: The `dict` method is deprecated; use `model_dump` instead. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.5/migration/\n", - " response = response.dict()\n", - "/workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages/pydantic/main.py:979: PydanticDeprecatedSince20: The `dict` method is deprecated; use `model_dump` instead. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.5/migration/\n", - " warnings.warn('The `dict` method is deprecated; use `model_dump` instead.', DeprecationWarning)\n" + "/workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages/pydantic/main.py:1024: PydanticDeprecatedSince20: The `dict` method is deprecated; use `model_dump` instead. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.6/migration/\n", + " warnings.warn('The `dict` method is deprecated; use `model_dump` instead.', category=PydanticDeprecatedSince20)\n", + "/workspaces/langchain-weaviate/.venv/lib/python3.12/site-packages/pydantic/main.py:1024: PydanticDeprecatedSince20: The `dict` method is deprecated; use `model_dump` instead. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.6/migration/\n", + " warnings.warn('The `dict` method is deprecated; use `model_dump` instead.', category=PydanticDeprecatedSince20)\n" ] }, { "data": { "text/plain": [ - "'The president thanked Justice Breyer for his service and referred to him as an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court.\\n'" + "\"The text is about the impact of inflation on families despite the bright spots in the economy, such as job growth and higher wages. It discusses how the pandemic affected businesses' ability to hire workers and disrupted global supply chains, leading to price increases. The author emphasizes the importance of controlling prices to address the challenges faced by families.\"" ] }, - "execution_count": 23, + "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ + "from langchain_community.chat_models import ChatOpenAI\n", "from langchain_core.output_parsers import StrOutputParser\n", "from langchain_core.runnables import RunnablePassthrough\n", "\n", + "\n", + "llm = ChatOpenAI(model_name=\"gpt-3.5-turbo\", temperature=0)\n", + "\n", "rag_chain = (\n", " {\"context\": retriever, \"question\": RunnablePassthrough()}\n", " | prompt\n", @@ -811,7 +999,15 @@ " | StrOutputParser()\n", ")\n", "\n", - "rag_chain.invoke(\"What did the president say about Justice Breyer\")" + "rag_chain.invoke(\"What is this text about?\")" + ] + }, + { + "cell_type": "markdown", + "id": "45d93578", + "metadata": {}, + "source": [ + "You can always mix and match the query filters. For more detailed information, please refer to the documentation [Here](https://weaviate.io/developers/weaviate/search)" ] } ], @@ -831,7 +1027,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.12.0" + "version": "3.12.2" } }, "nbformat": 4, diff --git a/poetry.lock b/poetry.lock index 8d15784..fba79cf 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1,4 +1,4 @@ -# This file is automatically @generated by Poetry 1.7.1 and should not be changed by hand. +# This file is automatically @generated by Poetry 1.8.2 and should not be changed by hand. [[package]] name = "aiohttp" @@ -1081,13 +1081,13 @@ files = [ [[package]] name = "ipykernel" -version = "6.29.0" +version = "6.29.4" description = "IPython Kernel for Jupyter" optional = false python-versions = ">=3.8" files = [ - {file = "ipykernel-6.29.0-py3-none-any.whl", hash = "sha256:076663ca68492576f051e4af7720d33f34383e655f2be0d544c8b1c9de915b2f"}, - {file = "ipykernel-6.29.0.tar.gz", hash = "sha256:b5dd3013cab7b330df712891c96cd1ab868c27a7159e606f762015e9bf8ceb3f"}, + {file = "ipykernel-6.29.4-py3-none-any.whl", hash = "sha256:1181e653d95c6808039c509ef8e67c4126b3b3af7781496c7cbfb5ed938a27da"}, + {file = "ipykernel-6.29.4.tar.gz", hash = "sha256:3d44070060f9475ac2092b760123fadf105d2e2493c24848b6691a7c4f42af5c"}, ] [package.dependencies] @@ -1110,7 +1110,7 @@ cov = ["coverage[toml]", "curio", "matplotlib", "pytest-cov", "trio"] docs = ["myst-parser", "pydata-sphinx-theme", "sphinx", "sphinx-autodoc-typehints", "sphinxcontrib-github-alt", "sphinxcontrib-spelling", "trio"] pyqt5 = ["pyqt5"] pyside6 = ["pyside6"] -test = ["flaky", "ipyparallel", "pre-commit", "pytest (>=7.0)", "pytest-asyncio (==0.23.2)", "pytest-cov", "pytest-timeout"] +test = ["flaky", "ipyparallel", "pre-commit", "pytest (>=7.0)", "pytest-asyncio (>=0.23.5)", "pytest-cov", "pytest-timeout"] [[package]] name = "ipython" @@ -2968,4 +2968,4 @@ testing = ["big-O", "jaraco.functools", "jaraco.itertools", "more-itertools", "p [metadata] lock-version = "2.0" python-versions = ">=3.9,<4.0" -content-hash = "bf4dcd99c2b117635eda34d5b4f567a726fdcc8948ca7fc7f49ed97a7e87990e" +content-hash = "d7dc5a518050215f5cbecf06096e3368c122ba4d88244b19ac8cc4c8c7272114" diff --git a/pyproject.toml b/pyproject.toml index ced6e8b..fa11a3c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -11,6 +11,7 @@ langchain-core = ">=0.0.12" weaviate-client = {version = "^4.0.0b", allow-prereleases = true} numpy = "^1.26.2" simsimd = "^3.6.1" +ipykernel = "^6.29.4" [tool.poetry.group.test] optional = true diff --git a/tests/docker-compose.yml b/tests/docker-compose.yml index 1ae2795..747aeb4 100644 --- a/tests/docker-compose.yml +++ b/tests/docker-compose.yml @@ -9,7 +9,7 @@ services: - '8080' - --scheme - http - image: semitechnologies/weaviate:1.23.6 + image: semitechnologies/weaviate:1.24.6 ports: - "8080" - "50051"