Ner
In [1]:
Copied!
desc = """
### Named Entity Recognition
Chain that does named entity recognition with arbitrary labels. [[Code](https://github.com/srush/MiniChain/blob/main/examples/ner.py)]
(Adapted from [promptify](https://github.com/promptslab/Promptify/blob/main/promptify/prompts/nlp/templates/ner.jinja)).
"""
desc = """
### Named Entity Recognition
Chain that does named entity recognition with arbitrary labels. [[Code](https://github.com/srush/MiniChain/blob/main/examples/ner.py)]
(Adapted from [promptify](https://github.com/promptslab/Promptify/blob/main/promptify/prompts/nlp/templates/ner.jinja)).
"""
$
In [2]:
Copied!
from minichain import prompt, show, OpenAI
from minichain import prompt, show, OpenAI
In [3]:
Copied!
@prompt(OpenAI(), template_file = "ner.pmpt.tpl", parser="json")
def ner_extract(model, **kwargs):
return model(kwargs)
@prompt(OpenAI(), template_file = "ner.pmpt.tpl", parser="json")
def ner_extract(model, **kwargs):
return model(kwargs)
In [4]:
Copied!
@prompt(OpenAI())
def team_describe(model, inp):
query = "Can you describe these basketball teams? " + \
" ".join([i["E"] for i in inp if i["T"] =="Team"])
return model(query)
@prompt(OpenAI())
def team_describe(model, inp):
query = "Can you describe these basketball teams? " + \
" ".join([i["E"] for i in inp if i["T"] =="Team"])
return model(query)
In [5]:
Copied!
def ner(text_input, labels, domain):
extract = ner_extract(dict(text_input=text_input, labels=labels, domain=domain))
return team_describe(extract)
def ner(text_input, labels, domain):
extract = ner_extract(dict(text_input=text_input, labels=labels, domain=domain))
return team_describe(extract)
$
In [6]:
Copied!
gradio = show(ner,
examples=[["An NBA playoff pairing a year ago, the 76ers (39-20) meet the Miami Heat (32-29) for the first time this season on Monday night at home.", "Team, Date", "Sports"]],
description=desc,
subprompts=[ner_extract, team_describe],
code=open("ner.py", "r").read().split("$")[1].strip().strip("#").strip(),
)
gradio = show(ner,
examples=[["An NBA playoff pairing a year ago, the 76ers (39-20) meet the Miami Heat (32-29) for the first time this season on Monday night at home.", "Team, Date", "Sports"]],
description=desc,
subprompts=[ner_extract, team_describe],
code=open("ner.py", "r").read().split("$")[1].strip().strip("#").strip(),
)
In [7]:
Copied!
if __name__ == "__main__":
gradio.launch()
if __name__ == "__main__":
gradio.launch()
Running on local URL: http://127.0.0.1:7861 To create a public link, set `share=True` in `launch()`.
View prompt examples.
In [8]:
Copied!
# NERPrompt().show(
# {
# "input": "I went to New York",
# "domain": "Travel",
# "labels": ["City"]
# },
# '[{"T": "City", "E": "New York"}]',
# )
# # -
# # View log.
# minichain.show_log("ner.log")
# NERPrompt().show(
# {
# "input": "I went to New York",
# "domain": "Travel",
# "labels": ["City"]
# },
# '[{"T": "City", "E": "New York"}]',
# )
# # -
# # View log.
# minichain.show_log("ner.log")