File size: 1,355 Bytes
006a13d
757c4ed
 
 
 
 
 
 
 
 
 
 
006a13d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
757c4ed
 
 
 
006a13d
757c4ed
006a13d
757c4ed
 
 
 
 
 
 
 
 
 
006a13d
 
757c4ed
 
006a13d
757c4ed
006a13d
757c4ed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

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)
"""

# $

from minichain import prompt, show, type_to_prompt, OpenAI
from dataclasses import dataclass
from typing import List
from enum import Enum

# Data specification

# +
class StatType(Enum):
    POINTS = 1
    REBOUNDS = 2
    ASSISTS = 3

@dataclass
class Stat:
    value: int
    stat: StatType

@dataclass
class Player:
    player: str
    stats: List[Stat]
# -


@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]  

# $

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()


# ExtractionPrompt().show({"passage": "Harden had 10 rebounds."},
#                         '[{"player": "Harden", "stats": {"value": 10, "stat": 2}}]')

# # View the run log.

# minichain.show_log("bash.log")