From 48c1a2bfd21d752abd8fcc3b141b4a8c36285909 Mon Sep 17 00:00:00 2001 From: Elvis Saravia Date: Thu, 11 Jan 2024 18:55:57 -0600 Subject: [PATCH] fixes --- notebooks/pe-chatgpt-intro.ipynb | 49 +- notebooks/pe-chatgpt-langchain.ipynb | 69 +- notebooks/pe-function-calling.ipynb | 73 +- notebooks/pe-lecture.ipynb | 720 +++--------- notebooks/pe-rag.ipynb | 1181 ++++++++++++++++++++ pages/applications/function_calling.en.mdx | 32 +- pages/introduction/examples.en.mdx | 16 + pages/models/chatgpt.en.mdx | 19 + pages/techniques/rag.en.mdx | 20 +- 9 files changed, 1531 insertions(+), 648 deletions(-) create mode 100644 notebooks/pe-rag.ipynb diff --git a/notebooks/pe-chatgpt-intro.ipynb b/notebooks/pe-chatgpt-intro.ipynb index ea93bcb..eacaae8 100644 --- a/notebooks/pe-chatgpt-intro.ipynb +++ b/notebooks/pe-chatgpt-intro.ipynb @@ -68,13 +68,13 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "MODEL = \"gpt-3.5-turbo\"\n", "\n", - "response = openai.ChatCompletion.create(\n", + "response = openai.chat.completions.create(\n", " model=MODEL,\n", " messages=[\n", " {\"role\": \"system\", \"content\": \"You are an AI research assistant. You use a tone that is technical and scientific.\"},\n", @@ -96,50 +96,58 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "'Certainly! Black holes are formed when a massive star runs out of fuel and can no longer produce the energy needed to counteract the force of gravity. This causes the star to collapse in on itself, creating a singularity - a point of infinite density and zero volume. The gravitational pull of the singularity is so strong that nothing, not even light, can escape its grasp, hence the name \"black hole\". \\n\\nThere are also supermassive black holes, which are found at the centers of galaxies and are thought to have formed through the merging of smaller black holes and the accretion of matter. \\n\\nThe study of black holes is a fascinating and active area of research in astrophysics, and there is still much to be learned about these mysterious objects.'" + "\"Certainly! Black holes are fascinating astronomical objects that form from the remnants of massive stars. The creation of a black hole occurs through a process known as stellar collapse.\\n\\nWhen a massive star exhausts its nuclear fuel, it can no longer sustain the outward pressure generated by nuclear fusion. As a result, the star's core collapses under the force of gravity. This collapse is triggered by the imbalance between the inward gravitational force and the outward pressure.\\n\\nDuring the collapse, the star's core becomes incredibly dense, packing an enormous amount of mass into a tiny volume. This extreme density leads to the formation of a singularity, a point of infinite density at the center of the black hole.\\n\\nSurrounding the singularity is the event horizon, which is the boundary beyond which nothing, not even light, can escape the gravitational pull of the black hole. The event horizon is determined by the mass of the black hole, with larger black holes having larger event horizons.\\n\\nThe formation of black holes is classified into three main types based on their mass: stellar black holes, intermediate-mass black holes, and supermassive black holes. Stellar black holes typically have masses several times that of our Sun, while supermassive black holes can have millions or even billions of times the mass of the Sun.\\n\\nIn addition to stellar collapse, black holes can also form through other mechanisms, such as the collision of neutron stars or the accretion of matter onto an existing black hole.\\n\\nUnderstanding the creation and behavior of black holes is a fascinating area of research in astrophysics, with implications for our understanding of gravity, spacetime, and the evolution of galaxies.\"" ] }, - "execution_count": 4, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "response.choices[0]['message']['content']" + "response.choices[0].message.content" ] }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ - "Certainly! Black holes are formed when a massive star runs out of fuel and can no longer produce the energy needed to counteract the force of gravity. This causes the star to collapse in on itself, creating a singularity - a point of infinite density and zero volume. The gravitational pull of the singularity is so strong that nothing, not even light, can escape its grasp, hence the name \"black hole\". \n", + "Certainly! Black holes are fascinating astronomical objects that form from the remnants of massive stars. The creation of a black hole occurs through a process known as stellar collapse.\n", + "\n", + "When a massive star exhausts its nuclear fuel, it can no longer sustain the outward pressure generated by nuclear fusion. As a result, the star's core collapses under the force of gravity. This collapse is triggered by the imbalance between the inward gravitational force and the outward pressure.\n", + "\n", + "During the collapse, the star's core becomes incredibly dense, packing an enormous amount of mass into a tiny volume. This extreme density leads to the formation of a singularity, a point of infinite density at the center of the black hole.\n", + "\n", + "Surrounding the singularity is the event horizon, which is the boundary beyond which nothing, not even light, can escape the gravitational pull of the black hole. The event horizon is determined by the mass of the black hole, with larger black holes having larger event horizons.\n", + "\n", + "The formation of black holes is classified into three main types based on their mass: stellar black holes, intermediate-mass black holes, and supermassive black holes. Stellar black holes typically have masses several times that of our Sun, while supermassive black holes can have millions or even billions of times the mass of the Sun.\n", "\n", - "There are also supermassive black holes, which are found at the centers of galaxies and are thought to have formed through the merging of smaller black holes and the accretion of matter. \n", + "In addition to stellar collapse, black holes can also form through other mechanisms, such as the collision of neutron stars or the accretion of matter onto an existing black hole.\n", "\n", - "The study of black holes is a fascinating and active area of research in astrophysics, and there is still much to be learned about these mysterious objects." + "Understanding the creation and behavior of black holes is a fascinating area of research in astrophysics, with implications for our understanding of gravity, spacetime, and the evolution of galaxies." ], "text/plain": [ "" ] }, - "execution_count": 5, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# pretty format the response\n", - "IPython.display.Markdown(response.choices[0]['message']['content'])" + "IPython.display.Markdown(response.choices[0].message.content)" ] }, { @@ -160,14 +168,14 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Mice.\n" + "mice\n" ] } ], @@ -181,7 +189,7 @@ "Answer:\n", "\"\"\"\n", "\n", - "response = openai.ChatCompletion.create(\n", + "response = openai.chat.completions.create(\n", " model=MODEL,\n", " messages=[\n", " {\"role\": \"user\", \"content\": CONTENT},\n", @@ -189,8 +197,15 @@ " temperature=0,\n", ")\n", "\n", - "print(response['choices'][0]['message']['content'])" + "print(response.choices[0].message.content)" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -209,7 +224,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.16" + "version": "3.9.18" }, "orig_nbformat": 4, "vscode": { diff --git a/notebooks/pe-chatgpt-langchain.ipynb b/notebooks/pe-chatgpt-langchain.ipynb index 894769f..9ebd3ca 100644 --- a/notebooks/pe-chatgpt-langchain.ipynb +++ b/notebooks/pe-chatgpt-langchain.ipynb @@ -20,7 +20,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -33,7 +33,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 2, "metadata": {}, "outputs": [ { @@ -42,7 +42,7 @@ "True" ] }, - "execution_count": 3, + "execution_count": 2, "metadata": {}, "output_type": "execute_result" } @@ -66,7 +66,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ @@ -83,7 +83,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ @@ -104,9 +104,18 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 5, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/Users/elvissaravia/opt/miniconda3/envs/peguide/lib/python3.9/site-packages/langchain_core/_api/deprecation.py:117: LangChainDeprecationWarning: The class `langchain_community.chat_models.openai.ChatOpenAI` was deprecated in langchain-community 0.1.0 and will be removed in 0.2.0. Use langchain_openai.ChatOpenAI instead.\n", + " warn_deprecated(\n" + ] + } + ], "source": [ "# chat mode instance\n", "chat = ChatOpenAI(temperature=0)" @@ -145,7 +154,7 @@ { "data": { "text/plain": [ - "AIMessage(content='Positive', additional_kwargs={})" + "AIMessage(content='positive')" ] }, "execution_count": 7, @@ -160,7 +169,7 @@ "Text: {user_input}. \n", "Sentiment:\"\"\"\n", "\n", - "chat([HumanMessage(content=FINAL_PROMPT.format(user_input=USER_INPUT))])" + "chat.invoke([HumanMessage(content=FINAL_PROMPT.format(user_input=USER_INPUT))])" ] }, { @@ -173,16 +182,16 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "AIMessage(content='Sentiment: Positive', additional_kwargs={})" + "AIMessage(content='The sentence \"I am doing brilliant today!\" can be classified as positive sentiment.')" ] }, - "execution_count": 11, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } @@ -193,7 +202,7 @@ " HumanMessage(content=\"Classify the following sentence: I am doing brilliant today!\"),\n", "]\n", "\n", - "chat(messages)" + "chat.invoke(messages)" ] }, { @@ -206,16 +215,16 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "AIMessage(content='Certainly! Black holes are formed when a massive star runs out of fuel and can no longer produce the energy needed to counteract the force of gravity. This causes the star to collapse in on itself, creating a singularity - a point of infinite density and zero volume. The gravitational pull of the singularity is so strong that nothing, not even light, can escape its grasp, hence the name \"black hole\". \\n\\nThere are also supermassive black holes, which are found at the centers of galaxies and are thought to have formed through the merging of smaller black holes and the accretion of matter. \\n\\nThe study of black holes is a fascinating and active area of research in astrophysics, and there is still much to be learned about these mysterious objects.', additional_kwargs={})" + "AIMessage(content=\"Certainly! Black holes are fascinating astronomical objects that form from the remnants of massive stars. The creation of a black hole occurs through a process known as stellar collapse.\\n\\nWhen a massive star exhausts its nuclear fuel, it can no longer sustain the outward pressure generated by nuclear fusion. As a result, the star's core collapses under the force of gravity. This collapse is triggered by the imbalance between the inward gravitational force and the outward pressure.\\n\\nDuring the collapse, the star's core becomes incredibly dense, packing an enormous amount of mass into a tiny volume. This extreme density leads to the formation of a singularity, a point of infinite density at the center of the black hole.\\n\\nSurrounding the singularity is the event horizon, which is the boundary beyond which nothing, not even light, can escape the gravitational pull of the black hole. The event horizon is determined by the mass of the black hole, with larger black holes having larger event horizons.\\n\\nThe formation of black holes is classified into three main types based on their mass: stellar black holes, intermediate-mass black holes, and supermassive black holes. Stellar black holes typically have masses several times that of our Sun, while supermassive black holes can have millions or even billions of times the mass of the Sun.\\n\\nIn addition to stellar collapse, black holes can also form through other mechanisms, such as the collision of neutron stars or the accretion of matter onto an existing black hole.\\n\\nUnderstanding the creation and behavior of black holes is a fascinating area of research in astrophysics, with implications for our understanding of gravity, spacetime, and the evolution of galaxies.\")" ] }, - "execution_count": 8, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" } @@ -228,7 +237,7 @@ " HumanMessage(content=\"Can you tell me about the creation of black holes?\")\n", "]\n", "\n", - "chat(messages)" + "chat.invoke(messages)" ] }, { @@ -241,16 +250,16 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "LLMResult(generations=[[ChatGeneration(text='Certainly! Black holes are formed when a massive star runs out of fuel and can no longer produce the energy needed to counteract the force of gravity. This causes the star to collapse in on itself, creating a singularity - a point of infinite density and zero volume. The gravitational pull of the singularity is so strong that nothing, not even light, can escape its grasp, hence the name \"black hole\". \\n\\nThere are also supermassive black holes, which are found at the centers of galaxies and are thought to have formed through the merging of smaller black holes and the accretion of matter. \\n\\nThe study of black holes is a fascinating and active area of research in astrophysics, and there is still much to be learned about these mysterious objects.', generation_info=None, message=AIMessage(content='Certainly! Black holes are formed when a massive star runs out of fuel and can no longer produce the energy needed to counteract the force of gravity. This causes the star to collapse in on itself, creating a singularity - a point of infinite density and zero volume. The gravitational pull of the singularity is so strong that nothing, not even light, can escape its grasp, hence the name \"black hole\". \\n\\nThere are also supermassive black holes, which are found at the centers of galaxies and are thought to have formed through the merging of smaller black holes and the accretion of matter. \\n\\nThe study of black holes is a fascinating and active area of research in astrophysics, and there is still much to be learned about these mysterious objects.', additional_kwargs={}))], [ChatGeneration(text='Certainly! Dark matter is a hypothetical form of matter that is thought to make up approximately 85% of the matter in the universe. It is called \"dark\" because it does not interact with light or other forms of electromagnetic radiation, making it invisible to telescopes and other instruments that detect light. \\n\\nThe existence of dark matter was first proposed in the 1930s by Swiss astronomer Fritz Zwicky, who noticed that the observed mass of galaxy clusters was not sufficient to account for the gravitational forces that held them together. Since then, many other observations have supported the idea of dark matter, including the rotation curves of galaxies and the cosmic microwave background radiation.\\n\\nDespite its name, dark matter is not completely mysterious. Scientists have some ideas about what it might be made of, such as weakly interacting massive particles (WIMPs) or axions. However, these particles have not yet been directly detected, and the nature of dark matter remains one of the biggest mysteries in modern physics.', generation_info=None, message=AIMessage(content='Certainly! Dark matter is a hypothetical form of matter that is thought to make up approximately 85% of the matter in the universe. It is called \"dark\" because it does not interact with light or other forms of electromagnetic radiation, making it invisible to telescopes and other instruments that detect light. \\n\\nThe existence of dark matter was first proposed in the 1930s by Swiss astronomer Fritz Zwicky, who noticed that the observed mass of galaxy clusters was not sufficient to account for the gravitational forces that held them together. Since then, many other observations have supported the idea of dark matter, including the rotation curves of galaxies and the cosmic microwave background radiation.\\n\\nDespite its name, dark matter is not completely mysterious. Scientists have some ideas about what it might be made of, such as weakly interacting massive particles (WIMPs) or axions. However, these particles have not yet been directly detected, and the nature of dark matter remains one of the biggest mysteries in modern physics.', additional_kwargs={}))]], llm_output=None)" + "LLMResult(generations=[[ChatGeneration(text=\"Certainly! Black holes are fascinating astronomical objects that are formed through the gravitational collapse of massive stars. The creation of a black hole occurs when a star exhausts its nuclear fuel and can no longer sustain the outward pressure generated by nuclear fusion in its core.\\n\\nWhen a massive star reaches the end of its life, it undergoes a supernova explosion. During this explosive event, the outer layers of the star are ejected into space, while the core collapses inward due to its own gravity. If the core's mass is above a certain threshold, known as the Tolman-Oppenheimer-Volkoff (TOV) limit, the collapse continues until it forms a black hole.\\n\\nThe core collapse is an incredibly dense and energetic process. As the core collapses, its mass becomes concentrated into an extremely small volume, resulting in a gravitational field so strong that not even light can escape from it. This region of space is known as the event horizon, which defines the boundary of a black hole.\\n\\nThe formation of a black hole is governed by the principles of general relativity, a theory proposed by Albert Einstein. According to general relativity, the collapse of matter creates a singularity at the center of the black hole, where the laws of physics as we currently understand them break down.\\n\\nBlack holes come in different sizes, ranging from stellar-mass black holes, which have masses several times that of our Sun, to supermassive black holes, which can have millions or even billions of times the mass of the Sun. Supermassive black holes are thought to form through a combination of processes, including the accretion of matter and the merging of smaller black holes.\\n\\nStudying black holes is a challenging task due to their elusive nature, but advancements in observational techniques, such as gravitational wave detectors and telescopes, have provided us with valuable insights into their formation and behavior.\", generation_info={'finish_reason': 'stop', 'logprobs': None}, message=AIMessage(content=\"Certainly! Black holes are fascinating astronomical objects that are formed through the gravitational collapse of massive stars. The creation of a black hole occurs when a star exhausts its nuclear fuel and can no longer sustain the outward pressure generated by nuclear fusion in its core.\\n\\nWhen a massive star reaches the end of its life, it undergoes a supernova explosion. During this explosive event, the outer layers of the star are ejected into space, while the core collapses inward due to its own gravity. If the core's mass is above a certain threshold, known as the Tolman-Oppenheimer-Volkoff (TOV) limit, the collapse continues until it forms a black hole.\\n\\nThe core collapse is an incredibly dense and energetic process. As the core collapses, its mass becomes concentrated into an extremely small volume, resulting in a gravitational field so strong that not even light can escape from it. This region of space is known as the event horizon, which defines the boundary of a black hole.\\n\\nThe formation of a black hole is governed by the principles of general relativity, a theory proposed by Albert Einstein. According to general relativity, the collapse of matter creates a singularity at the center of the black hole, where the laws of physics as we currently understand them break down.\\n\\nBlack holes come in different sizes, ranging from stellar-mass black holes, which have masses several times that of our Sun, to supermassive black holes, which can have millions or even billions of times the mass of the Sun. Supermassive black holes are thought to form through a combination of processes, including the accretion of matter and the merging of smaller black holes.\\n\\nStudying black holes is a challenging task due to their elusive nature, but advancements in observational techniques, such as gravitational wave detectors and telescopes, have provided us with valuable insights into their formation and behavior.\"))], [ChatGeneration(text='Certainly! Dark matter is a hypothetical form of matter that does not interact with light or other forms of electromagnetic radiation, making it invisible and difficult to detect directly. Its existence is inferred from its gravitational effects on visible matter and the structure of the universe.\\n\\nThe term \"dark matter\" was coined to explain the observed discrepancies between the predicted and observed gravitational effects in galaxies and galaxy clusters. These discrepancies suggest the presence of additional mass that cannot be accounted for by visible matter, such as stars, gas, and dust.\\n\\nThe exact nature of dark matter remains unknown, but various theories propose that it could consist of new types of particles that interact weakly with ordinary matter. These particles are often referred to as Weakly Interacting Massive Particles (WIMPs). Other theories suggest that dark matter could be composed of primordial black holes or exotic particles not yet discovered.\\n\\nDespite its elusive nature, dark matter plays a crucial role in the formation and evolution of the universe. It provides the gravitational scaffolding necessary for the formation of galaxies and galaxy clusters, influencing their distribution and dynamics. However, its precise properties and interactions continue to be a subject of intense scientific investigation and experimentation.', generation_info={'finish_reason': 'stop', 'logprobs': None}, message=AIMessage(content='Certainly! Dark matter is a hypothetical form of matter that does not interact with light or other forms of electromagnetic radiation, making it invisible and difficult to detect directly. Its existence is inferred from its gravitational effects on visible matter and the structure of the universe.\\n\\nThe term \"dark matter\" was coined to explain the observed discrepancies between the predicted and observed gravitational effects in galaxies and galaxy clusters. These discrepancies suggest the presence of additional mass that cannot be accounted for by visible matter, such as stars, gas, and dust.\\n\\nThe exact nature of dark matter remains unknown, but various theories propose that it could consist of new types of particles that interact weakly with ordinary matter. These particles are often referred to as Weakly Interacting Massive Particles (WIMPs). Other theories suggest that dark matter could be composed of primordial black holes or exotic particles not yet discovered.\\n\\nDespite its elusive nature, dark matter plays a crucial role in the formation and evolution of the universe. It provides the gravitational scaffolding necessary for the formation of galaxies and galaxy clusters, influencing their distribution and dynamics. However, its precise properties and interactions continue to be a subject of intense scientific investigation and experimentation.'))]], llm_output={'token_usage': {'completion_tokens': 607, 'prompt_tokens': 136, 'total_tokens': 743}, 'model_name': 'gpt-3.5-turbo'}, run=[RunInfo(run_id=UUID('0141a9f7-21e3-4df0-bb14-3ee815902407')), RunInfo(run_id=UUID('3f8f85fa-31c7-4a8b-a6ee-a6d9ee547c53'))])" ] }, - "execution_count": 9, + "execution_count": 10, "metadata": {}, "output_type": "execute_result" } @@ -284,7 +293,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 11, "metadata": {}, "outputs": [], "source": [ @@ -296,16 +305,16 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "AIMessage(content='The sentiment of the sentence \"I am doing brilliant today!\" is positive.', additional_kwargs={})" + "AIMessage(content='positive')" ] }, - "execution_count": 21, + "execution_count": 12, "metadata": {}, "output_type": "execute_result" } @@ -314,27 +323,27 @@ "chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt])\n", "\n", "\n", - "chat(chat_prompt.format_prompt(sentiment_labels=\"positive, negative, and neutral\", user_input=\"I am doing brilliant today!\").to_messages())" + "chat.invoke(chat_prompt.format_prompt(sentiment_labels=\"positive, negative, and neutral\", user_input=\"I am doing brilliant today!\").to_messages())" ] }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "AIMessage(content='The sentiment of the sentence \"Not sure what the weather is like today\" is neutral.', additional_kwargs={})" + "AIMessage(content='neutral')" ] }, - "execution_count": 22, + "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "chat(chat_prompt.format_prompt(sentiment_labels=\"positive, negative, and neutral\", user_input=\"Not sure what the weather is like today.\").to_messages())" + "chat.invoke(chat_prompt.format_prompt(sentiment_labels=\"positive, negative, and neutral\", user_input=\"Not sure what the weather is like today.\").to_messages())" ] } ], @@ -354,7 +363,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.16" + "version": "3.9.18" }, "orig_nbformat": 4, "vscode": { diff --git a/notebooks/pe-function-calling.ipynb b/notebooks/pe-function-calling.ipynb index b6a19ff..190bd52 100644 --- a/notebooks/pe-function-calling.ipynb +++ b/notebooks/pe-function-calling.ipynb @@ -1,8 +1,15 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Function Calling with OpenAI APIs" + ] + }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -26,7 +33,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -51,7 +58,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ @@ -80,7 +87,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ @@ -111,7 +118,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ @@ -127,14 +134,14 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "ChatCompletionMessage(content=None, role='assistant', function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_ADST8mP0bkzu12qibeywdtQ7', function=Function(arguments='{\"location\":\"London\",\"unit\":\"celsius\"}', name='get_current_weather'), type='function')])\n" + "ChatCompletionMessage(content=None, role='assistant', function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_GYg3yhSh1bbMabne9brwTqmA', function=Function(arguments='{\"location\":\"London\",\"unit\":\"celsius\"}', name='get_current_weather'), type='function')])\n" ] } ], @@ -159,7 +166,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 7, "metadata": {}, "outputs": [], "source": [ @@ -168,7 +175,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 8, "metadata": {}, "outputs": [ { @@ -177,7 +184,7 @@ "'{\"location\": \"London\", \"temperature\": \"50\", \"unit\": \"celsius\"}'" ] }, - "execution_count": 11, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } @@ -202,7 +209,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 9, "metadata": {}, "outputs": [], "source": [ @@ -216,7 +223,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 10, "metadata": {}, "outputs": [ { @@ -225,7 +232,7 @@ "ChatCompletionMessage(content=\"Hello! I'm here and ready to assist you. How can I help you today?\", role='assistant', function_call=None, tool_calls=None)" ] }, - "execution_count": 13, + "execution_count": 10, "metadata": {}, "output_type": "execute_result" } @@ -243,7 +250,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 11, "metadata": {}, "outputs": [ { @@ -252,7 +259,7 @@ "ChatCompletionMessage(content=\"Hello! I'm here and ready to assist you. How can I help you today?\", role='assistant', function_call=None, tool_calls=None)" ] }, - "execution_count": 14, + "execution_count": 11, "metadata": {}, "output_type": "execute_result" } @@ -270,7 +277,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 12, "metadata": {}, "outputs": [ { @@ -279,7 +286,7 @@ "ChatCompletionMessage(content=\"Hello! I'm here and ready to assist you. How can I help you today?\", role='assistant', function_call=None, tool_calls=None)" ] }, - "execution_count": 15, + "execution_count": 12, "metadata": {}, "output_type": "execute_result" } @@ -290,7 +297,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 13, "metadata": {}, "outputs": [ { @@ -299,7 +306,7 @@ "ChatCompletionMessage(content='I will check the current weather in London for you.', role='assistant', function_call=None, tool_calls=None)" ] }, - "execution_count": 16, + "execution_count": 13, "metadata": {}, "output_type": "execute_result" } @@ -323,16 +330,16 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "ChatCompletionMessage(content=None, role='assistant', function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_NzHx0V4zDnoCzvgaASGaUmCp', function=Function(arguments='{\"location\":\"London\",\"unit\":\"celsius\"}', name='get_current_weather'), type='function')])" + "ChatCompletionMessage(content=None, role='assistant', function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_ctUCZtwxZYhfF3sirByNY2qC', function=Function(arguments='{\"location\":\"London\",\"unit\":\"celsius\"}', name='get_current_weather'), type='function')])" ] }, - "execution_count": 17, + "execution_count": 14, "metadata": {}, "output_type": "execute_result" } @@ -356,16 +363,16 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "ChatCompletionMessage(content=None, role='assistant', function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_BZo3k606xCW8fnIPlLdJVpRo', function=Function(arguments='{\"location\": \"London\", \"unit\": \"celsius\"}', name='get_current_weather'), type='function'), ChatCompletionMessageToolCall(id='call_rx3uTL4l3PQztPzca04t1Ecu', function=Function(arguments='{\"location\": \"Belmopan\", \"unit\": \"celsius\"}', name='get_current_weather'), type='function')])" + "ChatCompletionMessage(content=None, role='assistant', function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_GhyrfrY9QWkrJs9vKqxpGW22', function=Function(arguments='{\"location\": \"London\", \"unit\": \"celsius\"}', name='get_current_weather'), type='function'), ChatCompletionMessageToolCall(id='call_Cl9fqGh1AZgi4yD8n5Lrc6NE', function=Function(arguments='{\"location\": \"Belmopan\", \"unit\": \"celsius\"}', name='get_current_weather'), type='function')])" ] }, - "execution_count": 18, + "execution_count": 15, "metadata": {}, "output_type": "execute_result" } @@ -391,14 +398,14 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Function Calling Response as Model Feedback\n", + "### Function Calling Response for Model Feedback\n", "\n", "You might also be interested in developing an agent that passes back the result obtained after calling your APIs with the inputs generated from function calling. Let's look at an example next:\n" ] }, { "cell_type": "code", - "execution_count": 29, + "execution_count": 16, "metadata": {}, "outputs": [], "source": [ @@ -415,7 +422,7 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": 17, "metadata": {}, "outputs": [], "source": [ @@ -431,7 +438,7 @@ }, { "cell_type": "code", - "execution_count": 32, + "execution_count": 18, "metadata": {}, "outputs": [], "source": [ @@ -446,7 +453,7 @@ }, { "cell_type": "code", - "execution_count": 34, + "execution_count": 19, "metadata": {}, "outputs": [], "source": [ @@ -455,16 +462,16 @@ }, { "cell_type": "code", - "execution_count": 35, + "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "ChatCompletionMessage(content='The current weather in Boston, MA is 50°F.', role='assistant', function_call=None, tool_calls=None)" + "ChatCompletionMessage(content='The current temperature in Boston, MA is 50°F.', role='assistant', function_call=None, tool_calls=None)" ] }, - "execution_count": 35, + "execution_count": 20, "metadata": {}, "output_type": "execute_result" } diff --git a/notebooks/pe-lecture.ipynb b/notebooks/pe-lecture.ipynb index 24fbf44..f23c2cd 100644 --- a/notebooks/pe-lecture.ipynb +++ b/notebooks/pe-lecture.ipynb @@ -59,7 +59,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ @@ -80,7 +80,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ @@ -90,18 +90,17 @@ "openai.api_key = os.getenv(\"OPENAI_API_KEY\")\n", "\n", "# for LangChain\n", - "os.environ[\"OPENAI_API_KEY\"] = os.getenv(\"OPENAI_API_KEY\")\n", - "os.environ[\"SERPER_API_KEY\"] = os.getenv(\"SERPER_API_KEY\")" + "os.environ[\"OPENAI_API_KEY\"] = os.getenv(\"OPENAI_API_KEY\")" ] }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "def set_open_params(\n", - " model=\"text-davinci-003\",\n", + " model=\"gpt-3.5-turbo\",\n", " temperature=0.7,\n", " max_tokens=256,\n", " top_p=1,\n", @@ -120,12 +119,12 @@ " openai_params['presence_penalty'] = presence_penalty\n", " return openai_params\n", "\n", - "def get_completion(params, prompt):\n", + "def get_completion(params, messages):\n", " \"\"\" GET completion from openai api\"\"\"\n", "\n", - " response = openai.Completion.create(\n", - " engine = params['model'],\n", - " prompt = prompt,\n", + " response = openai.chat.completions.create(\n", + " model = params['model'],\n", + " messages = messages,\n", " temperature = params['temperature'],\n", " max_tokens = params['max_tokens'],\n", " top_p = params['top_p'],\n", @@ -145,7 +144,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 11, "metadata": {}, "outputs": [], "source": [ @@ -154,52 +153,34 @@ "\n", "prompt = \"The sky is\"\n", "\n", - "response = get_completion(params, prompt)" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "' blue\\n\\nThe sky is blue because of the way that the atmosphere scatters the sunlight. The blue wavelengths of visible light are scattered more than other wavelengths. This is why the sky appears blue from the ground.'" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "response.choices[0].text" + "messages = [\n", + " {\n", + " \"role\": \"user\",\n", + " \"content\": prompt\n", + " }\n", + "]\n", + "\n", + "response = get_completion(params, messages)" ] }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 16, "metadata": {}, "outputs": [ { "data": { - "text/markdown": [ - " blue\n", - "\n", - "The sky is blue because of the way that the atmosphere scatters the sunlight. The blue wavelengths of visible light are scattered more than other wavelengths. This is why the sky appears blue from the ground." - ], "text/plain": [ - "" + "'blue.'" ] }, - "execution_count": 6, + "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "IPython.display.Markdown(response.choices[0].text)" + "response.choices[0].message.content" ] }, { @@ -212,30 +193,27 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ - " blue\n", - "\n", - "The sky is blue because of the way the atmosphere scatters sunlight. When sunlight passes through the atmosphere, the blue wavelengths are scattered more than the other colors, making the sky appear blue." + "blue during the day and black at night." ], "text/plain": [ "" ] }, - "execution_count": 7, + "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "params = set_open_params(temperature=0)\n", - "prompt = \"The sky is\"\n", - "response = get_completion(params, prompt)\n", - "IPython.display.Markdown(response.choices[0].text)" + "response = get_completion(params, messages)\n", + "IPython.display.Markdown(response.choices[0].message.content)" ] }, { @@ -248,20 +226,19 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ - "\n", - "Antibiotics are medications used to treat bacterial infections by either killing the bacteria or stopping them from reproducing, but they are not effective against viral infections and can lead to antibiotic resistance if used incorrectly." + "Antibiotics are medications that treat bacterial infections by either killing the bacteria or stopping their reproduction, but they are ineffective against viral infections and misuse can lead to antibiotic resistance." ], "text/plain": [ "" ] }, - "execution_count": 8, + "execution_count": 18, "metadata": {}, "output_type": "execute_result" } @@ -272,8 +249,15 @@ "\n", "Explain the above in one sentence:\"\"\"\n", "\n", - "response = get_completion(params, prompt)\n", - "IPython.display.Markdown(response.choices[0].text)" + "messages = [\n", + " {\n", + " \"role\": \"user\",\n", + " \"content\": prompt\n", + " }\n", + "]\n", + "\n", + "response = get_completion(params, messages)\n", + "IPython.display.Markdown(response.choices[0].message.content)" ] }, { @@ -294,19 +278,19 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ - " Mice." + "Mice." ], "text/plain": [ "" ] }, - "execution_count": 9, + "execution_count": 19, "metadata": {}, "output_type": "execute_result" } @@ -320,8 +304,15 @@ "\n", "Answer:\"\"\"\n", "\n", - "response = get_completion(params, prompt)\n", - "IPython.display.Markdown(response.choices[0].text)\n" + "messages = [\n", + " {\n", + " \"role\": \"user\",\n", + " \"content\": prompt\n", + " }\n", + "]\n", + "\n", + "response = get_completion(params, messages)\n", + "IPython.display.Markdown(response.choices[0].message.content)\n" ] }, { @@ -350,19 +341,19 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ - " Neutral" + "Neutral" ], "text/plain": [ "" ] }, - "execution_count": 10, + "execution_count": 21, "metadata": {}, "output_type": "execute_result" } @@ -374,8 +365,15 @@ "\n", "Sentiment:\"\"\"\n", "\n", - "response = get_completion(params, prompt)\n", - "IPython.display.Markdown(response.choices[0].text)" + "messages = [\n", + " {\n", + " \"role\": \"user\",\n", + " \"content\": prompt\n", + " }\n", + "]\n", + "\n", + "response = get_completion(params, messages)\n", + "IPython.display.Markdown(response.choices[0].message.content)" ] }, { @@ -396,19 +394,19 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ - " Sure. Black holes are regions of spacetime exhibiting gravitational acceleration so strong that nothing—not even particles and electromagnetic radiation such as light—can escape from inside it. The theory of general relativity predicts that a sufficiently compact mass can deform spacetime to form a black hole. The boundary of the region from which no escape is possible is called the event horizon. Although crossing the event horizon has enormous effect on the fate of the object crossing it, no locally detectable features appear to be observed. In many ways, a black hole acts like an idealized black body, as it reflects no light." + "Certainly! Black holes are formed from the remnants of massive stars that have exhausted their nuclear fuel and undergone a supernova explosion. During this explosion, the outer layers of the star are blown away, leaving behind a dense core known as a stellar remnant. If the mass of the stellar remnant is above a certain threshold, called the Tolman-Oppenheimer-Volkoff limit, gravity becomes so strong that the core collapses in on itself, forming a black hole. This collapse is driven by the inward pull of gravity, and it results in a region of space where the gravitational field is so strong that nothing, not even light, can escape from it. This region is known as the event horizon." ], "text/plain": [ "" ] }, - "execution_count": 11, + "execution_count": 22, "metadata": {}, "output_type": "execute_result" } @@ -421,8 +419,23 @@ "Human: Can you tell me about the creation of blackholes?\n", "AI:\"\"\"\n", "\n", - "response = get_completion(params, prompt)\n", - "IPython.display.Markdown(response.choices[0].text)" + "messages = [\n", + " {\n", + " \"role\": \"user\",\n", + " \"content\": prompt\n", + " }\n", + "]\n", + "\n", + "messages = [\n", + " {\n", + " \"role\": \"user\",\n", + " \"content\": prompt\n", + " }\n", + "\n", + "]\n", + "\n", + "response = get_completion(params, messages)\n", + "IPython.display.Markdown(response.choices[0].message.content)" ] }, { @@ -443,23 +456,21 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ - "\n", - "SELECT students.StudentId, students.StudentName \n", - "FROM students \n", - "INNER JOIN departments ON students.DepartmentId = departments.DepartmentId\n", - "WHERE departments.DepartmentName = 'Computer Science';" + "SELECT StudentName\n", + "FROM students\n", + "WHERE DepartmentId = (SELECT DepartmentId FROM departments WHERE DepartmentName = 'Computer Science')" ], "text/plain": [ "" ] }, - "execution_count": 12, + "execution_count": 23, "metadata": {}, "output_type": "execute_result" } @@ -467,8 +478,15 @@ "source": [ "prompt = \"\\\"\\\"\\\"\\nTable departments, columns = [DepartmentId, DepartmentName]\\nTable students, columns = [DepartmentId, StudentId, StudentName]\\nCreate a MySQL query for all students in the Computer Science Department\\n\\\"\\\"\\\"\"\n", "\n", - "response = get_completion(params, prompt)\n", - "IPython.display.Markdown(response.choices[0].text)\n" + "messages = [\n", + " {\n", + " \"role\": \"user\",\n", + " \"content\": prompt\n", + " }\n", + "]\n", + "\n", + "response = get_completion(params, messages)\n", + "IPython.display.Markdown(response.choices[0].message.content)\n" ] }, { @@ -481,25 +499,27 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ + "To solve this problem, we need to follow these steps:\n", "\n", + "Step 1: Identify the odd numbers in the given group. The odd numbers in the group are 15, 5, 13, 7, and 1.\n", "\n", - "The odd numbers in this group are: 5, 13, 7, and 1. \n", + "Step 2: Add the odd numbers together. 15 + 5 + 13 + 7 + 1 = 41.\n", "\n", - "5 + 13 + 7 + 1 = 26 \n", + "Step 3: Determine whether the sum is odd or even. In this case, the sum is 41, which is an odd number.\n", "\n", - "26 is an even number." + "Therefore, the sum of the odd numbers in the given group is an odd number." ], "text/plain": [ "" ] }, - "execution_count": 13, + "execution_count": 24, "metadata": {}, "output_type": "execute_result" } @@ -509,8 +529,15 @@ "\n", "Solve by breaking the problem into steps. First, identify the odd numbers, add them, and indicate whether the result is odd or even.\"\"\"\n", "\n", - "response = get_completion(params, prompt)\n", - "IPython.display.Markdown(response.choices[0].text)" + "messages = [\n", + " {\n", + " \"role\": \"user\",\n", + " \"content\": prompt\n", + " }\n", + "]\n", + "\n", + "response = get_completion(params, messages)\n", + "IPython.display.Markdown(response.choices[0].message.content)" ] }, { @@ -543,19 +570,19 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ - " The answer is False." + "The answer is False." ], "text/plain": [ "" ] }, - "execution_count": 14, + "execution_count": 25, "metadata": {}, "output_type": "execute_result" } @@ -576,8 +603,15 @@ "The odd numbers in this group add up to an even number: 15, 32, 5, 13, 82, 7, 1. \n", "A:\"\"\"\n", "\n", - "response = get_completion(params, prompt)\n", - "IPython.display.Markdown(response.choices[0].text)" + "messages = [\n", + " {\n", + " \"role\": \"user\",\n", + " \"content\": prompt\n", + " }\n", + "]\n", + "\n", + "response = get_completion(params, messages)\n", + "IPython.display.Markdown(response.choices[0].message.content)" ] }, { @@ -590,19 +624,19 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ - " Adding all the odd numbers (15, 5, 13, 7, 1) gives 41. The answer is False." + "Adding all the odd numbers (15, 5, 13, 7, 1) gives 41. The answer is False." ], "text/plain": [ "" ] }, - "execution_count": 15, + "execution_count": 26, "metadata": {}, "output_type": "execute_result" } @@ -614,8 +648,15 @@ "The odd numbers in this group add up to an even number: 15, 32, 5, 13, 82, 7, 1. \n", "A:\"\"\"\n", "\n", - "response = get_completion(params, prompt)\n", - "IPython.display.Markdown(response.choices[0].text)" + "messages = [\n", + " {\n", + " \"role\": \"user\",\n", + " \"content\": prompt\n", + " }\n", + "]\n", + "\n", + "response = get_completion(params, messages)\n", + "IPython.display.Markdown(response.choices[0].message.content)" ] }, { @@ -628,27 +669,27 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ + "Step 1: Bought 10 apples.\n", + "Step 2: Gave 2 apples to the neighbor and 2 apples to the repairman.\n", + "Remaining apples: 10 - 2 - 2 = 6 apples.\n", + "Step 3: Bought 5 more apples.\n", + "Total apples now: 6 + 5 = 11 apples.\n", + "Step 4: Ate 1 apple.\n", + "Remaining apples: 11 - 1 = 10 apples.\n", "\n", - "\n", - "You initially bought 10 apples.\n", - "\n", - "You then gave away 4 apples, leaving you with 6 apples.\n", - "\n", - "You bought 5 more apples, so now you have 11 apples.\n", - "\n", - "After eating 1 apple, you have 10 apples remaining." + "Final answer: You remained with 10 apples." ], "text/plain": [ "" ] }, - "execution_count": 16, + "execution_count": 28, "metadata": {}, "output_type": "execute_result" } @@ -658,8 +699,15 @@ "\n", "Let's think step by step.\"\"\"\n", "\n", - "response = get_completion(params, prompt)\n", - "IPython.display.Markdown(response.choices[0].text)" + "messages = [\n", + " {\n", + " \"role\": \"user\",\n", + " \"content\": prompt\n", + " }\n", + "]\n", + "\n", + "response = get_completion(params, messages)\n", + "IPython.display.Markdown(response.choices[0].message.content)" ] }, { @@ -675,193 +723,6 @@ "As an exercise, check examples in our [guide](https://github.com/dair-ai/Prompt-Engineering-Guide/blob/main/guides/prompts-advanced-usage.md#generated-knowledge-prompting) and try them here. " ] }, - { - "attachments": {}, - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### 2.6 PAL - Code as Reasoning" - ] - }, - { - "attachments": {}, - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We are developing a simple application that's able to reason about the question being asked through code. \n", - "\n", - "Specifically, the application takes in some data and answers a question about the data input. The prompt includes a few exemplars which are adopted from [here](https://github.com/reasoning-machines/pal/blob/main/pal/prompt/penguin_prompt.py). " - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "metadata": {}, - "outputs": [], - "source": [ - "# lm instance\n", - "llm = OpenAI(model_name='text-davinci-003', temperature=0)" - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": {}, - "outputs": [], - "source": [ - "question = \"Which is the oldest penguin?\"" - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "metadata": {}, - "outputs": [], - "source": [ - "PENGUIN_PROMPT = '''\n", - "\"\"\"\n", - "Q: Here is a table where the first line is a header and each subsequent line is a penguin:\n", - "name, age, height (cm), weight (kg) \n", - "Louis, 7, 50, 11\n", - "Bernard, 5, 80, 13\n", - "Vincent, 9, 60, 11\n", - "Gwen, 8, 70, 15\n", - "For example: the age of Louis is 7, the weight of Gwen is 15 kg, the height of Bernard is 80 cm. \n", - "We now add a penguin to the table:\n", - "James, 12, 90, 12\n", - "How many penguins are less than 8 years old?\n", - "\"\"\"\n", - "# Put the penguins into a list.\n", - "penguins = []\n", - "penguins.append(('Louis', 7, 50, 11))\n", - "penguins.append(('Bernard', 5, 80, 13))\n", - "penguins.append(('Vincent', 9, 60, 11))\n", - "penguins.append(('Gwen', 8, 70, 15))\n", - "# Add penguin James.\n", - "penguins.append(('James', 12, 90, 12))\n", - "# Find penguins under 8 years old.\n", - "penguins_under_8_years_old = [penguin for penguin in penguins if penguin[1] < 8]\n", - "# Count number of penguins under 8.\n", - "num_penguin_under_8 = len(penguins_under_8_years_old)\n", - "answer = num_penguin_under_8\n", - "\"\"\"\n", - "Q: Here is a table where the first line is a header and each subsequent line is a penguin:\n", - "name, age, height (cm), weight (kg) \n", - "Louis, 7, 50, 11\n", - "Bernard, 5, 80, 13\n", - "Vincent, 9, 60, 11\n", - "Gwen, 8, 70, 15\n", - "For example: the age of Louis is 7, the weight of Gwen is 15 kg, the height of Bernard is 80 cm.\n", - "Which is the youngest penguin?\n", - "\"\"\"\n", - "# Put the penguins into a list.\n", - "penguins = []\n", - "penguins.append(('Louis', 7, 50, 11))\n", - "penguins.append(('Bernard', 5, 80, 13))\n", - "penguins.append(('Vincent', 9, 60, 11))\n", - "penguins.append(('Gwen', 8, 70, 15))\n", - "# Sort the penguins by age.\n", - "penguins = sorted(penguins, key=lambda x: x[1])\n", - "# Get the youngest penguin's name.\n", - "youngest_penguin_name = penguins[0][0]\n", - "answer = youngest_penguin_name\n", - "\"\"\"\n", - "Q: Here is a table where the first line is a header and each subsequent line is a penguin:\n", - "name, age, height (cm), weight (kg) \n", - "Louis, 7, 50, 11\n", - "Bernard, 5, 80, 13\n", - "Vincent, 9, 60, 11\n", - "Gwen, 8, 70, 15\n", - "For example: the age of Louis is 7, the weight of Gwen is 15 kg, the height of Bernard is 80 cm.\n", - "What is the name of the second penguin sorted by alphabetic order?\n", - "\"\"\"\n", - "# Put the penguins into a list.\n", - "penguins = []\n", - "penguins.append(('Louis', 7, 50, 11))\n", - "penguins.append(('Bernard', 5, 80, 13))\n", - "penguins.append(('Vincent', 9, 60, 11))\n", - "penguins.append(('Gwen', 8, 70, 15))\n", - "# Sort penguins by alphabetic order.\n", - "penguins_alphabetic = sorted(penguins, key=lambda x: x[0])\n", - "# Get the second penguin sorted by alphabetic order.\n", - "second_penguin_name = penguins_alphabetic[1][0]\n", - "answer = second_penguin_name\n", - "\"\"\"\n", - "{question}\n", - "\"\"\"\n", - "'''.strip() + '\\n'" - ] - }, - { - "attachments": {}, - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now that we have the prompt and question. We can send it to the model. It should output the steps, in code, needed to get the solution to the answer." - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "# Put the penguins into a list.\n", - "penguins = []\n", - "penguins.append(('Louis', 7, 50, 11))\n", - "penguins.append(('Bernard', 5, 80, 13))\n", - "penguins.append(('Vincent', 9, 60, 11))\n", - "penguins.append(('Gwen', 8, 70, 15))\n", - "# Sort the penguins by age.\n", - "penguins = sorted(penguins, key=lambda x: x[1], reverse=True)\n", - "# Get the oldest penguin's name.\n", - "oldest_penguin_name = penguins[0][0]\n", - "answer = oldest_penguin_name\n" - ] - } - ], - "source": [ - "llm_out = llm(PENGUIN_PROMPT.format(question=question))\n", - "print(llm_out)" - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Vincent\n" - ] - } - ], - "source": [ - "exec(llm_out)\n", - "print(answer)" - ] - }, - { - "attachments": {}, - "cell_type": "markdown", - "metadata": {}, - "source": [ - "That's the correct answer! Vincent is the oldest penguin. " - ] - }, - { - "attachments": {}, - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Exercise: Try a different question and see what's the result." - ] - }, { "attachments": {}, "cell_type": "markdown", @@ -869,273 +730,6 @@ "source": [ "---" ] - }, - { - "attachments": {}, - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# 3. Tools and Applications\n", - "\n", - "Objective:\n", - "\n", - "- Demonstrate how to use LangChain to demonstrate simple applications using prompting techniques and LLMs" - ] - }, - { - "attachments": {}, - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### 3.1 LLMs & External Tools\n", - "\n", - "Example adopted from the [LangChain documentation](https://langchain.readthedocs.io/en/latest/modules/agents/getting_started.html)." - ] - }, - { - "cell_type": "code", - "execution_count": 22, - "metadata": {}, - "outputs": [], - "source": [ - "from langchain.agents import load_tools\n", - "from langchain.agents import initialize_agent" - ] - }, - { - "cell_type": "code", - "execution_count": 23, - "metadata": {}, - "outputs": [], - "source": [ - "llm = OpenAI(temperature=0)\n", - "\n", - "tools = load_tools([\"google-serper\", \"llm-math\"], llm=llm)\n", - "agent = initialize_agent(tools, llm, agent=\"zero-shot-react-description\", verbose=True)" - ] - }, - { - "cell_type": "code", - "execution_count": 24, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n", - "\n", - "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", - "\u001b[32;1m\u001b[1;3m I need to find out who Olivia Wilde's boyfriend is and then calculate his age raised to the 0.23 power.\n", - "Action: Search\n", - "Action Input: \"Olivia Wilde boyfriend\"\u001b[0m\n", - "Observation: \u001b[36;1m\u001b[1;3mJason Sudeikis\u001b[0m\n", - "Thought:\u001b[32;1m\u001b[1;3m I need to find out Jason Sudeikis' age\n", - "Action: Search\n", - "Action Input: \"Jason Sudeikis age\"\u001b[0m\n", - "Observation: \u001b[36;1m\u001b[1;3m47 years\u001b[0m\n", - "Thought:\u001b[32;1m\u001b[1;3m I need to calculate 47 raised to the 0.23 power\n", - "Action: Calculator\n", - "Action Input: 47^0.23\u001b[0m\n", - "Observation: \u001b[33;1m\u001b[1;3mAnswer: 2.4242784855673896\n", - "\u001b[0m\n", - "Thought:\u001b[32;1m\u001b[1;3m I now know the final answer\n", - "Final Answer: Jason Sudeikis, Olivia Wilde's boyfriend, is 47 years old and his age raised to the 0.23 power is 2.4242784855673896.\u001b[0m\n", - "\n", - "\u001b[1m> Finished chain.\u001b[0m\n" - ] - }, - { - "data": { - "text/plain": [ - "\"Jason Sudeikis, Olivia Wilde's boyfriend, is 47 years old and his age raised to the 0.23 power is 2.4242784855673896.\"" - ] - }, - "execution_count": 24, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# run the agent\n", - "agent.run(\"Who is Olivia Wilde's boyfriend? What is his current age raised to the 0.23 power?\")" - ] - }, - { - "attachments": {}, - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### 3.2 Data-Augmented Generation" - ] - }, - { - "attachments": {}, - "cell_type": "markdown", - "metadata": {}, - "source": [ - "First, we need to download the data we want to use as source to augment generation.\n", - "\n", - "Code example adopted from [LangChain Documentation](https://langchain.readthedocs.io/en/latest/modules/chains/combine_docs_examples/qa_with_sources.html). We are only using the examples for educational purposes." - ] - }, - { - "attachments": {}, - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Prepare the data first:" - ] - }, - { - "cell_type": "code", - "execution_count": 25, - "metadata": {}, - "outputs": [], - "source": [ - "from langchain.embeddings.openai import OpenAIEmbeddings\n", - "from langchain.embeddings.cohere import CohereEmbeddings\n", - "from langchain.text_splitter import CharacterTextSplitter\n", - "from langchain.vectorstores.elastic_vector_search import ElasticVectorSearch\n", - "from langchain.vectorstores import Chroma\n", - "from langchain.docstore.document import Document\n", - "from langchain.prompts import PromptTemplate" - ] - }, - { - "cell_type": "code", - "execution_count": 26, - "metadata": {}, - "outputs": [], - "source": [ - "with open('./state_of_the_union.txt') as f:\n", - " state_of_the_union = f.read()\n", - "text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)\n", - "texts = text_splitter.split_text(state_of_the_union)\n", - "\n", - "embeddings = OpenAIEmbeddings()" - ] - }, - { - "cell_type": "code", - "execution_count": 27, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Running Chroma using direct local API.\n", - "Using DuckDB in-memory for database. Data will be transient.\n" - ] - } - ], - "source": [ - "docsearch = Chroma.from_texts(texts, embeddings, metadatas=[{\"source\": str(i)} for i in range(len(texts))])" - ] - }, - { - "cell_type": "code", - "execution_count": 28, - "metadata": {}, - "outputs": [], - "source": [ - "query = \"What did the president say about Justice Breyer\"\n", - "docs = docsearch.similarity_search(query)" - ] - }, - { - "attachments": {}, - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let's quickly test it:" - ] - }, - { - "cell_type": "code", - "execution_count": 29, - "metadata": {}, - "outputs": [], - "source": [ - "from langchain.chains.qa_with_sources import load_qa_with_sources_chain\n", - "from langchain.llms import OpenAI" - ] - }, - { - "cell_type": "code", - "execution_count": 30, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'output_text': ' The president thanked Justice Breyer for his service.\\nSOURCES: 30-pl'}" - ] - }, - "execution_count": 30, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "chain = load_qa_with_sources_chain(OpenAI(temperature=0), chain_type=\"stuff\")\n", - "query = \"What did the president say about Justice Breyer\"\n", - "chain({\"input_documents\": docs, \"question\": query}, return_only_outputs=True)" - ] - }, - { - "attachments": {}, - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let's try a question with a custom prompt:" - ] - }, - { - "cell_type": "code", - "execution_count": 31, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'output_text': '\\nEl Presidente no dijo nada acerca de la Justicia Breyer.\\n\\nFUENTES: 30, 31, 33'}" - ] - }, - "execution_count": 31, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "template = \"\"\"Given the following extracted parts of a long document and a question, create a final answer with references (\"SOURCES\"). \n", - "If you don't know the answer, just say that you don't know. Don't try to make up an answer.\n", - "ALWAYS return a \"SOURCES\" part in your answer.\n", - "Respond in Spanish.\n", - "\n", - "QUESTION: {question}\n", - "=========\n", - "{summaries}\n", - "=========\n", - "FINAL ANSWER IN SPANISH:\"\"\"\n", - "\n", - "# create a prompt template\n", - "PROMPT = PromptTemplate(template=template, input_variables=[\"summaries\", \"question\"])\n", - "\n", - "# query \n", - "chain = load_qa_with_sources_chain(OpenAI(temperature=0), chain_type=\"stuff\", prompt=PROMPT)\n", - "query = \"What did the president say about Justice Breyer?\"\n", - "chain({\"input_documents\": docs, \"question\": query}, return_only_outputs=True)" - ] - }, - { - "attachments": {}, - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Exercise: Try using a different dataset from the internet and try different prompt, including all the techniques you learned in the lecture." - ] } ], "metadata": { @@ -1154,7 +748,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.16" + "version": "3.9.18" }, "orig_nbformat": 4, "vscode": { diff --git a/notebooks/pe-rag.ipynb b/notebooks/pe-rag.ipynb new file mode 100644 index 0000000..5a5d889 --- /dev/null +++ b/notebooks/pe-rag.ipynb @@ -0,0 +1,1181 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "qLiDBaSrzWu8" + }, + "source": [ + "# Getting Started with RAG\n", + "\n", + "While large language models (LLMs) show powerful capabilities that power advanced use cases, they suffer from issues such as factual inconsistency and hallucination. Retrieval-augmented generation (RAG) is a powerful approach to enrich LLM capabilities and improve their reliability. RAG involves combining LLMs with external knowledge by enriching the prompt context with relevant information that helps accomplish a task.\n", + "\n", + "This tutorial shows how to getting started with RAG by leveraging vector store and open-source LLMs. To showcase the power of RAG, this use case will cover building a RAG system that suggests short and easy to read ML paper titles from original ML paper titles. Paper tiles can be too technical for a general audience so using RAG to generate short titles based on previously created short titles can make research paper titles more accessible and used for science communication such as in the form of newsletters or blogs." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "UsChkJxn2CSZ" + }, + "source": [ + "Before getting started, let's first install the libraries we will use:" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": { + "id": "9gy2ijb5zWu-" + }, + "outputs": [], + "source": [ + "%%capture\n", + "!pip install chromadb tqdm fireworks-ai python-dotenv pandas\n", + "!pip install sentence-transformers" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "-3iYAReMAe1q" + }, + "source": [ + "Before continuing, you need to obtain a Fireworks API Key to use the Mistral 7B model.\n", + "\n", + "Checkout this quick guide to obtain your Fireworks API Key: https://readme.fireworks.ai/docs" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": { + "id": "pBSEMYFszWu_" + }, + "outputs": [], + "source": [ + "import fireworks.client\n", + "import os\n", + "import dotenv\n", + "import chromadb\n", + "import json\n", + "from tqdm.auto import tqdm\n", + "import pandas as pd\n", + "import random\n", + "\n", + "# you can set envs using Colab secrets\n", + "dotenv.load_dotenv()\n", + "\n", + "fireworks.client.api_key = os.getenv(\"FIREWORKS_API_KEY\")" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "q9v_0IEDtgov" + }, + "source": [ + "## Getting Started" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "J8wYyXMizWu_" + }, + "source": [ + "Let's define a function to get completions from the Fireworks inference platform." + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": { + "id": "1hZldHjmzWvA" + }, + "outputs": [], + "source": [ + "def get_completion(prompt, model=None, max_tokens=50):\n", + "\n", + " fw_model_dir = \"accounts/fireworks/models/\"\n", + "\n", + " if model is None:\n", + " model = fw_model_dir + \"llama-v2-7b\"\n", + " else:\n", + " model = fw_model_dir + model\n", + "\n", + " completion = fireworks.client.Completion.create(\n", + " model=model,\n", + " prompt=prompt,\n", + " max_tokens=max_tokens,\n", + " temperature=0\n", + " )\n", + "\n", + " return completion.choices[0].text" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Ys59WgrGzWvA" + }, + "source": [ + "Let's first try the function with a simple prompt:" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 53 + }, + "id": "edQeSLODzWvA", + "outputId": "fb0174c2-1490-424c-98a5-fd954a833d40" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "' Katie and I am a 20 year old student at the University of Leeds. I am currently studying a BA in English Literature and Creative Writing. I have been working as a tutor for over 3 years now and I'" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "get_completion(\"Hello, my name is\")" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "CwDjmi8EzWvB" + }, + "source": [ + "Now let's test with Mistral-7B-Instruct:" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 53 + }, + "id": "O9TwL-2DzWvB", + "outputId": "affb05c1-6a61-4a31-8dee-f2ac6a769fd9" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "' [Your Name]. I am a [Your Profession/Occupation]. I am writing to [Purpose of Writing].\\n\\nI am writing to [Purpose of Writing] because [Reason for Writing]. I believe that ['" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mistral_llm = \"mistral-7b-instruct-4k\"\n", + "\n", + "get_completion(\"Hello, my name is\", model=mistral_llm)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "LZdlBtx-zWvB" + }, + "source": [ + "The Mistral 7B Instruct model needs to be instructed using special instruction tokens `[INST] [/INST]` to get the right behavior. You can find more instructions on how to prompt Mistral 7B Instruct here: https://docs.mistral.ai/llm/mistral-instruct-v0.1" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 53 + }, + "id": "ITURzGa9zWvC", + "outputId": "5ba3d395-9887-438a-d2a8-808c1598cda6" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "\".\\n1. Why don't scientists trust atoms? Because they make up everything!\\n2. Did you hear about the mathematician who’s afraid of negative numbers? He will stop at nothing to avoid them.\"" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mistral_llm = \"mistral-7b-instruct-4k\"\n", + "\n", + "get_completion(\"Tell me 2 jokes\", model=mistral_llm)" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 53 + }, + "id": "QN6Y2y1GzWvC", + "outputId": "4181f5e5-cb73-47d4-c1f3-1ea1e2f8eb8b" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "\" Sure, here are two jokes for you:\\n\\n1. Why don't scientists trust atoms? Because they make up everything!\\n2. Why did the tomato turn red? Because it saw the salad dressing!\"" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mistral_llm = \"mistral-7b-instruct-4k\"\n", + "\n", + "get_completion(\"[INST]Tell me 2 jokes[/INST]\", model=mistral_llm)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "jZHyn0tJzWvC" + }, + "source": [ + "Now let's try with a more complex prompt that involves instructions:" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 87 + }, + "id": "dVwL--2kzWvC", + "outputId": "1e1d37cd-ba24-4b0a-ab2f-62b516d515f1" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "\" Dear John Doe,\\n\\nWe, Tom and Mary, would like to extend our heartfelt gratitude for your attendance at our wedding. It was a pleasure to have you there, and we truly appreciate the effort you made to be a part of our special day.\\n\\nWe were thrilled to learn about your fun fact - climbing Mount Everest is an incredible accomplishment! We hope you had a safe and memorable journey.\\n\\nThank you again for joining us on this special occasion. We hope to stay in touch and catch up on all the amazing things you've been up to.\\n\\nWith love,\\n\\nTom and Mary\"" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "prompt = \"\"\"[INST]\n", + "Given the following wedding guest data, write a very short 3-sentences thank you letter:\n", + "\n", + "{\n", + " \"name\": \"John Doe\",\n", + " \"relationship\": \"Bride's cousin\",\n", + " \"hometown\": \"New York, NY\",\n", + " \"fun_fact\": \"Climbed Mount Everest in 2020\",\n", + " \"attending_with\": \"Sophia Smith\",\n", + " \"bride_groom_name\": \"Tom and Mary\"\n", + "}\n", + "\n", + "Use only the data provided in the JSON object above.\n", + "\n", + "The senders of the letter is the bride and groom, Tom and Mary.\n", + "[/INST]\"\"\"\n", + "\n", + "get_completion(prompt, model=mistral_llm, max_tokens=150)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "9SROezW6zWvD" + }, + "source": [ + "## RAG Use Case: Generating Short Paper Titles\n", + "\n", + "For the RAG use case, we will be using [a dataset](https://github.com/dair-ai/ML-Papers-of-the-Week/tree/main/research) that contains a list of weekly top trending ML papers.\n", + "\n", + "The user will provide an original paper title. We will then take that input and then use the dataset to generate a context of short and catchy papers titles that will help generate catchy title for the original input title.\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "0sw7Uk6qzWvD" + }, + "source": [ + "### Step 1: Load the Dataset\n", + "\n", + "Let's first load the dataset we will use:" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": { + "id": "mv1z1LLczWvD" + }, + "outputs": [], + "source": [ + "# load dataset from data/ folder to pandas dataframe\n", + "# dataset contains column names\n", + "\n", + "ml_papers = pd.read_csv(\"../data/ml-potw-10232023.csv\", header=0)\n", + "\n", + "# remove rows with empty titles or descriptions\n", + "ml_papers = ml_papers.dropna(subset=[\"Title\", \"Description\"])" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 467 + }, + "id": "ErNNQRPqzWvD", + "outputId": "817472e8-8b94-4fd6-c207-b26c62d2babd" + }, + "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", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
TitleDescriptionPaperURLTweetURLAbstract
0Llemmaan LLM for mathematics which is based on conti...https://arxiv.org/abs/2310.10631https://x.com/zhangir_azerbay/status/171409802...We present Llemma, a large language model for ...
1LLMs for Software Engineeringa comprehensive survey of LLMs for software en...https://arxiv.org/abs/2310.03533https://x.com/omarsar0/status/1713940983199506...This paper provides a survey of the emerging a...
2Self-RAGpresents a new retrieval-augmented framework t...https://arxiv.org/abs/2310.11511https://x.com/AkariAsai/status/171511027707796...Despite their remarkable capabilities, large l...
3Retrieval-Augmentation for Long-form Question ...explores retrieval-augmented language models o...https://arxiv.org/abs/2310.12150https://x.com/omarsar0/status/1714986431859282...We present a study of retrieval-augmented lang...
4GenBenchpresents a framework for characterizing and un...https://www.nature.com/articles/s42256-023-007...https://x.com/AIatMeta/status/1715041427283902...NaN
\n", + "
" + ], + "text/plain": [ + " Title \\\n", + "0 Llemma \n", + "1 LLMs for Software Engineering \n", + "2 Self-RAG \n", + "3 Retrieval-Augmentation for Long-form Question ... \n", + "4 GenBench \n", + "\n", + " Description \\\n", + "0 an LLM for mathematics which is based on conti... \n", + "1 a comprehensive survey of LLMs for software en... \n", + "2 presents a new retrieval-augmented framework t... \n", + "3 explores retrieval-augmented language models o... \n", + "4 presents a framework for characterizing and un... \n", + "\n", + " PaperURL \\\n", + "0 https://arxiv.org/abs/2310.10631 \n", + "1 https://arxiv.org/abs/2310.03533 \n", + "2 https://arxiv.org/abs/2310.11511 \n", + "3 https://arxiv.org/abs/2310.12150 \n", + "4 https://www.nature.com/articles/s42256-023-007... \n", + "\n", + " TweetURL \\\n", + "0 https://x.com/zhangir_azerbay/status/171409802... \n", + "1 https://x.com/omarsar0/status/1713940983199506... \n", + "2 https://x.com/AkariAsai/status/171511027707796... \n", + "3 https://x.com/omarsar0/status/1714986431859282... \n", + "4 https://x.com/AIatMeta/status/1715041427283902... \n", + "\n", + " Abstract \n", + "0 We present Llemma, a large language model for ... \n", + "1 This paper provides a survey of the emerging a... \n", + "2 Despite their remarkable capabilities, large l... \n", + "3 We present a study of retrieval-augmented lang... \n", + "4 NaN " + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ml_papers.head()" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": { + "id": "KzyvzYcNzWvD" + }, + "outputs": [], + "source": [ + "# convert dataframe to list of dicts with Title and Description columns only\n", + "\n", + "ml_papers_dict = ml_papers.to_dict(orient=\"records\")" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "F3LUGNHIzWvE", + "outputId": "3b1aa123-e316-488f-d0a4-0369bb2f75dd" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{'Title': 'Llemma',\n", + " 'Description': 'an LLM for mathematics which is based on continued pretraining from Code Llama on the Proof-Pile-2 dataset; the dataset involves scientific paper, web data containing mathematics, and mathematical code; Llemma outperforms open base models and the unreleased Minerva on the MATH benchmark; the model is released, including dataset and code to replicate experiments.',\n", + " 'PaperURL': 'https://arxiv.org/abs/2310.10631',\n", + " 'TweetURL': 'https://x.com/zhangir_azerbay/status/1714098025956864031?s=20',\n", + " 'Abstract': 'We present Llemma, a large language model for mathematics. We continue pretraining Code Llama on the Proof-Pile-2, a mixture of scientific papers, web data containing mathematics, and mathematical code, yielding Llemma. On the MATH benchmark Llemma outperforms all known open base models, as well as the unreleased Minerva model suite on an equi-parameter basis. Moreover, Llemma is capable of tool use and formal theorem proving without any further finetuning. We openly release all artifacts, including 7 billion and 34 billion parameter models, the Proof-Pile-2, and code to replicate our experiments.'}" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ml_papers_dict[0]" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "WwfW0XoxzWvE" + }, + "source": [ + "We will be using SentenceTransformer for generating embeddings that we will store to a chroma document store." + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": { + "id": "1zFDOicHzWvE" + }, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + ".gitattributes: 100%|██████████| 1.18k/1.18k [00:00<00:00, 194kB/s]\n", + "1_Pooling/config.json: 100%|██████████| 190/190 [00:00<00:00, 204kB/s]\n", + "README.md: 100%|██████████| 10.6k/10.6k [00:00<00:00, 7.64MB/s]\n", + "config.json: 100%|██████████| 612/612 [00:00<00:00, 679kB/s]\n", + "config_sentence_transformers.json: 100%|██████████| 116/116 [00:00<00:00, 94.0kB/s]\n", + "data_config.json: 100%|██████████| 39.3k/39.3k [00:00<00:00, 7.80MB/s]\n", + "pytorch_model.bin: 100%|██████████| 90.9M/90.9M [00:03<00:00, 24.3MB/s]\n", + "sentence_bert_config.json: 100%|██████████| 53.0/53.0 [00:00<00:00, 55.4kB/s]\n", + "special_tokens_map.json: 100%|██████████| 112/112 [00:00<00:00, 161kB/s]\n", + "tokenizer.json: 100%|██████████| 466k/466k [00:00<00:00, 6.15MB/s]\n", + "tokenizer_config.json: 100%|██████████| 350/350 [00:00<00:00, 286kB/s]\n", + "train_script.py: 100%|██████████| 13.2k/13.2k [00:00<00:00, 12.2MB/s]\n", + "vocab.txt: 100%|██████████| 232k/232k [00:00<00:00, 9.15MB/s]\n", + "modules.json: 100%|██████████| 349/349 [00:00<00:00, 500kB/s]\n" + ] + } + ], + "source": [ + "from chromadb import Documents, EmbeddingFunction, Embeddings\n", + "from sentence_transformers import SentenceTransformer\n", + "embedding_model = SentenceTransformer('all-MiniLM-L6-v2')\n", + "\n", + "class MyEmbeddingFunction(EmbeddingFunction):\n", + " def __call__(self, input: Documents) -> Embeddings:\n", + " batch_embeddings = embedding_model.encode(input)\n", + " return batch_embeddings.tolist()\n", + "\n", + "embed_fn = MyEmbeddingFunction()\n", + "\n", + "# Initialize the chromadb directory, and client.\n", + "client = chromadb.PersistentClient(path=\"./chromadb\")\n", + "\n", + "# create collection\n", + "collection = client.get_or_create_collection(\n", + " name=f\"ml-papers-nov-2023\"\n", + ")" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "eu0_-PREzWvE" + }, + "source": [ + "We will now generate embeddings for batches:" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 49, + "referenced_widgets": [ + "b9da413d4f84436ab5dc0fd10d237b0a", + "c269f70baec246288519dbb2517c05c0", + "b568c3c04efb49acb00e44aadc247735", + "868c943d8a82435a8c3df6f32a3cc433", + "51ad257305a0438fbed46c613d2d59fb", + "cfbba792e6054178b504c2e2bbc23b2f", + "54c8ecbae313483e82879016cc49bd25", + "8e06dc7bf2f94d63a69b651f594ecf74", + "bacb31eccd5c4a2dba61503a658333f2", + "b9545ef27bf24e0c86d713ae8a3c0d2c", + "0285ca3156854ca09a252540ad6a43ff" + ] + }, + "id": "kUauose2zWvE", + "outputId": "18b7bf6a-0341-4843-8168-9875d78a6de9" + }, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "100%|██████████| 9/9 [00:01<00:00, 7.62it/s]\n" + ] + } + ], + "source": [ + "# Generate embeddings, and index titles in batches\n", + "batch_size = 50\n", + "\n", + "# loop through batches and generated + store embeddings\n", + "for i in tqdm(range(0, len(ml_papers_dict), batch_size)):\n", + "\n", + " i_end = min(i + batch_size, len(ml_papers_dict))\n", + " batch = ml_papers_dict[i : i + batch_size]\n", + "\n", + " # Replace title with \"No Title\" if empty string\n", + " batch_titles = [str(paper[\"Title\"]) if str(paper[\"Title\"]) != \"\" else \"No Title\" for paper in batch]\n", + " batch_ids = [str(sum(ord(c) + random.randint(1, 10000) for c in paper[\"Title\"])) for paper in batch]\n", + " batch_metadata = [dict(url=paper[\"PaperURL\"],\n", + " abstract=paper['Abstract'])\n", + " for paper in batch]\n", + "\n", + " # generate embeddings\n", + " batch_embeddings = embedding_model.encode(batch_titles)\n", + "\n", + " # upsert to chromadb\n", + " collection.upsert(\n", + " ids=batch_ids,\n", + " metadatas=batch_metadata,\n", + " documents=batch_titles,\n", + " embeddings=batch_embeddings.tolist(),\n", + " )" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "1xrbURsMzWvF" + }, + "source": [ + "Now we can test the retriever:" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "YoDlxtZhzWvF", + "outputId": "c258fabb-452d-4740-9073-3d3cf7791bb7" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[['LLMs for Software Engineering', 'Communicative Agents for Software Development']]\n" + ] + } + ], + "source": [ + "collection = client.get_or_create_collection(\n", + " name=f\"ml-papers-nov-2023\",\n", + " embedding_function=embed_fn\n", + ")\n", + "\n", + "retriever_results = collection.query(\n", + " query_texts=[\"Software Engineering\"],\n", + " n_results=2,\n", + ")\n", + "\n", + "print(retriever_results[\"documents\"])" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "NUHeag1XzWvF" + }, + "source": [ + "Now let's put together our final prompt:" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "x_A0VZ8YzWvF", + "outputId": "2b3074dc-381e-4cc0-9ee8-ea90673e0da9" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Model Suggestions:\n", + "\n", + "1. S3Eval: A Comprehensive Evaluation Suite for Large Language Models\n", + "2. Synthetic and Scalable Evaluation for Large Language Models\n", + "3. Systematic Evaluation of Large Language Models with S3Eval\n", + "4. S3Eval: A Synthetic and Scalable Approach to Language Model Evaluation\n", + "5. S3Eval: A Synthetic and Scalable Evaluation Suite for Large Language Models\n", + "\n", + "\n", + "\n", + "Prompt Template:\n", + "[INST]\n", + "\n", + "Your main task is to generate 5 SUGGESTED_TITLES based for the PAPER_TITLE\n", + "\n", + "You should mimic a similar style and length as SHORT_TITLES but PLEASE DO NOT include titles from SHORT_TITLES in the SUGGESTED_TITLES, only generate versions of the PAPER_TILE.\n", + "\n", + "PAPER_TITLE: S3Eval: A Synthetic, Scalable, Systematic Evaluation Suite for Large Language Models\n", + "\n", + "SHORT_TITLES: Pythia: A Suite for Analyzing Large Language Models Across Training and Scaling\n", + "ChemCrow: Augmenting large-language models with chemistry tools\n", + "A Survey of Large Language Models\n", + "LLaMA: Open and Efficient Foundation Language Models\n", + "SparseGPT: Massive Language Models Can Be Accurately Pruned In One-Shot\n", + "REPLUG: Retrieval-Augmented Black-Box Language Models\n", + "LLaMA-Adapter: Efficient Fine-tuning of Language Models with Zero-init Attention\n", + "Auditing large language models: a three-layered approach\n", + "Fine-Tuning Language Models with Just Forward Passes\n", + "DERA: Enhancing Large Language Model Completions with Dialog-Enabled Resolving Agents\n", + "\n", + "SUGGESTED_TITLES:\n", + "\n", + "[/INST]\n", + "\n" + ] + } + ], + "source": [ + "# user query\n", + "user_query = \"S3Eval: A Synthetic, Scalable, Systematic Evaluation Suite for Large Language Models\"\n", + "\n", + "# query for user query\n", + "results = collection.query(\n", + " query_texts=[user_query],\n", + " n_results=10,\n", + ")\n", + "\n", + "# concatenate titles into a single string\n", + "short_titles = '\\n'.join(results['documents'][0])\n", + "\n", + "prompt_template = f'''[INST]\n", + "\n", + "Your main task is to generate 5 SUGGESTED_TITLES based for the PAPER_TITLE\n", + "\n", + "You should mimic a similar style and length as SHORT_TITLES but PLEASE DO NOT include titles from SHORT_TITLES in the SUGGESTED_TITLES, only generate versions of the PAPER_TILE.\n", + "\n", + "PAPER_TITLE: {user_query}\n", + "\n", + "SHORT_TITLES: {short_titles}\n", + "\n", + "SUGGESTED_TITLES:\n", + "\n", + "[/INST]\n", + "'''\n", + "\n", + "responses = get_completion(prompt_template, model=mistral_llm, max_tokens=2000)\n", + "suggested_titles = ''.join([str(r) for r in responses])\n", + "\n", + "# Print the suggestions.\n", + "print(\"Model Suggestions:\")\n", + "print(suggested_titles)\n", + "print(\"\\n\\n\\nPrompt Template:\")\n", + "print(prompt_template)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "cSAJcQ6Y2cNt" + }, + "source": [ + "As you can see, the short titles generated by the LLM are somewhat okay. This use case still needs a lot more work and could potentially benefit from finetuning as well. For the purpose of this tutorial, we have provided a simple application of RAG using open-source models from Firework's blazing-fast models.\n", + "\n", + "Try out other open-source models here: https://app.fireworks.ai/models\n", + "\n", + "Read more about the Fireworks APIs here: https://readme.fireworks.ai/reference/createchatcompletion\n" + ] + } + ], + "metadata": { + "colab": { + "provenance": [] + }, + "kernelspec": { + "display_name": "rag", + "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.9.18" + }, + "widgets": { + "application/vnd.jupyter.widget-state+json": { + "0285ca3156854ca09a252540ad6a43ff": { + "model_module": "@jupyter-widgets/controls", + "model_module_version": "1.5.0", + "model_name": "DescriptionStyleModel", + "state": { + "_model_module": "@jupyter-widgets/controls", + "_model_module_version": "1.5.0", + "_model_name": "DescriptionStyleModel", + "_view_count": null, + "_view_module": "@jupyter-widgets/base", + "_view_module_version": "1.2.0", + "_view_name": "StyleView", + "description_width": "" + } + }, + "51ad257305a0438fbed46c613d2d59fb": { + "model_module": "@jupyter-widgets/base", + "model_module_version": "1.2.0", + "model_name": "LayoutModel", + "state": { + "_model_module": "@jupyter-widgets/base", + "_model_module_version": "1.2.0", + "_model_name": "LayoutModel", + "_view_count": null, + "_view_module": "@jupyter-widgets/base", + "_view_module_version": "1.2.0", + "_view_name": "LayoutView", + "align_content": null, + "align_items": null, + "align_self": null, + "border": null, + "bottom": null, + "display": null, + "flex": null, + "flex_flow": null, + "grid_area": null, + "grid_auto_columns": null, + "grid_auto_flow": null, + "grid_auto_rows": null, + "grid_column": null, + "grid_gap": null, + "grid_row": null, + "grid_template_areas": null, + "grid_template_columns": null, + "grid_template_rows": null, + "height": null, + "justify_content": null, + "justify_items": null, + "left": null, + "margin": null, + "max_height": null, + "max_width": null, + "min_height": null, + "min_width": null, + "object_fit": null, + "object_position": null, + "order": null, + "overflow": null, + "overflow_x": null, + "overflow_y": null, + "padding": null, + "right": null, + "top": null, + "visibility": null, + "width": null + } + }, + "54c8ecbae313483e82879016cc49bd25": { + "model_module": "@jupyter-widgets/controls", + "model_module_version": "1.5.0", + "model_name": "DescriptionStyleModel", + "state": { + "_model_module": "@jupyter-widgets/controls", + "_model_module_version": "1.5.0", + "_model_name": "DescriptionStyleModel", + "_view_count": null, + "_view_module": "@jupyter-widgets/base", + "_view_module_version": "1.2.0", + "_view_name": "StyleView", + "description_width": "" + } + }, + "868c943d8a82435a8c3df6f32a3cc433": { + "model_module": "@jupyter-widgets/controls", + "model_module_version": "1.5.0", + "model_name": "HTMLModel", + "state": { + "_dom_classes": [], + "_model_module": "@jupyter-widgets/controls", + "_model_module_version": "1.5.0", + "_model_name": "HTMLModel", + "_view_count": null, + "_view_module": "@jupyter-widgets/controls", + "_view_module_version": "1.5.0", + "_view_name": "HTMLView", + "description": "", + "description_tooltip": null, + "layout": "IPY_MODEL_b9545ef27bf24e0c86d713ae8a3c0d2c", + "placeholder": "​", + "style": "IPY_MODEL_0285ca3156854ca09a252540ad6a43ff", + "value": " 9/9 [00:04<00:00, 2.22it/s]" + } + }, + "8e06dc7bf2f94d63a69b651f594ecf74": { + "model_module": "@jupyter-widgets/base", + "model_module_version": "1.2.0", + "model_name": "LayoutModel", + "state": { + "_model_module": "@jupyter-widgets/base", + "_model_module_version": "1.2.0", + "_model_name": "LayoutModel", + "_view_count": null, + "_view_module": "@jupyter-widgets/base", + "_view_module_version": "1.2.0", + "_view_name": "LayoutView", + "align_content": null, + "align_items": null, + "align_self": null, + "border": null, + "bottom": null, + "display": null, + "flex": null, + "flex_flow": null, + "grid_area": null, + "grid_auto_columns": null, + "grid_auto_flow": null, + "grid_auto_rows": null, + "grid_column": null, + "grid_gap": null, + "grid_row": null, + "grid_template_areas": null, + "grid_template_columns": null, + "grid_template_rows": null, + "height": null, + "justify_content": null, + "justify_items": null, + "left": null, + "margin": null, + "max_height": null, + "max_width": null, + "min_height": null, + "min_width": null, + "object_fit": null, + "object_position": null, + "order": null, + "overflow": null, + "overflow_x": null, + "overflow_y": null, + "padding": null, + "right": null, + "top": null, + "visibility": null, + "width": null + } + }, + "b568c3c04efb49acb00e44aadc247735": { + "model_module": "@jupyter-widgets/controls", + "model_module_version": "1.5.0", + "model_name": "FloatProgressModel", + "state": { + "_dom_classes": [], + "_model_module": "@jupyter-widgets/controls", + "_model_module_version": "1.5.0", + "_model_name": "FloatProgressModel", + "_view_count": null, + "_view_module": "@jupyter-widgets/controls", + "_view_module_version": "1.5.0", + "_view_name": "ProgressView", + "bar_style": "success", + "description": "", + "description_tooltip": null, + "layout": "IPY_MODEL_8e06dc7bf2f94d63a69b651f594ecf74", + "max": 9, + "min": 0, + "orientation": "horizontal", + "style": "IPY_MODEL_bacb31eccd5c4a2dba61503a658333f2", + "value": 9 + } + }, + "b9545ef27bf24e0c86d713ae8a3c0d2c": { + "model_module": "@jupyter-widgets/base", + "model_module_version": "1.2.0", + "model_name": "LayoutModel", + "state": { + "_model_module": "@jupyter-widgets/base", + "_model_module_version": "1.2.0", + "_model_name": "LayoutModel", + "_view_count": null, + "_view_module": "@jupyter-widgets/base", + "_view_module_version": "1.2.0", + "_view_name": "LayoutView", + "align_content": null, + "align_items": null, + "align_self": null, + "border": null, + "bottom": null, + "display": null, + "flex": null, + "flex_flow": null, + "grid_area": null, + "grid_auto_columns": null, + "grid_auto_flow": null, + "grid_auto_rows": null, + "grid_column": null, + "grid_gap": null, + "grid_row": null, + "grid_template_areas": null, + "grid_template_columns": null, + "grid_template_rows": null, + "height": null, + "justify_content": null, + "justify_items": null, + "left": null, + "margin": null, + "max_height": null, + "max_width": null, + "min_height": null, + "min_width": null, + "object_fit": null, + "object_position": null, + "order": null, + "overflow": null, + "overflow_x": null, + "overflow_y": null, + "padding": null, + "right": null, + "top": null, + "visibility": null, + "width": null + } + }, + "b9da413d4f84436ab5dc0fd10d237b0a": { + "model_module": "@jupyter-widgets/controls", + "model_module_version": "1.5.0", + "model_name": "HBoxModel", + "state": { + "_dom_classes": [], + "_model_module": "@jupyter-widgets/controls", + "_model_module_version": "1.5.0", + "_model_name": "HBoxModel", + "_view_count": null, + "_view_module": "@jupyter-widgets/controls", + "_view_module_version": "1.5.0", + "_view_name": "HBoxView", + "box_style": "", + "children": [ + "IPY_MODEL_c269f70baec246288519dbb2517c05c0", + "IPY_MODEL_b568c3c04efb49acb00e44aadc247735", + "IPY_MODEL_868c943d8a82435a8c3df6f32a3cc433" + ], + "layout": "IPY_MODEL_51ad257305a0438fbed46c613d2d59fb" + } + }, + "bacb31eccd5c4a2dba61503a658333f2": { + "model_module": "@jupyter-widgets/controls", + "model_module_version": "1.5.0", + "model_name": "ProgressStyleModel", + "state": { + "_model_module": "@jupyter-widgets/controls", + "_model_module_version": "1.5.0", + "_model_name": "ProgressStyleModel", + "_view_count": null, + "_view_module": "@jupyter-widgets/base", + "_view_module_version": "1.2.0", + "_view_name": "StyleView", + "bar_color": null, + "description_width": "" + } + }, + "c269f70baec246288519dbb2517c05c0": { + "model_module": "@jupyter-widgets/controls", + "model_module_version": "1.5.0", + "model_name": "HTMLModel", + "state": { + "_dom_classes": [], + "_model_module": "@jupyter-widgets/controls", + "_model_module_version": "1.5.0", + "_model_name": "HTMLModel", + "_view_count": null, + "_view_module": "@jupyter-widgets/controls", + "_view_module_version": "1.5.0", + "_view_name": "HTMLView", + "description": "", + "description_tooltip": null, + "layout": "IPY_MODEL_cfbba792e6054178b504c2e2bbc23b2f", + "placeholder": "​", + "style": "IPY_MODEL_54c8ecbae313483e82879016cc49bd25", + "value": "100%" + } + }, + "cfbba792e6054178b504c2e2bbc23b2f": { + "model_module": "@jupyter-widgets/base", + "model_module_version": "1.2.0", + "model_name": "LayoutModel", + "state": { + "_model_module": "@jupyter-widgets/base", + "_model_module_version": "1.2.0", + "_model_name": "LayoutModel", + "_view_count": null, + "_view_module": "@jupyter-widgets/base", + "_view_module_version": "1.2.0", + "_view_name": "LayoutView", + "align_content": null, + "align_items": null, + "align_self": null, + "border": null, + "bottom": null, + "display": null, + "flex": null, + "flex_flow": null, + "grid_area": null, + "grid_auto_columns": null, + "grid_auto_flow": null, + "grid_auto_rows": null, + "grid_column": null, + "grid_gap": null, + "grid_row": null, + "grid_template_areas": null, + "grid_template_columns": null, + "grid_template_rows": null, + "height": null, + "justify_content": null, + "justify_items": null, + "left": null, + "margin": null, + "max_height": null, + "max_width": null, + "min_height": null, + "min_width": null, + "object_fit": null, + "object_position": null, + "order": null, + "overflow": null, + "overflow_x": null, + "overflow_y": null, + "padding": null, + "right": null, + "top": null, + "visibility": null, + "width": null + } + } + } + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/pages/applications/function_calling.en.mdx b/pages/applications/function_calling.en.mdx index cbb9801..dd44248 100644 --- a/pages/applications/function_calling.en.mdx +++ b/pages/applications/function_calling.en.mdx @@ -1,5 +1,8 @@ # Function Calling with LLMs +import {Cards, Card} from 'nextra-theme-docs' +import {CodeIcon} from 'components/icons' + ## Getting Started with Function Calling Function calling is the ability to reliably connect LLMs to external tools to enable effective tool usage and interaction with external APIs. @@ -99,14 +102,34 @@ In particular, the `arguments` object contains the important arguments extracted You can then choose to call an external weather API for the actual weather. Once you have the weather information available you can pass it back to the model to summarize a final response given the original user question. -Here is a [notebook](https://github.com/dair-ai/Prompt-Engineering-Guide/blob/main/notebooks/pe-function-calling.ipynb) with a simple example that demonstrates how to use function calling with the OpenAI APIs. +## Notebooks + +Here is a notebook with a simple example that demonstrates how to use function calling with the OpenAI APIs: + + + } + title="Function Calling with OpenAI APIs" + href="https://github.com/dair-ai/Prompt-Engineering-Guide/blob/main/notebooks/pe-function-calling.ipynb" + /> + ## Function Calling with Open-Source LLMs -More notes on function calling with open-source LLMs coming soon... +More notes on function calling with open-source LLMs coming soon. ## Function Calling Use Cases -More function calling use cases coming soon... +Below is a list of use cases that can benefit from the function calling capability of LLMs: + +- **Conversational Agents**: Function calling can be used to create complex conversational agents or chatbots that answer complex questions by calling external APIs or external knowledge base and providing more relevant and useful responses. + +- **Natural Language Understanding**: It can convert natural language into structured JSON data, extract structured data from text, and perform tasks like named entity recognition, sentiment analysis, and keyword extraction. + +- **Math Problem Solving**: Function calling can be used to define custom functions to solve complex mathematical problems that require multiple steps and different types of advanced calculations. + +- **API Integration**: It can be used to effectively integrate LLMs with external APIs to fetch data or perform actions based on the input. This could be helpful to build either a QA system or creative assistant. In general, function calling can convert natural language into valid API calls. + +- **Information Extraction**: Function calling be effectively used to extract specific information from a given input, such as retrieving relevant news stories or references from an article. ## References @@ -116,4 +139,5 @@ More function calling use cases coming soon... - [Interacting with APIs](https://python.langchain.com/docs/use_cases/apis) - [OpenAI's Function Calling](https://platform.openai.com/docs/guides/function-calling) - [How to call functions with chat models](https://cookbook.openai.com/examples/how_to_call_functions_with_chat_models) -- [Pushing ChatGPT's Structured Data Support To Its Limits](https://minimaxir.com/2023/12/chatgpt-structured-data/) \ No newline at end of file +- [Pushing ChatGPT's Structured Data Support To Its Limits](https://minimaxir.com/2023/12/chatgpt-structured-data/) +- [Math Problem Solving with Function Calling](https://github.com/svpino/openai-function-calling/blob/main/sample.ipynb) \ No newline at end of file diff --git a/pages/introduction/examples.en.mdx b/pages/introduction/examples.en.mdx index 79c2076..d3b6d24 100644 --- a/pages/introduction/examples.en.mdx +++ b/pages/introduction/examples.en.mdx @@ -1,5 +1,8 @@ # Examples of Prompts +import {Cards, Card} from 'nextra-theme-docs' +import {CodeIcon} from 'components/icons' + The previous section introduced a basic example of how to prompt LLMs. This section will provide more examples of how to use prompts to achieve different tasks and introduce key concepts along the way. Often, the best way to learn concepts is by going through examples. The few examples below illustrate how you can use well-crafted prompts to perform different types of tasks. @@ -288,3 +291,16 @@ Much better, right? By the way, I tried this a couple of times and the system so We will continue to include more examples of common applications in this section of the guide. In the upcoming section, we will cover even more advanced prompt engineering concepts and techniques for improving performance on all these and more difficult tasks. + +## Notebook + +If you want to practice with the prompts above using Python, we have prepared a notebook to test some of the prompts using the OpenAI models. + + + } + title="Getting Started with Prompt Engineering" + href="https://github.com/dair-ai/Prompt-Engineering-Guide/blob/main/notebooks/pe-lecture.ipynb" + /> + + diff --git a/pages/models/chatgpt.en.mdx b/pages/models/chatgpt.en.mdx index aa47104..66c96af 100644 --- a/pages/models/chatgpt.en.mdx +++ b/pages/models/chatgpt.en.mdx @@ -4,6 +4,8 @@ import { Callout, FileTree } from 'nextra-theme-docs' import {Screenshot} from 'components/screenshot' import CHATGPT1 from '../../img/chatgpt-1.png' import CHATGPTCLASSIC from '../../img/chatgpt-classic.png' +import {Cards, Card} from 'nextra-theme-docs' +import {CodeIcon} from 'components/icons' In this section, we cover the latest prompt engineering techniques for ChatGPT, including tips, applications, limitations, papers, and additional reading materials. @@ -138,6 +140,23 @@ According to the official OpenAI docs, snapshots of the `gpt-3.5-turbo` model wi The current recommendation for `gpt-3.5-turbo-0301` is to add instructions in the `user` message as opposed to the available `system` message. + +## Notebooks +Here is a notebook to learn more about how to make calls to the ChatGPT APIs using the official `openai` library: + + + } + title="Introduction to The ChatGPT APIs" + href="https://github.com/dair-ai/Prompt-Engineering-Guide/blob/main/notebooks/pe-chatgpt-intro.ipynb" + /> + } + title="ChatGPT with LangChain" + href="https://github.com/dair-ai/Prompt-Engineering-Guide/blob/main/notebooks/pe-chatgpt-langchain.ipynb" + /> + + --- ## References diff --git a/pages/techniques/rag.en.mdx b/pages/techniques/rag.en.mdx index 95fce46..b109942 100644 --- a/pages/techniques/rag.en.mdx +++ b/pages/techniques/rag.en.mdx @@ -1,5 +1,8 @@ # Retrieval Augmented Generation (RAG) +import {Cards, Card} from 'nextra-theme-docs' +import {TerminalIcon} from 'components/icons' +import {CodeIcon} from 'components/icons' import {Screenshot} from 'components/screenshot' import RAG from '../../img/rag.png' @@ -22,4 +25,19 @@ This shows the potential of RAG as a viable option for enhancing outputs of lang More recently, these retriever-based approaches have become more popular and are combined with popular LLMs like ChatGPT to improve capabilities and factual consistency. -You can find a [simple example of how to use retrievers and LLMs for question answering with sources](https://python.langchain.com/docs/use_cases/question_answering/how_to/vector_db_qa) from the LangChain documentation. +## RAG Use Case: Generating Friendly ML Paper Titles + +Below, we have prepared a notebook tutorial showcasing the use of open-source LLMs to build a RAG system for generating short and concise machine learning paper titles: + + + } + title="Getting Started with RAG" + href="https://github.com/dair-ai/Prompt-Engineering-Guide/blob/main/notebooks/pe-rag.ipynb" + /> + + +## References + +- [Retrieval-Augmented Generation for Large Language Models: A Survey](https://arxiv.org/abs/2312.10997) (Dec 2023) +- [Retrieval Augmented Generation: Streamlining the creation of intelligent natural language processing models](https://ai.meta.com/blog/retrieval-augmented-generation-streamlining-the-creation-of-intelligent-natural-language-processing-models/) (Sep 2020)