{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import chromadb\n", "\n", "client = chromadb.Client()\n", "collection = client.create_collection(name=\"my_collection\")" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "collection.add(\n", " documents=[\n", " \"This document is about New York\",\n", " \"This document is about Delhi\"\n", " ],\n", " ids = ['id1', 'id2']\n", ")" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'ids': ['id1', 'id2'],\n", " 'embeddings': None,\n", " 'metadatas': [None, None],\n", " 'documents': ['This document is about New York',\n", " 'This document is about Delhi'],\n", " 'uris': None,\n", " 'data': None}" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "all_docs = collection.get()\n", "all_docs" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'ids': ['id1'],\n", " 'embeddings': None,\n", " 'metadatas': [None],\n", " 'documents': ['This document is about New York'],\n", " 'uris': None,\n", " 'data': None}" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "documents = collection.get(ids=[\"id1\"])\n", "documents" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'ids': [['id1', 'id2']],\n", " 'distances': [[1.6312181949615479, 1.867558479309082]],\n", " 'metadatas': [[None, None]],\n", " 'embeddings': None,\n", " 'documents': [['This document is about New York',\n", " 'This document is about Delhi']],\n", " 'uris': None,\n", " 'data': None}" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "results = collection.query(\n", " query_texts=['Query is about big apple'],\n", " n_results=2\n", ")\n", "results" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'ids': [],\n", " 'embeddings': None,\n", " 'metadatas': [],\n", " 'documents': [],\n", " 'uris': None,\n", " 'data': None}" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "collection.delete(ids=all_docs['ids'])\n", "collection.get()" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "collection.add(\n", " documents=[\n", " \"This document is about New York\",\n", " \"This document is about Delhi\"\n", " ],\n", " ids=[\"id3\", \"id4\"],\n", " metadatas=[\n", " {\"url\": \"https://en.wikipedia.org/wiki/New_York_City\"},\n", " {\"url\": \"https://en.wikipedia.org/wiki/New_Delhi\"}\n", " ]\n", ")" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'ids': [['id4', 'id3']],\n", " 'distances': [[1.5588479042053223, 1.8114912509918213]],\n", " 'metadatas': [[{'url': 'https://en.wikipedia.org/wiki/New_Delhi'},\n", " {'url': 'https://en.wikipedia.org/wiki/New_York_City'}]],\n", " 'embeddings': None,\n", " 'documents': [['This document is about Delhi',\n", " 'This document is about New York']],\n", " 'uris': None,\n", " 'data': None}" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "results = collection.query(\n", " query_texts=[\"Query is about Chhole Bhature\"],\n", " n_results=2\n", ")\n", "results" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "venv", "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.12.3" } }, "nbformat": 4, "nbformat_minor": 2 }