diff --git a/v2.5.x/assets/attu_after_data_insertion_1.png b/v2.5.x/assets/attu_after_data_insertion_1.png new file mode 100644 index 000000000..838d04388 Binary files /dev/null and b/v2.5.x/assets/attu_after_data_insertion_1.png differ diff --git a/v2.5.x/assets/attu_after_data_insertion_2.png b/v2.5.x/assets/attu_after_data_insertion_2.png new file mode 100644 index 000000000..84b9f4e7a Binary files /dev/null and b/v2.5.x/assets/attu_after_data_insertion_2.png differ diff --git a/v2.5.x/assets/attu_expanded_searched_graph.png b/v2.5.x/assets/attu_expanded_searched_graph.png new file mode 100644 index 000000000..a1364d785 Binary files /dev/null and b/v2.5.x/assets/attu_expanded_searched_graph.png differ diff --git a/v2.5.x/assets/attu_login_page.png b/v2.5.x/assets/attu_login_page.png new file mode 100644 index 000000000..c02b74322 Binary files /dev/null and b/v2.5.x/assets/attu_login_page.png differ diff --git a/v2.5.x/assets/attu_searched_graph.png b/v2.5.x/assets/attu_searched_graph.png new file mode 100644 index 000000000..a8e377b25 Binary files /dev/null and b/v2.5.x/assets/attu_searched_graph.png differ diff --git a/v2.5.x/assets/attu_searched_table.png b/v2.5.x/assets/attu_searched_table.png new file mode 100644 index 000000000..a1dcd56ad Binary files /dev/null and b/v2.5.x/assets/attu_searched_table.png differ diff --git a/v2.5.x/site/en/getstarted/run-milvus-docker/install_standalone-windows.md b/v2.5.x/site/en/getstarted/run-milvus-docker/install_standalone-windows.md index 0b2f026cc..d20f8474a 100644 --- a/v2.5.x/site/en/getstarted/run-milvus-docker/install_standalone-windows.md +++ b/v2.5.x/site/en/getstarted/run-milvus-docker/install_standalone-windows.md @@ -31,7 +31,7 @@ If you are more familiar with PowerShell or Windows Command Prompt, the command 2. Download the installation script and save it as `standalone.bat`.​ ```powershell - C:\>Invoke-WebRequest https://github.com/milvus-io/milvus/blob/master/scripts/standalone_embed.bat -OutFile standalone.bat​ + C:\>Invoke-WebRequest https://raw.githubusercontent.com/milvus-io/milvus/refs/heads/master/scripts/standalone_embed.bat -OutFile standalone.bat​ ``` diff --git a/v2.5.x/site/en/integrations/build_RAG_with_milvus_and_deepseek.md b/v2.5.x/site/en/integrations/build_RAG_with_milvus_and_deepseek.md new file mode 100644 index 000000000..5c14096e2 --- /dev/null +++ b/v2.5.x/site/en/integrations/build_RAG_with_milvus_and_deepseek.md @@ -0,0 +1,284 @@ +--- +id: build_RAG_with_milvus_and_deepseek.md +summary: In this tutorial, we’ll show you how to build a Retrieval-Augmented Generation (RAG) pipeline using Milvus and DeepSeek. +title: Build RAG with Milvus and DeepSeek +--- + +# Build RAG with Milvus and DeepSeek + +Open In Colab +GitHub Repository + +[DeepSeek](https://www.deepseek.com/) enables developers to build and scale AI applications with high-performance language models. It offers efficient inference, flexible APIs, and advanced Mixture-of-Experts (MoE) architectures for robust reasoning and retrieval tasks. + +In this tutorial, we’ll show you how to build a Retrieval-Augmented Generation (RAG) pipeline using Milvus and DeepSeek. + + + +## Preparation +### Dependencies and Environment + + +```python +! pip install --upgrade pymilvus[model] openai requests tqdm +``` + +> If you are using Google Colab, to enable dependencies just installed, you may need to **restart the runtime** (click on the "Runtime" menu at the top of the screen, and select "Restart session" from the dropdown menu). + +DeepSeek enables the OpenAI-style API. You can login to its official website and prepare the [api key](https://platform.deepseek.com/api_keys) `DEEPSEEK_API_KEY` as an environment variable. + + +```python +import os + +os.environ["DEEPSEEK_API_KEY"] = "***********" +``` + +### Prepare the data + +We use the FAQ pages from the [Milvus Documentation 2.4.x](https://github.com/milvus-io/milvus-docs/releases/download/v2.4.6-preview/milvus_docs_2.4.x_en.zip) as the private knowledge in our RAG, which is a good data source for a simple RAG pipeline. + +Download the zip file and extract documents to the folder `milvus_docs`. + + +```python +! wget https://github.com/milvus-io/milvus-docs/releases/download/v2.4.6-preview/milvus_docs_2.4.x_en.zip +! unzip -q milvus_docs_2.4.x_en.zip -d milvus_docs +``` + +We load all markdown files from the folder `milvus_docs/en/faq`. For each document, we just simply use "# " to separate the content in the file, which can roughly separate the content of each main part of the markdown file. + + +```python +from glob import glob + +text_lines = [] + +for file_path in glob("milvus_docs/en/faq/*.md", recursive=True): + with open(file_path, "r") as file: + file_text = file.read() + + text_lines += file_text.split("# ") +``` + +### Prepare the LLM and Embedding Model + +DeepSeek enables the OpenAI-style API, and you can use the same API with minor adjustments to call the LLM. + + +```python +from openai import OpenAI + +deepseek_client = OpenAI( + api_key=os.environ["DEEPSEEK_API_KEY"], + base_url="https://api.deepseek.com", +) +``` + +Define a embedding model to generate text embeddings using the `milvus_model`. We use the `DefaultEmbeddingFunction` model as an example, which is a pre-trained and lightweight embedding model. + + +```python +from pymilvus import model as milvus_model + +embedding_model = milvus_model.DefaultEmbeddingFunction() +``` + +Generate a test embedding and print its dimension and first few elements. + + +```python +test_embedding = embedding_model.encode_queries(["This is a test"])[0] +embedding_dim = len(test_embedding) +print(embedding_dim) +print(test_embedding[:10]) +``` + + 768 + [-0.04836066 0.07163023 -0.01130064 -0.03789345 -0.03320649 -0.01318448 + -0.03041712 -0.02269499 -0.02317863 -0.00426028] + + +## Load data into Milvus + +### Create the Collection + + +```python +from pymilvus import MilvusClient + +milvus_client = MilvusClient(uri="./milvus_demo.db") + +collection_name = "my_rag_collection" +``` + +> As for the argument of `MilvusClient`: +> - Setting the `uri` as a local file, e.g.`./milvus.db`, is the most convenient method, as it automatically utilizes [Milvus Lite](https://milvus.io/docs/milvus_lite.md) to store all data in this file. +> - If you have large scale of data, you can set up a more performant Milvus server on [docker or kubernetes](https://milvus.io/docs/quickstart.md). In this setup, please use the server uri, e.g.`http://localhost:19530`, as your `uri`. +> - If you want to use [Zilliz Cloud](https://zilliz.com/cloud), the fully managed cloud service for Milvus, adjust the `uri` and `token`, which correspond to the [Public Endpoint and Api key](https://docs.zilliz.com/docs/on-zilliz-cloud-console#free-cluster-details) in Zilliz Cloud. + +Check if the collection already exists and drop it if it does. + + +```python +if milvus_client.has_collection(collection_name): + milvus_client.drop_collection(collection_name) +``` + +Create a new collection with specified parameters. + +If we don't specify any field information, Milvus will automatically create a default `id` field for primary key, and a `vector` field to store the vector data. A reserved JSON field is used to store non-schema-defined fields and their values. + + +```python +milvus_client.create_collection( + collection_name=collection_name, + dimension=embedding_dim, + metric_type="IP", # Inner product distance + consistency_level="Strong", # Strong consistency level +) +``` + +### Insert data +Iterate through the text lines, create embeddings, and then insert the data into Milvus. + +Here is a new field `text`, which is a non-defined field in the collection schema. It will be automatically added to the reserved JSON dynamic field, which can be treated as a normal field at a high level. + + +```python +from tqdm import tqdm + +data = [] + +doc_embeddings = embedding_model.encode_documents(text_lines) + +for i, line in enumerate(tqdm(text_lines, desc="Creating embeddings")): + data.append({"id": i, "vector": doc_embeddings[i], "text": line}) + +milvus_client.insert(collection_name=collection_name, data=data) +``` + + Creating embeddings: 0%| | 0/72 [00:00 tags to provide an answer to the question enclosed in tags. + +{context} + + +{question} + +""" +``` + +Use the `deepseek-chat` model provided by DeepSeek to generate a response based on the prompts. + + +```python +response = deepseek_client.chat.completions.create( + model="deepseek-chat", + messages=[ + {"role": "system", "content": SYSTEM_PROMPT}, + {"role": "user", "content": USER_PROMPT}, + ], +) +print(response.choices[0].message.content) +``` + + In Milvus, data is stored in two main categories: inserted data and metadata. + + 1. **Inserted Data**: This includes vector data, scalar data, and collection-specific schema. The inserted data is stored in persistent storage as incremental logs. Milvus supports various object storage backends for this purpose, such as MinIO, AWS S3, Google Cloud Storage (GCS), Azure Blob Storage, Alibaba Cloud OSS, and Tencent Cloud Object Storage (COS). + + 2. **Metadata**: Metadata is generated within Milvus and is specific to each Milvus module. This metadata is stored in etcd, a distributed key-value store. + + Additionally, when data is inserted, it is first loaded into a message queue, and Milvus returns success at this stage. The data is then written to persistent storage as incremental logs by the data node. If the `flush()` function is called, the data node is forced to write all data in the message queue to persistent storage immediately. + + +Great! We have successfully built a RAG pipeline with Milvus and DeepSeek. + + diff --git a/v2.5.x/site/en/integrations/integrate_with_camel.md b/v2.5.x/site/en/integrations/integrate_with_camel.md index 8975f72de..58a3ab3fb 100644 --- a/v2.5.x/site/en/integrations/integrate_with_camel.md +++ b/v2.5.x/site/en/integrations/integrate_with_camel.md @@ -1,7 +1,7 @@ --- id: integrate_with_camel.md -summary: This guide demonstrates how to use an open-source embedding model and large-language model on BentoCloud with Milvus vector database to build a Retrieval Augmented Generation (RAG) application. -title: Retrieval-Augmented Generation (RAG) with Milvus and BentoML +summary: This guide demonstrates how to build a Retrieval-Augmented Generation (RAG) system using CAMEL and Milvus. +title: Retrieval-Augmented Generation (RAG) with Milvus and Camel --- # Retrieval-Augmented Generation (RAG) with Milvus and Camel diff --git a/v2.5.x/site/en/integrations/integrate_with_langfuse.md b/v2.5.x/site/en/integrations/integrate_with_langfuse.md index 1f91fce58..9044e06ef 100644 --- a/v2.5.x/site/en/integrations/integrate_with_langfuse.md +++ b/v2.5.x/site/en/integrations/integrate_with_langfuse.md @@ -1,16 +1,20 @@ --- id: integrate_with_langfuse.md summary: This is a simple cookbook that demonstrates how to use the LlamaIndex Langfuse integration. It uses Milvus Lite to store the documents and Query. -title: Cookbook LlamaIndex & Milvus Integration +title: Using Langfuse to Evaluate RAG Quality --- -# Cookbook - LlamaIndex & Milvus Integration +# Using Langfuse to Trace Queries in RAG Open In Colab -This is a simple cookbook that demonstrates how to use the [LlamaIndex Langfuse integration](https://langfuse.com/docs/integrations/llama-index/get-started). It uses Milvus Lite to store the documents and Query. +This is a simple cookbook that demonstrates how to use Langfuse to trace your queries in RAG. The RAG pipeline is implemented with LlamaIndex and Milvus Lite to store and retrieve the documents. + +In this quickstart, we’ll show you how to set up a LlamaIndex application using Milvus Lite as the vector store. We’ll also show you how to use the Langfuse LlamaIndex integration to trace your application. + +[Langfuse](https://github.com/langfuse/langfuse) is an open-source LLM engineering platform that helps teams collaboratively debug, analyze, and iterate on their LLM applications. All platform features are natively integrated to accelerate the development workflow. [Milvus Lite](https://github.com/milvus-io/milvus-lite/) is the lightweight version of Milvus, an open-source vector database that powers AI applications with vector embeddings and similarity search. diff --git a/v2.5.x/site/en/integrations/integrations_overview.md b/v2.5.x/site/en/integrations/integrations_overview.md index 2082eee43..f394b8935 100644 --- a/v2.5.x/site/en/integrations/integrations_overview.md +++ b/v2.5.x/site/en/integrations/integrations_overview.md @@ -59,3 +59,4 @@ This page provides a list of tutorials for you to interact with Milvus and third | [Build RAG with Milvus and Gemini](build_RAG_with_milvus_and_gemini.md) | LLMs | Milvus, Gemini | | [Build RAG with Milvus and Ollama](build_RAG_with_milvus_and_ollama.md) | LLMs | Milvus, Ollama | | [Getting Started with Dynamiq and Milvus](milvus_rag_with_dynamiq.md) | Orchestration | Milvus, Dynamiq | +| [Build RAG with Milvus and DeepSeek](build_RAG_with_milvus_and_deepseek.md) | LLMs | Milvus, DeepSeek | diff --git a/v2.5.x/site/en/menuStructure/en.json b/v2.5.x/site/en/menuStructure/en.json index 85e2394b9..ffea3794b 100644 --- a/v2.5.x/site/en/menuStructure/en.json +++ b/v2.5.x/site/en/menuStructure/en.json @@ -1737,6 +1737,12 @@ "id": "build_RAG_with_milvus_and_ollama.md", "order": 7, "children": [] + }, + { + "label": "DeepSeek", + "id": "build_RAG_with_milvus_and_deepseek.md", + "order": 8, + "children": [] } ] }, @@ -2001,7 +2007,7 @@ "id": "tutorials-overview.md", "order": 0, "children": [] - }, + }, { "label": "Build RAG with Milvus", "id": "build-rag-with-milvus.md", @@ -2013,7 +2019,7 @@ "id": "how_to_enhance_your_rag.md", "order": 2, "children": [] - }, + }, { "label": "Full-Text Search with Milvus", "id": "full_text_search_with_milvus.md", @@ -2031,7 +2037,7 @@ "id": "image_similarity_search.md", "order": 5, "children": [] - }, + }, { "label": "Multimodal RAG", "id": "multimodal_rag_with_milvus.md", @@ -2080,6 +2086,18 @@ "order": 13, "children": [] }, + { + "label": "Quickstart with Attu", + "id": "quickstart_with_attu.md", + "order": 14, + "children": [] + }, + { + "label": "Use AsyncMilvusClient with asyncio", + "id": "use-async-milvus-client-with-asyncio.md", + "order": 15, + "children": [] + }, { "label": "Explore More", "id": "explore-more", diff --git a/v2.5.x/site/en/tutorials/quickstart_with_attu.md b/v2.5.x/site/en/tutorials/quickstart_with_attu.md new file mode 100644 index 000000000..cebb2d095 --- /dev/null +++ b/v2.5.x/site/en/tutorials/quickstart_with_attu.md @@ -0,0 +1,194 @@ +--- +id: quickstart_with_attu.md +summary: Attu is an all-in-one, open-source administration tool for Milvus. It features an intuitive graphical user interface (GUI), allowing you to easily interact with your databases. With just a few clicks, you can visualize your cluster status, manage metadata, perform data queries, and much more. +title: Question Answering System +--- + +# Quick Start with Attu Desktop + +## 1. Introduction +[Attu](https://github.com/zilliztech/attu) is an all-in-one, open-source administration tool for Milvus. It features an intuitive graphical user interface (GUI), allowing you to easily interact with your databases. With just a few clicks, you can visualize your cluster status, manage metadata, perform data queries, and much more. + + + +--- + +## 2. Install Desktop Application +Download the desktop version of Attu by visiting the [Attu GitHub Releases page](https://github.com/zilliztech/attu/releases). Select the appropriate version for your operating system and follow the installation steps. + +### Note for macOS (M series chip): +If you encounter the error: +``` +attu.app is damaged and cannot be opened. +``` +Run the following command in terminal to bypass this issue: +``` +sudo xattr -rd com.apple.quarantine /Applications/attu.app +``` + +--- +## 3. Connect to Milvus +Attu supports connecting to both **Milvus Standalone** and **Zilliz Cloud**, providing flexibility to work with local or cloud-hosted databases. + +To use Milvus Standalone locally: +1. Start Milvus Standalone by following the [Milvus installation guide](https://milvus.io/docs/install_standalone-docker.md). +2. Open Attu and enter the connection information: + - Milvus Address: Your Milvus Standalone server URI, e.g. http://localhost:19530 + - Other optional settings: You can set them depending on your Milvus configurations or just leave them as default. +3. Click Connect to access your database. +> You can also connect the fully managed Milvus on [Zilliz Cloud](https://zilliz.com/cloud). Simply set the `Milvus Address` and `token` to the [Public Endpoint and API key](https://docs.zilliz.com/docs/on-zilliz-cloud-console#cluster-details) of your Zilliz Cloud instance. + +4. Click to access your database. + +

