Created
February 7, 2026 12:28
-
-
Save wtnabe/1016a5001bc9edb367ab39118ef36308 to your computer and use it in GitHub Desktop.
LangExtract practice with Ruby and Pycall.rb ( and uv and Ollama )
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
| require "pycall" | |
| # see https://github.com/google/langextract/blob/main/examples/notebooks/romeo_juliet_extraction.ipynb | |
| ENV["PYTHON"] = File.join(__dir__, ".venv/bin/python") | |
| ENV["PYTHONPATH"] = File.join(__dir__, ".venv/lib/python3.14/site-packages") | |
| pylx = PyCall.import_module "langextract" | |
| pybuiltins = PyCall.import_module "builtins" | |
| prompt = <<EOD | |
| 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. | |
| EOD | |
| examples = [ | |
| pylx.data.ExampleData.new( | |
| text: "ROMEO. But soft! What light through yonder window breaks? It is the east, and Juliet is the sun.", | |
| extractions: [ | |
| pylx.data.Extraction.new( | |
| extraction_class: "character", | |
| extraction_text: "ROMEO", | |
| attributes: {"emotional_state": "wonder"} | |
| ), | |
| pylx.data.Extraction.new( | |
| extraction_class: "emotion", | |
| extraction_text: "But soft!", | |
| attributes: {"feeling": "gentle awe"} | |
| ), | |
| pylx.data.Extraction.new( | |
| extraction_class: "relationship", | |
| extraction_text: "Juliet is the sun", | |
| attributes: {"type": "metaphor"} | |
| ), | |
| ] | |
| ) | |
| ] | |
| r = pylx.extract( | |
| text_or_documents: "Lady Juliet gazed longingly at the stars, her heart aching for Romeo", | |
| prompt_description: prompt, | |
| examples: examples, | |
| model_id: "gemma3:1b", | |
| model_url: "http://localhost:11434" | |
| ) | |
| pp r.extractions.map { |e| pybuiltins.vars(e).to_h } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ruby -> Python
Class()を
にすればイケそう。
Python -> Ruby
deep にすべての Object が map されるわけではなさそうなので、自前でどうにかする。上の例は
to_hで Hash に持ってこれる