From 7eb03bd49da4377ef80f2b8def2d7928f396d23b Mon Sep 17 00:00:00 2001 From: Amir Mehr Date: Sun, 22 Sep 2024 11:54:51 -0600 Subject: [PATCH] Update Jupyter notebook with execution metadata and outputs (#28) --- py/cookbooks/labeled_few_shot/json.ipynb | 213 ++++++++++++----------- 1 file changed, 110 insertions(+), 103 deletions(-) diff --git a/py/cookbooks/labeled_few_shot/json.ipynb b/py/cookbooks/labeled_few_shot/json.ipynb index 5dedace..ab3e5ce 100644 --- a/py/cookbooks/labeled_few_shot/json.ipynb +++ b/py/cookbooks/labeled_few_shot/json.ipynb @@ -9,9 +9,12 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "metadata": { + "ExecuteTime": { + "end_time": "2024-09-22T17:40:30.630190Z", + "start_time": "2024-09-22T17:40:30.625616Z" + } + }, "source": [ "import sys\n", "import subprocess\n", @@ -57,31 +60,20 @@ "except ImportError as e:\n", " print(\"Failed to import zenbase: \", e)\n", " raise" - ] + ], + "outputs": [], + "execution_count": 66 }, { "cell_type": "code", - "execution_count": 11, "metadata": { - "ExecuteTime": { - "end_time": "2024-08-15T05:07:39.940886Z", - "start_time": "2024-08-15T05:07:39.935178Z" - }, "id": "c3tqXSlTbnfx", - "outputId": "aca30e48-4830-4b64-e23d-092c057ce195" - }, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 11, - "metadata": {}, - "output_type": "execute_result" + "outputId": "aca30e48-4830-4b64-e23d-092c057ce195", + "ExecuteTime": { + "end_time": "2024-09-22T17:40:30.642610Z", + "start_time": "2024-09-22T17:40:30.638026Z" } - ], + }, "source": [ "import random\n", "from pydantic import BaseModel\n", @@ -97,7 +89,20 @@ "# os.environ[\"OPENAI_API_KEY\"] = \"...\"\n", "\n", "load_dotenv('../../.env.test')" - ] + ], + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 67, + "metadata": {}, + "output_type": "execute_result" + } + ], + "execution_count": 67 }, { "cell_type": "markdown", @@ -121,21 +126,21 @@ }, { "cell_type": "code", - "execution_count": 12, "metadata": { - "ExecuteTime": { - "end_time": "2024-08-15T05:07:47.051766Z", - "start_time": "2024-08-15T05:07:47.013170Z" - }, "id": "6ikli_IQbnf0", - "outputId": "8ebf7c8b-7b40-4481-eb78-21439ce295ae" + "outputId": "8ebf7c8b-7b40-4481-eb78-21439ce295ae", + "ExecuteTime": { + "end_time": "2024-09-22T17:40:30.669819Z", + "start_time": "2024-09-22T17:40:30.649912Z" + } }, - "outputs": [], "source": [ "openai_client = OpenAI()\n", "instructor_client = instructor.from_openai(openai_client)\n", "zenbase_tracer = ZenbaseTracer()" - ] + ], + "outputs": [], + "execution_count": 68 }, { "cell_type": "markdown", @@ -148,18 +153,18 @@ }, { "cell_type": "code", - "execution_count": 13, "metadata": { + "id": "Cb6nVMWHbnf1", "ExecuteTime": { - "end_time": "2024-08-15T05:07:48.346002Z", - "start_time": "2024-08-15T05:07:48.343684Z" - }, - "id": "Cb6nVMWHbnf1" + "end_time": "2024-09-22T17:40:30.678123Z", + "start_time": "2024-09-22T17:40:30.676355Z" + } }, - "outputs": [], "source": [ "prompt_definition = \"Analyze the sentiment of the given text as either 'Positive', 'Negative', or 'Neutral'.\"" - ] + ], + "outputs": [], + "execution_count": 69 }, { "cell_type": "markdown", @@ -172,15 +177,13 @@ }, { "cell_type": "code", - "execution_count": 14, "metadata": { + "id": "lh40J51dbnf1", "ExecuteTime": { - "end_time": "2024-08-15T05:07:49.261307Z", - "start_time": "2024-08-15T05:07:49.256216Z" - }, - "id": "lh40J51dbnf1" + "end_time": "2024-09-22T17:40:30.687682Z", + "start_time": "2024-09-22T17:40:30.684218Z" + } }, - "outputs": [], "source": [ "class InputModel(BaseModel):\n", " text: str\n", @@ -188,7 +191,9 @@ "class OutputModel(BaseModel):\n", " reasoning: str\n", " sentiment: str" - ] + ], + "outputs": [], + "execution_count": 70 }, { "cell_type": "markdown", @@ -201,15 +206,13 @@ }, { "cell_type": "code", - "execution_count": 15, "metadata": { + "id": "ULmMnodkbnf2", "ExecuteTime": { - "end_time": "2024-08-15T05:07:50.368606Z", - "start_time": "2024-08-15T05:07:50.365080Z" - }, - "id": "ULmMnodkbnf2" + "end_time": "2024-09-22T17:40:30.696012Z", + "start_time": "2024-09-22T17:40:30.693579Z" + } }, - "outputs": [], "source": [ "dataset = [\n", " {\"text\": \"I love this product! It's amazing.\", \"sentiment\": \"Positive\"},\n", @@ -222,7 +225,9 @@ " {\"text\": \"I regret buying this product.\", \"sentiment\": \"Negative\"},\n", " {\"text\": \"I don't have strong feelings about it.\", \"sentiment\": \"Neutral\"},\n", "]" - ] + ], + "outputs": [], + "execution_count": 71 }, { "cell_type": "markdown", @@ -235,15 +240,13 @@ }, { "cell_type": "code", - "execution_count": 20, "metadata": { + "id": "EBvPfJWbbnf2", "ExecuteTime": { - "end_time": "2024-08-15T05:08:22.083507Z", - "start_time": "2024-08-15T05:08:22.079862Z" - }, - "id": "EBvPfJWbbnf2" + "end_time": "2024-09-22T17:40:30.704194Z", + "start_time": "2024-09-22T17:40:30.701629Z" + } }, - "outputs": [], "source": [ "random.shuffle(dataset)\n", "train_size = int(0.7 * len(dataset))\n", @@ -258,7 +261,9 @@ "\n", "training_set = process_data(train_set)\n", "validation_set = process_data(validation_set)" - ] + ], + "outputs": [], + "execution_count": 72 }, { "cell_type": "markdown", @@ -271,37 +276,29 @@ }, { "cell_type": "code", - "execution_count": 21, "metadata": { - "ExecuteTime": { - "end_time": "2024-08-15T05:08:36.836235Z", - "start_time": "2024-08-15T05:08:23.485298Z" - }, "id": "tDWnIxVdbnf2", - "outputId": "d225b6d5-24ad-4b12-ef1c-5416b93dff26" - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Evaluation of best candidate: {'score': 0.6666666666666666}\n" - ] + "outputId": "d225b6d5-24ad-4b12-ef1c-5416b93dff26", + "ExecuteTime": { + "end_time": "2024-09-22T17:40:41.954514Z", + "start_time": "2024-09-22T17:40:30.710191Z" } - ], + }, "source": [ "@zenbase_tracer.trace_function\n", "def sentiment_analyzer_function(request):\n", " messages = [\n", " {\"role\": \"system\", \"content\": prompt_definition},\n", - " {\"role\": \"user\", \"content\": str(request.inputs)},\n", + " \n", " ]\n", "\n", " if request.zenbase.task_demos:\n", " for demo in request.zenbase.task_demos:\n", " messages.append({\"role\": \"user\", \"content\": str(demo.inputs)})\n", " messages.append({\"role\": \"assistant\", \"content\": str(demo.outputs)})\n", - "\n", + " \n", + " messages.append({\"role\": \"user\", \"content\": str(request.inputs)})\n", + " \n", " return instructor_client.chat.completions.create(\n", " model=\"gpt-4o-mini\",\n", " response_model=OutputModel,\n", @@ -325,7 +322,17 @@ "sentiment_analyzer = result.best_function\n", "\n", "print(\"Evaluation of best candidate:\", result.best_candidate_result.evals)" - ] + ], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Evaluation of best candidate: {'score': 1.0}\n" + ] + } + ], + "execution_count": 73 }, { "cell_type": "markdown", @@ -338,15 +345,29 @@ }, { "cell_type": "code", - "execution_count": 22, "metadata": { - "ExecuteTime": { - "end_time": "2024-08-15T05:08:53.817823Z", - "start_time": "2024-08-15T05:08:50.533680Z" - }, "id": "PqIsfubObnf2", - "outputId": "a62c0442-bca9-4c7e-e2f2-cdea0addfc82" + "outputId": "a62c0442-bca9-4c7e-e2f2-cdea0addfc82", + "ExecuteTime": { + "end_time": "2024-09-22T17:40:44.662671Z", + "start_time": "2024-09-22T17:40:41.974800Z" + } }, + "source": [ + "sample_texts = [\n", + " \"I absolutely adore this new smartphone!\",\n", + " \"The customer service was terrible and unhelpful.\",\n", + " \"The movie was okay, nothing special.\",\n", + "]\n", + "\n", + "print(\"\\nAnalyzing sample texts:\")\n", + "for text in sample_texts:\n", + " answer = sentiment_analyzer(InputModel(text=text))\n", + " print(f\"Text: {text}\")\n", + " print(f\"Sentiment: {answer.sentiment}\")\n", + " print(f\"Reasoning: {answer.reasoning}\")\n", + " print()" + ], "outputs": [ { "name": "stdout", @@ -356,34 +377,20 @@ "Analyzing sample texts:\n", "Text: I absolutely adore this new smartphone!\n", "Sentiment: Positive\n", - "Reasoning: The text expresses a strong positive emotion towards the subject, indicating enthusiasm and satisfaction about the statement made.\n", + "Reasoning: The use of the word 'absolutely adore' expresses strong positive feelings toward the new smartphone, indicating a positive sentiment.\n", "\n", "Text: The customer service was terrible and unhelpful.\n", - "Sentiment: Positive\n", - "Reasoning: The text expresses a high level of satisfaction and enthusiasm, indicating a very positive sentiment towards the subject.\n", + "Sentiment: Negative\n", + "Reasoning: The phrase 'customer service was terrible and unhelpful' clearly expresses dissatisfaction and negative feelings towards the customer service experience.\n", "\n", "Text: The movie was okay, nothing special.\n", - "Sentiment: Positive\n", - "Reasoning: The text expresses a strong positive emotion toward something, indicating a high level of satisfaction and appreciation. The word 'great' reflects a positive sentiment.\n", + "Sentiment: Neutral\n", + "Reasoning: The phrase 'okay' indicates a moderate level of satisfaction, while 'nothing special' suggests that it did not exceed expectations or leave a strong impression. Together, these phrases convey a neutral sentiment towards the movie.\n", "\n" ] } ], - "source": [ - "sample_texts = [\n", - " \"I absolutely adore this new smartphone!\",\n", - " \"The customer service was terrible and unhelpful.\",\n", - " \"The movie was okay, nothing special.\",\n", - "]\n", - "\n", - "print(\"\\nAnalyzing sample texts:\")\n", - "for text in sample_texts:\n", - " answer = sentiment_analyzer(InputModel(text=text))\n", - " print(f\"Text: {text}\")\n", - " print(f\"Sentiment: {answer.sentiment}\")\n", - " print(f\"Reasoning: {answer.reasoning}\")\n", - " print()" - ] + "execution_count": 74 } ], "metadata": {