File size: 3,405 Bytes
7b856a8
 
4e3dc76
 
 
872b692
4e3dc76
f5ec828
4e3dc76
 
f5ec828
 
872b692
f5ec828
4e3dc76
 
 
f5ec828
872b692
4e3dc76
f5ec828
4e3dc76
 
 
 
 
 
f5ec828
4e3dc76
 
 
 
 
7b856a8
 
f5ec828
872b692
7b856a8
 
 
 
 
4e3dc76
7b856a8
 
 
 
 
 
 
872b692
7b856a8
 
 
 
 
 
 
f5ec828
872b692
f5ec828
7b856a8
 
 
 
 
 
4e3dc76
7b856a8
 
 
 
4e3dc76
7b856a8
 
 
 
 
 
 
 
f5ec828
872b692
f5ec828
7b856a8
 
4e3dc76
 
 
f5ec828
7b856a8
 
 
 
f5ec828
872b692
7b856a8
4e3dc76
7b856a8
f5ec828
4e3dc76
 
 
 
 
 
 
 
 
 
 
 
 
 
872b692
4e3dc76
7b856a8
 
 
 
 
 
 
872b692
7b856a8
 
4e3dc76
7b856a8
 
 
4e3dc76
872b692
4e3dc76
7b856a8
 
 
 
 
 
 
f5ec828
 
 
7b856a8
 
 
 
 
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "247c85dd",
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [],
   "source": [
    "!pip install -q git+https://github.com/srush/MiniChain\n",
    "!git clone https://github.com/srush/MiniChain; cp -fr MiniChain/examples/* . "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d36498d1",
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [],
   "source": [
    "desc = \"\"\"\n",
    "### Typed Extraction\n",
    "\n",
    "Information extraction that is automatically generated from a typed specification. [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/srush/MiniChain/blob/master/examples/pal.ipynb)\n",
    "\n",
    "(Novel to MiniChain)\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e894e4fb",
   "metadata": {
    "lines_to_next_cell": 1
   },
   "outputs": [],
   "source": [
    "from minichain import prompt, show, type_to_prompt, OpenAI\n",
    "from dataclasses import dataclass\n",
    "from typing import List\n",
    "from enum import Enum"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6ac1ff70",
   "metadata": {},
   "source": [
    "Data specification"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "64a00e69",
   "metadata": {},
   "outputs": [],
   "source": [
    "class StatType(Enum):\n",
    "    POINTS = 1\n",
    "    REBOUNDS = 2\n",
    "    ASSISTS = 3\n",
    "\n",
    "@dataclass\n",
    "class Stat:\n",
    "    value: int\n",
    "    stat: StatType\n",
    "\n",
    "@dataclass\n",
    "class Player:\n",
    "    player: str\n",
    "    stats: List[Stat]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "35e0ed85",
   "metadata": {},
   "outputs": [],
   "source": [
    "@prompt(OpenAI(), template_file=\"stats.pmpt.tpl\", parser=\"json\")\n",
    "def stats(model, passage):\n",
    "    out = model(dict(passage=passage, typ=type_to_prompt(Player)))\n",
    "    return [Player(**j) for j in out]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7241bdc6",
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [],
   "source": [
    "article = open(\"sixers.txt\").read()\n",
    "gradio = show(lambda passage: stats(passage),\n",
    "              examples=[article],\n",
    "              subprompts=[stats],\n",
    "              out_type=\"json\",\n",
    "              description=desc,\n",
    ")\n",
    "if __name__ == \"__main__\":\n",
    "    gradio.launch()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "18ed1b96",
   "metadata": {},
   "source": [
    "ExtractionPrompt().show({\"passage\": \"Harden had 10 rebounds.\"},\n",
    "                        '[{\"player\": \"Harden\", \"stats\": {\"value\": 10, \"stat\": 2}}]')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "02c96e7d",
   "metadata": {},
   "source": [
    "# View the run log."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0ea427dd",
   "metadata": {},
   "source": [
    "minichain.show_log(\"bash.log\")"
   ]
  }
 ],
 "metadata": {
  "jupytext": {
   "cell_metadata_filter": "-all",
   "main_language": "python",
   "notebook_metadata_filter": "-all"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}