Langextract Milvus Demo
LangExtract + Milvus Integration
This guide demonstrates how to use LangExtract with Milvus to build an intelligent document processing and retrieval system.
LangExtract is a Python library that uses Large Language Models (LLMs) to extract structured information from unstructured text documents with precise source grounding. The system combines LangExtract's extraction capabilities with Milvus's vector storage to enable both semantic similarity search and precise metadata filtering.
This integration is particularly valuable for content management, semantic search, knowledge discovery, and building recommendation systems based on extracted document attributes.
Prerequisites
Before running this notebook, make sure you have the following dependencies installed:
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).
We will use Gemini as the LLM in this example. You should prepare the api key GEMINI_API_KEY as an environment variable.
Define the LangExtract + Milvus pipeline
We will define the pipeline that uses LangExtract for structured information extraction and Milvus as the vector store.
Configuration and Setup
Let's configure our global parameters for the integration. We'll use Gemini's embedding model to generate vector representations for our documents.
Initialize Milvus Client
Now let's initialize our Milvus client. We'll use a local database file for simplicity, but this can easily be scaled to a full Milvus server deployment.
As for the argument of
MilvusClient:
- Setting the
urias a local file, e.g../milvus.db, is the most convenient method, as it automatically utilizes Milvus Lite 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. In this setup, please use the server uri, e.g.
http://localhost:19530, as youruri.- If you want to use Zilliz Cloud, the fully managed cloud service for Milvus, adjust the
uriandtoken, which correspond to the Public Endpoint and Api key in Zilliz Cloud.
Sample Data Preparation
For this demonstration, we'll use movie descriptions as our sample documents. This showcases LangExtract's ability to extract structured information like genres, characters, and themes from unstructured text.
=== LangExtract + Milvus Integration Demo === Preparing to process 10 documents
Setting Up the Milvus Collection
Before we can store our extracted data, we need to create a Milvus collection with the appropriate schema. This collection will store the original document text, vector embeddings, and extracted metadata fields.
1. Setting up Milvus collection... Dropped existing collection: document_extractions Collection 'document_extractions' created successfully Vector index created successfully
Defining the Extraction Schema
LangExtract uses prompts and examples to guide the LLM in extracting structured information. Let's define our extraction schema for movie descriptions, specifying what information to extract and how to categorize it.
2. Extracting tags from documents...
Providing Examples for Better Extraction
To improve the quality and consistency of extractions, we'll provide LangExtract with a few examples. These examples demonstrate the expected format and help the model understand our extraction requirements.
Processing and Vectorizing the Results
Now we need to process the extraction results and generate vector embeddings for each document. We'll also flatten the extracted attributes into separate fields to make them easily searchable in Milvus.
3. Processing extraction results and generating vectors... Successfully generated vector: John McClane fights terrorists... Successfully generated vector: A young wizard named Harry Pot... Successfully generated vector: Tony Stark builds an advanced ... Successfully generated vector: A group of friends get lost in... Successfully generated vector: Two detectives investigate a s... Successfully generated vector: A brilliant scientist creates ... Successfully generated vector: A romantic comedy about two fr... Successfully generated vector: An evil sorcerer threatens to ... Successfully generated vector: Space marines battle alien inv... Successfully generated vector: A detective investigates super... Completed data processing, ready to insert 10 records
Inserting Data into Milvus
With our processed data ready, let's insert it into the Milvus collection. This will enable us to perform both semantic searches and precise metadata filtering.
4. Inserting data into Milvus...
Successfully inserted 10 documents into Milvus
Insert result: {'insert_count': 10, 'ids': ['doc_f8797155', 'doc_78c7e586', 'doc_fa3a3ab5', 'doc_64981815', 'doc_3ab18cb2', 'doc_1ea42b18', 'doc_f0779243', 'doc_386590b7', 'doc_3b3ae1ab', 'doc_851089d6']}
Demonstrating Metadata Filtering
One of the key advantages of combining LangExtract with Milvus is the ability to perform precise filtering based on extracted metadata. Let's demonstrate this with some filter expression searches.
=== Filter Expression Search Examples === Loading collection into memory... Collection loaded successfully 1. Searching for thriller movies: - A brilliant scientist creates artificial intelligence that becomes self-aware. The sci-fi thriller e... Genre: sci-fi thriller (sci-fi-thriller) - Two detectives investigate a series of mysterious murders in New York City. The crime thriller featu... Genre: crime thriller (crime-thriller) - A detective investigates supernatural crimes in Victorian London. The horror thriller combines perio... Genre: horror thriller (horror-thriller) - John McClane fights terrorists in a Los Angeles skyscraper during Christmas Eve. The action-packed t... Genre: action-packed thriller (action-thriller) 2. Searching for movies with military characters: - Space marines battle alien invaders on a distant planet. The action sci-fi movie features futuristic... Genre: action sci-fi Character: protagonist (military)
Combining Semantic Search with Metadata Filtering
The real power of this integration comes from combining semantic vector search with precise metadata filtering. This allows us to find semantically similar content while applying specific constraints based on extracted attributes.
=== Semantic Search Examples === 1. Searching for action-related content + only thriller genre: - Similarity: 0.6947 Text: John McClane fights terrorists in a Los Angeles skyscraper during Christmas Eve. The action-packed t... Genre: action-packed thriller (action-thriller) - Similarity: 0.6128 Text: Two detectives investigate a series of mysterious murders in New York City. The crime thriller featu... Genre: crime thriller (crime-thriller) - Similarity: 0.5889 Text: A brilliant scientist creates artificial intelligence that becomes self-aware. The sci-fi thriller e... Genre: sci-fi thriller (sci-fi-thriller) 2. Searching for magic-related content + fantasy genre + conflict theme: - Similarity: 0.6986 Text: An evil sorcerer threatens to destroy the magical kingdom. A brave hero must gather allies and maste... Genre: fantasy (fantasy) Theme: conflict (fantasy_world) === Demo Complete ===