Skip to content

Commit

Permalink
Update Jupyter notebook with execution metadata and outputs (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
ammirsm authored Sep 22, 2024
1 parent 9470de0 commit 7eb03bd
Showing 1 changed file with 110 additions and 103 deletions.
213 changes: 110 additions & 103 deletions py/cookbooks/labeled_few_shot/json.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -172,23 +177,23 @@
},
{
"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",
"\n",
"class OutputModel(BaseModel):\n",
" reasoning: str\n",
" sentiment: str"
]
],
"outputs": [],
"execution_count": 70
},
{
"cell_type": "markdown",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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": {
Expand Down

0 comments on commit 7eb03bd

Please sign in to comment.