In [ ]:
Copied!
In [1]:
Copied!
desc = """
### Typed Extraction
Information extraction that is automatically generated from a typed specification. [[Code](https://github.com/srush/MiniChain/blob/main/examples/stats.py)]
(Novel to MiniChain)
"""
desc = """
### Typed Extraction
Information extraction that is automatically generated from a typed specification. [[Code](https://github.com/srush/MiniChain/blob/main/examples/stats.py)]
(Novel to MiniChain)
"""
$
In [2]:
Copied!
from minichain import prompt, show, type_to_prompt, OpenAI
from dataclasses import dataclass
from typing import List
from enum import Enum
from minichain import prompt, show, type_to_prompt, OpenAI
from dataclasses import dataclass
from typing import List
from enum import Enum
Data specification
In [3]:
Copied!
class StatType(Enum):
POINTS = 1
REBOUNDS = 2
ASSISTS = 3
@dataclass
class Stat:
value: int
stat: StatType
@dataclass
class Player:
player: str
stats: List[Stat]
class StatType(Enum):
POINTS = 1
REBOUNDS = 2
ASSISTS = 3
@dataclass
class Stat:
value: int
stat: StatType
@dataclass
class Player:
player: str
stats: List[Stat]
In [4]:
Copied!
@prompt(OpenAI(), template_file="stats.pmpt.tpl", parser="json")
def stats(model, passage):
out = model(dict(passage=passage, typ=type_to_prompt(Player)))
return [Player(**j) for j in out]
@prompt(OpenAI(), template_file="stats.pmpt.tpl", parser="json")
def stats(model, passage):
out = model(dict(passage=passage, typ=type_to_prompt(Player)))
return [Player(**j) for j in out]
$
In [5]:
Copied!
article = open("sixers.txt").read()
gradio = show(lambda passage: stats(passage),
examples=[article],
subprompts=[stats],
out_type="json",
description=desc,
code=open("stats.py", "r").read().split("$")[1].strip().strip("#").strip(),
)
if __name__ == "__main__":
gradio.launch()
article = open("sixers.txt").read()
gradio = show(lambda passage: stats(passage),
examples=[article],
subprompts=[stats],
out_type="json",
description=desc,
code=open("stats.py", "r").read().split("$")[1].strip().strip("#").strip(),
)
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()`.
ExtractionPrompt().show({"passage": "Harden had 10 rebounds."}, '[{"player": "Harden", "stats": {"value": 10, "stat": 2}}]')
View the run log.¶
minichain.show_log("bash.log")