+ Attu Login Page +

+ +--- + +## 4. Prepare Data, Create Collection, and Insert Data + +### 4.1 Prepare the Data +We use the FAQ pages from the [Milvus Documentation 2.4.x](https://github.com/milvus-io/milvus-docs/releases/download/v2.4.6-preview/milvus_docs_2.4.x_en.zip) as the dataset for this example. + +#### Download and Extract Data: +```bash +wget https://github.com/milvus-io/milvus-docs/releases/download/v2.4.6-preview/milvus_docs_2.4.x_en.zip +unzip -q milvus_docs_2.4.x_en.zip -d milvus_docs +``` + +#### Process Markdown Files: +```python +from glob import glob + +text_lines = [] +for file_path in glob("milvus_docs/en/faq/*.md", recursive=True): + with open(file_path, "r") as file: + file_text = file.read() + text_lines += file_text.split("# ") +``` + +--- + +### 4.2 Generate Embeddings +Define a embedding model to generate text embeddings using the `milvus_model`. We use the `DefaultEmbeddingFunction` model as an example, which is a pre-trained and lightweight embedding model. +```python +from pymilvus import model as milvus_model + +embedding_model = milvus_model.DefaultEmbeddingFunction() + +# Generate test embedding +test_embedding = embedding_model.encode_queries(["This is a test"])[0] +embedding_dim = len(test_embedding) +print(embedding_dim) +print(test_embedding[:10]) +``` +#### Output: +``` +768 +[-0.04836066 0.07163023 -0.01130064 -0.03789345 -0.03320649 -0.01318448 + -0.03041712 -0.02269499 -0.02317863 -0.00426028] +``` +--- + +### 4.3 Create Collection +Connect to Milvus and create a collection: +```python +from pymilvus import MilvusClient + +# Connect to Milvus Standalone +client = MilvusClient(uri="http://localhost:19530") + +collection_name = "attu_tutorial" + +# Drop collection if it exists +if client.has_collection(collection_name): + client.drop_collection(collection_name) + +# Create a new collection +client.create_collection( + collection_name=collection_name, + dimension=embedding_dim, + metric_type="IP", # Inner product distance + consistency_level="Strong" +) +``` + +--- + +### 4.4 Insert Data +Iterate through the text lines, create embeddings, and insert the data into Milvus: +```python +from tqdm import tqdm + +data = [] +doc_embeddings = embedding_model.encode_documents(text_lines) + +for i, line in enumerate(tqdm(text_lines, desc="Creating embeddings")): + data.append({"id": i, "vector": doc_embeddings[i], "text": line}) + +client.insert(collection_name=collection_name, data=data) +``` +--- + +### 4.5 Visualize Data and Schema + +Now we can visualize the data schema and inserted entities using Attu's interface. The schema displays defined fields, including an `id` field of type `Int64` and a `vector` field of type `FloatVector(768)` with an `Inner Product (IP)` metric. The collection is loaded with **72 entities**. + +Additionally, we can view the inserted data, including ID, vector embeddings, and dynamic fields storing metadata such as text content. The interface supports filtering and querying based on specified conditions or dynamic fields. +

