Skip to content

Instantly share code, notes, and snippets.

@fsndzomga
Created September 15, 2023 13:06
Show Gist options
  • Save fsndzomga/9c6bcaff18a1cd86279e5218e44c9dfc to your computer and use it in GitHub Desktop.
Save fsndzomga/9c6bcaff18a1cd86279e5218e44c9dfc to your computer and use it in GitHub Desktop.
relation extraction example
import os
from pathlib import Path
from typing import Optional
import typer
from wasabi import msg
from spacy_llm.util import assemble
Arg = typer.Argument
Opt = typer.Option
def run_pipeline(
# fmt: off
text: str = Arg("", help="Text to perform text categorization on."),
config_path: Path = Arg(..., help="Path to the configuration file to use."),
examples_path: Optional[Path] = Arg(None, help="Path to the examples file to use (few-shot only)."),
verbose: bool = Opt(False, "--verbose", "-v", help="Show extra information."),
# fmt: on
):
if not os.getenv("OPENAI_API_KEY", None):
msg.fail(
"OPENAI_API_KEY env variable was not found. "
"Set it by running 'export OPENAI_API_KEY=...' and try again.",
exits=1,
)
msg.text(f"Loading config from {config_path}", show=verbose)
nlp = assemble(
config_path,
overrides={}
if examples_path is None
else {"paths.examples": str(examples_path)},
)
doc = nlp(text)
msg.text(f"Text: {doc.text}")
msg.text(f"Entities: {[(ent.text, ent.label_) for ent in doc.ents]}")
msg.text("Relations:")
for r in doc._.rel:
msg.text(f" - {doc.ents[r.dep]} [{r.relation}] {doc.ents[r.dest]}")
if __name__ == "__main__":
typer.run(run_pipeline)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment