Skip to content

Instantly share code, notes, and snippets.

@wtnabe
Created February 7, 2026 12:28
Show Gist options
  • Select an option

  • Save wtnabe/1016a5001bc9edb367ab39118ef36308 to your computer and use it in GitHub Desktop.

Select an option

Save wtnabe/1016a5001bc9edb367ab39118ef36308 to your computer and use it in GitHub Desktop.
LangExtract practice with Ruby and Pycall.rb ( and uv and Ollama )
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 }
@wtnabe
Copy link
Author

wtnabe commented Feb 7, 2026

Ruby -> Python

Class()

Class.new

にすればイケそう。

Python -> Ruby

deep にすべての Object が map されるわけではなさそうなので、自前でどうにかする。上の例は

  • AnnotatedDocument の extractions プロパティが Extraction の List になっている
  • List は PyCall::List になって Ruby っぽく扱える
  • Extraction は残念ながら Object になっているのでそのままでは便利に扱えないので vars() で Dict にしている
  • vars() の時点でプロパティが Dict の key-value になって、Dict は PyCall::Dict に変換される
  • PyCall::Dict にも Ruby らしい便利メソッドがあり、ここで to_h で Hash に持ってこれる

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment