File size: 5,666 Bytes
ed4d993
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2def22ea",
   "metadata": {},
   "source": [
    "# Extraction with OpenAI Tools\n",
    "\n",
    "Performing extraction has never been easier! OpenAI's tool calling ability is the perfect thing to use as it allows for extracting multiple different elements from text that are different types. \n",
    "\n",
    "Models after 1106 use tools and support \"parallel function calling\" which makes this super easy."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "5c628496",
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List, Optional\n",
    "\n",
    "from langchain.chains.openai_tools import create_extraction_chain_pydantic\n",
    "from langchain_core.pydantic_v1 import BaseModel\n",
    "from langchain_openai import ChatOpenAI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "afe9657b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Make sure to use a recent model that supports tools\n",
    "model = ChatOpenAI(model=\"gpt-3.5-turbo-1106\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "bc0ca3b6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Pydantic is an easy way to define a schema\n",
    "class Person(BaseModel):\n",
    "    \"\"\"Information about people to extract.\"\"\"\n",
    "\n",
    "    name: str\n",
    "    age: Optional[int] = None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "2036af68",
   "metadata": {},
   "outputs": [],
   "source": [
    "chain = create_extraction_chain_pydantic(Person, model)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "1748ad21",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[Person(name='jane', age=2), Person(name='bob', age=3)]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chain.invoke({\"input\": \"jane is 2 and bob is 3\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "c8262ce5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Let's define another element\n",
    "class Class(BaseModel):\n",
    "    \"\"\"Information about classes to extract.\"\"\"\n",
    "\n",
    "    teacher: str\n",
    "    students: List[str]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "4973c104",
   "metadata": {},
   "outputs": [],
   "source": [
    "chain = create_extraction_chain_pydantic([Person, Class], model)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "e976a15e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[Person(name='jane', age=2),\n",
       " Person(name='bob', age=3),\n",
       " Class(teacher='Mrs Sampson', students=['jane', 'bob'])]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chain.invoke({\"input\": \"jane is 2 and bob is 3 and they are in Mrs Sampson's class\"})"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6575a7d6",
   "metadata": {},
   "source": [
    "## Under the hood\n",
    "\n",
    "Under the hood, this is a simple chain:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b8ba83e5",
   "metadata": {},
   "source": [
    "```python\n",
    "from typing import Union, List, Type, Optional\n",
    "\n",
    "from langchain.output_parsers.openai_tools import PydanticToolsParser\n",
    "from langchain.utils.openai_functions import convert_pydantic_to_openai_tool\n",
    "from langchain_core.runnables import Runnable\n",
    "from langchain_core.pydantic_v1 import BaseModel\n",
    "from langchain_core.prompts import ChatPromptTemplate\n",
    "from langchain_core.messages import SystemMessage\n",
    "from langchain_core.language_models import BaseLanguageModel\n",
    "\n",
    "_EXTRACTION_TEMPLATE = \"\"\"Extract and save the relevant entities mentioned \\\n",
    "in the following passage together with their properties.\n",
    "\n",
    "If a property is not present and is not required in the function parameters, do not include it in the output.\"\"\"  # noqa: E501\n",
    "\n",
    "\n",
    "def create_extraction_chain_pydantic(\n",
    "    pydantic_schemas: Union[List[Type[BaseModel]], Type[BaseModel]],\n",
    "    llm: BaseLanguageModel,\n",
    "    system_message: str = _EXTRACTION_TEMPLATE,\n",
    ") -> Runnable:\n",
    "    if not isinstance(pydantic_schemas, list):\n",
    "        pydantic_schemas = [pydantic_schemas]\n",
    "    prompt = ChatPromptTemplate.from_messages([\n",
    "        (\"system\", system_message),\n",
    "        (\"user\", \"{input}\")\n",
    "    ])\n",
    "    tools = [convert_pydantic_to_openai_tool(p) for p in pydantic_schemas]\n",
    "    model = llm.bind(tools=tools)\n",
    "    chain = prompt | model | PydanticToolsParser(tools=pydantic_schemas)\n",
    "    return chain\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2eac6b68",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}