diff --git a/examples/notebooks/ReplicateLlama2.ipynb b/examples/notebooks/ReplicateLlama2.ipynb index 17eda34d..a8591e46 100644 --- a/examples/notebooks/ReplicateLlama2.ipynb +++ b/examples/notebooks/ReplicateLlama2.ipynb @@ -98,8 +98,9 @@ "sd1 = \"replicate/llama-2-70b-chat:2796ee9483c3fd7aa2e171d38f4ca12251a30609463dcfd4cd76703f22e96cdf\"\n", "models = [sd1] # You can specify multiple models here\n", "input_kwargs = {\"prompt\": [\"Tell me a story about a 19th century wombat gentleman. Keep it short.\",\n", - " \"Tell me a story about a 22nd century wombat gentleman. Keep it short.\"]}\n", - "model_specific_kwargs = {sd1: {}}\n", + " \"Tell me a story about a 22nd century wombat gentleman. Keep it short.\"],\n", + " \"temperature\": [0.1, 1.5]}\n", + "model_specific_kwargs = {sd1: {}} # Optional, if you have any argument specifc to a model, you can pass it in there.\n", "\n", "experiment = ReplicateExperiment(models, input_kwargs, model_specific_kwargs)" ] @@ -114,7 +115,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 4, "id": "53b89b7f", "metadata": {}, "outputs": [ @@ -139,7 +140,8 @@ " \n", " \n", " \n", - " input\n", + " prompt\n", + " temperature\n", " response\n", " latency\n", " \n", @@ -147,32 +149,60 @@ " \n", " \n", " 0\n", - " {'prompt': 'Tell me a story about a 19th century wombat gentleman. Keep it short.'}\n", - " Once upon a time, in the late 19th century, there was a wombat named Winston who lived in the rolling hills of the English countryside. Winston was a gentleman of refined taste and manners, known for his impeccable dress sense and love of fine tea. Every afternoon, he would don his finest top hat and tails and venture into the nearby village for a spot of tea and a chat with the locals. Despite his somewhat unconventional appearance, Winston was well-liked by all who knew him, and his wit\n", - " 0.978888\n", + " Tell me a story about a 19th century wombat gentleman. Keep it short.\n", + " 0.1\n", + " Once upon a time, in the late 19th century, there was a wombat named Winston who lived in the rolling hills of the English countryside. Winston was a gentleman of refined taste and culture, known for his impeccable manners and love of fine tea. Every afternoon, he would don his finest top hat and take a stroll through the village, exchanging pleasantries with the locals and offering them scones and jam from his personal collection. Despite his dignified demeanor, Winston had a mischievous streak\n", + " 0.538249\n", " \n", " \n", " 1\n", - " {'prompt': 'Tell me a story about a 22nd century wombat gentleman. Keep it short.'}\n", - " Certainly! Here's a short tale about a 22nd century wombat gentleman:\\n\\nIn the year 2105, a wombat named Winston lived in a sprawling underground burrow complex beneath the city of New Sydney. Winston was a gentleman of leisure, spending his days lounging on plush cushions and sipping tea served by his loyal butler-bot. He was known throughout the city for his impeccable taste in fine hats and his ability to recite Shakespearean sonnets with ease.\n", - " 0.355229\n", + " Tell me a story about a 19th century wombat gentleman. Keep it short.\n", + " 1.5\n", + " Once upon a time, in the late 19th century, there was a wombat named Mr. Whiskers who lived in the rolling hills of the English countryside. Mr. Whiskers was a gentleman of great distinction and refinement, known for his impeccable manners and love of fine tea. He spent his days lounging in the sun, enjoying picnics with his friends, and engaging in spirited debates about the finer points of Victorian literature. Despite his leisurely lifestyle, Mr. Whiskers was a\n", + " 0.267989\n", + " \n", + " \n", + " 2\n", + " Tell me a story about a 22nd century wombat gentleman. Keep it short.\n", + " 0.1\n", + " Certainly! Here's a short tale about a 22nd century wombat gentleman:\\n\\nIn the year 2105, a wombat named Winston lived in a sprawling underground burrow complex beneath the city of New Sydney. Winston was a gentleman of leisure, spending his days lounging on plush cushions and sipping tea served by his loyal butler-bot. He was known throughout the city for his impeccable taste in fine hats and his extensive collection of rare, vintage cigars.\\n\n", + " 0.279196\n", + " \n", + " \n", + " 3\n", + " Tell me a story about a 22nd century wombat gentleman. Keep it short.\n", + " 1.5\n", + " Certainly! Here's a short tale about a 22nd century wombat gentleman:\\n\\nIn the year 2105, a wombat named Winston lived in a futuristic underground burrow. He was a gentleman of leisure, spending his days lounging on expensive virtual reality furniture and sipping champagne.\\n\\nOne day, Winston received an invitation to a high-society gathering hosted by a wealthy group of aliens. Eager to make a good impression, he donned his finest tux\n", + " 0.270601\n", " \n", " \n", "\n", "" ], "text/plain": [ - " input \\\n", - "0 {'prompt': 'Tell me a story about a 19th century wombat gentleman. Keep it short.'} \n", - "1 {'prompt': 'Tell me a story about a 22nd century wombat gentleman. Keep it short.'} \n", + " prompt \\\n", + "0 Tell me a story about a 19th century wombat gentleman. Keep it short. \n", + "1 Tell me a story about a 19th century wombat gentleman. Keep it short. \n", + "2 Tell me a story about a 22nd century wombat gentleman. Keep it short. \n", + "3 Tell me a story about a 22nd century wombat gentleman. Keep it short. \n", + "\n", + " temperature \\\n", + "0 0.1 \n", + "1 1.5 \n", + "2 0.1 \n", + "3 1.5 \n", "\n", - " response \\\n", - "0 Once upon a time, in the late 19th century, there was a wombat named Winston who lived in the rolling hills of the English countryside. Winston was a gentleman of refined taste and manners, known for his impeccable dress sense and love of fine tea. Every afternoon, he would don his finest top hat and tails and venture into the nearby village for a spot of tea and a chat with the locals. Despite his somewhat unconventional appearance, Winston was well-liked by all who knew him, and his wit \n", - "1 Certainly! Here's a short tale about a 22nd century wombat gentleman:\\n\\nIn the year 2105, a wombat named Winston lived in a sprawling underground burrow complex beneath the city of New Sydney. Winston was a gentleman of leisure, spending his days lounging on plush cushions and sipping tea served by his loyal butler-bot. He was known throughout the city for his impeccable taste in fine hats and his ability to recite Shakespearean sonnets with ease. \n", + " response \\\n", + "0 Once upon a time, in the late 19th century, there was a wombat named Winston who lived in the rolling hills of the English countryside. Winston was a gentleman of refined taste and culture, known for his impeccable manners and love of fine tea. Every afternoon, he would don his finest top hat and take a stroll through the village, exchanging pleasantries with the locals and offering them scones and jam from his personal collection. Despite his dignified demeanor, Winston had a mischievous streak \n", + "1 Once upon a time, in the late 19th century, there was a wombat named Mr. Whiskers who lived in the rolling hills of the English countryside. Mr. Whiskers was a gentleman of great distinction and refinement, known for his impeccable manners and love of fine tea. He spent his days lounging in the sun, enjoying picnics with his friends, and engaging in spirited debates about the finer points of Victorian literature. Despite his leisurely lifestyle, Mr. Whiskers was a \n", + "2 Certainly! Here's a short tale about a 22nd century wombat gentleman:\\n\\nIn the year 2105, a wombat named Winston lived in a sprawling underground burrow complex beneath the city of New Sydney. Winston was a gentleman of leisure, spending his days lounging on plush cushions and sipping tea served by his loyal butler-bot. He was known throughout the city for his impeccable taste in fine hats and his extensive collection of rare, vintage cigars.\\n \n", + "3 Certainly! Here's a short tale about a 22nd century wombat gentleman:\\n\\nIn the year 2105, a wombat named Winston lived in a futuristic underground burrow. He was a gentleman of leisure, spending his days lounging on expensive virtual reality furniture and sipping champagne.\\n\\nOne day, Winston received an invitation to a high-society gathering hosted by a wealthy group of aliens. Eager to make a good impression, he donned his finest tux \n", "\n", " latency \n", - "0 0.978888 \n", - "1 0.355229 " + "0 0.538249 \n", + "1 0.267989 \n", + "2 0.279196 \n", + "3 0.270601 " ] }, "metadata": {}, @@ -197,7 +227,118 @@ "id": "bebb8023", "metadata": {}, "source": [ - "This notebook is meant to showcase how you can use Replicate's API for Llama2. Please reference other notebook for evaluating the model's response. The examples from the notebook examples of OpenAI and Anthropic provide good references on how to evaluate LLM responses." + "This notebook is meant to showcase how you can use Replicate's API for Llama2. In this case, we will use a simple evalution - compute semantic distance to check if the model's response is similar to our expected output.\n", + "\n", + "Please reference other notebooks for various methods to evaluate a model's response. The examples include structual evaluation (e.g. for Python, JSON) and auto-evaluations (using another LLM)." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "bdc76ecc", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
prompttemperatureresponselatencysimilar_to_expected
0Tell me a story about a 19th century wombat gentleman. Keep it short.0.1Once upon a time, in the late 19th century, there was a wombat named Winston who lived in the rolling hills of the English countryside. Winston was a gentleman of refined taste and culture, known for his impeccable manners and love of fine tea. Every afternoon, he would don his finest top hat and take a stroll through the village, exchanging pleasantries with the locals and offering them scones and jam from his personal collection. Despite his dignified demeanor, Winston had a mischievous streak0.5382490.566713
1Tell me a story about a 19th century wombat gentleman. Keep it short.1.5Once upon a time, in the late 19th century, there was a wombat named Mr. Whiskers who lived in the rolling hills of the English countryside. Mr. Whiskers was a gentleman of great distinction and refinement, known for his impeccable manners and love of fine tea. He spent his days lounging in the sun, enjoying picnics with his friends, and engaging in spirited debates about the finer points of Victorian literature. Despite his leisurely lifestyle, Mr. Whiskers was a0.2679890.601912
2Tell me a story about a 22nd century wombat gentleman. Keep it short.0.1Certainly! Here's a short tale about a 22nd century wombat gentleman:\\n\\nIn the year 2105, a wombat named Winston lived in a sprawling underground burrow complex beneath the city of New Sydney. Winston was a gentleman of leisure, spending his days lounging on plush cushions and sipping tea served by his loyal butler-bot. He was known throughout the city for his impeccable taste in fine hats and his extensive collection of rare, vintage cigars.\\n0.2791960.688690
3Tell me a story about a 22nd century wombat gentleman. Keep it short.1.5Certainly! Here's a short tale about a 22nd century wombat gentleman:\\n\\nIn the year 2105, a wombat named Winston lived in a futuristic underground burrow. He was a gentleman of leisure, spending his days lounging on expensive virtual reality furniture and sipping champagne.\\n\\nOne day, Winston received an invitation to a high-society gathering hosted by a wealthy group of aliens. Eager to make a good impression, he donned his finest tux0.2706010.639222
\n", + "
" + ], + "text/plain": [ + " prompt \\\n", + "0 Tell me a story about a 19th century wombat gentleman. Keep it short. \n", + "1 Tell me a story about a 19th century wombat gentleman. Keep it short. \n", + "2 Tell me a story about a 22nd century wombat gentleman. Keep it short. \n", + "3 Tell me a story about a 22nd century wombat gentleman. Keep it short. \n", + "\n", + " temperature \\\n", + "0 0.1 \n", + "1 1.5 \n", + "2 0.1 \n", + "3 1.5 \n", + "\n", + " response \\\n", + "0 Once upon a time, in the late 19th century, there was a wombat named Winston who lived in the rolling hills of the English countryside. Winston was a gentleman of refined taste and culture, known for his impeccable manners and love of fine tea. Every afternoon, he would don his finest top hat and take a stroll through the village, exchanging pleasantries with the locals and offering them scones and jam from his personal collection. Despite his dignified demeanor, Winston had a mischievous streak \n", + "1 Once upon a time, in the late 19th century, there was a wombat named Mr. Whiskers who lived in the rolling hills of the English countryside. Mr. Whiskers was a gentleman of great distinction and refinement, known for his impeccable manners and love of fine tea. He spent his days lounging in the sun, enjoying picnics with his friends, and engaging in spirited debates about the finer points of Victorian literature. Despite his leisurely lifestyle, Mr. Whiskers was a \n", + "2 Certainly! Here's a short tale about a 22nd century wombat gentleman:\\n\\nIn the year 2105, a wombat named Winston lived in a sprawling underground burrow complex beneath the city of New Sydney. Winston was a gentleman of leisure, spending his days lounging on plush cushions and sipping tea served by his loyal butler-bot. He was known throughout the city for his impeccable taste in fine hats and his extensive collection of rare, vintage cigars.\\n \n", + "3 Certainly! Here's a short tale about a 22nd century wombat gentleman:\\n\\nIn the year 2105, a wombat named Winston lived in a futuristic underground burrow. He was a gentleman of leisure, spending his days lounging on expensive virtual reality furniture and sipping champagne.\\n\\nOne day, Winston received an invitation to a high-society gathering hosted by a wealthy group of aliens. Eager to make a good impression, he donned his finest tux \n", + "\n", + " latency similar_to_expected \n", + "0 0.538249 0.566713 \n", + "1 0.267989 0.601912 \n", + "2 0.279196 0.688690 \n", + "3 0.270601 0.639222 " + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "from prompttools.utils import semantic_similarity\n", + "\n", + "expected_responses = [\"wombat gentleman in 19th century\"] * 2 + [\"wombat gentleman in 22nd century\"] * 2\n", + "experiment.evaluate(\"similar_to_expected\", semantic_similarity, expected=expected_responses)\n", + "experiment.visualize()" ] } ],