From 85ead98a3e385191bc2782773f697d19eb14f267 Mon Sep 17 00:00:00 2001 From: Andrei Betlen Date: Fri, 10 Nov 2023 12:49:14 -0500 Subject: [PATCH] Update Functions notebook example --- examples/notebooks/Functions.ipynb | 96 ++++++++++++++++++++++++------ 1 file changed, 78 insertions(+), 18 deletions(-) diff --git a/examples/notebooks/Functions.ipynb b/examples/notebooks/Functions.ipynb index 81d58f6..7825cc8 100644 --- a/examples/notebooks/Functions.ipynb +++ b/examples/notebooks/Functions.ipynb @@ -1,15 +1,22 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Function Calling with OpenAI Python Client" + ] + }, { "cell_type": "code", - "execution_count": 29, + "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "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" + "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" ] } ], @@ -100,9 +107,26 @@ "print(run_conversation())" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Function Calling with Instructor\n", + "\n", + "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", + "\n", + "```bash\n", + "pip install instructor\n", + "```\n", + "\n", + "We'll highlight a few basic examples taken from the [instructor cookbook](https://jxnl.github.io/instructor/)\n", + "\n", + "## Basic Usage" + ] + }, { "cell_type": "code", - "execution_count": 30, + "execution_count": 5, "metadata": {}, "outputs": [ { @@ -140,10 +164,27 @@ ] }, { - "cell_type": "code", - "execution_count": 31, + "cell_type": "markdown", "metadata": {}, - "outputs": [], + "source": [ + "## Text Classification\n", + "\n", + "### Single-Label Classification" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "class_label=\n" + ] + } + ], "source": [ "import enum\n", "\n", @@ -172,19 +213,27 @@ " ) # type: ignore\n", "\n", "prediction = classify(\"Hello there I'm a Nigerian prince and I want to give you money\")\n", - "assert prediction.class_label == Labels.SPAM" + "assert prediction.class_label == Labels.SPAM\n", + "print(prediction)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Multi-Label Classification" ] }, { "cell_type": "code", - "execution_count": 32, + "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "class_labels=[, ]\n" + "class_labels=[, ]\n" ] } ], @@ -223,16 +272,27 @@ "print(prediction)" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Self-Critique" + ] + }, { "cell_type": "code", - "execution_count": 33, + "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "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" + "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", + "1 validation error for QuestionAnswerNoEvil\n", + "answer\n", + " 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", + " For further information visit https://errors.pydantic.dev/2.3/v/assertion_error\n" ] } ], @@ -294,6 +354,13 @@ " print(e)" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Answering Questions with Validated Citations" + ] + }, { "cell_type": "code", "execution_count": 42, @@ -366,13 +433,6 @@ "qa = ask_ai(question, context)\n", "print(qa)" ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": {