From 9b18fa3198a80c53dfd45ea4d16b746786880ef9 Mon Sep 17 00:00:00 2001 From: eb379 Date: Thu, 30 May 2024 11:31:40 +0100 Subject: [PATCH 1/4] Fix imports and positional argument --- 02_LangChain_Inputs_and_Outputs/code.ipynb | 160 +++++++++++++++++---- 1 file changed, 134 insertions(+), 26 deletions(-) diff --git a/02_LangChain_Inputs_and_Outputs/code.ipynb b/02_LangChain_Inputs_and_Outputs/code.ipynb index 20aa8088..fc00ae40 100644 --- a/02_LangChain_Inputs_and_Outputs/code.ipynb +++ b/02_LangChain_Inputs_and_Outputs/code.ipynb @@ -9,13 +9,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "from dotenv import load_dotenv, find_dotenv\n", "\n", - "load_dotenv(find_dotenv())" + "load_dotenv(find_dotenv())\n", + "\n" ] }, { @@ -27,9 +39,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'\\nWhy did the tomato turn red?\\n\\nBecause it saw the salad dressing!'" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "from langchain_openai import OpenAI\n", "\n", @@ -39,18 +62,40 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "\"\\n\\nWhy couldn't the bicycle stand up by itself? Because it was two-tired.\"" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "llm(\"Tell me a joke\")" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "\"\\n\\nWhy don't scientists trust atoms?\\n\\nBecause they make up everything.\"" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "llm.invoke(input=\"Tell me a joke\")" ] @@ -64,9 +109,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "generations=[[Generation(text='\\n\\nWhy was the cow afraid to leave the barn?\\n\\nBecause every time she stepped out, she heard \"moo-ve back in!\"', generation_info={'finish_reason': 'stop', 'logprobs': None})], [Generation(text='\\n\\nWhy did the parrot wear a raincoat?\\n\\nBecause it was a little birdie with a big umbrella!', generation_info={'finish_reason': 'stop', 'logprobs': None})]] llm_output={'token_usage': {'total_tokens': 64, 'completion_tokens': 51, 'prompt_tokens': 13}, 'model_name': 'gpt-3.5-turbo-instruct'} run=[RunInfo(run_id=UUID('ed06e20c-3aac-4b3d-ae15-64f9a6b56ce7')), RunInfo(run_id=UUID('5e564ae6-908f-4a03-9d09-df86a1a3c0a0'))]\n" + ] + } + ], "source": [ "result = llm.generate([\"Tell me a joke about cows\", \"Tell me a joke about parrots\"])\n", "print(result)" @@ -74,9 +127,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "{'token_usage': {'total_tokens': 64,\n", + " 'completion_tokens': 51,\n", + " 'prompt_tokens': 13},\n", + " 'model_name': 'gpt-3.5-turbo-instruct'}" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "result.llm_output" ] @@ -90,7 +157,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, "outputs": [], "source": [ @@ -101,9 +168,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "content='Why did the cow go to outer space?\\n\\nTo find the udder side of the moon!' response_metadata={'token_usage': {'completion_tokens': 20, 'prompt_tokens': 13, 'total_tokens': 33}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None} id='run-cee5954a-e956-4619-88d6-3f44f5b04e72-0' usage_metadata={'input_tokens': 13, 'output_tokens': 20, 'total_tokens': 33}\n" + ] + } + ], "source": [ "result = llm.invoke(\"Tell me a joke about cows\")\n", "print(result)" @@ -111,11 +186,11 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, "outputs": [], "source": [ - "from langchain.schema import SystemMessage, HumanMessage, AIMessage\n", + "from langchain_core.messages import SystemMessage, HumanMessage, AIMessage\n", "\n", "messages = [\n", " SystemMessage(content=\"You are a helpful assistant specialized in providing information about BellaVista Italian Restaurant.\"),\n", @@ -127,11 +202,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "AIMessage(content='Yes, BellaVista offers several vegan options on the menu. Some popular choices include vegan pasta dishes, salads, and vegetable-based pizzas.', response_metadata={'token_usage': {'completion_tokens': 28, 'prompt_tokens': 65, 'total_tokens': 93}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None}, id='run-e7622b08-2d27-445b-b43d-03a51abbf8e7-0', usage_metadata={'input_tokens': 65, 'output_tokens': 28, 'total_tokens': 93})" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "llm_result = llm.invoke(messages=messages)\n", + "llm_result = llm.invoke(messages)\n", "llm_result" ] }, @@ -144,9 +230,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "LLMResult(generations=[[ChatGeneration(text='Haben Sie vegane Optionen?', generation_info={'finish_reason': 'stop', 'logprobs': None}, message=AIMessage(content='Haben Sie vegane Optionen?', response_metadata={'token_usage': {'completion_tokens': 8, 'prompt_tokens': 27, 'total_tokens': 35}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None}, id='run-b8e4fbf7-d7a6-44ef-a3ed-523779e96487-0', usage_metadata={'input_tokens': 27, 'output_tokens': 8, 'total_tokens': 35}))], [ChatGeneration(text='¿Tienen opciones veganas?', generation_info={'finish_reason': 'stop', 'logprobs': None}, message=AIMessage(content='¿Tienen opciones veganas?', response_metadata={'token_usage': {'completion_tokens': 7, 'prompt_tokens': 29, 'total_tokens': 36}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None}, id='run-66410fe2-eadc-4a70-8fb9-29526df3baf6-0', usage_metadata={'input_tokens': 29, 'output_tokens': 7, 'total_tokens': 36}))]], llm_output={'token_usage': {'completion_tokens': 15, 'prompt_tokens': 56, 'total_tokens': 71}, 'model_name': 'gpt-3.5-turbo'}, run=[RunInfo(run_id=UUID('b8e4fbf7-d7a6-44ef-a3ed-523779e96487')), RunInfo(run_id=UUID('66410fe2-eadc-4a70-8fb9-29526df3baf6'))])" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "batch_messages = [\n", " [\n", @@ -171,9 +268,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "['Haben Sie vegane Optionen?', '¿Tienen opciones veganas?']" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "translations = [generation[0].text for generation in batch_result.generations]\n", "translations" @@ -196,7 +304,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.0" + "version": "3.11.3" } }, "nbformat": 4, From 76cad10d9eddc5c341599511ff977c1eb67a4a3a Mon Sep 17 00:00:00 2001 From: eb379 Date: Thu, 30 May 2024 11:45:52 +0100 Subject: [PATCH 2/4] Attempt to remove from ipynb --- 02_LangChain_Inputs_and_Outputs/code.ipynb | 151 +++------------------ 1 file changed, 22 insertions(+), 129 deletions(-) diff --git a/02_LangChain_Inputs_and_Outputs/code.ipynb b/02_LangChain_Inputs_and_Outputs/code.ipynb index fc00ae40..8ec6772d 100644 --- a/02_LangChain_Inputs_and_Outputs/code.ipynb +++ b/02_LangChain_Inputs_and_Outputs/code.ipynb @@ -9,20 +9,9 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "from dotenv import load_dotenv, find_dotenv\n", "\n", @@ -39,20 +28,9 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'\\nWhy did the tomato turn red?\\n\\nBecause it saw the salad dressing!'" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "from langchain_openai import OpenAI\n", "\n", @@ -62,40 +40,18 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "\"\\n\\nWhy couldn't the bicycle stand up by itself? Because it was two-tired.\"" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "llm(\"Tell me a joke\")" ] }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "\"\\n\\nWhy don't scientists trust atoms?\\n\\nBecause they make up everything.\"" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "llm.invoke(input=\"Tell me a joke\")" ] @@ -109,17 +65,9 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "generations=[[Generation(text='\\n\\nWhy was the cow afraid to leave the barn?\\n\\nBecause every time she stepped out, she heard \"moo-ve back in!\"', generation_info={'finish_reason': 'stop', 'logprobs': None})], [Generation(text='\\n\\nWhy did the parrot wear a raincoat?\\n\\nBecause it was a little birdie with a big umbrella!', generation_info={'finish_reason': 'stop', 'logprobs': None})]] llm_output={'token_usage': {'total_tokens': 64, 'completion_tokens': 51, 'prompt_tokens': 13}, 'model_name': 'gpt-3.5-turbo-instruct'} run=[RunInfo(run_id=UUID('ed06e20c-3aac-4b3d-ae15-64f9a6b56ce7')), RunInfo(run_id=UUID('5e564ae6-908f-4a03-9d09-df86a1a3c0a0'))]\n" - ] - } - ], + "outputs": [], "source": [ "result = llm.generate([\"Tell me a joke about cows\", \"Tell me a joke about parrots\"])\n", "print(result)" @@ -127,23 +75,9 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'token_usage': {'total_tokens': 64,\n", - " 'completion_tokens': 51,\n", - " 'prompt_tokens': 13},\n", - " 'model_name': 'gpt-3.5-turbo-instruct'}" - ] - }, - "execution_count": 8, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "result.llm_output" ] @@ -157,7 +91,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -168,17 +102,9 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "content='Why did the cow go to outer space?\\n\\nTo find the udder side of the moon!' response_metadata={'token_usage': {'completion_tokens': 20, 'prompt_tokens': 13, 'total_tokens': 33}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None} id='run-cee5954a-e956-4619-88d6-3f44f5b04e72-0' usage_metadata={'input_tokens': 13, 'output_tokens': 20, 'total_tokens': 33}\n" - ] - } - ], + "outputs": [], "source": [ "result = llm.invoke(\"Tell me a joke about cows\")\n", "print(result)" @@ -186,7 +112,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -202,20 +128,9 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "AIMessage(content='Yes, BellaVista offers several vegan options on the menu. Some popular choices include vegan pasta dishes, salads, and vegetable-based pizzas.', response_metadata={'token_usage': {'completion_tokens': 28, 'prompt_tokens': 65, 'total_tokens': 93}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None}, id='run-e7622b08-2d27-445b-b43d-03a51abbf8e7-0', usage_metadata={'input_tokens': 65, 'output_tokens': 28, 'total_tokens': 93})" - ] - }, - "execution_count": 13, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "llm_result = llm.invoke(messages)\n", "llm_result" @@ -230,20 +145,9 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "LLMResult(generations=[[ChatGeneration(text='Haben Sie vegane Optionen?', generation_info={'finish_reason': 'stop', 'logprobs': None}, message=AIMessage(content='Haben Sie vegane Optionen?', response_metadata={'token_usage': {'completion_tokens': 8, 'prompt_tokens': 27, 'total_tokens': 35}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None}, id='run-b8e4fbf7-d7a6-44ef-a3ed-523779e96487-0', usage_metadata={'input_tokens': 27, 'output_tokens': 8, 'total_tokens': 35}))], [ChatGeneration(text='¿Tienen opciones veganas?', generation_info={'finish_reason': 'stop', 'logprobs': None}, message=AIMessage(content='¿Tienen opciones veganas?', response_metadata={'token_usage': {'completion_tokens': 7, 'prompt_tokens': 29, 'total_tokens': 36}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None}, id='run-66410fe2-eadc-4a70-8fb9-29526df3baf6-0', usage_metadata={'input_tokens': 29, 'output_tokens': 7, 'total_tokens': 36}))]], llm_output={'token_usage': {'completion_tokens': 15, 'prompt_tokens': 56, 'total_tokens': 71}, 'model_name': 'gpt-3.5-turbo'}, run=[RunInfo(run_id=UUID('b8e4fbf7-d7a6-44ef-a3ed-523779e96487')), RunInfo(run_id=UUID('66410fe2-eadc-4a70-8fb9-29526df3baf6'))])" - ] - }, - "execution_count": 14, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "batch_messages = [\n", " [\n", @@ -268,20 +172,9 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "['Haben Sie vegane Optionen?', '¿Tienen opciones veganas?']" - ] - }, - "execution_count": 15, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "translations = [generation[0].text for generation in batch_result.generations]\n", "translations" From ef3ed35df530fb7764c66a68d30d93e0d0f3ce7c Mon Sep 17 00:00:00 2001 From: eb379 Date: Thu, 30 May 2024 11:49:39 +0100 Subject: [PATCH 3/4] Correct small accidental changes --- 02_LangChain_Inputs_and_Outputs/code.ipynb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/02_LangChain_Inputs_and_Outputs/code.ipynb b/02_LangChain_Inputs_and_Outputs/code.ipynb index 8ec6772d..d6fe1e54 100644 --- a/02_LangChain_Inputs_and_Outputs/code.ipynb +++ b/02_LangChain_Inputs_and_Outputs/code.ipynb @@ -15,8 +15,7 @@ "source": [ "from dotenv import load_dotenv, find_dotenv\n", "\n", - "load_dotenv(find_dotenv())\n", - "\n" + "load_dotenv(find_dotenv())", ] }, { @@ -197,7 +196,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.3" + "version": "3.11.0" } }, "nbformat": 4, From 6f429dec3295c06bc9304b899e5662f9f3ce9400 Mon Sep 17 00:00:00 2001 From: eb379 Date: Thu, 30 May 2024 11:50:47 +0100 Subject: [PATCH 4/4] remove erroneous comma --- 02_LangChain_Inputs_and_Outputs/code.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/02_LangChain_Inputs_and_Outputs/code.ipynb b/02_LangChain_Inputs_and_Outputs/code.ipynb index d6fe1e54..e29d0b9e 100644 --- a/02_LangChain_Inputs_and_Outputs/code.ipynb +++ b/02_LangChain_Inputs_and_Outputs/code.ipynb @@ -15,7 +15,7 @@ "source": [ "from dotenv import load_dotenv, find_dotenv\n", "\n", - "load_dotenv(find_dotenv())", + "load_dotenv(find_dotenv())" ] }, {