Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
steventkrawczyk committed Aug 4, 2023
2 parents 5b5cde1 + 1b9ef99 commit 15a60d2
Show file tree
Hide file tree
Showing 9 changed files with 762 additions and 11 deletions.
209 changes: 209 additions & 0 deletions examples/notebooks/LangChainSequentialChainExperiment.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Lang Chain Sequential Chain Experiment Example"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"\u001b[1m> Entering new SequentialChain chain...\u001b[0m\n",
"\n",
"\u001b[1m> Finished chain.\u001b[0m\n",
"\n",
"\n",
"\u001b[1m> Entering new SequentialChain chain...\u001b[0m\n",
"\n",
"\u001b[1m> Finished chain.\u001b[0m\n",
"\n",
"\n",
"\u001b[1m> Entering new SequentialChain chain...\u001b[0m\n",
"\n",
"\u001b[1m> Finished chain.\u001b[0m\n",
"\n",
"\n",
"\u001b[1m> Entering new SequentialChain chain...\u001b[0m\n",
"\n",
"\u001b[1m> Finished chain.\u001b[0m\n"
]
},
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>prompt</th>\n",
" <th>response</th>\n",
" <th>latency</th>\n",
" <th>similar_to_expected</th>\n",
" <th>temperature</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>أنا أحب أكل الشاورما</td>\n",
" <td>{'Review': 'أنا أحب أكل الشاورما', 'English_Review': 'I love eating shawarma.', 'summary': 'The reviewer expresses their strong enjoyment for eating shawarma.', 'followup_message': 'تشكر المراجع على تعبيرهم القوي عن استمتاعهم الكبير بتناول الشاورما. نحن سعداء جدًا لمعرفة أنكم استمتعتم بهذه الوجبة الشهية. نحن نبذل قصارى جهدنا لتقديم أفضل جودة للشاورما ونحن نقدر تقديركم العالي لذلك. نتطلع إلى خدمتكم مرة أخرى في المستقبل القريب. شكرًا جزيلاً على تقييمكم الإيجابي ونحن نتطلع لرؤيتكم قريبًا.'}</td>\n",
" <td>12.416809</td>\n",
" <td>0.179893</td>\n",
" <td>0.1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>أنا أحب أكل الشاورما</td>\n",
" <td>{'Review': 'أنا أحب أكل الشاورما', 'English_Review': 'I love eating shawarma.', 'summary': 'The reviewer expresses their strong affection for shawarma.', 'followup_message': 'تعليق متابع: أشكرك على تقديم هذا المراجعة الرائعة عن الشاورما! أنا متحمس جدًا لمشاركة هذه الشغف القوي لديك تجاه هذا الطبق الشهي. الشاورما حقًا أطباق لذيذة ولا يمكن مقاومتها، فمجرد التفكير فيها يجعل اللعاب يسيل من فمي! ومن ثمة، عندما تتناغم نكهات اللحم المشوي العطري والصلصات اللذيذة والخضروات الطازجة في رغيف الخبز الطري، فإنها تعتبر تجربة لا مثيل لها بالفعل. أحب كيف يمكن تخصيص الشاورما حسب الأذواق الشخصية، سواء بإضافة الثوم، البقدونس، أو الصلصة الحارة، وهذا يجعل كل شاورما فريدة من نوعها. أعتقد أنه لا يمكن لأحد أن يجرب هذا الطبق الرائع ويبقى مستغربًا من حبكم العميق للشاورما. شكرًا لمشاركتكم هذه التجربة المدهشة!'}</td>\n",
" <td>17.276991</td>\n",
" <td>0.131922</td>\n",
" <td>0.9</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>me encanta comer shawarma</td>\n",
" <td>{'Review': 'me encanta comer shawarma', 'English_Review': 'I love eating shawarma.', 'summary': 'The reviewer expresses their strong enjoyment for eating shawarma.', 'followup_message': 'Respuesta: Me alegra mucho que hayas disfrutado tanto de comer shawarma. ¡Es una delicia! Definitivamente es uno de mis platos favoritos también. Me encanta la combinación de sabores y especias que se encuentran en el shawarma. ¿Tienes algún lugar en particular donde te gusta comerlo? Si tienes alguna recomendación de un buen lugar para probarlo, ¡me encantaría escucharla! ¡Gracias por compartir tu entusiasmo por esta deliciosa comida!'}</td>\n",
" <td>7.916105</td>\n",
" <td>0.204782</td>\n",
" <td>0.1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>me encanta comer shawarma</td>\n",
" <td>{'Review': 'me encanta comer shawarma', 'English_Review': 'I love eating shawarma.', 'summary': 'The reviewer expressed their strong enjoyment of eating shawarma.', 'followup_message': 'Respuesta: Agradezco mucho tu reseña sobre el shawarma. Me alegra saber que lo disfrutaste tanto. El shawarma es realmente delicioso, ¿verdad? Es uno de mis platos favoritos también. Si tienes alguna otra recomendación de lugares donde pueda probar un buen shawarma, ¡por favor avísame! ¡Gracias de nuevo por compartir tu opinión positiva!'}</td>\n",
" <td>5.528532</td>\n",
" <td>0.174965</td>\n",
" <td>0.9</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" prompt \\\n",
"0 أنا أحب أكل الشاورما \n",
"1 أنا أحب أكل الشاورما \n",
"2 me encanta comer shawarma \n",
"3 me encanta comer shawarma \n",
"\n",
" response \\\n",
"0 {'Review': 'أنا أحب أكل الشاورما', 'English_Review': 'I love eating shawarma.', 'summary': 'The reviewer expresses their strong enjoyment for eating shawarma.', 'followup_message': 'تشكر المراجع على تعبيرهم القوي عن استمتاعهم الكبير بتناول الشاورما. نحن سعداء جدًا لمعرفة أنكم استمتعتم بهذه الوجبة الشهية. نحن نبذل قصارى جهدنا لتقديم أفضل جودة للشاورما ونحن نقدر تقديركم العالي لذلك. نتطلع إلى خدمتكم مرة أخرى في المستقبل القريب. شكرًا جزيلاً على تقييمكم الإيجابي ونحن نتطلع لرؤيتكم قريبًا.'} \n",
"1 {'Review': 'أنا أحب أكل الشاورما', 'English_Review': 'I love eating shawarma.', 'summary': 'The reviewer expresses their strong affection for shawarma.', 'followup_message': 'تعليق متابع: أشكرك على تقديم هذا المراجعة الرائعة عن الشاورما! أنا متحمس جدًا لمشاركة هذه الشغف القوي لديك تجاه هذا الطبق الشهي. الشاورما حقًا أطباق لذيذة ولا يمكن مقاومتها، فمجرد التفكير فيها يجعل اللعاب يسيل من فمي! ومن ثمة، عندما تتناغم نكهات اللحم المشوي العطري والصلصات اللذيذة والخضروات الطازجة في رغيف الخبز الطري، فإنها تعتبر تجربة لا مثيل لها بالفعل. أحب كيف يمكن تخصيص الشاورما حسب الأذواق الشخصية، سواء بإضافة الثوم، البقدونس، أو الصلصة الحارة، وهذا يجعل كل شاورما فريدة من نوعها. أعتقد أنه لا يمكن لأحد أن يجرب هذا الطبق الرائع ويبقى مستغربًا من حبكم العميق للشاورما. شكرًا لمشاركتكم هذه التجربة المدهشة!'} \n",
"2 {'Review': 'me encanta comer shawarma', 'English_Review': 'I love eating shawarma.', 'summary': 'The reviewer expresses their strong enjoyment for eating shawarma.', 'followup_message': 'Respuesta: Me alegra mucho que hayas disfrutado tanto de comer shawarma. ¡Es una delicia! Definitivamente es uno de mis platos favoritos también. Me encanta la combinación de sabores y especias que se encuentran en el shawarma. ¿Tienes algún lugar en particular donde te gusta comerlo? Si tienes alguna recomendación de un buen lugar para probarlo, ¡me encantaría escucharla! ¡Gracias por compartir tu entusiasmo por esta deliciosa comida!'} \n",
"3 {'Review': 'me encanta comer shawarma', 'English_Review': 'I love eating shawarma.', 'summary': 'The reviewer expressed their strong enjoyment of eating shawarma.', 'followup_message': 'Respuesta: Agradezco mucho tu reseña sobre el shawarma. Me alegra saber que lo disfrutaste tanto. El shawarma es realmente delicioso, ¿verdad? Es uno de mis platos favoritos también. Si tienes alguna otra recomendación de lugares donde pueda probar un buen shawarma, ¡por favor avísame! ¡Gracias de nuevo por compartir tu opinión positiva!'} \n",
"\n",
" latency similar_to_expected temperature \n",
"0 12.416809 0.179893 0.1 \n",
"1 17.276991 0.131922 0.9 \n",
"2 7.916105 0.204782 0.1 \n",
"3 5.528532 0.174965 0.9 "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from prompttools.experiment import SequentialChainExperiment\n",
"from langchain.chat_models import ChatOpenAI\n",
"from langchain.prompts import ChatPromptTemplate\n",
"\n",
"from prompttools.utils import similarity\n",
"\n",
"\n",
"llm = ChatOpenAI(temperature=0.9)\n",
"\n",
"# prompt template 1: translate to english\n",
"first_prompt = ChatPromptTemplate.from_template(\n",
" \"Translate the following review to english:\"\n",
" \"\\n\\n{Review}\"\n",
")\n",
"\n",
"second_prompt = ChatPromptTemplate.from_template(\n",
" \"Can you summarize the following review in 1 sentence:\"\n",
" \"\\n\\n{English_Review}\"\n",
")\n",
"\n",
"third_prompt = ChatPromptTemplate.from_template(\n",
" \"What language is the following review:\\n\\n{Review}\"\n",
")\n",
"\n",
"fourth_prompt = ChatPromptTemplate.from_template(\n",
" \"Write a follow up response to the following \"\n",
" \"summary in the specified language:\"\n",
" \"\\n\\nSummary: {summary}\\n\\nLanguage: {language}\"\n",
")\n",
"\n",
"experiment = SequentialChainExperiment(\n",
" llm=[ChatOpenAI],\n",
" prompt=[\"أنا أحب أكل الشاورما\", \"me encanta comer shawarma\"],\n",
" prompt_template=[[first_prompt, second_prompt, third_prompt, fourth_prompt]],\n",
" **{\n",
" \"temperature\": [0.1, 0.9],\n",
" \"input_variables\": [[\"Review\"]],\n",
" \"output_variables\": [[\"English_Review\", \"summary\", \"followup_message\"]],\n",
" \"output_key\": [[\"English_Review\", \"summary\", \"language\", \"followup_message\"]]\n",
" }\n",
")\n",
"\n",
"\n",
"experiment.run()\n",
"\n",
"experiment.evaluate(\"similar_to_expected\", similarity.evaluate, expected=\"I love to eat shawarma\")\n",
"\n",
"experiment.visualize()\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.10.6 ('env': venv)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "eec05f12730ef3ef66f433616fcd3cfdacd3dcf1f1c49c706eaa0465be8f325b"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
3 changes: 2 additions & 1 deletion examples/notebooks/OpenAIChatExperiment.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"metadata": {},
"outputs": [],
"source": [
"# !pip install --quiet --force-reinstall prompttools"
"# !pip install --quiet --force-reinstall prompttools\n",
"# !pip install --quiet matplotlib"
]
},
{
Expand Down
Loading

0 comments on commit 15a60d2

Please sign in to comment.