Last active
August 1, 2025 09:26
-
-
Save Yasushi/a0004643c5eaca96ae96042791db42ef to your computer and use it in GitHub Desktop.
langextract https://github.com/google/langextract ollama example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /// script | |
# dependencies = [ | |
# "langextract" | |
# ] | |
# /// | |
import langextract as lx | |
import textwrap | |
# 1. Define the prompt and extraction rules | |
prompt = textwrap.dedent("""\ | |
Extract characters, emotions, and relationships in order of appearance. | |
Use exact text for extractions. Do not paraphrase or overlap entities. | |
Provide meaningful attributes for each entity to add context.""") | |
# 2. Provide a high-quality example to guide the model | |
examples = [ | |
lx.data.ExampleData( | |
text="ROMEO. But soft! What light through yonder window breaks? It is the east, and Juliet is the sun.", | |
extractions=[ | |
lx.data.Extraction( | |
extraction_class="character", | |
extraction_text="ROMEO", | |
attributes={"emotional_state": "wonder"} | |
), | |
lx.data.Extraction( | |
extraction_class="emotion", | |
extraction_text="But soft!", | |
attributes={"feeling": "gentle awe"} | |
), | |
lx.data.Extraction( | |
extraction_class="relationship", | |
extraction_text="Juliet is the sun", | |
attributes={"type": "metaphor"} | |
), | |
] | |
) | |
] | |
# The input text to be processed | |
input_text = "Lady Juliet gazed longingly at the stars, her heart aching for Romeo" | |
# Run the extraction | |
result = lx.extract( | |
text_or_documents=input_text, | |
prompt_description=prompt, | |
examples=examples, | |
language_model_type=lx.inference.OllamaLanguageModel, | |
language_model_params={"model": "gemma3:latest"}, | |
model_url="http://localhost:11434" | |
) | |
# Save the results to a JSONL file | |
lx.io.save_annotated_documents([result], output_name="extraction_results.jsonl") | |
# Generate the visualization from the file | |
html_content = lx.visualize("test_output/extraction_results.jsonl") | |
with open("test_output/visualization.html", "w") as f: | |
f.write(html_content) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment