diff --git a/README.md b/README.md index f17bf875..9490078f 100644 --- a/README.md +++ b/README.md @@ -1,44 +1,72 @@
- - Pioneer AI - Fine-tune GLiNER with a single prompt - -
- -> [!IMPORTANT] -> **🚀 GLiNER2 is Now Available from [Fastino Labs](https://github.com/fastino-ai)!** A unified multi-task model for NER, Text Classification & Structured Data Extraction. Check out [fastino-ai/GLiNER2 →](https://github.com/fastino-ai/GLiNER2) -# 👑 GLiNER: Generalist and Lightweight Model for Named Entity Recognition +# GLiNER: Generalist and Lightweight Model for Named Entity Recognition ---- +**Zero-shot NER | Relation Extraction | PII Detection | Information Extraction | Token Classification** -
-
- GLiNER Downloads - GLiNER Paper - GLiNER Discord - GLiNER GitHub stars - License -
- Open GLiNER In Colab - Open GLiNER In HF Spaces - HuggingFace Models -
- Reddit r/GLiNER - Fastino Discord -
+
+ + GLiNER Documentation + GLiNER Paper + Open GLiNER In Colab + License
+ + GLiNER Community Discord + Reddit r/GLiNER + Open GLiNER In HF Spaces + HuggingFace Models +
+ + GLiNER Downloads + GLiNER GitHub stars +
+
-GLiNER is a framework for training and deploying small Named Entity Recognition (NER) models with zero-shot capabilities. In addition to tradition NER, it also supports joint entity and relation extraction. GLiNER is fine-tunable, optimized to run on CPUs and consumer hardware, and has performance competitive with LLMs several times its size, like ChatGPT and UniNER. - - -## Example Notebooks -Explore various examples including finetuning, ONNX conversion, and synthetic data generation. +
+ GLiNER Banner +
-- [Example Notebooks](https://github.com/urchade/GLiNER/tree/main/examples) -- Finetune on Colab  [](https://colab.research.google.com/drive/1HNKd74cmfS9tGvWrKeIjSxBt01QQS7bq?usp=sharing) -## 🛠 Installation & Usage +GLiNER is a framework for training and deploying small Named Entity Recognition (NER) models with zero-shot capabilities. In addition to traditional NER, it also supports joint entity and relation extraction, as well as multi-task token classification. GLiNER is fine-tunable, optimized to run on CPUs and consumer hardware, and has performance competitive with LLMs several times its size, like ChatGPT and UniNER. + +Other tasks such as text classification, entity linking, and schema extraction are supported through projects in the [Ecosystem](#ecosystem). + +## Why GLiNER? + + + + + + + + + + + + +
+Zero-shot Recognition +

Extract any entity type — no labeled data or task-specific training required

+
+Runs Anywhere +

CPU, INT8 quantization, torch.compile, ONNX export — deploy on any hardware

+
+Millions of Labels +

Bi-encoder pre-computes label embeddings, scaling to 100+ entity types without degradation

+
+NER + Relations +

Build knowledge graphs in a single pass with the joint RelEx architecture

+
+PII Detection +

State-of-the-art multilingual PII models covering major entity types across 100+ languages

+
+Fine-Tune in Minutes +

Few-shot learning on small datasets — bring your own labels and get competitive results fast

+
+ +## Quick Start ### Installation @@ -57,100 +85,159 @@ uv pip install gliner uv pip install gliner[serve] # or: pip install gliner ray[serve] ``` -### Usage -After the installation of the GLiNER library, import the `GLiNER` class. Following this, you can load your chosen model with `GLiNER.from_pretrained` and utilize `predict_entities` to discern entities within your text. +### Basic Usage ```python from gliner import GLiNER -# Initialize GLiNER with the base model -model = GLiNER.from_pretrained("urchade/gliner_medium-v2.1") +model = GLiNER.from_pretrained("gliner-community/gliner_small-v2.5") -# Sample text for entity prediction text = """ -Cristiano Ronaldo dos Santos Aveiro (Portuguese pronunciation: [kɾiʃˈtjɐnu ʁɔˈnaldu]; born 5 February 1985) is a Portuguese professional footballer who plays as a forward for and captains both Saudi Pro League club Al Nassr and the Portugal national team. Widely regarded as one of the greatest players of all time, Ronaldo has won five Ballon d'Or awards,[note 3] a record three UEFA Men's Player of the Year Awards, and four European Golden Shoes, the most by a European player. He has won 33 trophies in his career, including seven league titles, five UEFA Champions Leagues, the UEFA European Championship and the UEFA Nations League. Ronaldo holds the records for most appearances (183), goals (140) and assists (42) in the Champions League, goals in the European Championship (14), international goals (128) and international appearances (205). He is one of the few players to have made over 1,200 professional career appearances, the most by an outfield player, and has scored over 850 official senior career goals for club and country, making him the top goalscorer of all time. +Cristiano Ronaldo dos Santos Aveiro (born 5 February 1985) is a Portuguese +professional footballer who plays as a forward for and captains both Saudi Pro +League club Al Nassr and the Portugal national team. """ -# Labels for entity prediction -# Most GLiNER models should work best when entity types are in lower case or title case -labels = ["Person", "Award", "Date", "Competitions", "Teams"] +labels = ["person", "date", "organization", "location"] -# Perform entity prediction entities = model.predict_entities(text, labels, threshold=0.5) -# Display predicted entities and their labels for entity in entities: print(entity["text"], "=>", entity["label"]) ``` -#### Expected Output - +**Output:** ``` Cristiano Ronaldo dos Santos Aveiro => person 5 February 1985 => date -Al Nassr => teams -Portugal national team => teams -Ballon d'Or => award -UEFA Men's Player of the Year Awards => award -European Golden Shoes => award -UEFA Champions Leagues => competitions -UEFA European Championship => competitions -UEFA Nations League => competitions -European Championship => competitions +Al Nassr => organization +Portugal => location ``` -## 🚀 Serving +## 🚀 Optimizations -GLiNER ships with a production-ready Ray Serve deployment that adds dynamic batching, memory-aware batch sizing, precompiled power-of-two batch sizes, and multi-replica scaling. Install with `pip install gliner[serve]`. +GLiNER models are already small, but quantization and compilation can make them significantly faster and more memory-efficient, important when running on edge devices, serving at high throughput, or keeping GPU costs low. -**In-process (vLLM-style):** +- **`torch.compile`** fuses operations and removes Python overhead, yielding up to ~1.5x speedup with no quality loss. +- **FP16 quantization** (`quantize=True`) halves model memory and speeds up matrix operations. Combined with compilation, this gives up to **~1.9x faster GPU inference** with virtually no quality loss. +- **INT8 quantization** cuts memory by another 2x on top of FP16 and is supported out of the box, however, models need to be trained with Quantization-Aware Training (QAT) to preserve accuracy at INT8 precision. ```python -from gliner.serve import GLiNERFactory - -with GLiNERFactory( - model="urchade/gliner_medium-v2.1", - dtype="bfloat16", - enable_flashdeberta=True, -) as llm: - outputs = llm.predict( - ["John works at Google", "Paris is in France"], - labels=["person", "organization", "location"], - ) +model = GLiNER.from_pretrained( + "gliner-community/gliner_small-v2.5", + map_location="cuda", + quantize=True, + compile_torch_model=True, +) ``` -Passing a list of texts preserves dynamic batching — each text is dispatched as a separate request so Ray Serve's `@serve.batch` accumulates them into a single forward pass. Use `predict_async` for concurrent `asyncio` calls and `.handle` to reach the underlying Ray Serve handle. +Find more information on compilation and other optimizations in the [documentation](https://urchade.github.io/GLiNER/usage.html#quantization-compilation-flashdeberta). + +## Serving -**Standalone HTTP server:** +For production workloads — high-throughput pipelines, multi-user services, or anywhere you need to go beyond single-process `model.inference()` calls — GLiNER provides a Ray Serve-based serving layer. It adds dynamic batching that automatically groups incoming requests, memory-aware batch sizing that prevents CUDA OOM by calibrating against your GPU, precompiled kernels for common batch sizes to avoid first-call latency, horizontal scaling across multiple GPUs via Ray replicas, and an HTTP API for language-agnostic access. ```bash -python -m gliner.serve --model urchade/gliner_small-v2.1 --enable-flashdeberta +python -m gliner.serve --model gliner-community/gliner_small-v2.5 --dtype fp16 ``` +Then query from Python: + +```python +from gliner.serve import GLiNERClient + +client = GLiNERClient() # connects to http://localhost:8000/gliner +results = client.predict( + ["John works at Google", "Paris is in France"], + labels=["person", "organization", "location"], +) +``` + +More information on serving options and parameters can be found in the [documentation](https://urchade.github.io/GLiNER/serving.html). + +## Training + +GLiNER models are easy to fine-tune on your own data. Prepare your dataset as a JSON file and use the training script: + ```bash -curl -X POST http://localhost:8000/gliner \ - -H "Content-Type: application/json" \ - -d '{"text": "John works at Google", "labels": ["person", "organization"]}' +python train.py --config configs/config.yaml ``` -**Attach a remote client to a running server:** +Or train programmatically: ```python -from gliner.serve import GLiNERClient -client = GLiNERClient() -result = client.predict("John works at Google", labels=["person", "organization"]) +from gliner import GLiNER + +model = GLiNER.from_pretrained("gliner-community/gliner_small-v2.5") + +model.train_model( + train_dataset=train_data, + eval_dataset=eval_data, + output_dir="models", + max_steps=10000, + per_device_train_batch_size=8, + learning_rate=1e-5, + bf16=True, +) ``` -For all CLI flags, Docker usage, relation-extraction examples, and tuning knobs (memory fractions, precompiled batch sizes, sequence packing), see the [Serving guide](docs/serving.md). +For detailed training examples, see the [example notebooks](https://github.com/urchade/GLiNER/tree/main/examples): +- [Documentation on training](https://urchade.github.io/GLiNER/training.html) +- [Fine-tuning on Colab](https://colab.research.google.com/drive/1HNKd74cmfS9tGvWrKeIjSxBt01QQS7bq?usp=sharing) +- [Synthetic Data Generation](https://github.com/urchade/GLiNER/blob/main/examples/synthetic_data_generation.ipynb) + +## Architectures + +GLiNER supports multiple architectures tailored to different use cases: + +| Architecture | Description | Example Model | +|---|---|---| +| **Uni-encoder** | Strong zero-shot capabilities, supports up to ~50 entity types. The original GLiNER architecture. | [gliner_multi_pii-v1](https://huggingface.co/urchade/gliner_multi_pii-v1) | +| **Bi-encoder** | Scalable to massive numbers of entity types via separate text and label encoding. | [gliner-bi-base-v2.0](https://huggingface.co/knowledgator/gliner-bi-base-v2.0) | +| **RelEx** | Joint NER and relation extraction in a single model. | [gliner-relex-large-v1.0](https://huggingface.co/knowledgator/gliner-relex-large-v1.0) | +| **GLiNER Decoder** | Hybrid architecture for open NER: entity types are generated with a small decoder for maximum flexibility. | [gliner-decoder-large-v1.0](https://huggingface.co/knowledgator/gliner-decoder-large-v1.0) | + +For more details, see the [documentation](https://urchade.github.io/GLiNER/architectures.html). + +## Popular Use Cases + +- **[Compliance & PII Redaction](https://urchade.github.io/GLiNER/usage.html#compliance--pii-redaction)** — detect and mask 40+ types of personal data (SSN, credit cards, passports, emails, IBANs, etc.) across documents and data pipelines +- **[Knowledge Graph Construction](https://urchade.github.io/GLiNER/usage.html#knowledge-graph-construction)** — jointly extract entities and relations to power Graph RAG, semantic search, and analytics +- **[Large-Scale Entity Extraction](https://urchade.github.io/GLiNER/usage.html#large-scale-entity-extraction)** — use the bi-encoder to tag millions of documents against hundreds or thousands of entity types in production +- **[Domain-Specific NER](https://urchade.github.io/GLiNER/usage.html#domain-specific-ner)** — fine-tune on biomedical, legal, financial, or any specialized corpus with minimal labeled data +- **[Multi-lingual Information Extraction](https://urchade.github.io/GLiNER/usage.html#multi-lingual-information-extraction)** — extract structured data from 100+ languages with a single model +- **[Search & Retrieval Augmentation](https://urchade.github.io/GLiNER/usage.html#search--retrieval-augmentation)** — parse queries into structured entities to improve search relevance and RAG pipelines + +## Ecosystem + +GLiNER has a rich ecosystem of community projects and integrations: + +| Project | Description | +|---|---| +| [GLiNER2](https://github.com/fastino-ai/GLiNER2) | Unified multi-task model for NER, text classification, and structured data extraction | +| [GLiClass](https://github.com/Knowledgator/GLiClass) | Zero-shot text classification using GLiNER-style architecture | +| [GLinker](https://github.com/Knowledgator/GLinker) | Entity linking with GLiNER | +| [GLiNER.cpp](https://github.com/Knowledgator/GLiNER.cpp) | C++ implementation for high-performance inference | +| [gline-rs](https://github.com/fbilhaut/gline-rs) | Rust implementation of GLiNER | +| [vllm-factory](https://github.com/ddickmann/vllm-factory) | vLLM integration for scalable GLiNER serving | +| [gliner-spacy](https://github.com/theirstory/gliner-spacy) | spaCy integration for GLiNER | + +## Documentation + +Full documentation is available at [urchade.github.io/GLiNER](https://urchade.github.io/GLiNER). + +## Authors & Creators -## 👨‍💻 Model Authors GLiNER was originally developed by: -* [Urchade Zaratiana](urchade.github.io) +* [Urchade Zaratiana](https://www.linkedin.com/in/urchade-zaratiana-36ba9814b/) * Nadi Tomeh * Pierre Holat * Thierry Charnois -## 🌟 Maintainers +Alternative architectures, such as bi-encoder and GLiNER-relex, were developed by [Ihor Stepanov](https://www.linkedin.com/in/ihor-knowledgator/). + + +## Maintainers
@@ -169,8 +256,30 @@ GLiNER was originally developed by:
+## Community + +- [Discord — GLiNER Community](https://discord.gg/x7hQsjX2Kk) +- [Reddit — r/GLiNER](https://www.reddit.com/r/GLiNER/) +- [HuggingFace](https://huggingface.co/gliner-community) + +## Contributing + +We welcome contributions from the community! Here's how to get started: + +1. **Fork** the repository and create a new branch from `main`. +2. **Install** the development dependencies: `pip install -e ".[dev]"`. +3. **Make your changes** — bug fixes, new features, documentation improvements, and new examples are all appreciated. +4. **Lint and format** your code with [Ruff](https://docs.astral.sh/ruff/) before committing: + ```bash + ruff check . --fix + ruff format . + ``` +5. **Write tests** for any new functionality and make sure existing tests pass. +6. **Submit a pull request** with a clear description of what you changed and why. + +For bug reports and feature requests, please [open an issue](https://github.com/urchade/GLiNER/issues). For questions and discussions, join us on [Discord](https://discord.gg/x7hQsjX2Kk). -## 📚 Citations +## Citations If you find GLiNER useful in your research, please consider citing our papers: @@ -192,7 +301,6 @@ If you find GLiNER useful in your research, please consider citing our papers: url = "https://aclanthology.org/2024.naacl-long.300", doi = "10.18653/v1/2024.naacl-long.300", pages = "5364--5376", - abstract = "Named Entity Recognition (NER) is essential in various Natural Language Processing (NLP) applications. Traditional NER models are effective but limited to a set of predefined entity types. In contrast, Large Language Models (LLMs) can extract arbitrary entities through natural language instructions, offering greater flexibility. However, their size and cost, particularly for those accessed via APIs like ChatGPT, make them impractical in resource-limited scenarios. In this paper, we introduce a compact NER model trained to identify any type of entity. Leveraging a bidirectional transformer encoder, our model, GLiNER, facilitates parallel entity extraction, an advantage over the slow sequential token generation of LLMs. Through comprehensive testing, GLiNER demonstrate strong performance, outperforming both ChatGPT and fine-tuned LLMs in zero-shot evaluations on various NER benchmarks.", } ``` @@ -219,7 +327,8 @@ If you find GLiNER useful in your research, please consider citing our papers: url={https://arxiv.org/abs/2602.18487}, } ``` -## Support and funding + +## Support and Funding This project has been supported and funded by **F.initiatives** and **Laboratoire Informatique de Paris Nord**. @@ -229,4 +338,10 @@ F.initiatives has been an expert in public funding strategies for R&D, Innovatio FI Group

-We also extend our heartfelt gratitude to the open-source community for their invaluable contributions, which have been instrumental in the success of this project. +We also extend our heartfelt gratitude to the open-source community for their invaluable contributions, which have been instrumental in the success of this project. ❤️ + +--- + +
+GLiNER — open-source named entity recognition, zero-shot NER, relation extraction, PII detection, information extraction, knowledge graph construction, NLP, natural language processing, token classification, text mining, lightweight NER model, transformer-based NER +
diff --git a/assets/banner.png b/assets/banner.png new file mode 100644 index 00000000..b6c101da Binary files /dev/null and b/assets/banner.png differ diff --git a/docs/usage.md b/docs/usage.md index cdcf0a3e..e8273c53 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -910,84 +910,233 @@ relations = [ ## Practical Examples -### Example 1: Extract Company Information +### Compliance & PII Redaction + +Detect and mask personal data across documents using GLiNER's multilingual PII model, which covers 40+ entity types (SSN, credit cards, passports, emails, IBANs, etc.) in 100+ languages. ```python from gliner import GLiNER -model = GLiNER.from_pretrained("urchade/gliner_small-v2.1") +model = GLiNER.from_pretrained("urchade/gliner_multi_pii-v1") text = """ -Apple Inc. is headquartered in Cupertino, California. The company was founded -by Steve Jobs, Steve Wozniak, and Ronald Wayne in April 1976. Tim Cook is the -current CEO. Apple's main products include iPhone, iPad, and Mac computers. +Patient John Smith (DOB: 03/15/1982, SSN: 123-45-6789) was seen at +Mayo Clinic on January 10, 2024. Contact: john.smith@email.com, ++1-555-867-5309. Insurance ID: BC-9876543. His home address is +742 Evergreen Terrace, Springfield, IL 62704. """ -labels = ["company", "location", "person", "position", "product", "date"] -entities = model.predict_entities(text, labels, threshold=0.5) +pii_labels = [ + "person", "date of birth", "social security number", "email", + "phone number", "medical facility", "insurance id", "address", +] -# Organize by type -from collections import defaultdict -by_type = defaultdict(list) -for entity in entities: - by_type[entity['label']].append(entity['text']) +entities = model.predict_entities(text, pii_labels, threshold=0.5) + +# Redact PII from text +redacted = text +for entity in sorted(entities, key=lambda e: e["start"], reverse=True): + redacted = redacted[: entity["start"]] + f"[{entity['label'].upper()}]" + redacted[entity["end"] :] -for label, items in by_type.items(): - print(f"{label}: {', '.join(set(items))}") +print(redacted) ``` -### Example 2: Process Scientific Papers +
+Expected Output + +``` +Patient [PERSON] (DOB: [DATE OF BIRTH], SSN: [SOCIAL SECURITY NUMBER]) was seen at +[MEDICAL FACILITY] on January 10, 2024. Contact: [EMAIL], +[PHONE NUMBER]. Insurance ID: [INSURANCE ID]. His home address is +[ADDRESS]. +``` +
+ +### Knowledge Graph Construction + +Jointly extract entities and relations in a single pass to build knowledge graphs for Graph RAG, semantic search, and analytics. ```python from gliner import GLiNER -model = GLiNER.from_pretrained("urchade/gliner_medium-v2.1") +model = GLiNER.from_pretrained("knowledgator/gliner-relex-large-v1.0") -abstract = """ -We introduce GPT-4, a large-scale multimodal model developed by OpenAI. -The model was trained on a diverse dataset and exhibits strong performance -on various benchmarks including MMLU, HumanEval, and GSM-8K. +text = """ +Elon Musk founded SpaceX in 2002 in Hawthorne, California. The company +developed the Falcon 9 rocket and the Dragon spacecraft. SpaceX was awarded +a $1.6 billion NASA contract for cargo resupply missions to the International +Space Station. """ +entity_labels = ["person", "organization", "date", "location", "product", "monetary value"] +relation_labels = ["founded", "founded_in", "headquartered_in", "developed", "awarded_by"] + +entities, relations = model.inference( + [text], + labels=entity_labels, + relations=relation_labels, + threshold=0.5, + relation_threshold=0.5, +) + +print("Entities:") +for entity in entities[0]: + print(f" {entity['text']} ({entity['label']})") + +print("\nRelations (knowledge graph edges):") +for relation in relations[0]: + head = entities[0][relation["head"]["entity_idx"]] + tail = entities[0][relation["tail"]["entity_idx"]] + print(f" {head['text']} --[{relation['relation']}]--> {tail['text']}") +``` + +### Large-Scale Entity Extraction + +Use the bi-encoder to tag millions of documents against hundreds of entity types. Pre-compute label embeddings once and reuse them across all documents for maximum throughput. + +```python +from gliner import GLiNER + +model = GLiNER.from_pretrained("knowledgator/gliner-bi-base-v2.0", map_location="cuda") + labels = [ - "model_name", "organization", "dataset", "benchmark", - "metric", "task", "method" + "person", "organization", "location", "date", "product", "event", + "technology", "software", "programming_language", "framework", + "database", "protocol", "standard", "regulation", "currency", + "measurement", "chemical_compound", "disease", "medication", + # ... scale to hundreds of types ] -entities = model.predict_entities(abstract, labels, threshold=0.4) +# Pre-compute label embeddings once +label_embeddings = model.encode_labels(labels, batch_size=32) + +# Process a large document collection efficiently +documents = [ + "Python 3.12 was released by the PSF in October 2023.", + "The FDA approved a new treatment for Type 2 diabetes.", + "Tesla announced record Q4 revenue of $25.2 billion.", + # ... millions of documents +] -print("Extracted Information:") +all_entities = model.batch_predict_with_embeds( + documents, + label_embeddings, + labels, + threshold=0.5, + batch_size=64, +) + +for doc, entities in zip(documents, all_entities): + print(f"\n{doc[:60]}...") + for entity in entities: + print(f" {entity['text']} => {entity['label']} ({entity['score']:.2f})") +``` + +### Domain-Specific NER + +Fine-tune GLiNER on your specialized corpus (biomedical, legal, financial, etc.) with minimal labeled data to get high-quality extraction for domain terms. + +```python +from gliner import GLiNER + +# Start from a pre-trained model +model = GLiNER.from_pretrained("gliner-community/gliner_small-v2.5") + +# Prepare domain-specific training data (NER format) +train_data = [ + { + "tokenized_text": ["Aspirin", "reduces", "inflammation", "in", "rheumatoid", "arthritis", "patients", "."], + "ner": [ + [0, 0, "medication"], + [2, 2, "condition"], + [4, 5, "disease"], + ], + }, + { + "tokenized_text": ["Metformin", "is", "prescribed", "for", "Type", "2", "diabetes", "."], + "ner": [ + [0, 0, "medication"], + [4, 6, "disease"], + ], + }, + # ... add more examples from your domain +] + +# Fine-tune — even 50–200 examples can yield strong results +model.train_model( + train_dataset=train_data, + output_dir="models/bio-gliner", + max_steps=500, + per_device_train_batch_size=8, + learning_rate=1e-5, + bf16=True, +) + +# Use the fine-tuned model +text = "The patient was started on Lisinopril 10mg for hypertension." +entities = model.predict_entities(text, ["medication", "dosage", "disease"], threshold=0.5) for entity in entities: - print(f" {entity['label']}: {entity['text']}") + print(f" {entity['text']} => {entity['label']}") ``` -### Example 3: Analyze News Articles +For detailed training guides, see the [training documentation](https://urchade.github.io/GLiNER/training.html). + +### Multi-lingual Information Extraction + +Extract structured data from 100+ languages with a single model — no per-language setup required. ```python from gliner import GLiNER -model = GLiNER.from_pretrained("knowledgator/gliner-bi-small-v1.0") +model = GLiNER.from_pretrained("urchade/gliner_multi-v2.1") -article = """ -Tesla CEO Elon Musk announced on Twitter that the company will open a new -Gigafactory in Austin, Texas. The facility will produce the Cybertruck and -Model Y vehicles. Construction began in July 2020 and operations started in 2021. -""" +texts = { + "English": "Barack Obama was born in Honolulu, Hawaii on August 4, 1961.", + "French": "Emmanuel Macron est le président de la République française depuis 2017.", + "Japanese": "東京都は日本の首都であり、2021年にオリンピックが開催されました。", + "Arabic": "تأسست شركة أرامكو السعودية في عام 1933 في المملكة العربية السعودية.", +} -labels = [ - "person", "position", "company", "location", "facility", - "product", "date", "event" +labels = ["person", "location", "organization", "date"] + +for lang, text in texts.items(): + entities = model.predict_entities(text, labels, threshold=0.5) + print(f"\n{lang}: {text[:60]}...") + for entity in entities: + print(f" {entity['text']} => {entity['label']}") +``` + +### Search & Retrieval Augmentation + +Parse user queries into structured entities to improve search relevance and RAG pipelines — route queries, filter results, or enrich retrieval context. + +```python +from gliner import GLiNER + +model = GLiNER.from_pretrained("gliner-community/gliner_small-v2.5") + +queries = [ + "What were Apple's revenue numbers in Q3 2023?", + "Find clinical trials for Alzheimer's treatment in Europe", + "Show me Python machine learning libraries released after 2022", ] -# Process with BiEncoder for efficiency -entities = model.predict_entities(article, labels, threshold=0.5) +query_labels = [ + "company", "metric", "time_period", "disease", "treatment", + "location", "programming_language", "topic", "product_type", +] + +for query in queries: + entities = model.predict_entities(query, query_labels, threshold=0.4) + print(f"\nQuery: {query}") + + # Build structured filters from extracted entities + filters = {entity["label"]: entity["text"] for entity in entities} + print(f" Extracted filters: {filters}") -# Group related entities -print("Key Information:") -print(f"- Company: {[e['text'] for e in entities if e['label'] == 'company']}") -print(f"- Location: {[e['text'] for e in entities if e['label'] == 'location']}") -print(f"- Products: {[e['text'] for e in entities if e['label'] == 'product']}") -print(f"- Timeline: {[e['text'] for e in entities if e['label'] == 'date']}") + # Use filters to enhance retrieval + # e.g., add metadata filters to your vector DB query, + # or expand the search with entity synonyms ``` ## ⚡ Prompt Compression (Precomputed Prompt Embeddings)