+ Schema View + Data View +

+ + +## 5. Visualizing Search Results and Relationships + +Attu provides a powerful interface for visualizing and exploring data relationships. To examine the inserted data points and their similarity relationships, follow these steps: + +### 5.1 **Perform a Search** +Navigate to the **Vector Search** tab in Attu. +1. Click on the **Generate Random Data** button to create test queries. +2. Click **Search** to retrieve results based on the generated data. + +The results are displayed in a table, showing IDs, similarity scores, and dynamic fields for each matching entity. + +

+ Search Results Table +

+ +--- + +### 5.2 **Explore Data Relationships** +Click the **Explore** button in the results panel to visualize the relationships between the query vector and the search results in a **knowledge graph-like structure**. + +- The **central node** represents the search vector. +- The **connected nodes** represent the search results, clicking them will display the detailed information of the corresponding node. + +

+ Knowledge Graph Visualization +

+ +--- + +### 5.3 **Expand the Graph** +Double-click on any result node to expand its connections. This action reveals additional relationships between the selected node and other data points in the collection, creating a **larger, interconnected knowledge graph**. + +This expanded view allows for deeper exploration of how data points are related, based on vector similarity. + +

+ Expanded Knowledge Graph +

+ +--- + +## 6. Conclusion + +Attu simplifies the management and visualization of vector data stored in Milvus. From data insertion to query execution and interactive exploration, it provides an intuitive interface for handling complex vector search tasks. With features like dynamic schema support, graphical search visualizations, and flexible query filters, Attu empowers users to analyze large-scale datasets effectively. + +By leveraging Attu’s visual exploration tools, users can better understand their data, identify hidden relationships, and make data-driven decisions. Start exploring your own datasets today with Attu and Milvus! + + +--- + + diff --git a/v2.5.x/site/en/tutorials/tutorials-overview.md b/v2.5.x/site/en/tutorials/tutorials-overview.md index 4cd4828c0..99aed5fd7 100644 --- a/v2.5.x/site/en/tutorials/tutorials-overview.md +++ b/v2.5.x/site/en/tutorials/tutorials-overview.md @@ -32,4 +32,6 @@ This page provides a list of tutorials for you to interact with Milvus. | [Text Search Engine](text_search_engine.md) | Semantic Search | vector search | | [Search Image by Text](text_image_search.md) | Semantic Search | vector search | | [Image Deduplication](image_deduplication_system.md) | Deduplication | vector search | +| [Quickstart with Attu](quickstart_with_attu.md) | Quickstart | vector search | +| [Use AsyncMilvusClient with asyncio](use-async-milvus-client-with-asyncio.md) | AsyncIO | AsyncIO, vector search | diff --git a/v2.5.x/site/en/tutorials/use-async-milvus-client-with-asyncio.md b/v2.5.x/site/en/tutorials/use-async-milvus-client-with-asyncio.md new file mode 100644 index 000000000..91128df62 --- /dev/null +++ b/v2.5.x/site/en/tutorials/use-async-milvus-client-with-asyncio.md @@ -0,0 +1,325 @@ +--- +id: use-async-milvus-client-with-asyncio.md +summary: AsyncMilvusClient is an asynchronous MilvusClient that offers a coroutine-based API for non-blocking access to Milvus via asyncio. In this article, you will learn about the process for calling the APIs that AsyncMilvusClient provides and the aspects to which you need to pay attention to.​ +title: Question Answering System +--- + +# Tutorial: Use AsyncMilvusClient with asyncio​ + +**AsyncMilvusClient** is an asynchronous MilvusClient that offers a coroutine-based API for non-blocking access to Milvus via [asyncio](https://docs.python.org/3/library/asyncio.html). In this article, you will learn about the process for calling the APIs that AsyncMilvusClient provides and the aspects to which you need to pay attention to.​ + +## Overview​ + +Asyncio is a library for writing concurrent code using **async**/**await** syntax and serves as the foundation for Milvus' high-performance asynchronous client, which will fit into your code library running on top of asyncio.​ + +The methods that AsyncMilvusClient provides have identical parameter sets and behaviors as those of MilvusClient. The only difference lies in the way you call them. The following table lists the methods available in AsyncMilvusClient.​ + +

