gliner-haystack 0.2.0__tar.gz → 0.3.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,13 +1,26 @@
1
+ Metadata-Version: 2.3
2
+ Name: gliner-haystack
3
+ Version: 0.3.0
4
+ Summary: Haystack entity extractors backed by GLiNER 2.5
5
+ Author: Max Swain
6
+ Requires-Dist: haystack-ai>=3.0,<4
7
+ Requires-Dist: httpx2>=0.1
8
+ Requires-Python: >=3.11
9
+ Project-URL: Homepage, https://github.com/maxdswain/gliner-haystack
10
+ Project-URL: Repository, https://github.com/maxdswain/gliner-haystack
11
+ Project-URL: Issues, https://github.com/maxdswain/gliner-haystack/issues
12
+ Description-Content-Type: text/markdown
13
+
1
14
  # gliner-haystack
2
15
 
3
- [Haystack](https://haystack.deepset.ai/) components for extracting named entities with
16
+ [Haystack](https://haystack.deepset.ai/) components for extracting named entities and directed relations with
4
17
  [GLiNER2 and GLiNER2.5](https://github.com/fastino-ai/GLiNER2). Extract entities from text
5
18
  or enrich Haystack `Document` metadata, with synchronous and asynchronous pipeline support.
6
19
 
7
- The Python package is a **client** for a compatible GLiNER server exposing vLLM Factory's
8
- `POST /pooling` endpoint; it does not download or run a model in the Python process.
9
- You can connect to an existing server or use the [Docker images](#serving-gliner-with-docker)
10
- provided by this repository for a production-ready, higher-throughput inference backend.
20
+ The Python package is a **client**, not a model runtime. It requires a running
21
+ vLLM Factory server from one of the prebuilt GLiNER Docker images below; installing
22
+ the package alone does not download a model or perform inference. Start the
23
+ [required inference server](#required-docker-inference-server) before using the components.
11
24
 
12
25
  ## Install
13
26
 
@@ -16,9 +29,11 @@ pip install gliner-haystack
16
29
  # Or: uv add gliner-haystack
17
30
  ```
18
31
 
19
- Requires Python 3.11+ and Haystack 3.x. Start a compatible GLiNER2 or GLiNER2.5 server before
20
- running the examples below. The default endpoint is `http://localhost:8000` and the
21
- model name used in requests is `/models/current` (matching the supplied Compose files).
32
+ Requires Python 3.11+ and Haystack 3.x. **You must also run a compatible Docker
33
+ image** (GPU or CPU) serving GLiNER2 or GLiNER2.5 before using the examples below.
34
+ The default endpoint is `http://localhost:8000` and the model name in requests is
35
+ `/models/current`, matching the supplied Compose files. Set `api_base_url` to the
36
+ server's URL if it runs elsewhere.
22
37
 
23
38
  ## Import
24
39
 
@@ -57,6 +72,22 @@ Labels are **run inputs**, so you can reuse the same component with different la
57
72
  The extractor requests character spans and confidence from the server; returned records
58
73
  reflect the server's actual detections.
59
74
 
75
+ ### Extract directed relations
76
+
77
+ Use `GLiNERRelationExtractor` with a GLiNER2 or GLiNER2.5 `/pooling` plugin that supports relation schemas. No second model service is needed.
78
+
79
+ ```python
80
+ from haystack_integrations.components.extractors.gliner import GLiNERRelationExtractor
81
+
82
+ extractor = GLiNERRelationExtractor()
83
+ result = extractor.run(text="Alice works for Acme.", relation_types=["works_for"])
84
+ for relation in result["relations"]:
85
+ print(relation["head"]["text"], relation["relation_type"], relation["tail"]["text"])
86
+ # Example detection: Alice works_for Acme
87
+ ```
88
+
89
+ The `relations` output is an empty list when nothing is detected. Each endpoint preserves the server's text and any confidence and character offsets it supplies. Offsets can be absent: do not assume the first matching substring is the correct mention. The component does not resolve entity IDs or write graph edges. Use `run_async` and `close_async` for asynchronous pipelines.
90
+
60
91
  ### Enrich Haystack documents
61
92
 
62
93
  ```python
@@ -77,6 +108,18 @@ extractor.close()
77
108
  The component returns new documents, preserves their existing metadata, and adds
78
109
  `meta["entities"]`; it does not change the input documents or their text.
79
110
 
111
+ For a knowledge graph, pass `relation_types` to `GLiNERDocumentExtractor.run` or `run_async` to extract entities and directed relations in **one pooling request per document**. Omit it for entity-only extraction:
112
+
113
+ ```python
114
+ chunk = extractor.run(
115
+ documents=[Document(content="Alice works for Acme.")],
116
+ labels=["person", "organization"],
117
+ relation_types=["works_for"],
118
+ )["documents"][0]
119
+ for relation in chunk.meta["relations"]:
120
+ print(relation["head"]["text"], relation["relation_type"], relation["tail"]["text"])
121
+ ```
122
+
80
123
  ### Use in a Haystack pipeline
81
124
 
82
125
  ```python
@@ -157,12 +200,12 @@ vLLM Factory also serves older span-based GLiNER models, but those plugins take
157
200
  not support that response format. They are entity extractors, not wrappers for other
158
201
  GLiNER2 tasks such as relation extraction or classification.
159
202
 
160
- ## Serving GLiNER with Docker
203
+ ## Required Docker inference server
161
204
 
162
- For production inference, this repository publishes vLLM Factory-based images
163
- configured for GLiNER2.5 by default, for NVIDIA GPUs and x86-64 CPUs. Both serve `POST /pooling` on port 8000.
164
- The GPU image is intended for higher-throughput deployments; the CPU image also
165
- allows development and integration testing on machines without a supported GPU.
205
+ Run a vLLM Factory GLiNER Docker image before using the Python components. The
206
+ Compose files pull the images from GHCR; both serve `POST /pooling` on port 8000
207
+ and default to GLiNER2.5. Use the GPU image for higher-throughput inference or
208
+ the x86-64 CPU image on machines without a supported GPU.
166
209
 
167
210
  | Image | Compose file | Requirements |
168
211
  | --- | --- | --- |
@@ -175,14 +218,12 @@ docker compose up -d # GPU
175
218
  # docker compose -f compose.cpu.yaml up -d # CPU instead
176
219
  ```
177
220
 
178
- The first startup downloads and prepares `fastino/gliner2.5-multi-v1` in a persistent
221
+ The image's first startup downloads and prepares `fastino/gliner2.5-multi-v1` in a persistent
179
222
  Hugging Face cache; wait until the server is ready before sending requests. With either
180
223
  Compose file, the Python examples above work without changes. To serve GLiNER2 instead,
181
224
  change the Compose file's environment entries to `MODEL_ID: fastino/gliner2-large-v1`,
182
225
  `MODEL_PLUGIN: deberta_gliner2`, and `IO_PLUGIN: deberta_gliner2_io` (keep the
183
- `/models/current` path). To build an image yourself,
184
- initialize the submodule and build with `docker build -t vllm-gliner .`; the Dockerfile
185
- supports a `VLLM_BASE_IMAGE` build argument for selecting the CPU base image.
226
+ `/models/current` path).
186
227
 
187
228
  You can also call the server directly:
188
229
 
@@ -212,5 +253,4 @@ these tests; a GPU is not required.
212
253
  Python package checks and publishing are defined in `.github/workflows/publish-package.yml`.
213
254
  After checks pass on `main`, releasable Conventional Commits drive git-cliff release
214
255
  notes, a version bump, a GitHub Release, and PyPI Trusted Publishing via the `pypi`
215
- GitHub environment. Docker image publishing is a separate workflow that rebuilds only
216
- when image inputs change.
256
+ GitHub environment.
@@ -1,23 +1,13 @@
1
- Metadata-Version: 2.3
2
- Name: gliner-haystack
3
- Version: 0.2.0
4
- Summary: Haystack entity extractors backed by GLiNER 2.5
5
- Author: Max Swain
6
- Requires-Dist: haystack-ai>=3.0,<4
7
- Requires-Dist: httpx2>=0.1
8
- Requires-Python: >=3.11
9
- Description-Content-Type: text/markdown
10
-
11
1
  # gliner-haystack
12
2
 
13
- [Haystack](https://haystack.deepset.ai/) components for extracting named entities with
3
+ [Haystack](https://haystack.deepset.ai/) components for extracting named entities and directed relations with
14
4
  [GLiNER2 and GLiNER2.5](https://github.com/fastino-ai/GLiNER2). Extract entities from text
15
5
  or enrich Haystack `Document` metadata, with synchronous and asynchronous pipeline support.
16
6
 
17
- The Python package is a **client** for a compatible GLiNER server exposing vLLM Factory's
18
- `POST /pooling` endpoint; it does not download or run a model in the Python process.
19
- You can connect to an existing server or use the [Docker images](#serving-gliner-with-docker)
20
- provided by this repository for a production-ready, higher-throughput inference backend.
7
+ The Python package is a **client**, not a model runtime. It requires a running
8
+ vLLM Factory server from one of the prebuilt GLiNER Docker images below; installing
9
+ the package alone does not download a model or perform inference. Start the
10
+ [required inference server](#required-docker-inference-server) before using the components.
21
11
 
22
12
  ## Install
23
13
 
@@ -26,9 +16,11 @@ pip install gliner-haystack
26
16
  # Or: uv add gliner-haystack
27
17
  ```
28
18
 
29
- Requires Python 3.11+ and Haystack 3.x. Start a compatible GLiNER2 or GLiNER2.5 server before
30
- running the examples below. The default endpoint is `http://localhost:8000` and the
31
- model name used in requests is `/models/current` (matching the supplied Compose files).
19
+ Requires Python 3.11+ and Haystack 3.x. **You must also run a compatible Docker
20
+ image** (GPU or CPU) serving GLiNER2 or GLiNER2.5 before using the examples below.
21
+ The default endpoint is `http://localhost:8000` and the model name in requests is
22
+ `/models/current`, matching the supplied Compose files. Set `api_base_url` to the
23
+ server's URL if it runs elsewhere.
32
24
 
33
25
  ## Import
34
26
 
@@ -67,6 +59,22 @@ Labels are **run inputs**, so you can reuse the same component with different la
67
59
  The extractor requests character spans and confidence from the server; returned records
68
60
  reflect the server's actual detections.
69
61
 
62
+ ### Extract directed relations
63
+
64
+ Use `GLiNERRelationExtractor` with a GLiNER2 or GLiNER2.5 `/pooling` plugin that supports relation schemas. No second model service is needed.
65
+
66
+ ```python
67
+ from haystack_integrations.components.extractors.gliner import GLiNERRelationExtractor
68
+
69
+ extractor = GLiNERRelationExtractor()
70
+ result = extractor.run(text="Alice works for Acme.", relation_types=["works_for"])
71
+ for relation in result["relations"]:
72
+ print(relation["head"]["text"], relation["relation_type"], relation["tail"]["text"])
73
+ # Example detection: Alice works_for Acme
74
+ ```
75
+
76
+ The `relations` output is an empty list when nothing is detected. Each endpoint preserves the server's text and any confidence and character offsets it supplies. Offsets can be absent: do not assume the first matching substring is the correct mention. The component does not resolve entity IDs or write graph edges. Use `run_async` and `close_async` for asynchronous pipelines.
77
+
70
78
  ### Enrich Haystack documents
71
79
 
72
80
  ```python
@@ -87,6 +95,18 @@ extractor.close()
87
95
  The component returns new documents, preserves their existing metadata, and adds
88
96
  `meta["entities"]`; it does not change the input documents or their text.
89
97
 
98
+ For a knowledge graph, pass `relation_types` to `GLiNERDocumentExtractor.run` or `run_async` to extract entities and directed relations in **one pooling request per document**. Omit it for entity-only extraction:
99
+
100
+ ```python
101
+ chunk = extractor.run(
102
+ documents=[Document(content="Alice works for Acme.")],
103
+ labels=["person", "organization"],
104
+ relation_types=["works_for"],
105
+ )["documents"][0]
106
+ for relation in chunk.meta["relations"]:
107
+ print(relation["head"]["text"], relation["relation_type"], relation["tail"]["text"])
108
+ ```
109
+
90
110
  ### Use in a Haystack pipeline
91
111
 
92
112
  ```python
@@ -167,12 +187,12 @@ vLLM Factory also serves older span-based GLiNER models, but those plugins take
167
187
  not support that response format. They are entity extractors, not wrappers for other
168
188
  GLiNER2 tasks such as relation extraction or classification.
169
189
 
170
- ## Serving GLiNER with Docker
190
+ ## Required Docker inference server
171
191
 
172
- For production inference, this repository publishes vLLM Factory-based images
173
- configured for GLiNER2.5 by default, for NVIDIA GPUs and x86-64 CPUs. Both serve `POST /pooling` on port 8000.
174
- The GPU image is intended for higher-throughput deployments; the CPU image also
175
- allows development and integration testing on machines without a supported GPU.
192
+ Run a vLLM Factory GLiNER Docker image before using the Python components. The
193
+ Compose files pull the images from GHCR; both serve `POST /pooling` on port 8000
194
+ and default to GLiNER2.5. Use the GPU image for higher-throughput inference or
195
+ the x86-64 CPU image on machines without a supported GPU.
176
196
 
177
197
  | Image | Compose file | Requirements |
178
198
  | --- | --- | --- |
@@ -185,14 +205,12 @@ docker compose up -d # GPU
185
205
  # docker compose -f compose.cpu.yaml up -d # CPU instead
186
206
  ```
187
207
 
188
- The first startup downloads and prepares `fastino/gliner2.5-multi-v1` in a persistent
208
+ The image's first startup downloads and prepares `fastino/gliner2.5-multi-v1` in a persistent
189
209
  Hugging Face cache; wait until the server is ready before sending requests. With either
190
210
  Compose file, the Python examples above work without changes. To serve GLiNER2 instead,
191
211
  change the Compose file's environment entries to `MODEL_ID: fastino/gliner2-large-v1`,
192
212
  `MODEL_PLUGIN: deberta_gliner2`, and `IO_PLUGIN: deberta_gliner2_io` (keep the
193
- `/models/current` path). To build an image yourself,
194
- initialize the submodule and build with `docker build -t vllm-gliner .`; the Dockerfile
195
- supports a `VLLM_BASE_IMAGE` build argument for selecting the CPU base image.
213
+ `/models/current` path).
196
214
 
197
215
  You can also call the server directly:
198
216
 
@@ -222,5 +240,4 @@ these tests; a GPU is not required.
222
240
  Python package checks and publishing are defined in `.github/workflows/publish-package.yml`.
223
241
  After checks pass on `main`, releasable Conventional Commits drive git-cliff release
224
242
  notes, a version bump, a GitHub Release, and PyPI Trusted Publishing via the `pypi`
225
- GitHub environment. Docker image publishing is a separate workflow that rebuilds only
226
- when image inputs change.
243
+ GitHub environment.
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "gliner-haystack"
3
- version = "0.2.0"
3
+ version = "0.3.0"
4
4
  description = "Haystack entity extractors backed by GLiNER 2.5"
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.11"
@@ -12,6 +12,11 @@ dependencies = [
12
12
  [[project.authors]]
13
13
  name = "Max Swain"
14
14
 
15
+ [project.urls]
16
+ Homepage = "https://github.com/maxdswain/gliner-haystack"
17
+ Repository = "https://github.com/maxdswain/gliner-haystack"
18
+ Issues = "https://github.com/maxdswain/gliner-haystack/issues"
19
+
15
20
  [dependency-groups]
16
21
  dev = [
17
22
  "poethepoet>=0.48",
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "gliner-haystack"
3
- version = "0.2.0"
3
+ version = "0.3.0"
4
4
  description = "Haystack entity extractors backed by GLiNER 2.5"
5
5
  readme = "README.md"
6
6
  authors = [
@@ -12,6 +12,11 @@ dependencies = [
12
12
  "httpx2>=0.1",
13
13
  ]
14
14
 
15
+ [project.urls]
16
+ Homepage = "https://github.com/maxdswain/gliner-haystack"
17
+ Repository = "https://github.com/maxdswain/gliner-haystack"
18
+ Issues = "https://github.com/maxdswain/gliner-haystack/issues"
19
+
15
20
  [dependency-groups]
16
21
  dev = ["poethepoet>=0.48", "ruff>=0.13", "pyrefly>=0.35"]
17
22
  test = ["pytest>=8,<10"]
@@ -0,0 +1,9 @@
1
+ """vLLM GLiNER entity and relation extraction components for Haystack."""
2
+
3
+ from haystack_integrations.components.extractors.gliner.extractors import (
4
+ GLiNERDocumentExtractor,
5
+ GLiNERRelationExtractor,
6
+ GLiNERTextExtractor,
7
+ )
8
+
9
+ __all__ = ["GLiNERDocumentExtractor", "GLiNERRelationExtractor", "GLiNERTextExtractor"]
@@ -25,35 +25,8 @@ def _validate_labels(labels: list[str]) -> None:
25
25
  raise ValueError("labels must be a non-empty list of non-empty strings")
26
26
 
27
27
 
28
- @component
29
- class GLiNERTextExtractor:
30
- """Extract labeled entities from text using a remote GLiNER2 or GLiNER2.5 server.
31
-
32
- Connect to vLLM Factory's ``POST /pooling`` endpoint. The server must be
33
- running separately; this component does not load a model locally. Pass
34
- entity labels to each call, so the same instance can extract different
35
- types of entities from different texts. The ``entities`` output maps each
36
- label to records containing text, character offsets, and confidence when
37
- the server detects that label.
38
-
39
- Examples
40
- --------
41
- Run against the GLiNER2.5 server at the default URL::
42
-
43
- from haystack_integrations.components.extractors.gliner import GLiNERTextExtractor
44
-
45
- extractor = GLiNERTextExtractor()
46
- result = extractor.run(
47
- text="Alice works at Acme.",
48
- labels=["person", "organization"],
49
- )
50
- entities = result["entities"]
51
- extractor.close()
52
-
53
- Use ``await extractor.run_async(text=..., labels=...)`` and
54
- ``await extractor.close_async()`` inside an async application or a
55
- Haystack 3 ``Pipeline.run_async`` call.
56
- """
28
+ class _PoolingClient:
29
+ """Shared transport for schema-based GLiNER components."""
57
30
 
58
31
  def __init__(
59
32
  self,
@@ -127,6 +100,43 @@ class GLiNERTextExtractor:
127
100
  await self.async_client.aclose()
128
101
  self.async_client = None
129
102
 
103
+ def _schema_payload(self, text: str, schema: dict[str, Any]) -> dict[str, Any]:
104
+ return {
105
+ "model": self.model,
106
+ "data": {
107
+ "text": text,
108
+ "schema": schema,
109
+ "runtime": "classic",
110
+ "threshold": self.threshold,
111
+ "include_spans": True,
112
+ "include_confidence": True,
113
+ },
114
+ }
115
+
116
+
117
+ @component
118
+ class GLiNERTextExtractor(_PoolingClient):
119
+ """Extract labeled entities from text using a remote GLiNER2 or GLiNER2.5 server.
120
+
121
+ Pass entity labels to each call. The ``entities`` output maps each label to
122
+ records containing text, character offsets, and confidence.
123
+
124
+ Examples
125
+ --------
126
+ With a pooling server running on localhost:8000, extract mentions and use
127
+ their offsets to recover the exact substring in the source text::
128
+
129
+ from haystack_integrations.components.extractors.gliner import GLiNERTextExtractor
130
+
131
+ extractor = GLiNERTextExtractor()
132
+ text = "Alice works for Acme."
133
+ result = extractor.run(text=text, labels=["person", "organization"])
134
+ for label, mentions in result["entities"].items():
135
+ for mention in mentions:
136
+ span = text[mention["start"] : mention["end"]]
137
+ print(label, span) # e.g. person Alice
138
+ """
139
+
130
140
  def _payload(self, text: str, labels: list[str]) -> dict[str, Any]:
131
141
  """Build a schema-based pooling request for text and entity labels.
132
142
 
@@ -143,26 +153,16 @@ class GLiNERTextExtractor:
143
153
  JSON request body with the model, schema, and extraction options.
144
154
  """
145
155
  _validate_labels(labels)
146
- return {
147
- "model": self.model,
148
- "data": {
149
- "text": text,
150
- "schema": {"entities": {label: "" for label in labels}},
151
- "runtime": "classic",
152
- "threshold": self.threshold,
153
- "include_spans": True,
154
- "include_confidence": True,
155
- },
156
- }
156
+ return self._schema_payload(text, {"entities": {label: "" for label in labels}})
157
157
 
158
158
  @staticmethod
159
- def _entities(response: httpx2.Response) -> dict[str, list[dict[str, Any]]]:
159
+ def _entities(data: dict[str, Any]) -> dict[str, list[dict[str, Any]]]:
160
160
  """Validate a pooling response and return its labeled entity records.
161
161
 
162
162
  Parameters
163
163
  ----------
164
- response : httpx2.Response
165
- HTTP response from a compatible GLiNER ``/pooling`` endpoint.
164
+ data : dict[str, Any]
165
+ Decoded data from a compatible GLiNER ``/pooling`` endpoint.
166
166
 
167
167
  Returns
168
168
  -------
@@ -176,11 +176,7 @@ class GLiNERTextExtractor:
176
176
  ValueError
177
177
  If the response does not contain a labeled entity mapping.
178
178
  """
179
- response.raise_for_status()
180
- payload = response.json()
181
- if not isinstance(payload, dict) or not isinstance(payload.get("data"), dict):
182
- raise ValueError("Invalid vLLM GLiNER pooling response: expected a data object") # noqa: TRY004
183
- entities = payload["data"].get("entities")
179
+ entities = data.get("entities")
184
180
  if not isinstance(entities, dict) or any(
185
181
  not isinstance(label, str)
186
182
  or not isinstance(records, list)
@@ -218,7 +214,8 @@ class GLiNERTextExtractor:
218
214
  self.warm_up()
219
215
  assert self.client is not None
220
216
  response = self.client.post(f"{self.api_base_url}/pooling", json=payload)
221
- return {"entities": self._entities(response)}
217
+ response.raise_for_status()
218
+ return {"entities": self._entities(response.json()["data"])}
222
219
 
223
220
  @component.output_types(entities=dict[str, list[dict[str, Any]]])
224
221
  async def run_async(self, text: str, labels: list[str]) -> dict[str, dict[str, list[dict[str, Any]]]]:
@@ -251,7 +248,67 @@ class GLiNERTextExtractor:
251
248
  await self.warm_up_async()
252
249
  assert self.async_client is not None
253
250
  response = await self.async_client.post(f"{self.api_base_url}/pooling", json=payload)
254
- return {"entities": self._entities(response)}
251
+ response.raise_for_status()
252
+ return {"entities": self._entities(response.json()["data"])}
253
+
254
+
255
+ @component
256
+ class GLiNERRelationExtractor(_PoolingClient):
257
+ """Extract directed relations from text using a relation-capable GLiNER pooling server.
258
+
259
+ Each output record contains ``relation_type``, ``head`` and ``tail``. Endpoints
260
+ retain the server's text, confidence and offsets when available. These are
261
+ mentions in the input text, not resolved knowledge graph identities.
262
+
263
+ Examples
264
+ --------
265
+ With a relation-capable pooling server running on localhost:8000, request
266
+ a predicate and read the detected mentions in subject-to-object order::
267
+
268
+ from haystack_integrations.components.extractors.gliner import GLiNERRelationExtractor
269
+
270
+ extractor = GLiNERRelationExtractor()
271
+ result = extractor.run(text="Alice works for Acme.", relation_types=["works_for"])
272
+ for relation in result["relations"]:
273
+ subject = relation["head"]["text"]
274
+ predicate = relation["relation_type"]
275
+ obj = relation["tail"]["text"]
276
+ print(subject, predicate, obj) # e.g. Alice works_for Acme
277
+
278
+ If no relation is detected, ``result["relations"]`` is an empty list.
279
+ """
280
+
281
+ def _payload(self, text: str, relation_types: list[str]) -> dict[str, Any]:
282
+ return self._schema_payload(text, {"relations": relation_types})
283
+
284
+ @staticmethod
285
+ def _relations(data: dict[str, Any]) -> list[dict[str, Any]]:
286
+ groups = data["relation_extraction"]
287
+ return [
288
+ {"relation_type": name, "head": record["head"], "tail": record["tail"]}
289
+ for name, records in groups.items()
290
+ for record in records
291
+ ]
292
+
293
+ @component.output_types(relations=list[dict[str, Any]])
294
+ def run(self, text: str, relation_types: list[str]) -> dict[str, list[dict[str, Any]]]:
295
+ """Extract directed head/tail mentions for the supplied relation types."""
296
+ payload = self._payload(text, relation_types)
297
+ self.warm_up()
298
+ assert self.client is not None
299
+ response = self.client.post(f"{self.api_base_url}/pooling", json=payload)
300
+ response.raise_for_status()
301
+ return {"relations": self._relations(response.json()["data"])}
302
+
303
+ @component.output_types(relations=list[dict[str, Any]])
304
+ async def run_async(self, text: str, relation_types: list[str]) -> dict[str, list[dict[str, Any]]]:
305
+ """Extract directed head/tail mentions without blocking the event loop."""
306
+ payload = self._payload(text, relation_types)
307
+ await self.warm_up_async()
308
+ assert self.async_client is not None
309
+ response = await self.async_client.post(f"{self.api_base_url}/pooling", json=payload)
310
+ response.raise_for_status()
311
+ return {"relations": self._relations(response.json()["data"])}
255
312
 
256
313
 
257
314
  @component
@@ -345,9 +402,20 @@ class GLiNERDocumentExtractor:
345
402
  """Close the asynchronous HTTP client after processing documents."""
346
403
  await self.text_extractor.close_async()
347
404
 
405
+ @staticmethod
406
+ def _enrich(document: Document, response: httpx2.Response, relation_types: list[str] | None) -> Document:
407
+ response.raise_for_status()
408
+ data = response.json()["data"]
409
+ meta = {**document.meta, "entities": GLiNERTextExtractor._entities(data)}
410
+ if relation_types is not None:
411
+ meta["relations"] = GLiNERRelationExtractor._relations(data)
412
+ return replace(document, meta=meta)
413
+
348
414
  @component.output_types(documents=list[Document])
349
- def run(self, documents: list[Document], labels: list[str]) -> dict[str, list[Document]]:
350
- """Add entities to copies of documents, processing one at a time.
415
+ def run(
416
+ self, documents: list[Document], labels: list[str], relation_types: list[str] | None = None
417
+ ) -> dict[str, list[Document]]:
418
+ """Add entities and optional relations to copies of documents, processing one at a time.
351
419
 
352
420
  Parameters
353
421
  ----------
@@ -355,6 +423,8 @@ class GLiNERDocumentExtractor:
355
423
  Text documents to analyze in input order.
356
424
  labels : list[str]
357
425
  Nonempty names of entity types to extract from every document.
426
+ relation_types : list[str] | None
427
+ Optional predicates to extract in the same request into ``meta["relations"]``.
358
428
 
359
429
  Returns
360
430
  -------
@@ -371,16 +441,26 @@ class GLiNERDocumentExtractor:
371
441
  If the server returns an error status.
372
442
  """
373
443
  _validate_labels(labels)
444
+ schema: dict[str, Any] = {"entities": dict.fromkeys(labels, "")}
445
+ if relation_types is not None:
446
+ schema["relations"] = relation_types
447
+ extractor = self.text_extractor
374
448
  result = []
375
449
  for document in documents:
376
450
  if document.content is None:
377
451
  raise ValueError("GLiNERDocumentExtractor requires text document content")
378
- entities = self.text_extractor.run(document.content, labels)["entities"]
379
- result.append(replace(document, meta={**document.meta, "entities": entities}))
452
+ extractor.warm_up()
453
+ assert extractor.client is not None
454
+ response = extractor.client.post(
455
+ f"{extractor.api_base_url}/pooling", json=extractor._schema_payload(document.content, schema)
456
+ )
457
+ result.append(self._enrich(document, response, relation_types))
380
458
  return {"documents": result}
381
459
 
382
460
  @component.output_types(documents=list[Document])
383
- async def run_async(self, documents: list[Document], labels: list[str]) -> dict[str, list[Document]]:
461
+ async def run_async(
462
+ self, documents: list[Document], labels: list[str], relation_types: list[str] | None = None
463
+ ) -> dict[str, list[Document]]:
384
464
  """Add entities to document copies with bounded concurrent requests.
385
465
 
386
466
  The shared async HTTP client sends at most ``max_concurrency`` requests
@@ -392,6 +472,8 @@ class GLiNERDocumentExtractor:
392
472
  Text documents to analyze in input order.
393
473
  labels : list[str]
394
474
  Nonempty names of entity types to extract from every document.
475
+ relation_types : list[str] | None
476
+ Optional predicates to extract in the same request into ``meta["relations"]``.
395
477
 
396
478
  Returns
397
479
  -------
@@ -408,6 +490,9 @@ class GLiNERDocumentExtractor:
408
490
  If the server returns an error status.
409
491
  """
410
492
  _validate_labels(labels)
493
+ schema: dict[str, Any] = {"entities": dict.fromkeys(labels, "")}
494
+ if relation_types is not None:
495
+ schema["relations"] = relation_types
411
496
  for document in documents:
412
497
  if document.content is None:
413
498
  raise ValueError("GLiNERDocumentExtractor requires text document content")
@@ -423,9 +508,8 @@ class GLiNERDocumentExtractor:
423
508
  async def extract(document: Document) -> Document:
424
509
  async with semaphore:
425
510
  response = await client.post(
426
- f"{extractor.api_base_url}/pooling", json=extractor._payload(document.content or "", labels)
511
+ f"{extractor.api_base_url}/pooling", json=extractor._schema_payload(document.content or "", schema)
427
512
  )
428
- entities = extractor._entities(response)
429
- return replace(document, meta={**document.meta, "entities": entities})
513
+ return self._enrich(document, response, relation_types)
430
514
 
431
515
  return {"documents": list(await asyncio.gather(*(extract(document) for document in documents)))}
@@ -1,5 +0,0 @@
1
- """vLLM GLiNER entity extraction components for Haystack."""
2
-
3
- from haystack_integrations.components.extractors.gliner.extractors import GLiNERDocumentExtractor, GLiNERTextExtractor
4
-
5
- __all__ = ["GLiNERDocumentExtractor", "GLiNERTextExtractor"]