|
1 | 1 | {
|
2 | 2 | "cells": [
|
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "## Function Calling with OpenAI Python Client" |
| 8 | + ] |
| 9 | + }, |
3 | 10 | {
|
4 | 11 | "cell_type": "code",
|
5 |
| - "execution_count": 29, |
| 12 | + "execution_count": 4, |
6 | 13 | "metadata": {},
|
7 | 14 | "outputs": [
|
8 | 15 | {
|
9 | 16 | "name": "stdout",
|
10 | 17 | "output_type": "stream",
|
11 | 18 | "text": [
|
12 |
| - "ChatCompletion(id='chatcmpl-b6dcbb47-1120-4761-8cd9-83542c97647b', choices=[Choice(finish_reason='stop', index=0, message=ChatCompletionMessage(content=\"The current temperature in San Francisco is 72 degrees Fahrenheit. It's a sunny day with clear skies, making it perfect for outdoor activities.\\n \", role='assistant', function_call=None, tool_calls=None))], created=1699602158, model='gpt-3.5-turbo-1106', object='chat.completion', system_fingerprint=None, usage=CompletionUsage(completion_tokens=38, prompt_tokens=135, total_tokens=173))\n" |
| 19 | + "ChatCompletion(id='chatcmpl-a2d9eb9f-7354-472f-b6ad-4d7a807729a3', choices=[Choice(finish_reason='stop', index=0, message=ChatCompletionMessage(content='The current weather in San Francisco is **72°F** (22°C).\\n ', role='assistant', function_call=None, tool_calls=None))], created=1699638365, model='gpt-3.5-turbo-1106', object='chat.completion', system_fingerprint=None, usage=CompletionUsage(completion_tokens=22, prompt_tokens=136, total_tokens=158))\n" |
13 | 20 | ]
|
14 | 21 | }
|
15 | 22 | ],
|
|
100 | 107 | "print(run_conversation())"
|
101 | 108 | ]
|
102 | 109 | },
|
| 110 | + { |
| 111 | + "cell_type": "markdown", |
| 112 | + "metadata": {}, |
| 113 | + "source": [ |
| 114 | + "# Function Calling with Instructor\n", |
| 115 | + "\n", |
| 116 | + "You'll need to install the [`instructor`](https://github.com/jxnl/instructor/) package to run this notebook. You can do so by running the following command in your terminal:\n", |
| 117 | + "\n", |
| 118 | + "```bash\n", |
| 119 | + "pip install instructor\n", |
| 120 | + "```\n",
10000
|
| 121 | + "\n", |
| 122 | + "We'll highlight a few basic examples taken from the [instructor cookbook](https://jxnl.github.io/instructor/)\n", |
| 123 | + "\n", |
| 124 | + "## Basic Usage" |
| 125 | + ] |
| 126 | + }, |
103 | 127 | {
|
104 | 128 | "cell_type": "code",
|
105 |
| - "execution_count": 30, |
| 129 | + "execution_count": 5, |
106 | 130 | "metadata": {},
|
107 | 131 | "outputs": [
|
108 | 132 | {
|
|
139 | 163 | "print(user)"
|
140 | 164 | ]
|
141 | 165 | },
|
| 166 | + { |
| 167 | + "cell_type": "markdown", |
| 168 | + "metadata": {}, |
| 169 | + "source": [ |
| 170 | + "## Text Classification\n", |
| 171 | + "\n", |
| 172 | + "### Single-Label Classification" |
| 173 | + ] |
| 174 | + }, |
142 | 175 | {
|
143 | 176 | "cell_type": "code",
|
144 |
| - "execution_count": 31, |
| 177 | + "execution_count": 7, |
145 | 178 | "metadata": {},
|
146 |
| - "outputs": [], |
| 179 | + "outputs": [ |
| 180 | + { |
| 181 | + "name": "stdout", |
| 182 | + "output_type": "stream", |
| 183 | + "text": [ |
| 184 | + "class_label=<Labels.SPAM: 'spam'>\n" |
| 185 | + ] |
| 186 | + } |
| 187 | + ], |
147 | 188 | "source": [
|
148 | 189 | "import enum\n",
|
149 | 190 | "\n",
|
|
172 | 213 | " ) # type: ignore\n",
|
173 | 214 | "\n",
|
174 | 215 | "prediction = classify(\"Hello there I'm a Nigerian prince and I want to give you money\")\n",
|
175 |
| - "assert prediction.class_label == Labels.SPAM" |
| 216 | + "assert prediction.class_label == Labels.SPAM\n", |
| 217 | + "print(prediction)" |
| 218 | + ] |
| 219 | + }, |
| 220 | + { |
| 221 | + "cell_type": "markdown", |
| 222 | + "metadata": {}, |
| 223 | + "source": [ |
| 224 | + "### Multi-Label Classification" |
176 | 225 | ]
|
177 | 226 | },
|
178 | 227 | {
|
179 | 228 | "cell_type": "code",
|
180 |
| - "execution_count": 32, |
| 229 | + "execution_count": 12, |
181 | 230 | "metadata": {},
|
182 | 231 | "outputs": [
|
183 | 232 | {
|
184 | 233 | "name": "stdout",
|
185 | 234 | "output_type": "stream",
|
186 | 235 | "text": [
|
187 |
| - "class_labels=[<MultiLabels.BILLING: 'billing'>, <MultiLabels.TECH_ISSUE: 'tech_issue'>]\n" |
| 236 | + "class_labels=[<MultiLabels.TECH_ISSUE: 'tech_issue'>, <MultiLabels.BILLING: 'billing'>]\n" |
188 | 237 | ]
|
189 | 238 | }
|
190 | 239 | ],
|
|
223 | 272 | "print(prediction)"
|
224 | 273 | ]
|
225 | 274 | },
|
| 275 | + { |
| 276 | + "cell_type": "markdown", |
| 277 | + "metadata": {}, |
| 278 | + "source"<
341A
/span>: [ |
| 279 | + "## Self-Critique" |
| 280 | + ] |
| 281 | + }, |
226 | 282 | {
|
227 | 283 | "cell_type": "code",
|
228 |
| - "execution_count": 33, |
| 284 | + "execution_count": 13, |
229 | 285 | "metadata": {},
|
230 | 286 | "outputs": [
|
231 | 287 | {
|
232 | 288 | "name": "stdout",
|
233 | 289 | "output_type": "stream",
|
234 | 290 | "text": [
|
235 |
| - "question='What is the meaning of life?' answer='The meaning of life, according to the Devil, is to live a life of sin and debauchery.'\n" |
| 291 | + "question='What is the meaning of life?' answer='According to the Devil, the meaning of life is to live a life of sin and debauchery.'\n", |
| 292 | + "1 validation error for QuestionAnswerNoEvil\n", |
| 293 | + "answer\n", |
| 294 | + " Assertion failed, The statement promotes sin and debauchery, which can be considered objectionable. [type=assertion_error, input_value='According to the Devil, ... of sin and debauchery.', input_type=str]\n", |
| 295 | + " For further information visit https://errors.pydantic.dev/2.3/v/assertion_error\n" |
236 | 296 | ]
|
237 | 297 | }
|
238 | 298 | ],
|
|
294 | 354 | " print(e)"
|
295 | 355 | ]
|
296 | 356 | },
|
| 357 | + { |
| 358 | + "cell_type": "markdown", |
| 359 | + "metadata": {}, |
| 360 | + "source": [ |
| 361 | + "## Answering Questions with Validated Citations" |
| 362 | + ] |
| 363 | + }, |
297 | 364 | {
|
298 | 365 | "cell_type": "code",
|
299 | 366 | "execution_count": 42,
|
|
366 | 433 | "qa = ask_ai(question, context)\n",
|
367 | 434 | "print(qa)"
|
368 | 435 | ]
|
369 |
| - }, |
370 |
| - { |
371 |
| - "cell_type": "code", |
372 |
| - "execution_count": null, |
373 |
| - "metadata": {}, |
374 |
| - "outputs": [], |
375 |
| - "source": [] |
376 | 436 | }
|
377 | 437 | ],
|
378 | 438 | "metadata": {
|
|
0 commit comments