**Client**

+ +

`close()`

+ +

+ +

+ +

**Collection & Partition**

+ +

`create_collection()`

+ +

`drop_collection()`

+ +

`create_partition()`

+ +

`drop_partition()`

+ +

+ +

+ +

**Index**

+ +

`create_index()`

+ +

`drop_index()`

+ +

`load_collection()`

+ +

`release_collection()`

+ +

`load_partitions()`

+ +

`release_partitions()`

+ +

**Vector**

+ +

`insert()`

+ +

`upsert()`

+ +

`delete()`

+ +

`search()`

+ +

`query()`

+ +

`hybrid_search()`

+ +

`get()`

+ +

+ +

+ +
+ +If you still need the asynchronous version of any other MilvusClient method, you can submit a feature request in the [pymilvus](https://github.com/milvus-io/pymilvus) repo. Code contribution is also welcome.​ + +## Create an event loop​ + +Applications using asyncio typically use the event loop as the orchestrator for managing asynchronous tasks and I/O operations. In this tutorial, we will get an event loop from asyncio and use it as the orchestrator.​ + +```python +import asyncio​ +import numpy as np​ +from scipy.sparse import csr_matrix​ +from pymilvus import MilvusClient, AsyncMilvusClient, DataType, RRFRanker, AnnSearchRequest​ +​ +loop = asyncio.get_event_loop()​ + +``` + +## Connect with AsyncMilvusClient​ + +The following example demonstrates how to connect Milvus in an asynchronous manner.​ + +```python +# Connect to Milvus server using AsyncMilvusClient​ +async_client = AsyncMilvusClient(​ + uri="http://localhost:19530",​ + token="root:Milvus"​ +)​ + +``` + +## Create schema​ + +Currently, `create_schema()` is not available in AsyncMilvusClient. Instead, we will use MilvusClient to create the schema for the collection.​ + +```python +schema = async_client.create_schema(​ + auto_id=False,​ + description="This is a sample schema",​ +)​ +​ +schema.add_field("id", DataType.INT64, is_primary=True)​ +schema.add_field("dense_vector", DataType.FLOAT_VECTOR, dim=5)​ +schema.add_field("sparse_vector", DataType.SPARSE_FLOAT_VECTOR)​ +schema.add_field("text", DataType.VARCHAR, max_length=512)​ + +``` + +
+ +AsyncMilvusClient calls the `create_schema()` method synchronously; therefore, you do not need to orchestrate the call using the event loop.​ + +
+ +## Create collection​ + +Now we will use the schema to create a collection. Note that you need to prefix the `await` keyword to any call to the `AsyncMilvusClient` methods and place the call inside an `async` function as follows:​ + +```python +async def create_my_collection(collection_name, schema):​ + if (client.has_collection(collection_name)):​ + await async_client.drop_collection(collection_name)​ +​ + await async_client.create_collection(​ + collection_name=collection_name,​ + schema=schema​ + )​ +​ + if (client.has_collection(collection_name)):​ + print("Collection created successfully")​ + else:​ + print("Failed to create collection")​ + ​ +# Call the above function asynchronously ​ +loop.run_until_complete(create_my_collection("my_collection", schema))​ +​ +# Output​ +#​ +# Collection created successfully​ + +``` + +## Create index​ + +You also need to create indexes for all vector fields and optional scalar fields. According to the schema defined above, there are two vector fields in the collection, and you will create indexes for them as follows:​ + +```python +async def create_indexes(collection_name):​ + index_params = client.prepare_index_params()​ +​ + index_params.add_index(field_name="dense_vector", index_type="AUTOINDEX", metric_type="IP")​ + index_params.add_index(field_name="sparse_vector", index_type="AUTOINDEX", metric_type="IP")​ + index_params.add_index(field_name="text", index_type="AUTOINDEX")​ +​ + await async_client.create_index(collection_name, index_params)​ +​ +# Call the above function asynchronously ​ +loop.run_until_complete(create_indexes("my_collection"))​ + +``` + +## Load collection​ + +A collection can be loaded after the necessary fields are indexed. The following code demonstrates how to load the collection asynchronously.​ + +```python +async def load_my_collection(collection_name):​ + await async_client.load_collection(collection_name)​ + print(client.get_load_state(collection_name))​ + ​ +# Call the above function asynchronously ​ +loop.run_until_complete(load_my_collection("my_collection"))​ +​ +# Output​ +#​ +# {'state': }​ + +``` + +## Insert data​ + +You can use the embedding models available in pymilvus to generate vector embeddings for your texts. For details, refer to [Embedding Overview](https://milvus.io/docs/embeddings.md). In this section, we will insert randomly generated data into the collection.​ + +```python +async def insert_sample_data(collection_name):​ + # Randomly generated data will be used here​ + rng = np.random.default_rng(42)​ +​ + def generate_random_text(length):​ + seed = "this is a seed paragraph to generate random text, which is used for testing purposes. Specifically, a random text is generated by randomly selecting words from this sentence."​ + words = seed.split()​ + return " ".join(rng.choice(words, length))​ + ​ + data = [{​ + 'id': i, ​ + 'dense_vector': rng.random(5).tolist(), ​ + 'sparse_vector': csr_matrix(rng.random(5)), ​ + 'text': generate_random_text(10)​ + } for i in range(10000)]​ +​ + res = await async_client.insert(collection_name, data)​ +​ + print(res)​ +​ +# Call the above function asynchronously ​ +loop.run_until_complete(insert_sample_data("my_collection"))​ +​ +# Output​ +#​ +# {'insert_count': 10000, 'ids': [0, 1, 2, 3, ..., 9999]}​ + +``` + +## Query​ + +After the collection is loaded and filled with data, you can conduct searches and queries in it. In this section, you are going to find the number of entities in the `text` field starting with the word `random` in the collection named `my_collection`.​ + +```python +async def query_my_collection(collection_name):​ + # Find the number of entities with the `text` fields starting with the word "random" in the `my_collection` collection.​ +​ + res = await async_client.query(​ + collection_name="my_collection",​ + filter='text like "%random%"',​ + output_fields=["count(*)"]​ + )​ +​ + print(res) ​ + ​ +# Call the above function asynchronously ​ +loop.run_until_complete(query_my_collection("my_collection"))​ +​ +# Output​ +#​ +# data: ["{'count(*)': 6802}"] ​ + +``` + +## Search​ + +In this section, you will conduct vector searches on the target collection's dense and sparse vector fields.​ + +```python +async def conduct_vector_search(collection_name, type, field):​ + # Generate a set of three random query vectors​ + query_vectors = []​ + if type == "dense":​ + query_vectors = [ rng.random(5) for _ in range(3) ]​ + ​ + if type == "sparse":​ + query_vectors = [ csr_matrix(rng.random(5)) for _ in range(3) ]​ +​ + print(query_vectors)​ +​ + res = await async_client.search(​ + collection_name="my_collection",​ + data=query_vectors,​ + anns_field=field,​ + output_fields=["text", field]​ + )​ +​ + print(res)​ + ​ +# To search against the dense vector field asynchronously ​ +loop.run_until_complete(conduct_vector_search("my_collection", "dense", "dense_vector"))​ +​ +# To search against the sparse vector field asynchronously ​ +loop.run_until_complete(conduct_vector_search("my_collection", "sparse", "sparse_vector"))​ + +``` + +The search output should list three sets of results corresponding to the specified query vectors.​ + +## Hybrid Search​ + +A hybrid search combines the results of multiple searches and reranks them to get a better recall. In this section, you are going to conduct a hybrid search using the dense and sparse vector fields.​ + +```python +async def conduct_hybrid_search(collection_name):​ + req_dense = AnnSearchRequest(​ + data=[ rng.random(5) for _ in range(3) ],​ + anns_field="dense_vector",​ + param={"metric_type": "IP"},​ + limit=10​ + )​ +​ + req_sparse = AnnSearchRequest(​ + data=[ csr_matrix(rng.random(5)) for _ in range(3) ],​ + anns_field="sparse_vector",​ + param={"metric_type": "IP"},​ + limit=10​ + )​ +​ + reqs = [req_dense, req_sparse]​ +​ + ranker = RRFRanker()​ +​ + res = await async_client.hybrid_search(​ + collection_name="my_collection",​ + reqs=reqs,​ + ranker=ranker,​ + output_fields=["text", "dense_vector", "sparse_vector"]​ + )​ +​ + print(res)​ + ​ +# Call the above function asynchronously ​ +loop.run_until_complete(conduct_hybrid_search("my_collection"))​ + +``` + +​ + diff --git a/v2.5.x/site/en/userGuide/search-query-get/full-text-search.md b/v2.5.x/site/en/userGuide/search-query-get/full-text-search.md index 76ef238af..52b99f10e 100644 --- a/v2.5.x/site/en/userGuide/search-query-get/full-text-search.md +++ b/v2.5.x/site/en/userGuide/search-query-get/full-text-search.md @@ -7,7 +7,7 @@ summary: Full text search is a feature that retrieves documents containing speci # Full Text Search​ -Full text search is a feature that retrieves documents containing specific terms or phrases in text datasets, then ranking the results based on relevance. This feature overcomes semantic search limitations, which might overlook precise terms, ensuring you receive the most accurate and contextually relevant results. Additionally, it simplifies vector searches by accepting raw text input, automatically converting your text data into sparse embeddings without the need to manually generate vector embeddings.​ +Full text search is a feature that retrieves documents containing specific terms or phrases in text datasets, then ranks the results based on relevance. This feature overcomes the limitations of semantic search, which might overlook precise terms, ensuring you receive the most accurate and contextually relevant results. Additionally, it simplifies vector searches by accepting raw text input, automatically converting your text data into sparse embeddings without the need to manually generate vector embeddings.​ Using the BM25 algorithm for relevance scoring, this feature is particularly valuable in retrieval-augmented generation (RAG) scenarios, where it prioritizes documents that closely match specific search terms.​ @@ -22,15 +22,15 @@ Using the BM25 algorithm for relevance scoring, this feature is particularly val Full text search simplifies the process of text-based searching by eliminating the need for manual embedding. This feature operates through the following workflow:​ -1. **Text input**: You insert raw text documents or provide query text without any need for manual embedding.​ +1. **Text input**: You insert raw text documents or provide query text without needing to manually embed them.​ -2. **Text analysis**: Milvus uses an analyzer to tokenize input text into individual, searchable terms.​ For more information on analyzers, refer to [Analyzer Overview](analyzer-overview.md). +2. **Text analysis**: Milvus uses an analyzer to tokenize the input text into individual, searchable terms.​ For more information on analyzers, refer to [Analyzer Overview](analyzer-overview.md). 3. **Function processing**: The built-in function receives tokenized terms and converts them into sparse vector representations.​ 4. **Collection store**: Milvus stores these sparse embeddings in a collection for efficient retrieval.​ -5. **BM25 scoring**: During a search, Milvus applies the BM25 algorithm to calculate scores for the stored documents and ranks matched results based on relevance to the query text.​ +5. **BM25 scoring**: During a search, Milvus applies the BM25 algorithm to calculate scores for the stored documents and ranks matched results based on their relevance to the query text.​ ![Full text search](../../../../assets/full-text-search.png)