langextract 0.1.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.
- langextract-0.1.0/LICENSE +202 -0
- langextract-0.1.0/PKG-INFO +347 -0
- langextract-0.1.0/README.md +310 -0
- langextract-0.1.0/langextract/__init__.py +244 -0
- langextract-0.1.0/langextract/annotation.py +542 -0
- langextract-0.1.0/langextract/chunking.py +490 -0
- langextract-0.1.0/langextract/data.py +236 -0
- langextract-0.1.0/langextract/data_lib.py +123 -0
- langextract-0.1.0/langextract/inference.py +441 -0
- langextract-0.1.0/langextract/io.py +318 -0
- langextract-0.1.0/langextract/progress.py +351 -0
- langextract-0.1.0/langextract/prompting.py +165 -0
- langextract-0.1.0/langextract/resolver.py +912 -0
- langextract-0.1.0/langextract/schema.py +159 -0
- langextract-0.1.0/langextract/tokenizer.py +357 -0
- langextract-0.1.0/langextract/visualization.py +608 -0
- langextract-0.1.0/langextract.egg-info/PKG-INFO +347 -0
- langextract-0.1.0/langextract.egg-info/SOURCES.txt +21 -0
- langextract-0.1.0/langextract.egg-info/dependency_links.txt +1 -0
- langextract-0.1.0/langextract.egg-info/requires.txt +28 -0
- langextract-0.1.0/langextract.egg-info/top_level.txt +1 -0
- langextract-0.1.0/pyproject.toml +77 -0
- langextract-0.1.0/setup.cfg +4 -0
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: langextract
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: LangExtract: A library for extracting structured data from language models
|
|
5
|
+
Author-email: Akshay Goel <goelak@google.com>
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Requires-Python: >=3.10
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Requires-Dist: absl-py>=1.0.0
|
|
11
|
+
Requires-Dist: aiohttp>=3.8.0
|
|
12
|
+
Requires-Dist: async_timeout>=4.0.0
|
|
13
|
+
Requires-Dist: exceptiongroup>=1.1.0
|
|
14
|
+
Requires-Dist: google-genai>=0.1.0
|
|
15
|
+
Requires-Dist: langfun>=0.1.0
|
|
16
|
+
Requires-Dist: ml-collections>=0.1.0
|
|
17
|
+
Requires-Dist: more-itertools>=8.0.0
|
|
18
|
+
Requires-Dist: numpy>=1.20.0
|
|
19
|
+
Requires-Dist: openai>=0.27.0
|
|
20
|
+
Requires-Dist: pandas>=1.3.0
|
|
21
|
+
Requires-Dist: pydantic>=1.8.0
|
|
22
|
+
Requires-Dist: python-dotenv>=0.19.0
|
|
23
|
+
Requires-Dist: python-magic>=0.4.27
|
|
24
|
+
Requires-Dist: requests>=2.25.0
|
|
25
|
+
Requires-Dist: tqdm>=4.64.0
|
|
26
|
+
Requires-Dist: typing-extensions>=4.0.0
|
|
27
|
+
Provides-Extra: dev
|
|
28
|
+
Requires-Dist: black>=23.7.0; extra == "dev"
|
|
29
|
+
Requires-Dist: pylint>=2.17.5; extra == "dev"
|
|
30
|
+
Requires-Dist: pytest>=7.4.0; extra == "dev"
|
|
31
|
+
Requires-Dist: pytype>=2024.10.11; extra == "dev"
|
|
32
|
+
Requires-Dist: tox>=4.0.0; extra == "dev"
|
|
33
|
+
Provides-Extra: test
|
|
34
|
+
Requires-Dist: pytest>=7.4.0; extra == "test"
|
|
35
|
+
Requires-Dist: tomli>=2.0.0; extra == "test"
|
|
36
|
+
Dynamic: license-file
|
|
37
|
+
|
|
38
|
+
<p align="center">
|
|
39
|
+
<a href="https://github.com/google/langextract">
|
|
40
|
+
<img src="docs/_static/logo.svg" alt="LangExtract Logo" width="128" />
|
|
41
|
+
</a>
|
|
42
|
+
</p>
|
|
43
|
+
|
|
44
|
+
# LangExtract
|
|
45
|
+
|
|
46
|
+
[](https://badge.fury.io/py/langextract)
|
|
47
|
+
[](https://github.com/google/langextract)
|
|
48
|
+

|
|
49
|
+
|
|
50
|
+
## Table of Contents
|
|
51
|
+
|
|
52
|
+
- [Introduction](#introduction)
|
|
53
|
+
- [Why LangExtract?](#why-langextract)
|
|
54
|
+
- [Quick Start](#quick-start)
|
|
55
|
+
- [Installation](#installation)
|
|
56
|
+
- [API Key Setup for Cloud Models](#api-key-setup-for-cloud-models)
|
|
57
|
+
- [More Examples](#more-examples)
|
|
58
|
+
- [*Romeo and Juliet* Full Text Extraction](#romeo-and-juliet-full-text-extraction)
|
|
59
|
+
- [Medication Extraction](#medication-extraction)
|
|
60
|
+
- [Radiology Report Structuring: RadExtract](#radiology-report-structuring-radextract)
|
|
61
|
+
- [Contributing](#contributing)
|
|
62
|
+
- [Testing](#testing)
|
|
63
|
+
- [Disclaimer](#disclaimer)
|
|
64
|
+
|
|
65
|
+
## Introduction
|
|
66
|
+
|
|
67
|
+
LangExtract is a Python library that uses LLMs to extract structured information from unstructured text documents based on user-defined instructions. It processes materials such as clinical notes or reports, identifying and organizing key details while ensuring the extracted data corresponds to the source text.
|
|
68
|
+
|
|
69
|
+
## Why LangExtract?
|
|
70
|
+
|
|
71
|
+
1. **Precise Source Grounding:** Maps every extraction to its exact location in the source text, enabling visual highlighting for easy traceability and verification.
|
|
72
|
+
2. **Reliable Structured Outputs:** Enforces a consistent output schema based on your few-shot examples, leveraging controlled generation in supported models like Gemini to guarantee robust, structured results.
|
|
73
|
+
3. **Optimized for Long Documents:** Overcomes the "needle-in-a-haystack" challenge of large document extraction by using an optimized strategy of text chunking, parallel processing, and multiple passes for higher recall.
|
|
74
|
+
4. **Interactive Visualization:** Instantly generates a self-contained, interactive HTML file to visualize and review thousands of extracted entities in their original context.
|
|
75
|
+
5. **Flexible LLM Support:** Supports your preferred models, from cloud-based LLMs like the Google Gemini family to local open-source models via the built-in Ollama interface.
|
|
76
|
+
6. **Adaptable to Any Domain:** Define extraction tasks for any domain using just a few examples. LangExtract adapts to your needs without requiring any model fine-tuning.
|
|
77
|
+
7. **Leverages LLM World Knowledge:** Utilize precise prompt wording and few-shot examples to influence how the extraction task may utilize LLM knowledge. The accuracy of any inferred information and its adherence to the task specification are contingent upon the selected LLM, the complexity of the task, the clarity of the prompt instructions, and the nature of the prompt examples.
|
|
78
|
+
|
|
79
|
+
## Quick Start
|
|
80
|
+
|
|
81
|
+
> **Note:** Using cloud-hosted models like Gemini requires an API key. See the [API Key Setup](#api-key-setup-for-cloud-models) section for instructions on how to get and configure your key.
|
|
82
|
+
|
|
83
|
+
Extract structured information with just a few lines of code.
|
|
84
|
+
|
|
85
|
+
### 1. Define Your Extraction Task
|
|
86
|
+
|
|
87
|
+
First, create a prompt that clearly describes what you want to extract. Then, provide a high-quality example to guide the model.
|
|
88
|
+
|
|
89
|
+
```python
|
|
90
|
+
import langextract as lx
|
|
91
|
+
import textwrap
|
|
92
|
+
|
|
93
|
+
# 1. Define the prompt and extraction rules
|
|
94
|
+
prompt = textwrap.dedent("""\
|
|
95
|
+
Extract characters, emotions, and relationships in order of appearance.
|
|
96
|
+
Use exact text for extractions. Do not paraphrase or overlap entities.
|
|
97
|
+
Provide meaningful attributes for each entity to add context.""")
|
|
98
|
+
|
|
99
|
+
# 2. Provide a high-quality example to guide the model
|
|
100
|
+
examples = [
|
|
101
|
+
lx.data.ExampleData(
|
|
102
|
+
text="ROMEO. But soft! What light through yonder window breaks? It is the east, and Juliet is the sun.",
|
|
103
|
+
extractions=[
|
|
104
|
+
lx.data.Extraction(
|
|
105
|
+
extraction_class="character",
|
|
106
|
+
extraction_text="ROMEO",
|
|
107
|
+
attributes={"emotional_state": "wonder"}
|
|
108
|
+
),
|
|
109
|
+
lx.data.Extraction(
|
|
110
|
+
extraction_class="emotion",
|
|
111
|
+
extraction_text="But soft!",
|
|
112
|
+
attributes={"feeling": "gentle awe"}
|
|
113
|
+
),
|
|
114
|
+
lx.data.Extraction(
|
|
115
|
+
extraction_class="relationship",
|
|
116
|
+
extraction_text="Juliet is the sun",
|
|
117
|
+
attributes={"type": "metaphor"}
|
|
118
|
+
),
|
|
119
|
+
]
|
|
120
|
+
)
|
|
121
|
+
]
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
### 2. Run the Extraction
|
|
125
|
+
|
|
126
|
+
Provide your input text and the prompt materials to the `lx.extract` function.
|
|
127
|
+
|
|
128
|
+
```python
|
|
129
|
+
# The input text to be processed
|
|
130
|
+
input_text = "Lady Juliet gazed longingly at the stars, her heart aching for Romeo"
|
|
131
|
+
|
|
132
|
+
# Run the extraction
|
|
133
|
+
result = lx.extract(
|
|
134
|
+
text_or_documents=input_text,
|
|
135
|
+
prompt_description=prompt,
|
|
136
|
+
examples=examples,
|
|
137
|
+
model_id="gemini-2.5-flash",
|
|
138
|
+
)
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
> **Model Selection**: `gemini-2.5-flash` is the recommended default, offering an excellent balance of speed, cost, and quality. For highly complex tasks requiring deeper reasoning, `gemini-2.5-pro` may provide superior results. For large-scale or production use, a Tier 2 Gemini quota is suggested to increase throughput and avoid rate limits. See the [rate-limit documentation](https://ai.google.dev/gemini-api/docs/rate-limits#tier-2) for details.
|
|
142
|
+
>
|
|
143
|
+
> **Model Lifecycle**: Note that Gemini models have a lifecycle with defined retirement dates. Users should consult the [official model version documentation](https://cloud.google.com/vertex-ai/generative-ai/docs/learn/model-versions) to stay informed about the latest stable and legacy versions.
|
|
144
|
+
|
|
145
|
+
### 3. Visualize the Results
|
|
146
|
+
|
|
147
|
+
The extractions can be saved to a `.jsonl` file, a popular format for working with language model data. LangExtract can then generate an interactive HTML visualization from this file to review the entities in context.
|
|
148
|
+
|
|
149
|
+
```python
|
|
150
|
+
# Save the results to a JSONL file
|
|
151
|
+
lx.io.save_annotated_documents([result], output_name="extraction_results.jsonl")
|
|
152
|
+
|
|
153
|
+
# Generate the visualization from the file
|
|
154
|
+
html_content = lx.visualize("extraction_results.jsonl")
|
|
155
|
+
with open("visualization.html", "w") as f:
|
|
156
|
+
f.write(html_content)
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
This creates an animated and interactive HTML file:
|
|
160
|
+
|
|
161
|
+

|
|
162
|
+
|
|
163
|
+
> **Note on LLM Knowledge Utilization:** This example demonstrates extractions that stay close to the text evidence - extracting "longing" for Lady Juliet's emotional state and identifying "yearning" from "gazed longingly at the stars." The task could be modified to generate attributes that draw more heavily from the LLM's world knowledge (e.g., adding `"identity": "Capulet family daughter"` or `"literary_context": "tragic heroine"`). The balance between text-evidence and knowledge-inference is controlled by your prompt instructions and example attributes.
|
|
164
|
+
|
|
165
|
+
### Scaling to Longer Documents
|
|
166
|
+
|
|
167
|
+
For larger texts, you can process entire documents directly from URLs with parallel processing and enhanced sensitivity:
|
|
168
|
+
|
|
169
|
+
```python
|
|
170
|
+
# Process Romeo & Juliet directly from Project Gutenberg
|
|
171
|
+
result = lx.extract(
|
|
172
|
+
text_or_documents="https://www.gutenberg.org/files/1513/1513-0.txt",
|
|
173
|
+
prompt_description=prompt,
|
|
174
|
+
examples=examples,
|
|
175
|
+
model_id="gemini-2.5-flash",
|
|
176
|
+
extraction_passes=3, # Improves recall through multiple passes
|
|
177
|
+
max_workers=20, # Parallel processing for speed
|
|
178
|
+
max_char_buffer=1000 # Smaller contexts for better accuracy
|
|
179
|
+
)
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
This approach can extract hundreds of entities from full novels while maintaining high accuracy. The interactive visualization seamlessly handles large result sets, making it easy to explore hundreds of entities from the output JSONL file. **[See the full *Romeo and Juliet* extraction example →](docs/examples/longer_text_example.md)** for detailed results and performance insights.
|
|
183
|
+
|
|
184
|
+
## Installation
|
|
185
|
+
|
|
186
|
+
### From PyPI
|
|
187
|
+
|
|
188
|
+
```bash
|
|
189
|
+
pip install langextract
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
*Recommended for most users. For isolated environments, consider using a virtual environment:*
|
|
193
|
+
|
|
194
|
+
```bash
|
|
195
|
+
python -m venv langextract_env
|
|
196
|
+
source langextract_env/bin/activate # On Windows: langextract_env\Scripts\activate
|
|
197
|
+
pip install langextract
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
### From Source
|
|
201
|
+
|
|
202
|
+
LangExtract uses modern Python packaging with `pyproject.toml` for dependency management:
|
|
203
|
+
|
|
204
|
+
*Installing with `-e` puts the package in development mode, allowing you to modify the code without reinstalling.*
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
```bash
|
|
208
|
+
git clone https://github.com/google/langextract.git
|
|
209
|
+
cd langextract
|
|
210
|
+
|
|
211
|
+
# For basic installation:
|
|
212
|
+
pip install -e .
|
|
213
|
+
|
|
214
|
+
# For development (includes linting tools):
|
|
215
|
+
pip install -e ".[dev]"
|
|
216
|
+
|
|
217
|
+
# For testing (includes pytest):
|
|
218
|
+
pip install -e ".[test]"
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
## API Key Setup for Cloud Models
|
|
223
|
+
|
|
224
|
+
When using LangExtract with cloud-hosted models (like Gemini), you'll need to
|
|
225
|
+
set up an API key. On-device models don't require an API key. For developers
|
|
226
|
+
using local LLMs, LangExtract offers built-in support for Ollama and can be
|
|
227
|
+
extended to other third-party APIs by updating the inference endpoints.
|
|
228
|
+
|
|
229
|
+
### API Key Sources
|
|
230
|
+
|
|
231
|
+
Get API keys from:
|
|
232
|
+
|
|
233
|
+
* [AI Studio](https://aistudio.google.com/app/apikey) for Gemini models
|
|
234
|
+
* [Vertex AI](https://cloud.google.com/vertex-ai/generative-ai/docs/sdks/overview) for enterprise use
|
|
235
|
+
|
|
236
|
+
### Setting up API key in your environment
|
|
237
|
+
|
|
238
|
+
**Option 1: Environment Variable**
|
|
239
|
+
|
|
240
|
+
```bash
|
|
241
|
+
export LANGEXTRACT_API_KEY="your-api-key-here"
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
**Option 2: .env File (Recommended)**
|
|
245
|
+
|
|
246
|
+
Add your API key to a `.env` file:
|
|
247
|
+
|
|
248
|
+
```bash
|
|
249
|
+
# Add API key to .env file
|
|
250
|
+
cat >> .env << 'EOF'
|
|
251
|
+
LANGEXTRACT_API_KEY=your-api-key-here
|
|
252
|
+
EOF
|
|
253
|
+
|
|
254
|
+
# Keep your API key secure
|
|
255
|
+
echo '.env' >> .gitignore
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
In your Python code:
|
|
259
|
+
```python
|
|
260
|
+
import langextract as lx
|
|
261
|
+
|
|
262
|
+
result = lx.extract(
|
|
263
|
+
text_or_documents=input_text,
|
|
264
|
+
prompt_description="Extract information...",
|
|
265
|
+
examples=[...],
|
|
266
|
+
model_id="gemini-2.5-flash"
|
|
267
|
+
)
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
**Option 3: Direct API Key (Not Recommended for Production)**
|
|
271
|
+
|
|
272
|
+
You can also provide the API key directly in your code, though this is not recommended for production use:
|
|
273
|
+
|
|
274
|
+
```python
|
|
275
|
+
result = lx.extract(
|
|
276
|
+
text_or_documents=input_text,
|
|
277
|
+
prompt_description="Extract information...",
|
|
278
|
+
examples=[...],
|
|
279
|
+
model_id="gemini-2.5-flash",
|
|
280
|
+
api_key="your-api-key-here" # Only use this for testing/development
|
|
281
|
+
)
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
## More Examples
|
|
285
|
+
|
|
286
|
+
Additional examples of LangExtract in action:
|
|
287
|
+
|
|
288
|
+
### *Romeo and Juliet* Full Text Extraction
|
|
289
|
+
|
|
290
|
+
LangExtract can process complete documents directly from URLs. This example demonstrates extraction from the full text of *Romeo and Juliet* from Project Gutenberg (147,843 characters), showing parallel processing, sequential extraction passes, and performance optimization for long document processing.
|
|
291
|
+
|
|
292
|
+
**[View *Romeo and Juliet* Full Text Example →](docs/examples/longer_text_example.md)**
|
|
293
|
+
|
|
294
|
+
### Medication Extraction
|
|
295
|
+
|
|
296
|
+
> **Disclaimer:** This demonstration is for illustrative purposes of LangExtract's baseline capability only. It does not represent a finished or approved product, is not intended to diagnose or suggest treatment of any disease or condition, and should not be used for medical advice.
|
|
297
|
+
|
|
298
|
+
LangExtract excels at extracting structured medical information from clinical text. These examples demonstrate both basic entity recognition (medication names, dosages, routes) and relationship extraction (connecting medications to their attributes), showing LangExtract's effectiveness for healthcare applications.
|
|
299
|
+
|
|
300
|
+
**[View Medication Examples →](docs/examples/medication_examples.md)**
|
|
301
|
+
|
|
302
|
+
### Radiology Report Structuring: RadExtract
|
|
303
|
+
|
|
304
|
+
Explore RadExtract, a live interactive demo on HuggingFace Spaces that shows how LangExtract can automatically structure radiology reports. Try it directly in your browser with no setup required.
|
|
305
|
+
|
|
306
|
+
**[View RadExtract Demo →](https://huggingface.co/spaces/google/radextract)**
|
|
307
|
+
|
|
308
|
+
## Contributing
|
|
309
|
+
|
|
310
|
+
Contributions are welcome! See [CONTRIBUTING.md](CONTRIBUTING.md) to get started
|
|
311
|
+
with development, testing, and pull requests. You must sign a
|
|
312
|
+
[Contributor License Agreement](https://cla.developers.google.com/about)
|
|
313
|
+
before submitting patches.
|
|
314
|
+
|
|
315
|
+
## Testing
|
|
316
|
+
|
|
317
|
+
To run tests locally from the source:
|
|
318
|
+
|
|
319
|
+
```bash
|
|
320
|
+
# Clone the repository
|
|
321
|
+
git clone https://github.com/google/langextract.git
|
|
322
|
+
cd langextract
|
|
323
|
+
|
|
324
|
+
# Install with test dependencies
|
|
325
|
+
pip install -e ".[test]"
|
|
326
|
+
|
|
327
|
+
# Run all tests
|
|
328
|
+
pytest tests
|
|
329
|
+
```
|
|
330
|
+
|
|
331
|
+
Or reproduce the full CI matrix locally with tox:
|
|
332
|
+
|
|
333
|
+
```bash
|
|
334
|
+
tox # runs pylint + pytest on Python 3.10 and 3.11
|
|
335
|
+
```
|
|
336
|
+
|
|
337
|
+
## Disclaimer
|
|
338
|
+
|
|
339
|
+
This is not an officially supported Google product. If you use
|
|
340
|
+
LangExtract in production or publications, please cite accordingly and
|
|
341
|
+
acknowledge usage. Use is subject to the [Apache 2.0 License](LICENSE).
|
|
342
|
+
For health-related applications, use of LangExtract is also subject to the
|
|
343
|
+
[Health AI Developer Foundations Terms of Use](https://developers.google.com/health-ai-developer-foundations/terms).
|
|
344
|
+
|
|
345
|
+
---
|
|
346
|
+
|
|
347
|
+
**Happy Extracting!**
|