├── .DS_Store ├── .ipynb_checkpoints └── Vertical Chat-checkpoint.ipynb ├── 1-Introduction to LLMs with OpenAI ├── 1_1-First_Chatbot_OpenAI.ipynb ├── 1_1-First_Chatbot_OpenAI_Gradio.ipynb ├── 1_2-Easy_NL2SQL.ipynb ├── 1_2-Easy_NL2SQL_Gradio.ipynb ├── 1_2b-Easy_NL2SQL.ipynb ├── 1_3-Intro_Prompt_Engineering.ipynb ├── Prompt_Engineering_OpenAI.ipynb ├── Vertical Chat.ipynb └── readme.md ├── 2-Vector Databases with LLMs ├── .ipynb_checkpoints │ └── how-to-use-a-embedding-database-with-a-llm-from-hf-checkpoint.ipynb ├── 2_1_Vector_Databases_LLMs.ipynb ├── 2_2-ChromaDB Sever mode.ipynb ├── 2_3-ChromaDB Client.ipynb ├── how-to-use-a-embedding-database-with-a-llm-from-hf.ipynb ├── readme.md └── semantic_cache_chroma_vector_database.ipynb ├── 3-LangChain ├── .DS_Store ├── .ipynb_checkpoints │ └── HF_LLAMA2_LangChain_Moderation_System-checkpoint.ipynb ├── 3_1_RAG_langchain.ipynb ├── 3_2_GPT_Moderation_System.ipynb ├── 3_2_LLAMA2_Moderation_Chat.ipynb ├── 3_2_OpenAI_Moderation_Chat.ipynb ├── 3_3_Data_Analyst_Agent.ipynb ├── 3_4_Medical_Assistant_Agent.ipynb ├── 3_4b_Medical_Assistant_Agent_Gradio.ipynb ├── HF_LLAMA2_LangChain_Moderation_System.ipynb ├── HF_gpt_j_6b_Moderation_System.ipynb ├── LangChain_Agent_create_Data_Scientist_Assistant.ipynb ├── LangChain_OpenAI_Moderation_System.ipynb ├── ask-your-documents-with-langchain-vectordb-hf.ipynb ├── langchain_retrieval_agent.ipynb └── readme.md ├── 4-Evaluating LLMs ├── .DS_Store ├── .ipynb_checkpoints │ └── rouge-evaluation-untrained-vs-trained-llm-checkpoint.ipynb ├── 4_1_bleu_evaluation.ipynb ├── 4_1_rouge_evaluations.ipynb ├── 4_2_Evaluating_summaries_embeddings.ipynb ├── 4_2_tracing_medical_agent.ipynb ├── 4_3_evaluating_rag_giskard.ipynb ├── 4_4_lm-evaluation-harness.ipynb ├── LangSmithSumarizations.ipynb ├── bleu_evaluation.ipynb ├── evaluating_rag_giskard.ipynb ├── langsmith_Medical_Assistant_Agent.ipynb ├── readme.md └── rouge-evaluation-untrained-vs-trained-llm.ipynb ├── 5-Fine Tuning ├── .DS_Store ├── .ipynb_checkpoints │ └── LoRA_Tuning_PEFT-checkpoint.ipynb ├── 5_2_LoRA_Tuning.ipynb ├── 5_3_QLoRA_Tuning.ipynb ├── 5_4_Prompt_Tuning.ipynb ├── LoRA_Tuning_PEFT.ipynb ├── Prompt_Tuning_PEFT-V2.ipynb ├── Prompt_Tuning_PEFT.ipynb ├── QLoRA_Tuning_PEFT.ipynb └── readme.md ├── 6-PRUNING ├── .ipynb_checkpoints │ └── 6_5_pruning_depth_st_llama3.2-1b_OK-checkpoint.ipynb ├── 6_1_pruning_structured_l1_diltilgpt2.ipynb ├── 6_2_pruning_structured_llama3.2-1b_KO.ipynb ├── 6_3_pruning_structured_llama3.2-1b_OK.ipynb ├── 6_3b_pruning_llama_optipfair.ipynb ├── 6_5_pruning_depth_st_llama3.2-1b_OK.ipynb ├── 6_6_pruning_attention_layers.ipynb ├── 6_6b_Adaptive_Inference_Attention_Pruning.ipynb ├── 6_7_benchmark_inference_pruned_vs_base.ipynb ├── 7_1_knowledge_distillation_Llama.ipynb ├── 8_1_transformer_activations_visualization.ipynb ├── 8_1b_optipfair_activations_visualization.ipynb ├── old_pruning_attention_layers.ipynb └── readme.md ├── Datasets └── summaries_cnn.csv ├── E1-NL2SQL for big Databases ├── Select_hs_Tables.ipynb └── readme.md ├── E2-Transforming Banks With Embeddings └── Readme.md ├── LICENSE.md ├── LLM_course_diagram.jpg ├── P1-NL2SQL ├── .ipynb_checkpoints │ └── 6_4_nl2sql_Ollama-checkpoint.ipynb ├── 6_1_nl2sql_prompt_OpenAI.ipynb ├── 6_2_Azure_NL2SQL_Client.ipynb ├── 6_3_AWS_Bedrock_NL2SQL_Client.ipynb ├── 6_4_nl2sql_Ollama.ipynb ├── NL2SQL_OpenAI_Azure.ipynb ├── nl2sql_prompt_OpenAI.ipynb ├── nl2sql_prompt_SQLCoder.ipynb └── readme.md ├── P2-MHF ├── 7_2_Aligning_DPO_phi3.ipynb ├── Aligning_DPO_open_gemma-2b-it.ipynb ├── Aligning_DPO_phi3.ipynb └── readme.md ├── P3-CustomFinancialLLM └── Readme.md ├── README.md ├── clean_notebooks.py └── img ├── .DS_Store ├── LLM_course_diagram.jpg ├── Large_Language_Models_Projects_Book.jpg ├── Martra_Figure_2-7.jpg ├── Martra_Figure_4_1AE_1.jpg ├── Martra_Figure_4_1AE_2.jpg ├── Martra_Figure_4_1AE_3.jpg ├── Martra_Figure_4_1AE_Final.jpg ├── Martra_Figure_4_2SDL_CompareOpenAI_HF.jpg ├── Martra_Figure_4_2SDL_CompareTestst.jpg ├── Martra_Figure_4_2SDL_Dataset.jpg ├── Martra_Figure_4_2SDL_Tests.jpg ├── Martra_Figure_4_Retriever_1.jpg ├── Martra_Figure_4_Retriever_2.jpg ├── Martra_Figure_5_Prompt_Tuning.jpg ├── Martra_Figure_5_Quantization_Graph.jpg ├── Martra_Figure_6-13.jpg ├── Martra_Figure_6-18.jpg ├── colab.svg ├── depth_rpunedvsbase.png ├── lambada_BooQ_Accuracy.png ├── langsmith.png ├── langsmith_API_KEY.jpg └── semantic_cache.jpg /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peremartra/Large-Language-Model-Notebooks-Course/4c655e82cf149bfd302a0e8f32366bda4595eec4/.DS_Store -------------------------------------------------------------------------------- /.ipynb_checkpoints/Vertical Chat-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "c23b4dd1", 6 | "metadata": {}, 7 | "source": [ 8 | "# Vertical Chat\n", 9 | "A sample how to build a chat for small businees using: \n", 10 | "\n", 11 | "* GPT 35\n", 12 | "* Panel\n", 13 | "* OpenAI \n", 14 | "\n", 15 | "Create a .py file to be executed with Streamlt \n", 16 | "I'm using jupyter just to keep it simple\n", 17 | "feel free to use you favourite IDE like Pycharm or any other. \n", 18 | "\n", 19 | "*Note: This is a sample notebook, if the list of items is too large you can reach the max tokens in the OpenAI API. " 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": null, 25 | "id": "a1d00720", 26 | "metadata": {}, 27 | "outputs": [], 28 | "source": [ 29 | "!pip install openai\n", 30 | "!pip install panel" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": null, 36 | "id": "a03f026a", 37 | "metadata": {}, 38 | "outputs": [], 39 | "source": [ 40 | "import openai \n", 41 | "import panel as pn\n", 42 | "from mykeys import openai_api_key\n", 43 | "openai.api_key=openai_api_key" 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": null, 49 | "id": "77eac86d", 50 | "metadata": {}, 51 | "outputs": [], 52 | "source": [ 53 | "def continue_conversation(messages, temperature=0):\n", 54 | " response = openai.ChatCompletion.create(\n", 55 | " model=\"gpt-3.5-turbo\",\n", 56 | " messages=messages,\n", 57 | " temperature=temperature, \n", 58 | " )\n", 59 | " #print(str(response.choices[0].message[\"content\"]))\n", 60 | " return response.choices[0].message[\"content\"]" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": null, 66 | "id": "51bec475", 67 | "metadata": {}, 68 | "outputs": [], 69 | "source": [ 70 | "def add_prompts_conversation(_):\n", 71 | " #Get the value introduced by the user\n", 72 | " prompt = client_prompt.value_input\n", 73 | " client_prompt.value = ''\n", 74 | " \n", 75 | " #Append to the context the User promnopt. \n", 76 | " context.append({'role':'user', 'content':f\"{prompt}\"})\n", 77 | " \n", 78 | " #Get the response. \n", 79 | " response = continue_conversation(context) \n", 80 | " \n", 81 | " #Add the response to the context. \n", 82 | " context.append({'role':'assistant', 'content':f\"{response}\"})\n", 83 | " \n", 84 | " #Undate the panels to shjow the conversation. \n", 85 | " panels.append(\n", 86 | " pn.Row('User:', pn.pane.Markdown(prompt, width=600)))\n", 87 | " panels.append(\n", 88 | " pn.Row('Assistant:', pn.pane.Markdown(response, width=600, style={'background-color': '#F6F6F6'})))\n", 89 | " \n", 90 | " return pn.Column(*panels)" 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": null, 96 | "id": "922f8d24", 97 | "metadata": {}, 98 | "outputs": [], 99 | "source": [ 100 | "\n", 101 | "\n", 102 | "context = [ {'role':'system', 'content':\"\"\"\n", 103 | "Act as an OrderBot, you work collecting orders in a delivery only fast food restaurant called \n", 104 | "My Dear Frankfurt. \\\n", 105 | "First welcome the customer, in a very friedly way, then collects the order. \\\n", 106 | "You wait to collect the entire order, beverages included \\\n", 107 | "then summarize it and check for a final \\\n", 108 | "time if everithing is ok or the customer wants to add anything else. \\\n", 109 | "Finally you collect the payment.\\\n", 110 | "Make sure to clarify all options, extras and sizes to uniquely \\\n", 111 | "identify the item from the menu.\\\n", 112 | "You respond in a short, very friendly style. \\\n", 113 | "The menu includes \\\n", 114 | "burguer 12.95, 10.00, 7.00 \\\n", 115 | "frankfurt 10.95, 9.25, 6.50 \\\n", 116 | "sandwich 11.95, 9.75, 6.75 \\\n", 117 | "fries 4.50, 3.50 \\\n", 118 | "salad 7.25 \\\n", 119 | "Toppings: \\\n", 120 | "extra cheese 2.00, \\\n", 121 | "mushrooms 1.50 \\\n", 122 | "martra sausage 3.00 \\\n", 123 | "canadian bacon 3.50 \\\n", 124 | "romesco sauce 1.50 \\\n", 125 | "peppers 1.00 \\\n", 126 | "Drinks: \\\n", 127 | "coke 3.00, 2.00, 1.00 \\\n", 128 | "sprite 3.00, 2.00, 1.00 \\\n", 129 | "vichy catalan 5.00 \\\n", 130 | "\"\"\"} ] \n", 131 | "\n", 132 | "#Creamos el panel. \n", 133 | "pn.extension()\n", 134 | "\n", 135 | "panels = [] \n", 136 | "\n", 137 | "client_prompt = pn.widgets.TextInput(value=\"Hi\", placeholder='Enter text here…')\n", 138 | "button_conversation = pn.widgets.Button(name=\"talk\")\n", 139 | "\n", 140 | "interactive_conversation = pn.bind(add_prompts_conversation, button_conversation)\n", 141 | "\n", 142 | "dashboard = pn.Column(\n", 143 | " client_prompt,\n", 144 | " pn.Row(button_conversation),\n", 145 | " pn.panel(interactive_conversation, loading_indicator=True, height=300),\n", 146 | ")\n", 147 | "\n", 148 | "dashboard" 149 | ] 150 | }, 151 | { 152 | "cell_type": "code", 153 | "execution_count": null, 154 | "id": "88db4691", 155 | "metadata": {}, 156 | "outputs": [], 157 | "source": [] 158 | } 159 | ], 160 | "metadata": { 161 | "kernelspec": { 162 | "display_name": "Python 3 (ipykernel)", 163 | "language": "python", 164 | "name": "python3" 165 | }, 166 | "language_info": { 167 | "codemirror_mode": { 168 | "name": "ipython", 169 | "version": 3 170 | }, 171 | "file_extension": ".py", 172 | "mimetype": "text/x-python", 173 | "name": "python", 174 | "nbconvert_exporter": "python", 175 | "pygments_lexer": "ipython3", 176 | "version": "3.8.13" 177 | } 178 | }, 179 | "nbformat": 4, 180 | "nbformat_minor": 5 181 | } 182 | -------------------------------------------------------------------------------- /1-Introduction to LLMs with OpenAI/1_1-First_Chatbot_OpenAI.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "51411bb4", 6 | "metadata": { 7 | "colab_type": "text", 8 | "id": "view-in-github" 9 | }, 10 | "source": [ 11 | "\"Open" 12 | ] 13 | }, 14 | { 15 | "cell_type": "markdown", 16 | "id": "zwzWskLgXgj5", 17 | "metadata": { 18 | "id": "zwzWskLgXgj5" 19 | }, 20 | "source": [ 21 | "
\n", 22 | "

Large Language Models Projects

\n", 23 | "

Apply and Implement Strategies for Large Language Models

\n", 24 | "

1.1-Create your first Chatbot With OpenAI

\n", 25 | "
\n", 26 | "\n", 27 | "by [Pere Martra](https://www.linkedin.com/in/pere-martra/)\n", 28 | "___________\n", 29 | "\n", 30 | "Models: gpt-3.5-turbo / gpt-4o-mini\n", 31 | "\n", 32 | "Colab Environment: CPU\n", 33 | "\n", 34 | "Keys:\n", 35 | "* OpenAI roles.\n", 36 | "* Memory in conversations.\n", 37 | "\n", 38 | "Related article: [Create Your First Chatbot Using GPT gpt-3.5-turbo / gpt-4o-mini, OpenAI, Python and Panel.](https://medium.com/towards-artificial-intelligence/create-your-first-chatbot-using-gpt-3-5-openai-python-and-panel-7ec180b9d7f2)\n", 39 | "___________________________\n", 40 | "\n", 41 | "This is the unofficial repository for the book:\n", 42 | " Large Language Models: Apply and Implement Strategies for Large Language Models (Apress).\n", 43 | " The book is based on the content of this repository, but the notebooks are being updated, and I am incorporating new examples and chapters.\n", 44 | " If you are looking for the official repository for the book, with the original notebooks, you should visit the\n", 45 | " Apress repository, where you can find all the notebooks in their original format as they appear in the book." 46 | ] 47 | }, 48 | { 49 | "cell_type": "markdown", 50 | "id": "c23b4dd1", 51 | "metadata": { 52 | "id": "c23b4dd1" 53 | }, 54 | "source": [ 55 | "# Vertical Chat\n", 56 | "How to build a chat for small businees using:\n", 57 | "\n", 58 | "* GPT 3.5 / GPT-4o mini\n", 59 | "* Panel\n", 60 | "* OpenAI\n", 61 | "\n", 62 | "\n", 63 | "This is just a simple sample to start understanding how the OpenAI API works, and how to create Prompts. It Is really far from beign a complete solution, but we are going to introduce some interesting points:\n", 64 | "\n", 65 | "* The roles in a conversation.\n", 66 | "* How is the conversations’ memory preserved?\n" 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": null, 72 | "id": "a1d00720", 73 | "metadata": { 74 | "colab": { 75 | "base_uri": "https://localhost:8080/" 76 | }, 77 | "id": "a1d00720", 78 | "outputId": "10c27673-9123-49b5-d4a6-f2761eda6f00" 79 | }, 80 | "outputs": [], 81 | "source": [ 82 | "#First install the necesary libraries\n", 83 | "!pip install -q openai==1.1.1\n", 84 | "!pip install -q panel" 85 | ] 86 | }, 87 | { 88 | "cell_type": "code", 89 | "execution_count": null, 90 | "id": "a03f026a", 91 | "metadata": { 92 | "id": "a03f026a" 93 | }, 94 | "outputs": [], 95 | "source": [ 96 | "#if you need a API Key from OpenAI\n", 97 | "#https://platform.openai.com/account/api-keys\n", 98 | "\n", 99 | "import openai\n", 100 | "import panel as pn\n", 101 | "openai.api_key=\"your-openai-key\"\n", 102 | "#model = \"gpt-3.5-turbo\"\n", 103 | "model = \"gpt-4o-mini\"" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": null, 109 | "id": "77eac86d", 110 | "metadata": { 111 | "id": "77eac86d" 112 | }, 113 | "outputs": [], 114 | "source": [ 115 | "#This function will receive the different messages in the conversation,\n", 116 | "#and call OpenAI passing the full conversartion.\n", 117 | "def continue_conversation(messages, temperature=0):\n", 118 | " response = openai.chat.completions.create(\n", 119 | " model=model,\n", 120 | " messages=messages,\n", 121 | " temperature=temperature,\n", 122 | " )\n", 123 | " return response.choices[0].message.content" 124 | ] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "execution_count": null, 129 | "id": "51bec475", 130 | "metadata": { 131 | "id": "51bec475" 132 | }, 133 | "outputs": [], 134 | "source": [ 135 | "def add_prompts_conversation(_):\n", 136 | " #Get the value introduced by the user\n", 137 | " prompt = client_prompt.value_input\n", 138 | " client_prompt.value = ''\n", 139 | "\n", 140 | " #Append to the context the User promnopt.\n", 141 | " context.append({'role':'user', 'content':f\"{prompt}\"})\n", 142 | "\n", 143 | " #Get the response.\n", 144 | " response = continue_conversation(context)\n", 145 | "\n", 146 | " #Add the response to the context.\n", 147 | " context.append({'role':'assistant', 'content':f\"{response}\"})\n", 148 | "\n", 149 | " #Update the panels to show the conversation.\n", 150 | " panels.append(\n", 151 | " pn.Row('User:', pn.pane.Markdown(prompt, width=600)))\n", 152 | " panels.append(\n", 153 | " pn.Row('Assistant:', pn.pane.Markdown(response, width=600)))\n", 154 | "\n", 155 | " return pn.Column(*panels)" 156 | ] 157 | }, 158 | { 159 | "cell_type": "code", 160 | "execution_count": null, 161 | "id": "922f8d24", 162 | "metadata": { 163 | "colab": { 164 | "base_uri": "https://localhost:8080/", 165 | "height": 520 166 | }, 167 | "id": "922f8d24", 168 | "outputId": "ac2023e3-5bfd-4160-8274-9ffa46d59e60" 169 | }, 170 | "outputs": [], 171 | "source": [ 172 | "#Creating the system part of the prompt\n", 173 | "#Read and understand it.\n", 174 | "\n", 175 | "context = [ {'role':'system', 'content':\"\"\"\n", 176 | "You work collecting orders in a delivery IceCream shop called\n", 177 | "I'm freezed.\n", 178 | "\n", 179 | "First welcome the customer, in a very friedly way, then collects the order.\n", 180 | "\n", 181 | "Your instuctions are:\n", 182 | "-Collect the entire order, only from options in our menu, toppings included.\n", 183 | "-Summarize it\n", 184 | "-check for a final time if everithing is ok or the customer wants to add anything else.\n", 185 | "-collect the payment, be sure to include topings and the size of the ice cream.\n", 186 | "-Make sure to clarify all options, extras and sizes to uniquely\n", 187 | "identify the item from the menu.\n", 188 | "-Your answer should be short in a very friendly style.\n", 189 | "\n", 190 | "Our Menu:\n", 191 | "The IceCream menu includes only the flavors:\n", 192 | "-Vainilla.\n", 193 | "-Chocolate.\n", 194 | "-Lemon.\n", 195 | "-Strawberry.\n", 196 | "-Coffe.\n", 197 | "\n", 198 | "The IceCreams are available in two sizes:\n", 199 | "-Big: 3$\n", 200 | "-Medium: 2$\n", 201 | "\n", 202 | "Toppings:\n", 203 | "-Caramel sausage\n", 204 | "-White chocolate\n", 205 | "-melted peanut butter\n", 206 | "Each topping cost 0.5$\n", 207 | "\n", 208 | "\"\"\"} ]\n", 209 | "\n", 210 | "#Creating the panel.\n", 211 | "pn.extension()\n", 212 | "\n", 213 | "panels = []\n", 214 | "\n", 215 | "client_prompt = pn.widgets.TextInput(value=\"Hi\", placeholder='Enter text here…')\n", 216 | "button_conversation = pn.widgets.Button(name=\"talk\")\n", 217 | "\n", 218 | "interactive_conversation = pn.bind(add_prompts_conversation, button_conversation)\n", 219 | "\n", 220 | "dashboard = pn.Column(\n", 221 | " client_prompt,\n", 222 | " pn.Row(button_conversation),\n", 223 | " pn.panel(interactive_conversation, loading_indicator=True),\n", 224 | ")\n", 225 | "\n", 226 | "#To talk with the chat push the botton: 'talk' after your sentence.\n", 227 | "dashboard" 228 | ] 229 | } 230 | ], 231 | "metadata": { 232 | "colab": { 233 | "include_colab_link": true, 234 | "provenance": [] 235 | }, 236 | "kernelspec": { 237 | "display_name": "Python 3 (ipykernel)", 238 | "language": "python", 239 | "name": "python3" 240 | }, 241 | "language_info": { 242 | "codemirror_mode": { 243 | "name": "ipython", 244 | "version": 3 245 | }, 246 | "file_extension": ".py", 247 | "mimetype": "text/x-python", 248 | "name": "python", 249 | "nbconvert_exporter": "python", 250 | "pygments_lexer": "ipython3", 251 | "version": "3.8.13" 252 | } 253 | }, 254 | "nbformat": 4, 255 | "nbformat_minor": 5 256 | } 257 | -------------------------------------------------------------------------------- /1-Introduction to LLMs with OpenAI/1_1-First_Chatbot_OpenAI_Gradio.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "c6e9d322", 6 | "metadata": { 7 | "colab_type": "text", 8 | "id": "view-in-github" 9 | }, 10 | "source": [ 11 | "\"Open" 12 | ] 13 | }, 14 | { 15 | "cell_type": "markdown", 16 | "id": "zwzWskLgXgj5", 17 | "metadata": { 18 | "id": "zwzWskLgXgj5" 19 | }, 20 | "source": [ 21 | "
\n", 22 | "

Large Language Models Projects

\n", 23 | "

Apply and Implement Strategies for Large Language Models

\n", 24 | "

1.1-Create your first Chatbot With OpenAI and GRADIO

\n", 25 | "
\n", 26 | "\n", 27 | "by [Pere Martra](https://www.linkedin.com/in/pere-martra/)\n", 28 | "___________\n", 29 | "\n", 30 | "Models: gpt-3.5-turbo / gpt-4o-mini\n", 31 | "\n", 32 | "Colab Environment: CPU\n", 33 | "\n", 34 | "Keys:\n", 35 | "* OpenAI roles.\n", 36 | "* Memory in conversations.\n", 37 | "* Gradio\n", 38 | "\n", 39 | "Related article: [Create a simple Chatbot with OpenAI and Gradio.](https://medium.com/ai-in-plain-english/create-a-simple-chatbot-with-openai-and-gradio-202684d18f35?sk=e449515ec7a803ae828418011bbaca52)\n", 40 | "___________________________\n", 41 | "\n", 42 | "This is the unofficial repository for the book:\n", 43 | " Large Language Models: Apply and Implement Strategies for Large Language Models (Apress).\n", 44 | " The book is based on the content of this repository, but the notebooks are being updated, and I am incorporating new examples and chapters.\n", 45 | " If you are looking for the official repository for the book, with the original notebooks, you should visit the\n", 46 | " Apress repository, where you can find all the notebooks in their original format as they appear in the book." 47 | ] 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "id": "c23b4dd1", 52 | "metadata": { 53 | "id": "c23b4dd1" 54 | }, 55 | "source": [ 56 | "# Vertical Chat Gradio\n", 57 | "This is just a simple sample to start understanding how the OpenAI API works, and how to create Prompts. It Is really far from beign a complete solution, but we are going to introduce some interesting points:\n", 58 | "\n", 59 | "* The roles in a conversation.\n", 60 | "* How is the conversations’ memory preserved?\n", 61 | "* How Gradio works.\n", 62 | "\n", 63 | "It is based in the first example of the course that use Panel instead of Gradio.\n", 64 | "https://github.com/peremartra/Large-Language-Model-Notebooks-Course/blob/main/1-Introduction%20to%20LLMs%20with%20OpenAI/1_1-First_Chatbot_OpenAI.ipynb\n" 65 | ] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "execution_count": null, 70 | "id": "a1d00720", 71 | "metadata": { 72 | "colab": { 73 | "base_uri": "https://localhost:8080/" 74 | }, 75 | "id": "a1d00720", 76 | "outputId": "0fe8fddf-c833-4865-9452-7e4d4e503a27" 77 | }, 78 | "outputs": [], 79 | "source": [ 80 | "#First install the necesary libraries\n", 81 | "!pip install -q openai==1.1.1\n", 82 | "!pip install -q gradio==4.40.0" 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "execution_count": null, 88 | "id": "a03f026a", 89 | "metadata": { 90 | "colab": { 91 | "base_uri": "https://localhost:8080/" 92 | }, 93 | "id": "a03f026a", 94 | "outputId": "31927aa7-5f7e-4936-db1b-9f9e1d8746ef" 95 | }, 96 | "outputs": [], 97 | "source": [ 98 | "#if you need a API Key from OpenAI\n", 99 | "#https://platform.openai.com/account/api-keys\n", 100 | "import openai\n", 101 | "import random\n", 102 | "import gradio as gr\n", 103 | "\n", 104 | "from getpass import getpass\n", 105 | "openai.api_key=getpass(\"OpenAI API Key: \")\n", 106 | "#model = \"gpt-3.5-turbo\"\n", 107 | "model = \"gpt-4o-mini\"" 108 | ] 109 | }, 110 | { 111 | "cell_type": "markdown", 112 | "id": "cf7bba1f-f84c-4066-9509-b98d3eaa6757", 113 | "metadata": { 114 | "id": "cf7bba1f-f84c-4066-9509-b98d3eaa6757" 115 | }, 116 | "source": [ 117 | "At OpenAI, we find three distinct roles:\n", 118 | "\n", 119 | "* system: Provides instructions to the model on how it should behave. We can define its personality here.\n", 120 | "* user: This role is used to send messages from the user.\n", 121 | "* assistant: The responses generated by the model.\n", 122 | "\n", 123 | "In the *context* variable, we will store the instructions for the model, which contain how it should act and the ice cream shop's menu." 124 | ] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "execution_count": null, 129 | "id": "922f8d24", 130 | "metadata": { 131 | "id": "922f8d24" 132 | }, 133 | "outputs": [], 134 | "source": [ 135 | "#Creating the system part of the prompt\n", 136 | "#Read and understand it.\n", 137 | "\n", 138 | "context = [ {'role':'system', 'content':\"\"\"\n", 139 | "You work collecting orders in a delivery IceCream shop called\n", 140 | "I'm freezed.\n", 141 | "\n", 142 | "First welcome the customer, in a very friedly way, then collects the order.\n", 143 | "\n", 144 | "Your instuctions are:\n", 145 | "-Collect the entire order, only from options in our menu, toppings included.\n", 146 | "-Summarize it\n", 147 | "-check for a final time if everithing is ok or the customer wants to add anything else.\n", 148 | "-collect the payment, be sure to include topings and the size of the ice cream.\n", 149 | "-Make sure to clarify all options, extras and sizes to uniquely\n", 150 | "identify the item from the menu.\n", 151 | "-Your answer should be short in a very friendly style.\n", 152 | "\n", 153 | "Our Menu:\n", 154 | "The IceCream menu includes only the flavors:\n", 155 | "-Vainilla.\n", 156 | "-Chocolate.\n", 157 | "-Lemon.\n", 158 | "-Strawberry.\n", 159 | "-Coffe.\n", 160 | "\n", 161 | "The IceCreams are available in two sizes:\n", 162 | "-Big: 3$\n", 163 | "-Medium: 2$\n", 164 | "\n", 165 | "Toppings:\n", 166 | "-Caramel\n", 167 | "-White chocolate\n", 168 | "-melted peanut butter\n", 169 | "Each topping cost 0.5$\n", 170 | "\n", 171 | "\"\"\"} ]" 172 | ] 173 | }, 174 | { 175 | "cell_type": "markdown", 176 | "id": "7e0fd845-c725-4bb0-bac4-9ded5e1b75a9", 177 | "metadata": { 178 | "id": "7e0fd845-c725-4bb0-bac4-9ded5e1b75a9" 179 | }, 180 | "source": [ 181 | "This function is just a wrapper for the OpenAI API. It will receives the messages in OpenAI format and return the model response.\n", 182 | "\n", 183 | "Sample of OpenAI Conversation:\n", 184 | "\n", 185 | "[{\"role\":\"user\", \"content\":\"hi\"},\n", 186 | " {\"role\":\"system\", \"content\":\"what can I do for you.\"}]" 187 | ] 188 | }, 189 | { 190 | "cell_type": "code", 191 | "execution_count": null, 192 | "id": "b92b6dbe-f285-4955-b59f-84df43886a64", 193 | "metadata": { 194 | "id": "b92b6dbe-f285-4955-b59f-84df43886a64" 195 | }, 196 | "outputs": [], 197 | "source": [ 198 | "#This function will receive the different messages in the conversation,\n", 199 | "#and call OpenAI passing the full conversartion.\n", 200 | "def continue_conversation(messages, temperature=0):\n", 201 | " response = openai.chat.completions.create(\n", 202 | " model=model,\n", 203 | " messages=messages,\n", 204 | " temperature=temperature,\n", 205 | " )\n", 206 | " return response.choices[0].message.content\n" 207 | ] 208 | }, 209 | { 210 | "cell_type": "markdown", 211 | "id": "36235d9c-e1c0-4c64-8e70-8700d381dc20", 212 | "metadata": { 213 | "id": "36235d9c-e1c0-4c64-8e70-8700d381dc20" 214 | }, 215 | "source": [ 216 | "The GradioChat function will be called by Gradio and is responsible for maintaining the conversation. In the first parameter, it will receive the current message from the user, and in the second parameter, the complete history with all previous messages and responses.\n", 217 | "\n", 218 | "In this case, Gradio is responsible for maintaining the conversation history. This function is responsible for formatting it to work correctly with OpenAI models." 219 | ] 220 | }, 221 | { 222 | "cell_type": "code", 223 | "execution_count": null, 224 | "id": "2749aa82-d857-420f-8bda-21df6a531a38", 225 | "metadata": { 226 | "id": "2749aa82-d857-420f-8bda-21df6a531a38" 227 | }, 228 | "outputs": [], 229 | "source": [ 230 | "#The function that Gradio will use.\n", 231 | "def gradio_chat(message, history):\n", 232 | " #Add the instructions to the prompt.\n", 233 | " history_chat = context\n", 234 | "\n", 235 | " #Add the history that Gradio send to us.\n", 236 | " for user, assistant in history:\n", 237 | " history_chat.append({\"role\":\"user\", \"content\":user})\n", 238 | " history_chat.append({\"role\":\"assistant\", \"content\":assistant})\n", 239 | "\n", 240 | " #Add the las user message.\n", 241 | " history_chat.append({\"role\":\"user\", \"content\":message})\n", 242 | "\n", 243 | " #Call OpenAI and return the response.\n", 244 | " return continue_conversation(history_chat, 0)" 245 | ] 246 | }, 247 | { 248 | "cell_type": "code", 249 | "execution_count": null, 250 | "id": "ec097501-b389-4f39-a66d-36e091d76700", 251 | "metadata": { 252 | "id": "ec097501-b389-4f39-a66d-36e091d76700" 253 | }, 254 | "outputs": [], 255 | "source": [ 256 | "#Customized gradio textbox.\n", 257 | "InputText = gr.Textbox(label=\"order\", info=\"Your Order here.\", scale= 6)" 258 | ] 259 | }, 260 | { 261 | "cell_type": "markdown", 262 | "id": "ab2993fd-fd13-4182-b646-42a551c74ccf", 263 | "metadata": { 264 | "id": "ab2993fd-fd13-4182-b646-42a551c74ccf" 265 | }, 266 | "source": [ 267 | "I'm going to use the simplest Gradio function for creating chats: *ChatInterface*. The first parameter it should receive is the previously built function responsible for maintaining the dialogue with OpenAI. In this case, *gradio_chat*.\n", 268 | "\n", 269 | "The function can be parameterized with a multitude of parameters, you can consult them in the official Gradio documentation: https://www.gradio.app/docs/gradio/chatinterface\n", 270 | "\n", 271 | "In this case, I pass it a customized textbox, otherwise we would have the default one, which would work the same, but without the customized text. I indicate that it should not show the retry or undo buttons, in addition to giving it a title and changing the text of the Submit button.\n", 272 | "\n" 273 | ] 274 | }, 275 | { 276 | "cell_type": "code", 277 | "execution_count": null, 278 | "id": "87bfc859-cb60-4e13-ac80-f03657422a5d", 279 | "metadata": { 280 | "colab": { 281 | "base_uri": "https://localhost:8080/", 282 | "height": 645 283 | }, 284 | "id": "87bfc859-cb60-4e13-ac80-f03657422a5d", 285 | "outputId": "6f870176-855a-43fd-a2f3-f5714faf9496" 286 | }, 287 | "outputs": [], 288 | "source": [ 289 | "gr.ChatInterface(gradio_chat,\n", 290 | " textbox=InputText,\n", 291 | " retry_btn=None,\n", 292 | " undo_btn=None,\n", 293 | " title=\"I'm freezed\",\n", 294 | " submit_btn=\"Order\").launch()" 295 | ] 296 | }, 297 | { 298 | "cell_type": "markdown", 299 | "id": "002dd2eb-f8c0-4908-b1ef-9d09aba74a6e", 300 | "metadata": { 301 | "id": "002dd2eb-f8c0-4908-b1ef-9d09aba74a6e" 302 | }, 303 | "source": [ 304 | "Gradio is very simple to use, yet incredibly powerful in terms of customization. It's a great tool for creating demos and quickly seeing how the built solutions work." 305 | ] 306 | }, 307 | { 308 | "cell_type": "code", 309 | "execution_count": null, 310 | "id": "68f02004-6047-43e8-a1d9-71660b795dce", 311 | "metadata": { 312 | "id": "68f02004-6047-43e8-a1d9-71660b795dce" 313 | }, 314 | "outputs": [], 315 | "source": [] 316 | } 317 | ], 318 | "metadata": { 319 | "colab": { 320 | "include_colab_link": true, 321 | "provenance": [] 322 | }, 323 | "kernelspec": { 324 | "display_name": "Python 3 (ipykernel)", 325 | "language": "python", 326 | "name": "python3" 327 | }, 328 | "language_info": { 329 | "codemirror_mode": { 330 | "name": "ipython", 331 | "version": 3 332 | }, 333 | "file_extension": ".py", 334 | "mimetype": "text/x-python", 335 | "name": "python", 336 | "nbconvert_exporter": "python", 337 | "pygments_lexer": "ipython3", 338 | "version": "3.12.4" 339 | } 340 | }, 341 | "nbformat": 4, 342 | "nbformat_minor": 5 343 | } 344 | -------------------------------------------------------------------------------- /1-Introduction to LLMs with OpenAI/1_2-Easy_NL2SQL.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "7c2b258e", 6 | "metadata": { 7 | "colab_type": "text", 8 | "id": "view-in-github" 9 | }, 10 | "source": [ 11 | "\"Open" 12 | ] 13 | }, 14 | { 15 | "cell_type": "markdown", 16 | "id": "MDJ3_BRhasUG", 17 | "metadata": { 18 | "id": "MDJ3_BRhasUG" 19 | }, 20 | "source": [ 21 | "
\n", 22 | "

Large Language Models Projects

\n", 23 | "

Apply and Implement Strategies for Large Language Models

\n", 24 | "

1.2-Create a simple Natural Language to SQL using OpenAI

\n", 25 | " \n", 26 | "
\n", 27 | "\n", 28 | "by [Pere Martra](https://www.linkedin.com/in/pere-martra/)\n", 29 | "
\n", 30 | "\n", 31 | "Models: gpt-3.5-turbo / gpt-4o-mini\n", 32 | "\n", 33 | "Colab Environment: CPU\n", 34 | "\n", 35 | "Keys:\n", 36 | "* NL2SQL\n", 37 | "* Code Generation\n", 38 | "* Prompt Hardening.\n", 39 | "\n", 40 | "Related article: [How to Create a Natural Language to SQL Translator Using OpenAI API](https://pub.towardsai.net/how-to-create-a-natural-language-to-sql-translator-using-openai-api-e1b1f72ac35a)\n", 41 | "____________________\n", 42 | "This is the unofficial repository for the book:\n", 43 | " Large Language Models: Apply and Implement Strategies for Large Language Models (Apress).\n", 44 | " The book is based on the content of this repository, but the notebooks are being updated, and I am incorporating new examples and chapters.\n", 45 | " If you are looking for the official repository for the book, with the original notebooks, you should visit the\n", 46 | " Apress repository, where you can find all the notebooks in their original format as they appear in the book." 47 | ] 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "id": "c23b4dd1", 52 | "metadata": { 53 | "id": "c23b4dd1" 54 | }, 55 | "source": [ 56 | "# SQL Generator\n", 57 | "A sample of how to build a translator from natural language to SQL:\n", 58 | "\n", 59 | "* GPT 35 / gpt-4o-mini\n", 60 | "* OpenAI\n" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": null, 66 | "id": "a1d00720", 67 | "metadata": { 68 | "id": "a1d00720" 69 | }, 70 | "outputs": [], 71 | "source": [ 72 | "!pip install -q openai==1.1.1\n", 73 | "!pip install -q panel" 74 | ] 75 | }, 76 | { 77 | "cell_type": "code", 78 | "execution_count": null, 79 | "id": "a03f026a", 80 | "metadata": { 81 | "id": "a03f026a" 82 | }, 83 | "outputs": [], 84 | "source": [ 85 | "import openai\n", 86 | "import panel as pn\n", 87 | "openai.api_key=\"your-openai-key\"\n", 88 | "#model = \"gpt-3.5-turbo\"\n", 89 | "model = \"gpt-4o-mini\"" 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "execution_count": null, 95 | "id": "77eac86d", 96 | "metadata": { 97 | "id": "77eac86d" 98 | }, 99 | "outputs": [], 100 | "source": [ 101 | "def continue_conversation(messages, temperature=0):\n", 102 | " response = openai.chat.completions.create(\n", 103 | " model=model,\n", 104 | " messages=messages,\n", 105 | " temperature=temperature,\n", 106 | " )\n", 107 | " #print(str(response.choices[0].message[\"content\"]))\n", 108 | " return response.choices[0].message.content" 109 | ] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "execution_count": null, 114 | "id": "51bec475", 115 | "metadata": { 116 | "id": "51bec475" 117 | }, 118 | "outputs": [], 119 | "source": [ 120 | "def add_prompts_conversation(_):\n", 121 | " #Get the value introduced by the user\n", 122 | " prompt = client_prompt.value_input\n", 123 | " client_prompt.value = ''\n", 124 | "\n", 125 | " #Append to the context the User promnopt.\n", 126 | " context.append({'role':'user', 'content':f\"{prompt}.\"})\n", 127 | " context.append({'role':'system', 'content':f\"\"\"Only return SQL Orders.\n", 128 | " If you can't return and SQL order, say sorry, and ask, politely but concisely, for a new question.\"\"\"})\n", 129 | "\n", 130 | " #Get the response.\n", 131 | " response = continue_conversation(context)\n", 132 | "\n", 133 | " #Add the response to the context.\n", 134 | " context.append({'role':'assistant', 'content':f\"{response}\"})\n", 135 | "\n", 136 | " #Undate the panels to shjow the conversation.\n", 137 | " panels.append(\n", 138 | " pn.Row('User:', pn.pane.Markdown(prompt, width=600)))\n", 139 | " panels.append(\n", 140 | " pn.Row('Assistant:', pn.pane.Markdown(response, width=600, styles={'background-color': '#F6F6F6'})))\n", 141 | "\n", 142 | " return pn.Column(*panels)" 143 | ] 144 | }, 145 | { 146 | "cell_type": "code", 147 | "execution_count": null, 148 | "id": "922f8d24", 149 | "metadata": { 150 | "colab": { 151 | "base_uri": "https://localhost:8080/", 152 | "height": 17 153 | }, 154 | "id": "922f8d24", 155 | "outputId": "cc5bf6a2-08a9-4c9c-80ff-b6bbf3fc9194" 156 | }, 157 | "outputs": [], 158 | "source": [ 159 | "context = [ {'role':'system', 'content':\"\"\"\n", 160 | "you are a bot to assist in create SQL commands, all your answers should start with\n", 161 | "this is your SQL, and after that an SQL that can do what the user request.\n", 162 | "\n", 163 | "Your SQL Database is composed by some tables.\n", 164 | "Try to Maintain the SQL order simple.\n", 165 | "Just after the SQL add a simple and concise text explaining how it works.\n", 166 | "If the user ask for something that can not be solved with an SQL Order\n", 167 | "just answer something nice and simple, maximum 10 words, asking him for something that\n", 168 | "can be solved with SQL.\n", 169 | "\"\"\"} ]\n", 170 | "\n", 171 | "context.append( {'role':'system', 'content':\"\"\"\n", 172 | "first table:\n", 173 | "{\n", 174 | " \"tableName\": \"employees\",\n", 175 | " \"fields\": [\n", 176 | " {\n", 177 | " \"nombre\": \"ID_usr\",\n", 178 | " \"tipo\": \"int\"\n", 179 | " },\n", 180 | " {\n", 181 | " \"nombre\": \"name\",\n", 182 | " \"tipo\": \"string\"\n", 183 | " }\n", 184 | " ]\n", 185 | "}\n", 186 | "\"\"\"\n", 187 | "})\n", 188 | "\n", 189 | "context.append( {'role':'system', 'content':\"\"\"\n", 190 | "second table:\n", 191 | "{\n", 192 | " \"tableName\": \"salary\",\n", 193 | " \"fields\": [\n", 194 | " {\n", 195 | " \"nombre\": \"ID_usr\",\n", 196 | " \"type\": \"int\"\n", 197 | " },\n", 198 | " {\n", 199 | " \"name\": \"year\",\n", 200 | " \"type\": \"date\"\n", 201 | " },\n", 202 | " {\n", 203 | " \"name\": \"salary\",\n", 204 | " \"type\": \"float\"\n", 205 | " }\n", 206 | " ]\n", 207 | "}\n", 208 | "\"\"\"\n", 209 | "})\n", 210 | "\n", 211 | "context.append( {'role':'system', 'content':\"\"\"\n", 212 | "third table:\n", 213 | "{\n", 214 | " \"tablename\": \"studies\",\n", 215 | " \"fields\": [\n", 216 | " {\n", 217 | " \"name\": \"ID\",\n", 218 | " \"type\": \"int\"\n", 219 | " },\n", 220 | " {\n", 221 | " \"name\": \"ID_usr\",\n", 222 | " \"type\": \"int\"\n", 223 | " },\n", 224 | " {\n", 225 | " \"name\": \"educational level\",\n", 226 | " \"type\": \"int\"\n", 227 | " },\n", 228 | " {\n", 229 | " \"name\": \"Institution\",\n", 230 | " \"type\": \"string\"\n", 231 | " },\n", 232 | " {\n", 233 | " \"name\": \"Years\",\n", 234 | " \"type\": \"date\"\n", 235 | " }\n", 236 | " {\n", 237 | " \"name\": \"Speciality\",\n", 238 | " \"type\": \"string\"\n", 239 | " }\n", 240 | " ]\n", 241 | "}\n", 242 | "\"\"\"\n", 243 | "})\n", 244 | "\n", 245 | "#Creating the panel.\n", 246 | "pn.extension()\n", 247 | "\n", 248 | "panels = []\n", 249 | "\n", 250 | "client_prompt = pn.widgets.TextInput(value=\"Hi\", placeholder='Order your data…')\n", 251 | "button_conversation = pn.widgets.Button(name=\"generate SQL\")\n", 252 | "\n", 253 | "interactive_conversation = pn.bind(add_prompts_conversation, button_conversation)\n", 254 | "\n", 255 | "dashboard = pn.Column(\n", 256 | " client_prompt,\n", 257 | " pn.Row(button_conversation),\n", 258 | " pn.panel(interactive_conversation, loading_indicator=True),\n", 259 | ")" 260 | ] 261 | }, 262 | { 263 | "cell_type": "code", 264 | "execution_count": null, 265 | "id": "88db4691", 266 | "metadata": { 267 | "colab": { 268 | "base_uri": "https://localhost:8080/", 269 | "height": 512 270 | }, 271 | "id": "88db4691", 272 | "outputId": "cd2aa205-0bab-4d13-e3f8-f83087ea6807" 273 | }, 274 | "outputs": [], 275 | "source": [ 276 | "#Sample question: \"Return the name of the best paid employee\"\n", 277 | "dashboard" 278 | ] 279 | } 280 | ], 281 | "metadata": { 282 | "colab": { 283 | "include_colab_link": true, 284 | "provenance": [] 285 | }, 286 | "kernelspec": { 287 | "display_name": "Python 3 (ipykernel)", 288 | "language": "python", 289 | "name": "python3" 290 | }, 291 | "language_info": { 292 | "codemirror_mode": { 293 | "name": "ipython", 294 | "version": 3 295 | }, 296 | "file_extension": ".py", 297 | "mimetype": "text/x-python", 298 | "name": "python", 299 | "nbconvert_exporter": "python", 300 | "pygments_lexer": "ipython3", 301 | "version": "3.8.13" 302 | } 303 | }, 304 | "nbformat": 4, 305 | "nbformat_minor": 5 306 | } 307 | -------------------------------------------------------------------------------- /1-Introduction to LLMs with OpenAI/1_2-Easy_NL2SQL_Gradio.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "72c5bfbe", 6 | "metadata": { 7 | "colab_type": "text", 8 | "id": "view-in-github" 9 | }, 10 | "source": [ 11 | "\"Open" 12 | ] 13 | }, 14 | { 15 | "cell_type": "markdown", 16 | "id": "MDJ3_BRhasUG", 17 | "metadata": { 18 | "id": "MDJ3_BRhasUG" 19 | }, 20 | "source": [ 21 | "
\n", 22 | "

Large Language Models Projects

\n", 23 | "

Apply and Implement Strategies for Large Language Models

\n", 24 | "

1.2-Create a simple Natural Language to SQL with OpenAI and Gradio.

\n", 25 | " \n", 26 | "
\n", 27 | "\n", 28 | "by [Pere Martra](https://www.linkedin.com/in/pere-martra/)\n", 29 | "
\n", 30 | "\n", 31 | "Models: gpt-3.5-turbo / gpt-4o-mini\n", 32 | "\n", 33 | "Colab Environment: CPU\n", 34 | "\n", 35 | "Keys:\n", 36 | "* NL2SQL\n", 37 | "* Gradio\n", 38 | "* Code Generation\n", 39 | "* Prompt Hardening.\n", 40 | "\n", 41 | "Article related: [Create a Natural Language to SQL Solution with OpenAI and Gradio](https://pub.towardsai.net/first-nl2sql-chat-with-openai-and-gradio-b1de0d6541b4?sk=7e0346b93130e70574645d5d390adfe9)" 42 | ] 43 | }, 44 | { 45 | "cell_type": "markdown", 46 | "id": "c23b4dd1", 47 | "metadata": { 48 | "id": "c23b4dd1" 49 | }, 50 | "source": [ 51 | "# SQL Generator\n", 52 | "A sample of how to build a translator from natural language to SQL:\n", 53 | "\n", 54 | "* GPT 35 / gpt-4o-mini\n", 55 | "* OpenAI\n" 56 | ] 57 | }, 58 | { 59 | "cell_type": "code", 60 | "execution_count": null, 61 | "id": "a1d00720", 62 | "metadata": { 63 | "id": "a1d00720" 64 | }, 65 | "outputs": [], 66 | "source": [ 67 | "!pip install -q openai==1.1.1\n", 68 | "!pip install -q gradio==4.41.0" 69 | ] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": null, 74 | "id": "a03f026a", 75 | "metadata": { 76 | "id": "a03f026a" 77 | }, 78 | "outputs": [], 79 | "source": [ 80 | "import openai\n", 81 | "import panel as pn\n", 82 | "from getpass import getpass\n", 83 | "import gradio as gr" 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "execution_count": null, 89 | "id": "9uS06-2jbNs1", 90 | "metadata": { 91 | "colab": { 92 | "base_uri": "https://localhost:8080/" 93 | }, 94 | "id": "9uS06-2jbNs1", 95 | "outputId": "c991fc4f-675f-4c88-cd08-b5fcae830f18" 96 | }, 97 | "outputs": [], 98 | "source": [ 99 | "openai.api_key=getpass(\"OpenAI API Key: \")\n", 100 | "#model = \"gpt-3.5-turbo\"\n", 101 | "model = \"gpt-4o-mini\"" 102 | ] 103 | }, 104 | { 105 | "cell_type": "code", 106 | "execution_count": null, 107 | "id": "xEx1chXxXE2h", 108 | "metadata": { 109 | "id": "xEx1chXxXE2h" 110 | }, 111 | "outputs": [], 112 | "source": [ 113 | "context = [ {'role':'system', 'content':\"\"\"\n", 114 | "you are a bot to assist in create SQL commands, all your answers should start with\n", 115 | "this is your SQL, and after that an SQL that can do what the user request.\n", 116 | "\n", 117 | "Your SQL Database is composed by some tables.\n", 118 | "Try to Maintain the SQL order simple.\n", 119 | "Just after the SQL add a simple and concise text explaining how it works.\n", 120 | "If the user ask for something that can not be answered with information from the DB\n", 121 | "just answer something nice and simple, maximum 10 words, asking him for a new question that\n", 122 | "can be solved with SQL.\n", 123 | "\"\"\"} ]\n", 124 | "\n", 125 | "context.append( {'role':'system', 'content':\"\"\"\n", 126 | "first table:\n", 127 | "{\n", 128 | " \"tableName\": \"employees\",\n", 129 | " \"fields\": [\n", 130 | " {\n", 131 | " \"nombre\": \"ID_usr\",\n", 132 | " \"tipo\": \"int\"\n", 133 | " },\n", 134 | " {\n", 135 | " \"nombre\": \"name\",\n", 136 | " \"tipo\": \"string\"\n", 137 | " }\n", 138 | " ]\n", 139 | "}\n", 140 | "\"\"\"\n", 141 | "})\n", 142 | "\n", 143 | "context.append( {'role':'system', 'content':\"\"\"\n", 144 | "second table:\n", 145 | "{\n", 146 | " \"tableName\": \"salary\",\n", 147 | " \"fields\": [\n", 148 | " {\n", 149 | " \"nombre\": \"ID_usr\",\n", 150 | " \"type\": \"int\"\n", 151 | " },\n", 152 | " {\n", 153 | " \"name\": \"year\",\n", 154 | " \"type\": \"date\"\n", 155 | " },\n", 156 | " {\n", 157 | " \"name\": \"salary\",\n", 158 | " \"type\": \"float\"\n", 159 | " }\n", 160 | " ]\n", 161 | "}\n", 162 | "\"\"\"\n", 163 | "})\n", 164 | "\n", 165 | "context.append( {'role':'system', 'content':\"\"\"\n", 166 | "third table:\n", 167 | "{\n", 168 | " \"tablename\": \"studies\",\n", 169 | " \"fields\": [\n", 170 | " {\n", 171 | " \"name\": \"ID\",\n", 172 | " \"type\": \"int\"\n", 173 | " },\n", 174 | " {\n", 175 | " \"name\": \"ID_usr\",\n", 176 | " \"type\": \"int\"\n", 177 | " },\n", 178 | " {\n", 179 | " \"name\": \"educational level\",\n", 180 | " \"type\": \"int\"\n", 181 | " },\n", 182 | " {\n", 183 | " \"name\": \"Institution\",\n", 184 | " \"type\": \"string\"\n", 185 | " },\n", 186 | " {\n", 187 | " \"name\": \"Years\",\n", 188 | " \"type\": \"date\"\n", 189 | " }\n", 190 | " {\n", 191 | " \"name\": \"Speciality\",\n", 192 | " \"type\": \"string\"\n", 193 | " }\n", 194 | " ]\n", 195 | "}\n", 196 | "\"\"\"\n", 197 | "})" 198 | ] 199 | }, 200 | { 201 | "cell_type": "code", 202 | "execution_count": null, 203 | "id": "77eac86d", 204 | "metadata": { 205 | "id": "77eac86d" 206 | }, 207 | "outputs": [], 208 | "source": [ 209 | "def continue_conversation(messages, temperature=0):\n", 210 | " response = openai.chat.completions.create(\n", 211 | " model=model,\n", 212 | " messages=messages,\n", 213 | " temperature=temperature,\n", 214 | " )\n", 215 | " return response.choices[0].message.content" 216 | ] 217 | }, 218 | { 219 | "cell_type": "code", 220 | "execution_count": null, 221 | "id": "nDrtcJviXjwb", 222 | "metadata": { 223 | "id": "nDrtcJviXjwb" 224 | }, 225 | "outputs": [], 226 | "source": [ 227 | "#The function that Gradio will use.\n", 228 | "def gradio_chat(message, history):\n", 229 | " #Add the instructions to the prompt.\n", 230 | " history_chat = context\n", 231 | "\n", 232 | " #Add the history that Gradio send to us.\n", 233 | " for user, assistant in history:\n", 234 | " history_chat.append({\"role\":\"user\", \"content\":user})\n", 235 | " history_chat.append({\"role\":\"assistant\", \"content\":assistant})\n", 236 | "\n", 237 | " #Add the las user message.\n", 238 | " history_chat.append({\"role\":\"user\", \"content\":message})\n", 239 | " history_chat.append({'role':'system', 'content':f\"\"\"Only return SQL Orders.\n", 240 | " If you can't return and SQL order, say sorry, and ask, politely but concisely, for a new question.\"\"\"})\n", 241 | "\n", 242 | " #Call OpenAI and return the response.\n", 243 | " return continue_conversation(history_chat, 0)" 244 | ] 245 | }, 246 | { 247 | "cell_type": "code", 248 | "execution_count": null, 249 | "id": "myN9PronasVJ", 250 | "metadata": { 251 | "id": "myN9PronasVJ" 252 | }, 253 | "outputs": [], 254 | "source": [ 255 | "#Customized gradio textbox.\n", 256 | "InputText = gr.Textbox(label=\"order\", info=\"Talk with DB\", scale= 6)\n", 257 | "examples=[\"Who is the highest-paid employee?\", \"How many employes with degrees do we have?\"]" 258 | ] 259 | }, 260 | { 261 | "cell_type": "code", 262 | "execution_count": null, 263 | "id": "ZA2QmOWfavry", 264 | "metadata": { 265 | "colab": { 266 | "base_uri": "https://localhost:8080/", 267 | "height": 645 268 | }, 269 | "id": "ZA2QmOWfavry", 270 | "outputId": "a591418f-b89c-4a3d-bc46-84b2b1024eb9" 271 | }, 272 | "outputs": [], 273 | "source": [ 274 | "gr.ChatInterface(gradio_chat,\n", 275 | " textbox=InputText,\n", 276 | " retry_btn=None,\n", 277 | " undo_btn=None,\n", 278 | " title=\"SQL Generator\",\n", 279 | " examples=examples,\n", 280 | " submit_btn=\"Get My SQL\").launch()" 281 | ] 282 | }, 283 | { 284 | "cell_type": "code", 285 | "execution_count": null, 286 | "id": "MSn19fgAcpCa", 287 | "metadata": { 288 | "id": "MSn19fgAcpCa" 289 | }, 290 | "outputs": [], 291 | "source": [] 292 | } 293 | ], 294 | "metadata": { 295 | "colab": { 296 | "include_colab_link": true, 297 | "provenance": [] 298 | }, 299 | "kernelspec": { 300 | "display_name": "Python 3 (ipykernel)", 301 | "language": "python", 302 | "name": "python3" 303 | }, 304 | "language_info": { 305 | "codemirror_mode": { 306 | "name": "ipython", 307 | "version": 3 308 | }, 309 | "file_extension": ".py", 310 | "mimetype": "text/x-python", 311 | "name": "python", 312 | "nbconvert_exporter": "python", 313 | "pygments_lexer": "ipython3", 314 | "version": "3.8.13" 315 | } 316 | }, 317 | "nbformat": 4, 318 | "nbformat_minor": 5 319 | } 320 | -------------------------------------------------------------------------------- /1-Introduction to LLMs with OpenAI/1_2b-Easy_NL2SQL.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "704b1800", 6 | "metadata": { 7 | "colab_type": "text", 8 | "id": "view-in-github" 9 | }, 10 | "source": [ 11 | "\"Open" 12 | ] 13 | }, 14 | { 15 | "cell_type": "markdown", 16 | "id": "MDJ3_BRhasUG", 17 | "metadata": { 18 | "id": "MDJ3_BRhasUG" 19 | }, 20 | "source": [ 21 | "
\n", 22 | "

Large Language Models Projects

\n", 23 | "

Apply and Implement Strategies for Large Language Models

\n", 24 | "

Create a Simple Natural Language to SQL translator

\n", 25 | " contributions by Fabricio Q\n", 26 | " \n", 27 | "
\n", 28 | "\n", 29 | "
\n", 30 | "\n", 31 | "
\n", 32 | "  \n", 33 | " \n", 34 | " \n", 35 | "
\n", 36 | "\n", 37 | "
\n", 38 | "
" 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "id": "ZVFLu27kVQor", 44 | "metadata": { 45 | "id": "ZVFLu27kVQor" 46 | }, 47 | "source": [ 48 | "This is the unofficial repository for the book:\n", 49 | " Large Language Models: Apply and Implement Strategies for Large Language Models (Apress).\n", 50 | " The book is based on the content of this repository, but the notebooks are being updated, and I am incorporating new examples and chapters.\n", 51 | " If you are looking for the official repository for the book, with the original notebooks, you should visit the\n", 52 | " Apress repository, where you can find all the notebooks in their original format as they appear in the book." 53 | ] 54 | }, 55 | { 56 | "cell_type": "markdown", 57 | "id": "c23b4dd1", 58 | "metadata": { 59 | "id": "c23b4dd1" 60 | }, 61 | "source": [ 62 | "# SQL Generator\n", 63 | "A sample of how to build a translator from natural language to SQL:\n", 64 | "\n", 65 | "* GPT 35\n", 66 | "* Panel\n", 67 | "* OpenAI\n", 68 | "\n", 69 | "Deeper explanations in the article: [How to Create a Natural Language to SQL Translator Using OpenAI API](https://medium.com/towards-artificial-intelligence/how-to-create-a-natural-language-to-sql-translator-using-openai-api-e1b1f72ac35a)" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": null, 75 | "id": "a1d00720", 76 | "metadata": { 77 | "colab": { 78 | "base_uri": "https://localhost:8080/" 79 | }, 80 | "id": "a1d00720", 81 | "outputId": "84107c89-10cd-423a-c8ee-fa97aa1916fd" 82 | }, 83 | "outputs": [], 84 | "source": [ 85 | "!pip install openai==1.1.1\n", 86 | "!pip install panel" 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": null, 92 | "id": "a03f026a", 93 | "metadata": { 94 | "id": "a03f026a" 95 | }, 96 | "outputs": [], 97 | "source": [ 98 | "import openai\n", 99 | "import panel as pn\n", 100 | "#from mykeys import openai_api_key\n", 101 | "openai.api_key=\"your-openai-api-key\"" 102 | ] 103 | }, 104 | { 105 | "cell_type": "code", 106 | "execution_count": null, 107 | "id": "77eac86d", 108 | "metadata": { 109 | "id": "77eac86d" 110 | }, 111 | "outputs": [], 112 | "source": [ 113 | "def continue_conversation(messages, temperature=0):\n", 114 | " response = openai.chat.completions.create(\n", 115 | " model=\"gpt-3.5-turbo\",\n", 116 | " messages=messages,\n", 117 | " temperature=temperature,\n", 118 | " )\n", 119 | " #print(str(response.choices[0].message[\"content\"]))\n", 120 | " return response.choices[0].message.content" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": null, 126 | "id": "51bec475", 127 | "metadata": { 128 | "id": "51bec475" 129 | }, 130 | "outputs": [], 131 | "source": [ 132 | "def add_prompts_conversation(_):\n", 133 | " #Get the value introduced by the user\n", 134 | " prompt = client_prompt.value_input\n", 135 | " client_prompt.value = ''\n", 136 | "\n", 137 | " #Append to the context the User promnopt.\n", 138 | " context.append({'role':'user', 'content':f\"{prompt}.\"})\n", 139 | " context.append({'role':'system', 'content':f\"\"\"YOU ARE A SQL Assistant. \\\n", 140 | " If the user ask for something that the result is not an SQL Order \\\n", 141 | " just answer something nice and simple asking him, with maximum 10 words, for something that \\\n", 142 | " can be solved with SQL.\"\"\"})\n", 143 | "\n", 144 | " #Get the response.\n", 145 | " response = continue_conversation(context)\n", 146 | "\n", 147 | " #Add the response to the context.\n", 148 | " context.append({'role':'assistant', 'content':f\"{response}\"})\n", 149 | "\n", 150 | " #Undate the panels to shjow the conversation.\n", 151 | " panels.append(\n", 152 | " pn.Row('User:', pn.pane.Markdown(prompt, width=600)))\n", 153 | " panels.append(\n", 154 | " pn.Row('Assistant:', pn.pane.Markdown(response, width=600, styles={'background-color': '#F6F6F6'})))\n", 155 | "\n", 156 | " return pn.Column(*panels)" 157 | ] 158 | }, 159 | { 160 | "cell_type": "markdown", 161 | "id": "7ffec1d9f3cbe24d", 162 | "metadata": { 163 | "id": "7ffec1d9f3cbe24d" 164 | }, 165 | "source": [ 166 | "**Note:** The database information will be provided to the LLM using DBML, an expressive Domain-Specific Language (DSL) designed for describing database structures. [DBML](https://dbml.dbdiagram.io/docs/) is human-readable and well understood by modern Large Language Models (LLMs) as well, enabling the rich description of databases while using fewer tokens." 167 | ] 168 | }, 169 | { 170 | "cell_type": "code", 171 | "execution_count": null, 172 | "id": "922f8d24", 173 | "metadata": { 174 | "colab": { 175 | "base_uri": "https://localhost:8080/", 176 | "height": 17 177 | }, 178 | "id": "922f8d24", 179 | "outputId": "73379791-f1e9-4810-9fdb-2b0365bf7039" 180 | }, 181 | "outputs": [], 182 | "source": [ 183 | "context = [ {'role':'system', 'content':\"\"\"\n", 184 | "you are a bot to assist in create SQL commands, all your answers should start with \\\n", 185 | "this is your SQL, and after that an SQL that can do what the user request. \\\n", 186 | "Your Database is composed by a SQL database with some tables. \\\n", 187 | "Try to Maintain the SQL order simple.\n", 188 | "Put the SQL command in white letters with a black background, and just after \\\n", 189 | "a simple and concise text explaining how it works.\n", 190 | "If the user ask for something that can not be solved with an SQL Order \\\n", 191 | "just answer something nice and simple, maximum 10 words, asking him for something that \\\n", 192 | "can be solved with SQL.\n", 193 | "\"\"\"} ]\n", 194 | "\n", 195 | "context.append( {'role':'system', 'content':\"\"\"\n", 196 | "This is the definition of your database tables:\n", 197 | "\n", 198 | "```dbml\n", 199 | "# first table\n", 200 | "Table employees {\n", 201 | " ID_usr int [pk]\n", 202 | " name string\n", 203 | "}\n", 204 | "\n", 205 | "#second table\n", 206 | "Table salary {\n", 207 | " ID_usr int [ref: > employees.ID_usr]\n", 208 | " year date\n", 209 | " salary float\n", 210 | "}\n", 211 | "\n", 212 | "# third table\n", 213 | "Table studies {\n", 214 | " ID int [pk]\n", 215 | " ID_usr int [ref: > employees.ID_usr]\n", 216 | " educational_level int\n", 217 | " Institution string\n", 218 | " Years date\n", 219 | " Speciality string\n", 220 | "}\n", 221 | "```\n", 222 | "\"\"\"\n", 223 | "})\n", 224 | "\n", 225 | "#Creamos el panel.\n", 226 | "pn.extension()\n", 227 | "\n", 228 | "panels = []\n", 229 | "\n", 230 | "client_prompt = pn.widgets.TextInput(value=\"Hi\", placeholder='Order your data…')\n", 231 | "button_conversation = pn.widgets.Button(name=\"generate SQL\")\n", 232 | "\n", 233 | "interactive_conversation = pn.bind(add_prompts_conversation, button_conversation)\n", 234 | "\n", 235 | "dashboard = pn.Column(\n", 236 | " client_prompt,\n", 237 | " pn.Row(button_conversation),\n", 238 | " pn.panel(interactive_conversation, loading_indicator=True, height=300),\n", 239 | ")\n", 240 | "\n" 241 | ] 242 | }, 243 | { 244 | "cell_type": "code", 245 | "execution_count": null, 246 | "id": "88db4691", 247 | "metadata": { 248 | "colab": { 249 | "base_uri": "https://localhost:8080/", 250 | "height": 399 251 | }, 252 | "id": "88db4691", 253 | "outputId": "0a6d5382-0835-4092-ad32-c5a515a7e1a4" 254 | }, 255 | "outputs": [], 256 | "source": [ 257 | "dashboard" 258 | ] 259 | }, 260 | { 261 | "cell_type": "code", 262 | "execution_count": null, 263 | "id": "5334f942", 264 | "metadata": { 265 | "id": "5334f942" 266 | }, 267 | "outputs": [], 268 | "source": [] 269 | } 270 | ], 271 | "metadata": { 272 | "colab": { 273 | "include_colab_link": true, 274 | "provenance": [] 275 | }, 276 | "kernelspec": { 277 | "display_name": "Python 3 (ipykernel)", 278 | "language": "python", 279 | "name": "python3" 280 | }, 281 | "language_info": { 282 | "codemirror_mode": { 283 | "name": "ipython", 284 | "version": 3 285 | }, 286 | "file_extension": ".py", 287 | "mimetype": "text/x-python", 288 | "name": "python", 289 | "nbconvert_exporter": "python", 290 | "pygments_lexer": "ipython3", 291 | "version": "3.8.13" 292 | } 293 | }, 294 | "nbformat": 4, 295 | "nbformat_minor": 5 296 | } 297 | -------------------------------------------------------------------------------- /1-Introduction to LLMs with OpenAI/1_3-Intro_Prompt_Engineering.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "a0428da2", 6 | "metadata": { 7 | "colab_type": "text", 8 | "id": "view-in-github" 9 | }, 10 | "source": [ 11 | "\"Open" 12 | ] 13 | }, 14 | { 15 | "cell_type": "markdown", 16 | "id": "9c4700eb-00ef-433f-b574-2fb7f644226d", 17 | "metadata": { 18 | "id": "9c4700eb-00ef-433f-b574-2fb7f644226d" 19 | }, 20 | "source": [ 21 | "
\n", 22 | "

Large Language Models Projects

\n", 23 | "

Apply and Implement Strategies for Large Language Models

\n", 24 | "

Influencing the model's response with in-context learning

\n", 25 | " \n", 26 | "
\n", 27 | "\n", 28 | "by [Pere Martra](https://www.linkedin.com/in/pere-martra/)\n", 29 | "
\n", 30 | "\n", 31 | "Models: gpt-3.5-turbo / gpt-4o-mini\n", 32 | "\n", 33 | "Colab Environment: CPU\n", 34 | "\n", 35 | "Keys:\n", 36 | "* In context learning.\n", 37 | "* Response formating.\n", 38 | "* Sentiment classification.\n", 39 | "\n", 40 | "Related article: [Influencing a Large Language Model response with in-context learning.](https://medium.com/gitconnected/influencing-a-large-language-model-response-with-in-context-learning-b212f0eaa113)\n", 41 | "_______________________________\n", 42 | "\n", 43 | "This is the unofficial repository for the book:\n", 44 | " Large Language Models: Apply and Implement Strategies for Large Language Models (Apress).\n", 45 | " The book is based on the content of this repository, but the notebooks are being updated, and I am incorporating new examples and chapters.\n", 46 | " If you are looking for the official repository for the book, with the original notebooks, you should visit the\n", 47 | " Apress repository, where you can find all the notebooks in their original format as they appear in the book.\n", 48 | "\n" 49 | ] 50 | }, 51 | { 52 | "cell_type": "markdown", 53 | "id": "34723a72-1601-4685-a0ba-bff544425d48", 54 | "metadata": { 55 | "id": "34723a72-1601-4685-a0ba-bff544425d48" 56 | }, 57 | "source": [ 58 | "In this notebook, we'll explore small prompt engineering techniques and recommendations that will help us elicit responses from the models that are better suited to our needs." 59 | ] 60 | }, 61 | { 62 | "cell_type": "code", 63 | "execution_count": null, 64 | "id": "fe5f658a-708f-406c-a1cb-95e3482ae760", 65 | "metadata": { 66 | "colab": { 67 | "base_uri": "https://localhost:8080/" 68 | }, 69 | "id": "fe5f658a-708f-406c-a1cb-95e3482ae760", 70 | "outputId": "572e089f-049b-42a6-a660-a5efefe0c521" 71 | }, 72 | "outputs": [], 73 | "source": [ 74 | "!pip install -q openai==1.1.1" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": null, 80 | "id": "fba193cc-d8a0-4ad2-8177-380204426859", 81 | "metadata": { 82 | "id": "fba193cc-d8a0-4ad2-8177-380204426859" 83 | }, 84 | "outputs": [], 85 | "source": [ 86 | "#if you need a API Key from OpenAI\n", 87 | "#https://platform.openai.com/account/api-keys\n", 88 | "\n", 89 | "import openai\n", 90 | "openai.api_key=\"your-openai-key\"\n", 91 | "model = \"gpt-3.5-turbo\"\n", 92 | "#model = \"gpt-4o\"" 93 | ] 94 | }, 95 | { 96 | "cell_type": "markdown", 97 | "id": "502cfc93-21e0-498f-9650-37bc6ddd514d", 98 | "metadata": { 99 | "id": "502cfc93-21e0-498f-9650-37bc6ddd514d" 100 | }, 101 | "source": [ 102 | "# Formating the answer with Few Shot Samples.\n", 103 | "\n", 104 | "To obtain the model's response in a specific format, we have various options, but one of the most convenient is to use Few-Shot Samples. This involves presenting the model with pairs of user queries and example responses.\n", 105 | "\n", 106 | "Large models like GPT-3.5 respond well to the examples provided, adapting their response to the specified format.\n", 107 | "\n", 108 | "Depending on the number of examples given, this technique can be referred to as:\n", 109 | "* Zero-Shot.\n", 110 | "* One-Shot.\n", 111 | "* Few-Shots.\n", 112 | "\n", 113 | "With One Shot should be enough, and it is recommended to use a maximum of six shots. It's important to remember that this information is passed in each query and occupies space in the input prompt.\n", 114 | "\n" 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": null, 120 | "id": "a8344712-06d7-4c24-83d8-f36d62926e5e", 121 | "metadata": { 122 | "id": "a8344712-06d7-4c24-83d8-f36d62926e5e" 123 | }, 124 | "outputs": [], 125 | "source": [ 126 | "#Functio to call the model.\n", 127 | "def return_OAIResponse(user_message, context):\n", 128 | "\n", 129 | " newcontext = context.copy()\n", 130 | " newcontext.append({'role':'user', 'content':\"question: \" + user_message})\n", 131 | "\n", 132 | " response = openai.chat.completions.create(\n", 133 | " model=model,\n", 134 | " messages=newcontext,\n", 135 | " temperature=1,\n", 136 | " )\n", 137 | "\n", 138 | " return (response.choices[0].message.content)" 139 | ] 140 | }, 141 | { 142 | "cell_type": "markdown", 143 | "id": "f611d73d-9330-466d-b705-543667e1b561", 144 | "metadata": { 145 | "id": "f611d73d-9330-466d-b705-543667e1b561" 146 | }, 147 | "source": [ 148 | "In this zero-shots prompt we obtain a correct response, but without formatting, as the model incorporates the information he wants." 149 | ] 150 | }, 151 | { 152 | "cell_type": "code", 153 | "execution_count": null, 154 | "id": "647790be-fdb8-4692-a82e-7e3a0220f72a", 155 | "metadata": { 156 | "colab": { 157 | "base_uri": "https://localhost:8080/" 158 | }, 159 | "id": "647790be-fdb8-4692-a82e-7e3a0220f72a", 160 | "outputId": "ed07d20e-e90b-41a3-cd1a-64d84f0ac7d2" 161 | }, 162 | "outputs": [], 163 | "source": [ 164 | "#zero-shot\n", 165 | "context_user = [\n", 166 | " {'role':'system', 'content':'You are an expert in F1.'}\n", 167 | "]\n", 168 | "print(return_OAIResponse(\"Who won the F1 2010?\", context_user))" 169 | ] 170 | }, 171 | { 172 | "cell_type": "markdown", 173 | "id": "e87a9a0a-c1b9-4759-b52f-f6547d29b4c8", 174 | "metadata": { 175 | "id": "e87a9a0a-c1b9-4759-b52f-f6547d29b4c8" 176 | }, 177 | "source": [ 178 | "Models as large and good as GPT 3.5 and GPT 4 only need a single shot to learn the output format we expect.\n" 179 | ] 180 | }, 181 | { 182 | "cell_type": "code", 183 | "execution_count": null, 184 | "id": "33ac7693-6cf3-44f7-b2ff-55d8a36fe775", 185 | "metadata": { 186 | "colab": { 187 | "base_uri": "https://localhost:8080/" 188 | }, 189 | "id": "33ac7693-6cf3-44f7-b2ff-55d8a36fe775", 190 | "outputId": "9cb248e1-19e3-4ef1-f03c-d126adf4a851" 191 | }, 192 | "outputs": [], 193 | "source": [ 194 | "#one-shot\n", 195 | "context_user = [\n", 196 | " {'role':'system', 'content':\n", 197 | " \"\"\"You are an expert in F1.\n", 198 | "\n", 199 | " Who won the 2000 f1 championship?\n", 200 | " Driver: Michael Schumacher.\n", 201 | " Team: Ferrari.\"\"\"}\n", 202 | "]\n", 203 | "print(return_OAIResponse(\"Who won the F1 2011?\", context_user))" 204 | ] 205 | }, 206 | { 207 | "cell_type": "markdown", 208 | "id": "32c454a8-181b-482b-873a-81d6ffde4674", 209 | "metadata": { 210 | "id": "32c454a8-181b-482b-873a-81d6ffde4674" 211 | }, 212 | "source": [ 213 | "Smaller models, or more complicated formats, may require more than one shot. Here a sample with two shots." 214 | ] 215 | }, 216 | { 217 | "cell_type": "code", 218 | "execution_count": null, 219 | "id": "8ce600f7-f92e-4cf7-be4a-408f12eb39d6", 220 | "metadata": { 221 | "colab": { 222 | "base_uri": "https://localhost:8080/" 223 | }, 224 | "id": "8ce600f7-f92e-4cf7-be4a-408f12eb39d6", 225 | "outputId": "de30bb15-f660-448b-96d4-556879da5d50" 226 | }, 227 | "outputs": [], 228 | "source": [ 229 | "#Few shots\n", 230 | "context_user = [\n", 231 | " {'role':'system', 'content':\n", 232 | " \"\"\"You are an expert in F1.\n", 233 | "\n", 234 | " Who won the 2010 f1 championship?\n", 235 | " Driver: Sebastian Vettel.\n", 236 | " Team: Red Bull Renault.\n", 237 | "\n", 238 | " Who won the 2009 f1 championship?\n", 239 | " Driver: Jenson Button.\n", 240 | " Team: BrawnGP.\"\"\"}\n", 241 | "]\n", 242 | "print(return_OAIResponse(\"Who won the F1 2006?\", context_user))" 243 | ] 244 | }, 245 | { 246 | "cell_type": "markdown", 247 | "id": "5f1b71c4-6583-4dcb-b987-02abf6aa4a86", 248 | "metadata": { 249 | "id": "5f1b71c4-6583-4dcb-b987-02abf6aa4a86" 250 | }, 251 | "source": [ 252 | "We've been creating the prompt without using OpenAI's roles, and as we've seen, it worked correctly.\n", 253 | "\n", 254 | "However, the proper way to do this is by using these roles to construct the prompt, making the model's learning process even more effective.\n", 255 | "\n", 256 | "By not feeding it the entire prompt as if they were system commands, we enable the model to learn from a conversation, which is more realistic for it." 257 | ] 258 | }, 259 | { 260 | "cell_type": "code", 261 | "execution_count": null, 262 | "id": "20fa4a25-01a6-4f22-98db-ab7ccc9ba115", 263 | "metadata": { 264 | "colab": { 265 | "base_uri": "https://localhost:8080/" 266 | }, 267 | "id": "20fa4a25-01a6-4f22-98db-ab7ccc9ba115", 268 | "outputId": "fcf7518f-1c75-43d6-9257-89913eeb2bd1" 269 | }, 270 | "outputs": [], 271 | "source": [ 272 | "#Recomended solution\n", 273 | "context_user = [\n", 274 | " {'role':'system', 'content':'You are and expert in f1.\\n\\n'},\n", 275 | " {'role':'user', 'content':'Who won the 2010 f1 championship?'},\n", 276 | " {'role':'assistant', 'content':\"\"\"Driver: Sebastian Vettel. \\nTeam: Red Bull. \\nPoints: 256. \"\"\"},\n", 277 | " {'role':'user', 'content':'Who won the 2009 f1 championship?'},\n", 278 | " {'role':'assistant', 'content':\"\"\"Driver: Jenson Button. \\nTeam: BrawnGP. \\nPoints: 95. \"\"\"},\n", 279 | "]\n", 280 | "\n", 281 | "print(return_OAIResponse(\"Who won the F1 2019?\", context_user))" 282 | ] 283 | }, 284 | { 285 | "cell_type": "markdown", 286 | "id": "ac6f6b42-f351-496b-a7e8-1286426457eb", 287 | "metadata": { 288 | "id": "ac6f6b42-f351-496b-a7e8-1286426457eb" 289 | }, 290 | "source": [ 291 | "We could also address it by using a more conventional prompt, describing what we want and how we want the format.\n", 292 | "\n", 293 | "However, it's essential to understand that in this case, the model is following instructions, whereas in the case of use shots, it is learning in real-time during inference." 294 | ] 295 | }, 296 | { 297 | "cell_type": "code", 298 | "execution_count": null, 299 | "id": "36c32a32-c348-45b2-85ee-ab4500438c49", 300 | "metadata": { 301 | "colab": { 302 | "base_uri": "https://localhost:8080/" 303 | }, 304 | "id": "36c32a32-c348-45b2-85ee-ab4500438c49", 305 | "outputId": "c179cefd-4089-406d-d2fb-af81bb0113a0" 306 | }, 307 | "outputs": [], 308 | "source": [ 309 | "context_user = [\n", 310 | " {'role':'system', 'content':\"\"\"You are and expert in f1.\n", 311 | " You are going to answer the question of the user giving the name of the rider,\n", 312 | " the name of the team and the points of the champion, following the format:\n", 313 | " Drive:\n", 314 | " Team:\n", 315 | " Points: \"\"\"\n", 316 | " }\n", 317 | "]\n", 318 | "\n", 319 | "print(return_OAIResponse(\"Who won the F1 2019?\", context_user))" 320 | ] 321 | }, 322 | { 323 | "cell_type": "markdown", 324 | "id": "qZPNTLMPnkQ4", 325 | "metadata": { 326 | "id": "qZPNTLMPnkQ4" 327 | }, 328 | "source": [ 329 | "Few Shots for classification.\n" 330 | ] 331 | }, 332 | { 333 | "cell_type": "code", 334 | "execution_count": null, 335 | "id": "ejcstgTxnnX5", 336 | "metadata": { 337 | "colab": { 338 | "base_uri": "https://localhost:8080/" 339 | }, 340 | "id": "ejcstgTxnnX5", 341 | "outputId": "179ce58a-8592-40dd-8e4b-282d33be9489" 342 | }, 343 | "outputs": [], 344 | "source": [ 345 | "context_user = [\n", 346 | " {'role':'system', 'content':\n", 347 | " \"\"\"You are an expert in reviewing product opinions and classifying them as positive or negative.\n", 348 | "\n", 349 | " It fulfilled its function perfectly, I think the price is fair, I would buy it again.\n", 350 | " Sentiment: Positive\n", 351 | "\n", 352 | " It didn't work bad, but I wouldn't buy it again, maybe it's a bit expensive for what it does.\n", 353 | " Sentiment: Negative.\n", 354 | "\n", 355 | " I wouldn't know what to say, my son uses it, but he doesn't love it.\n", 356 | " Sentiment: Neutral\n", 357 | " \"\"\"}\n", 358 | "]\n", 359 | "print(return_OAIResponse(\"I'm not going to return it, but I don't plan to buy it again.\", context_user))" 360 | ] 361 | }, 362 | { 363 | "cell_type": "code", 364 | "execution_count": null, 365 | "id": "ZHr_75sDqDJp", 366 | "metadata": { 367 | "id": "ZHr_75sDqDJp" 368 | }, 369 | "outputs": [], 370 | "source": [] 371 | } 372 | ], 373 | "metadata": { 374 | "colab": { 375 | "include_colab_link": true, 376 | "provenance": [] 377 | }, 378 | "kernelspec": { 379 | "display_name": "Python 3 (ipykernel)", 380 | "language": "python", 381 | "name": "python3" 382 | }, 383 | "language_info": { 384 | "codemirror_mode": { 385 | "name": "ipython", 386 | "version": 3 387 | }, 388 | "file_extension": ".py", 389 | "mimetype": "text/x-python", 390 | "name": "python", 391 | "nbconvert_exporter": "python", 392 | "pygments_lexer": "ipython3", 393 | "version": "3.10.12" 394 | } 395 | }, 396 | "nbformat": 4, 397 | "nbformat_minor": 5 398 | } 399 | -------------------------------------------------------------------------------- /1-Introduction to LLMs with OpenAI/Prompt_Engineering_OpenAI.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "42abd0d1768faece", 6 | "metadata": { 7 | "colab_type": "text", 8 | "id": "view-in-github" 9 | }, 10 | "source": [ 11 | "\"Open" 12 | ] 13 | }, 14 | { 15 | "cell_type": "markdown", 16 | "id": "9c4700eb-00ef-433f-b574-2fb7f644226d", 17 | "metadata": { 18 | "collapsed": false 19 | }, 20 | "source": [ 21 | "
\n", 22 | "

Learn by Doing LLM Projects

\n", 23 | "

Understand And Apply Large Language Models

\n", 24 | "

Brief Introduction to Prompt Engineering with OpenAI

\n", 25 | " by Pere Martra\n", 26 | "
\n", 27 | "\n", 28 | "
\n", 29 | "\n", 30 | "
\n", 31 | "  \n", 32 | " \n", 33 | " \n", 34 | "
\n", 35 | "\n", 36 | "
\n", 37 | "
\n" 38 | ] 39 | }, 40 | { 41 | "cell_type": "markdown", 42 | "id": "34723a72-1601-4685-a0ba-bff544425d48", 43 | "metadata": { 44 | "id": "34723a72-1601-4685-a0ba-bff544425d48" 45 | }, 46 | "source": [ 47 | "In this notebook, we'll explore small prompt engineering techniques and recommendations that will help us elicit responses from the models that are better suited to our needs." 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": null, 53 | "id": "fe5f658a-708f-406c-a1cb-95e3482ae760", 54 | "metadata": { 55 | "colab": { 56 | "base_uri": "https://localhost:8080/" 57 | }, 58 | "id": "fe5f658a-708f-406c-a1cb-95e3482ae760", 59 | "outputId": "a44c5457-94f4-4357-c66b-8682253aee0a" 60 | }, 61 | "outputs": [], 62 | "source": [ 63 | "!pip install -q openai==1.1.1" 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": null, 69 | "id": "fba193cc-d8a0-4ad2-8177-380204426859", 70 | "metadata": { 71 | "id": "fba193cc-d8a0-4ad2-8177-380204426859" 72 | }, 73 | "outputs": [], 74 | "source": [ 75 | "# if you need an API Key from OpenAI\n", 76 | "#https://platform.openai.com/account/api-keys\n", 77 | "\n", 78 | "import openai\n", 79 | "\n", 80 | "openai.api_key=\"your-api-key\"" 81 | ] 82 | }, 83 | { 84 | "cell_type": "markdown", 85 | "id": "502cfc93-21e0-498f-9650-37bc6ddd514d", 86 | "metadata": { 87 | "id": "502cfc93-21e0-498f-9650-37bc6ddd514d" 88 | }, 89 | "source": [ 90 | "# Formatting the answer with Few Shot Samples.\n", 91 | "\n", 92 | "To obtain the model's response in a specific format, we have various options, but one of the most convenient is to use Few-Shot Samples. This involves presenting the model with pairs of user queries and example responses.\n", 93 | "\n", 94 | "Large models like GPT-3.5 respond well to the examples provided, adapting their response to the specified format.\n", 95 | "\n", 96 | "Depending on the number of examples given, this technique can be referred to as:\n", 97 | "* Zero-Shot.\n", 98 | "* One-Shot.\n", 99 | "* Few-Shots.\n", 100 | "\n", 101 | "With One Shot should be enough, and it is recommended to use a maximum of six shots. It's important to remember that this information is passed in each query and occupies space in the input prompt.\n", 102 | "\n" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": null, 108 | "id": "a8344712-06d7-4c24-83d8-f36d62926e5e", 109 | "metadata": { 110 | "id": "a8344712-06d7-4c24-83d8-f36d62926e5e" 111 | }, 112 | "outputs": [], 113 | "source": [ 114 | "# Function to call the model.\n", 115 | "def return_OAIResponse(user_message, context):\n", 116 | "\n", 117 | " newcontext = context.copy()\n", 118 | " newcontext.append({'role':'user', 'content':\"question: \" + user_message})\n", 119 | "\n", 120 | " response = openai.chat.completions.create(\n", 121 | " model=\"gpt-3.5-turbo\",\n", 122 | " messages=newcontext,\n", 123 | " temperature=1,\n", 124 | " )\n", 125 | "\n", 126 | " return (response.choices[0].message.content)" 127 | ] 128 | }, 129 | { 130 | "cell_type": "markdown", 131 | "id": "f611d73d-9330-466d-b705-543667e1b561", 132 | "metadata": { 133 | "id": "f611d73d-9330-466d-b705-543667e1b561" 134 | }, 135 | "source": [ 136 | "In this zero-shots prompt we obtain a correct response, but without formatting, as the model incorporates the information he wants." 137 | ] 138 | }, 139 | { 140 | "cell_type": "code", 141 | "execution_count": null, 142 | "id": "647790be-fdb8-4692-a82e-7e3a0220f72a", 143 | "metadata": { 144 | "colab": { 145 | "base_uri": "https://localhost:8080/" 146 | }, 147 | "id": "647790be-fdb8-4692-a82e-7e3a0220f72a", 148 | "outputId": "4c4a9f4f-67c9-4a11-837f-1a1fd6b516ff" 149 | }, 150 | "outputs": [], 151 | "source": [ 152 | "#zero-shot\n", 153 | "context_user = [\n", 154 | " {'role':'system', 'content':'You are an expert in F1.'}\n", 155 | "]\n", 156 | "print(return_OAIResponse(\"Who won the F1 2010?\", context_user))" 157 | ] 158 | }, 159 | { 160 | "cell_type": "markdown", 161 | "id": "e87a9a0a-c1b9-4759-b52f-f6547d29b4c8", 162 | "metadata": { 163 | "id": "e87a9a0a-c1b9-4759-b52f-f6547d29b4c8" 164 | }, 165 | "source": [ 166 | "For a model as large and good as GPT 3.5, a single shot is enough to learn the output format we expect.\n" 167 | ] 168 | }, 169 | { 170 | "cell_type": "code", 171 | "execution_count": null, 172 | "id": "33ac7693-6cf3-44f7-b2ff-55d8a36fe775", 173 | "metadata": { 174 | "colab": { 175 | "base_uri": "https://localhost:8080/" 176 | }, 177 | "id": "33ac7693-6cf3-44f7-b2ff-55d8a36fe775", 178 | "outputId": "5278df23-8797-4dc2-9340-ac29c1318a9c" 179 | }, 180 | "outputs": [], 181 | "source": [ 182 | "#one-shot\n", 183 | "context_user = [\n", 184 | " {'role':'system', 'content':\n", 185 | " \"\"\"You are an expert in F1.\n", 186 | "\n", 187 | " Who won the 2000 f1 championship?\n", 188 | " Driver: Michael Schumacher.\n", 189 | " Team: Ferrari.\"\"\"}\n", 190 | "]\n", 191 | "print(return_OAIResponse(\"Who won the F1 2011?\", context_user))" 192 | ] 193 | }, 194 | { 195 | "cell_type": "markdown", 196 | "id": "32c454a8-181b-482b-873a-81d6ffde4674", 197 | "metadata": { 198 | "id": "32c454a8-181b-482b-873a-81d6ffde4674" 199 | }, 200 | "source": [ 201 | "Smaller models, or more complicated formats, may require more than one shot. Here a sample with two shots." 202 | ] 203 | }, 204 | { 205 | "cell_type": "code", 206 | "execution_count": null, 207 | "id": "8ce600f7-f92e-4cf7-be4a-408f12eb39d6", 208 | "metadata": { 209 | "colab": { 210 | "base_uri": "https://localhost:8080/" 211 | }, 212 | "id": "8ce600f7-f92e-4cf7-be4a-408f12eb39d6", 213 | "outputId": "a6f90f5d-6d68-4b3d-ccb5-5848ae4e3e62" 214 | }, 215 | "outputs": [], 216 | "source": [ 217 | "#Few shots\n", 218 | "context_user = [\n", 219 | " {'role':'system', 'content':\n", 220 | " \"\"\"You are an expert in F1.\n", 221 | "\n", 222 | " Who won the 2010 f1 championship?\n", 223 | " Driver: Sebastian Vettel.\n", 224 | " Team: Red Bull Renault.\n", 225 | "\n", 226 | " Who won the 2009 f1 championship?\n", 227 | " Driver: Jenson Button.\n", 228 | " Team: BrawnGP.\"\"\"}\n", 229 | "]\n", 230 | "print(return_OAIResponse(\"Who won the F1 2006?\", context_user))" 231 | ] 232 | }, 233 | { 234 | "cell_type": "code", 235 | "execution_count": null, 236 | "id": "4b29898a-f715-46d4-b74b-9f95d3112d38", 237 | "metadata": { 238 | "colab": { 239 | "base_uri": "https://localhost:8080/" 240 | }, 241 | "id": "4b29898a-f715-46d4-b74b-9f95d3112d38", 242 | "outputId": "75f63fe3-0efc-45ed-dd45-71dbbb08d7a6" 243 | }, 244 | "outputs": [], 245 | "source": [ 246 | "print(return_OAIResponse(\"Who won the F1 2019?\", context_user))" 247 | ] 248 | }, 249 | { 250 | "cell_type": "markdown", 251 | "id": "5f1b71c4-6583-4dcb-b987-02abf6aa4a86", 252 | "metadata": { 253 | "id": "5f1b71c4-6583-4dcb-b987-02abf6aa4a86" 254 | }, 255 | "source": [ 256 | "We've been creating the prompt without using OpenAI's roles, and as we've seen, it worked correctly.\n", 257 | "\n", 258 | "However, the proper way to do this is by using these roles to construct the prompt, making the model's learning process even more effective.\n", 259 | "\n", 260 | "By not feeding it the entire prompt as if they were system commands, we enable the model to learn from a conversation, which is more realistic for it." 261 | ] 262 | }, 263 | { 264 | "cell_type": "code", 265 | "execution_count": null, 266 | "id": "20fa4a25-01a6-4f22-98db-ab7ccc9ba115", 267 | "metadata": { 268 | "colab": { 269 | "base_uri": "https://localhost:8080/" 270 | }, 271 | "id": "20fa4a25-01a6-4f22-98db-ab7ccc9ba115", 272 | "outputId": "868d2040-ca3c-4a47-a1e8-1e08d581191d" 273 | }, 274 | "outputs": [], 275 | "source": [ 276 | "#Recomended solution\n", 277 | "context_user = [\n", 278 | " {'role':'system', 'content':'You are and expert in f1.\\n\\n'},\n", 279 | " {'role':'user', 'content':'Who won the 2010 f1 championship?'},\n", 280 | " {'role':'assistant', 'content':\"\"\"Driver: Sebastian Vettel. \\nTeam: Red Bull. \\nPoints: 256. \"\"\"},\n", 281 | " {'role':'user', 'content':'Who won the 2009 f1 championship?'},\n", 282 | " {'role':'assistant', 'content':\"\"\"Driver: Jenson Button. \\nTeam: BrawnGP. \\nPoints: 95. \"\"\"},\n", 283 | "]\n", 284 | "\n", 285 | "print(return_OAIResponse(\"Who won the F1 2019?\", context_user))" 286 | ] 287 | }, 288 | { 289 | "cell_type": "markdown", 290 | "id": "ac6f6b42-f351-496b-a7e8-1286426457eb", 291 | "metadata": { 292 | "id": "ac6f6b42-f351-496b-a7e8-1286426457eb" 293 | }, 294 | "source": [ 295 | "We could also address it by using a more conventional prompt, describing what we want and how we want the format.\n", 296 | "\n", 297 | "However, it's essential to understand that in this case, the model is following instructions, whereas in the case of use shots, it is learning in real-time during inference." 298 | ] 299 | }, 300 | { 301 | "cell_type": "code", 302 | "execution_count": null, 303 | "id": "36c32a32-c348-45b2-85ee-ab4500438c49", 304 | "metadata": { 305 | "colab": { 306 | "base_uri": "https://localhost:8080/" 307 | }, 308 | "id": "36c32a32-c348-45b2-85ee-ab4500438c49", 309 | "outputId": "4c970dde-37ff-41a9-8d4e-37bb727f47a6" 310 | }, 311 | "outputs": [], 312 | "source": [ 313 | "context_user = [\n", 314 | " {'role':'system', 'content':\"\"\"You are and expert in f1.\n", 315 | " You are going to answer the question of the user giving the name of the rider,\n", 316 | " the name of the team and the points of the champion, following the format:\n", 317 | " Drive:\n", 318 | " Team:\n", 319 | " Points: \"\"\"\n", 320 | " }\n", 321 | "]\n", 322 | "\n", 323 | "print(return_OAIResponse(\"Who won the F1 2019?\", context_user))" 324 | ] 325 | }, 326 | { 327 | "cell_type": "code", 328 | "execution_count": null, 329 | "id": "KNDL1GzVngyL", 330 | "metadata": { 331 | "id": "KNDL1GzVngyL" 332 | }, 333 | "outputs": [], 334 | "source": [ 335 | "context_user = [\n", 336 | " {'role':'system', 'content':\n", 337 | " \"\"\"You are classifying .\n", 338 | "\n", 339 | " Who won the 2010 f1 championship?\n", 340 | " Driver: Sebastian Vettel.\n", 341 | " Team: Red Bull Renault.\n", 342 | "\n", 343 | " Who won the 2009 f1 championship?\n", 344 | " Driver: Jenson Button.\n", 345 | " Team: BrawnGP.\"\"\"}\n", 346 | "]\n", 347 | "print(return_OAIResponse(\"Who won the F1 2006?\", context_user))" 348 | ] 349 | }, 350 | { 351 | "cell_type": "markdown", 352 | "id": "qZPNTLMPnkQ4", 353 | "metadata": { 354 | "id": "qZPNTLMPnkQ4" 355 | }, 356 | "source": [ 357 | "Few Shots for classification.\n" 358 | ] 359 | }, 360 | { 361 | "cell_type": "code", 362 | "execution_count": null, 363 | "id": "ejcstgTxnnX5", 364 | "metadata": { 365 | "colab": { 366 | "base_uri": "https://localhost:8080/" 367 | }, 368 | "id": "ejcstgTxnnX5", 369 | "outputId": "4b91cc73-18f6-4944-a46b-806b02b7becb" 370 | }, 371 | "outputs": [], 372 | "source": [ 373 | "context_user = [\n", 374 | " {'role':'system', 'content':\n", 375 | " \"\"\"You are an expert in reviewing product opinions and classifying them as positive or negative.\n", 376 | "\n", 377 | " It fulfilled its function perfectly, I think the price is fair, I would buy it again.\n", 378 | " Sentiment: Positive\n", 379 | "\n", 380 | " It didn't work bad, but I wouldn't buy it again, maybe it's a bit expensive for what it does.\n", 381 | " Sentiment: Negative.\n", 382 | "\n", 383 | " I wouldn't know what to say, my son uses it, but he doesn't love it.\n", 384 | " Sentiment: Neutral\n", 385 | " \"\"\"}\n", 386 | "]\n", 387 | "print(return_OAIResponse(\"I'm not going to return it, but I don't plan to buy it again.\", context_user))" 388 | ] 389 | }, 390 | { 391 | "cell_type": "code", 392 | "execution_count": null, 393 | "id": "ZHr_75sDqDJp", 394 | "metadata": { 395 | "id": "ZHr_75sDqDJp" 396 | }, 397 | "outputs": [], 398 | "source": [] 399 | } 400 | ], 401 | "metadata": { 402 | "colab": { 403 | "include_colab_link": true, 404 | "provenance": [] 405 | }, 406 | "kernelspec": { 407 | "display_name": "Python 3 (ipykernel)", 408 | "language": "python", 409 | "name": "python3" 410 | }, 411 | "language_info": { 412 | "codemirror_mode": { 413 | "name": "ipython", 414 | "version": 3 415 | }, 416 | "file_extension": ".py", 417 | "mimetype": "text/x-python", 418 | "name": "python", 419 | "nbconvert_exporter": "python", 420 | "pygments_lexer": "ipython3", 421 | "version": "3.10.12" 422 | } 423 | }, 424 | "nbformat": 4, 425 | "nbformat_minor": 5 426 | } 427 | -------------------------------------------------------------------------------- /1-Introduction to LLMs with OpenAI/Vertical Chat.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "00aad948", 6 | "metadata": { 7 | "colab_type": "text", 8 | "id": "view-in-github" 9 | }, 10 | "source": [ 11 | "\"Open" 12 | ] 13 | }, 14 | { 15 | "cell_type": "markdown", 16 | "id": "zwzWskLgXgj5", 17 | "metadata": { 18 | "id": "zwzWskLgXgj5" 19 | }, 20 | "source": [ 21 | "
\n", 22 | "

Learn by Doing LLM Projects

\n", 23 | "

Understand And Apply Large Language Models

\n", 24 | "

Create your first ChatBot with OpenAI

\n", 25 | "

Using GPT 3.5, Python and Panel

\n", 26 | " by Pere Martra\n", 27 | "
\n", 28 | "\n", 29 | "
\n", 30 | "\n", 31 | "
\n", 32 | "  \n", 33 | " \n", 34 | " \n", 35 | "
\n", 36 | "\n", 37 | "
\n", 38 | "
" 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "id": "c23b4dd1", 44 | "metadata": { 45 | "id": "c23b4dd1" 46 | }, 47 | "source": [ 48 | "# Vertical Chat\n", 49 | "A sample how to build a chat for small businees using:\n", 50 | "\n", 51 | "* GPT 35\n", 52 | "* Panel\n", 53 | "* OpenAI\n", 54 | "\n", 55 | "\n", 56 | "This is just a simple sample to start to understand how the OpenAI API works, and how to create Prompts. It Is really far from beign a complete solution.\n", 57 | "We are going to introduce some interesting points:\n", 58 | "\n", 59 | "* The roles in a conversation.\n", 60 | "* How is the conversations’ memory preserved?\n", 61 | "\n", 62 | "Deeper explanations in the article: [Create Your First Chatbot Using GPT 3.5, OpenAI, Python and Panel.](https://medium.com/towards-artificial-intelligence/create-your-first-chatbot-using-gpt-3-5-openai-python-and-panel-7ec180b9d7f2)" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": null, 68 | "id": "a1d00720", 69 | "metadata": { 70 | "colab": { 71 | "base_uri": "https://localhost:8080/" 72 | }, 73 | "id": "a1d00720", 74 | "outputId": "507ec869-bd20-4837-e731-4188221494dd" 75 | }, 76 | "outputs": [], 77 | "source": [ 78 | "#First install the necessary libraries\n", 79 | "!pip install openai==1.1.1\n", 80 | "!pip install panel" 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": null, 86 | "id": "a03f026a", 87 | "metadata": { 88 | "id": "a03f026a" 89 | }, 90 | "outputs": [], 91 | "source": [ 92 | "#if you need an API Key from OpenAI\n", 93 | "#https://platform.openai.com/account/api-keys\n", 94 | "\n", 95 | "import openai\n", 96 | "import panel as pn\n", 97 | "#from mykeys import openai_api_key\n", 98 | "openai.api_key=\"your-api-key\"" 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": null, 104 | "id": "77eac86d", 105 | "metadata": { 106 | "id": "77eac86d" 107 | }, 108 | "outputs": [], 109 | "source": [ 110 | "def continue_conversation(messages, temperature=0):\n", 111 | " response = openai.chat.completions.create(\n", 112 | " model=\"gpt-3.5-turbo\",\n", 113 | " messages=messages,\n", 114 | " temperature=temperature,\n", 115 | " )\n", 116 | " #print(str(response.choices[0].message[\"content\"]))\n", 117 | " return response.choices[0].message.content" 118 | ] 119 | }, 120 | { 121 | "cell_type": "code", 122 | "execution_count": null, 123 | "id": "51bec475", 124 | "metadata": { 125 | "id": "51bec475" 126 | }, 127 | "outputs": [], 128 | "source": [ 129 | "def add_prompts_conversation(_):\n", 130 | " #Get the value introduced by the user\n", 131 | " prompt = client_prompt.value_input\n", 132 | " client_prompt.value = ''\n", 133 | "\n", 134 | " #Append to the context the User prompt.\n", 135 | " context.append({'role':'user', 'content':f\"{prompt}\"})\n", 136 | "\n", 137 | " #Get the response.\n", 138 | " response = continue_conversation(context)\n", 139 | "\n", 140 | " #Add the response to the context.\n", 141 | " context.append({'role':'assistant', 'content':f\"{response}\"})\n", 142 | "\n", 143 | " #Update the panels to show the conversation.\n", 144 | " panels.append(\n", 145 | " pn.Row('User:', pn.pane.Markdown(prompt, width=600)))\n", 146 | " panels.append(\n", 147 | " pn.Row('Assistant:', pn.pane.Markdown(response, width=600)))\n", 148 | "\n", 149 | " return pn.Column(*panels)" 150 | ] 151 | }, 152 | { 153 | "cell_type": "code", 154 | "execution_count": null, 155 | "id": "922f8d24", 156 | "metadata": { 157 | "colab": { 158 | "base_uri": "https://localhost:8080/", 159 | "height": 573 160 | }, 161 | "id": "922f8d24", 162 | "outputId": "93183987-3b29-4430-d8cc-6d6453a98574" 163 | }, 164 | "outputs": [], 165 | "source": [ 166 | "#Creating the prompt\n", 167 | "#read and understand it.\n", 168 | "context = [ {'role':'system', 'content':\"\"\"\n", 169 | "Act as an OrderBot, you work collecting orders in a delivery only fast food restaurant called\n", 170 | "My Dear Frankfurt. \\\n", 171 | "First welcome the customer, in a very friendly way, then collects the order. \\\n", 172 | "You wait to collect the entire order, beverages included \\\n", 173 | "then summarize it and check for a final \\\n", 174 | "time if everything is ok or the customer wants to add anything else. \\\n", 175 | "Finally you collect the payment.\\\n", 176 | "Make sure to clarify all options, extras and sizes to uniquely \\\n", 177 | "identify the item from the menu.\\\n", 178 | "You respond in a short, very friendly style. \\\n", 179 | "The menu includes \\\n", 180 | "burger 12.95, 10.00, 7.00 \\\n", 181 | "frankfurt 10.95, 9.25, 6.50 \\\n", 182 | "sandwich 11.95, 9.75, 6.75 \\\n", 183 | "fries 4.50, 3.50 \\\n", 184 | "salad 7.25 \\\n", 185 | "Toppings: \\\n", 186 | "extra cheese 2.00, \\\n", 187 | "mushrooms 1.50 \\\n", 188 | "martra sausage 3.00 \\\n", 189 | "canadian bacon 3.50 \\\n", 190 | "romesco sauce 1.50 \\\n", 191 | "peppers 1.00 \\\n", 192 | "Drinks: \\\n", 193 | "coke 3.00, 2.00, 1.00 \\\n", 194 | "sprite 3.00, 2.00, 1.00 \\\n", 195 | "vichy catalan 5.00 \\\n", 196 | "\"\"\"} ]\n", 197 | "\n", 198 | "#Creating the panel.\n", 199 | "pn.extension()\n", 200 | "\n", 201 | "panels = []\n", 202 | "\n", 203 | "client_prompt = pn.widgets.TextInput(value=\"Hi\", placeholder='Enter text here…')\n", 204 | "button_conversation = pn.widgets.Button(name=\"talk\")\n", 205 | "\n", 206 | "interactive_conversation = pn.bind(add_prompts_conversation, button_conversation)\n", 207 | "\n", 208 | "dashboard = pn.Column(\n", 209 | " client_prompt,\n", 210 | " pn.Row(button_conversation),\n", 211 | " pn.panel(interactive_conversation, loading_indicator=True),\n", 212 | ")\n", 213 | "\n", 214 | "dashboard" 215 | ] 216 | }, 217 | { 218 | "cell_type": "code", 219 | "execution_count": null, 220 | "id": "88db4691", 221 | "metadata": { 222 | "id": "88db4691" 223 | }, 224 | "outputs": [], 225 | "source": [] 226 | } 227 | ], 228 | "metadata": { 229 | "colab": { 230 | "include_colab_link": true, 231 | "provenance": [] 232 | }, 233 | "kernelspec": { 234 | "display_name": "Python 3 (ipykernel)", 235 | "language": "python", 236 | "name": "python3" 237 | }, 238 | "language_info": { 239 | "codemirror_mode": { 240 | "name": "ipython", 241 | "version": 3 242 | }, 243 | "file_extension": ".py", 244 | "mimetype": "text/x-python", 245 | "name": "python", 246 | "nbconvert_exporter": "python", 247 | "pygments_lexer": "ipython3", 248 | "version": "3.8.13" 249 | } 250 | }, 251 | "nbformat": 4, 252 | "nbformat_minor": 5 253 | } 254 | -------------------------------------------------------------------------------- /1-Introduction to LLMs with OpenAI/readme.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 8 | 17 | 18 |
4 | 5 | 6 | 7 | 9 |

10 | This is the unofficial repository for the book: 11 | Large Language Models: Apply and Implement Strategies for Large Language Models (Apress). 12 | The book is based on the content of this repository, but the notebooks are being updated, and I am incorporating new examples and chapters. 13 | If you are looking for the official repository for the book, with the original notebooks, you should visit the 14 | Apress repository, where you can find all the notebooks in their original format as they appear in the book. Buy it at: [Amazon] [Springer] 15 |

16 |
19 | 20 | # Introduction to LLMs with OpenAI API. 21 | 22 | In this first section of the course, **you will learn to work with the OpenAI API** by creating two small projects. You'll delve into OpenAI's roles and how to provide the necessary instructions to the model through the prompt to make it behave as desired. 23 | 24 | The first project is a restaurant chatbot where the model will take customer orders. Building upon this project, we will construct an SQL statement generator. Here, you'll attempt to create a secure prompt that only accepts SQL creation commands and nothing else. 25 | 26 | ### Create Your First Chatbot Using GPT 3.5, OpenAI, Python and Panel. 27 | | Description | Article | Notebook | 28 | | -------- | ---| --- | 29 | | We will be utilizing OpenAI GPT-3.5 and gpt-4o-mini to develop a straightforward Chatbot tailored for a fast food restaurant. During the article, we will explore the fundamentals of prompt engineering, including understanding the various OpenAI roles, manipulating temperature settings, and how to avoid Prompt Injections. | [article panel](https://medium.com/towards-artificial-intelligence/create-your-first-chatbot-using-gpt-3-5-openai-python-and-panel-7ec180b9d7f2)
-
[article gradio](https://ai.plainenglish.io/create-a-simple-chatbot-with-openai-and-gradio-202684d18f35?sk=e449515ec7a803ae828418011bbaca52)| [notebook panel](https://github.com/peremartra/Large-Language-Model-Notebooks-Course/blob/main/1-Introduction%20to%20LLMs%20with%20OpenAI/1_1-First_Chatbot_OpenAI.ipynb)
-
[notebook gradio](https://github.com/peremartra/Large-Language-Model-Notebooks-Course/blob/main/1-Introduction%20to%20LLMs%20with%20OpenAI/1_1-First_Chatbot_OpenAI_Gradio.ipynb) | 30 | 31 | ### How to Create a Natural Language to SQL Translator Using OpenAI API. 32 | | Description | Article | Notebook | 33 | | -------- | ---| --- | 34 | | Following the same framework utilized in the previous article to create the ChatBot, we made a few modifications to develop a Natural Language to SQL translator. In this case, the Model needs to be provided with the table structures, and adjustments were made to the prompt to ensure smooth functionality and avoid any potential malfunctions. With these modifications in place, the translator is capable of converting natural language queries into SQL queries. @fmquaglia has created a notebook using DBML to describe the tables that by far is a better aproach than the original.| [article](https://pub.towardsai.net/how-to-create-a-natural-language-to-sql-translator-using-openai-api-e1b1f72ac35a)
-
[article gradio](https://medium.com/towards-artificial-intelligence/first-nl2sql-chat-with-openai-and-gradio-b1de0d6541b4)| [notebook](https://github.com/peremartra/Large-Language-Model-Notebooks-Course/blob/main/1-Introduction%20to%20LLMs%20with%20OpenAI/1_2-Easy_NL2SQL.ipynb)
-
[notebok Gradio](https://github.com/peremartra/Large-Language-Model-Notebooks-Course/blob/main/1-Introduction%20to%20LLMs%20with%20OpenAI/1_2-Easy_NL2SQL_Gradio.ipynb)
-
[notebook DBML](https://github.com/peremartra/Large-Language-Model-Notebooks-Course/blob/main/1-Introduction%20to%20LLMs%20with%20OpenAI/1_2b-Easy_NL2SQL.ipynb)| 35 | 36 | ### Brief Introduction to Prompt Engineering with OpenAI. 37 | | Description | Article | Notebook | 38 | | -------- | ---| --- | 39 | |We will explore prompt engineering techniques to improve the results we obtain from Models. Like how to format the answer and obtain a structured response using Few Shot Samples.| [Article](https://medium.com/gitconnected/influencing-a-large-language-model-response-with-in-context-learning-b212f0eaa113) | [notebook](https://github.com/peremartra/Large-Language-Model-Notebooks-Course/blob/main/1-Introduction%20to%20LLMs%20with%20OpenAI/1_3-Intro_Prompt_Engineering.ipynb) 40 | -------------------------------------------------------------------------------- /2-Vector Databases with LLMs/2_2-ChromaDB Sever mode.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "7e4c9601-fcb8-49e6-ae48-4de8b6cac494", 6 | "metadata": { 7 | "id": "7e4c9601-fcb8-49e6-ae48-4de8b6cac494" 8 | }, 9 | "source": [ 10 | "
\n", 11 | "

Large Language Models Projects

\n", 12 | "

Apply and Implement Strategies for Large Language Models

\n", 13 | "

2_2-ChromaDB Server mode

\n", 14 | " \n", 15 | "
\n", 16 | "\n", 17 | "by [Pere Martra](https://www.linkedin.com/in/pere-martra/)\n", 18 | "_________\n", 19 | "This notebook is set up to run in a local environment. If executed in Google Colab, access to the ChromaDB server that will be created won't be possible.\n", 20 | "\n", 21 | "The code is the same as the one used in notebook 2_1_Vector_Databases_LLMs.ipynb.\n", 22 | "The client has been implemented in the notebook 2_3-ChromaDB Client.\n", 23 | "__________" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": null, 29 | "id": "fbdcd16f-1aca-442b-8d84-3fafdba82dd2", 30 | "metadata": { 31 | "id": "fbdcd16f-1aca-442b-8d84-3fafdba82dd2", 32 | "outputId": "15a5c4e0-ba39-41f0-f9fc-7245b1f98919" 33 | }, 34 | "outputs": [], 35 | "source": [ 36 | "!pip install chromadb==0.4.20" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": null, 42 | "id": "d6c765af-dc87-41e3-891d-a6e934a53259", 43 | "metadata": { 44 | "id": "d6c765af-dc87-41e3-891d-a6e934a53259" 45 | }, 46 | "outputs": [], 47 | "source": [ 48 | "import numpy as np\n", 49 | "import pandas as pd" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": null, 55 | "id": "8be6c63a-5077-4550-98f7-6e3e118872a5", 56 | "metadata": { 57 | "id": "8be6c63a-5077-4550-98f7-6e3e118872a5" 58 | }, 59 | "outputs": [], 60 | "source": [ 61 | "#download and unzip the dataset from kaggle:\n", 62 | "#https://www.kaggle.com/datasets/kotartemiy/topic-labeled-news-dataset\n", 63 | "\n", 64 | "#Pass the directory where the .csv file is stored to read_csv\n", 65 | "news = pd.read_csv('./kaggle/labelled_newscatcher_dataset.csv', sep=';')\n", 66 | "MAX_NEWS = 1000\n", 67 | "DOCUMENT=\"title\"\n", 68 | "TOPIC=\"topic\"" 69 | ] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": null, 74 | "id": "6a2fd5a3-29a4-4d5b-9527-fd431671ed93", 75 | "metadata": { 76 | "id": "6a2fd5a3-29a4-4d5b-9527-fd431671ed93" 77 | }, 78 | "outputs": [], 79 | "source": [ 80 | "#Because it is just a example we select a small portion of News.\n", 81 | "subset_news = news.head(MAX_NEWS)" 82 | ] 83 | }, 84 | { 85 | "cell_type": "code", 86 | "execution_count": null, 87 | "id": "5e7fc76a-c828-4db4-bada-b1e14c2eefea", 88 | "metadata": { 89 | "id": "5e7fc76a-c828-4db4-bada-b1e14c2eefea" 90 | }, 91 | "outputs": [], 92 | "source": [ 93 | "import chromadb" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": null, 99 | "id": "04b25934-62bd-4720-871a-7e516807c3e7", 100 | "metadata": { 101 | "id": "04b25934-62bd-4720-871a-7e516807c3e7" 102 | }, 103 | "outputs": [], 104 | "source": [ 105 | "chroma_client = chromadb.PersistentClient(path=\"./chromadb\")" 106 | ] 107 | }, 108 | { 109 | "cell_type": "code", 110 | "execution_count": null, 111 | "id": "06549d87-b66f-44b7-a1dc-b6c122685602", 112 | "metadata": { 113 | "id": "06549d87-b66f-44b7-a1dc-b6c122685602" 114 | }, 115 | "outputs": [], 116 | "source": [ 117 | "collection_name = \"local_news_collection\"\n", 118 | "if len(chroma_client.list_collections()) > 0 and collection_name in [chroma_client.list_collections()[0].name]:\n", 119 | " chroma_client.delete_collection(name=collection_name)\n", 120 | "\n", 121 | "collection = chroma_client.create_collection(name=collection_name)" 122 | ] 123 | }, 124 | { 125 | "cell_type": "code", 126 | "execution_count": null, 127 | "id": "f8b6d5c4-0f6c-4bfd-9bab-6f0df0fa2469", 128 | "metadata": { 129 | "id": "f8b6d5c4-0f6c-4bfd-9bab-6f0df0fa2469", 130 | "outputId": "a281dc8b-4d31-4ecf-a50a-95cec127d7d6" 131 | }, 132 | "outputs": [], 133 | "source": [ 134 | "collection.add(\n", 135 | " documents=subset_news[DOCUMENT].tolist(),\n", 136 | " metadatas=[{TOPIC: topic} for topic in subset_news[TOPIC].tolist()],\n", 137 | " ids=[f\"id{x}\" for x in range(MAX_NEWS)],\n", 138 | ")" 139 | ] 140 | }, 141 | { 142 | "cell_type": "code", 143 | "execution_count": null, 144 | "id": "9c9211e7-bd14-4cd0-b617-6b520de23e1b", 145 | "metadata": { 146 | "id": "9c9211e7-bd14-4cd0-b617-6b520de23e1b", 147 | "outputId": "2358d16f-6269-4aa9-de1a-5f6f4c15134d" 148 | }, 149 | "outputs": [], 150 | "source": [ 151 | "results = collection.query(query_texts=[\"laptop\"], n_results=10 )\n", 152 | "\n", 153 | "print(results)" 154 | ] 155 | }, 156 | { 157 | "cell_type": "code", 158 | "execution_count": null, 159 | "id": "65a107f7-efbb-4a4a-8fe4-4137f00d925f", 160 | "metadata": { 161 | "id": "65a107f7-efbb-4a4a-8fe4-4137f00d925f", 162 | "outputId": "fdc6fa12-0122-45a1-e3d6-07c23e8f994d" 163 | }, 164 | "outputs": [], 165 | "source": [ 166 | "#Running Chroma in Server Mode\n", 167 | "!chroma run --path ./chromadb" 168 | ] 169 | }, 170 | { 171 | "cell_type": "markdown", 172 | "id": "bcb943bf-a5b2-4bb3-a265-72e2633ab98a", 173 | "metadata": { 174 | "id": "bcb943bf-a5b2-4bb3-a265-72e2633ab98a" 175 | }, 176 | "source": [ 177 | "You have the code to test this server in the notebook 2_3-ChromaDB Client.ipynb" 178 | ] 179 | }, 180 | { 181 | "cell_type": "code", 182 | "execution_count": null, 183 | "id": "a9428dd9-39dd-4d96-9bdb-05b6c00cfe67", 184 | "metadata": { 185 | "id": "a9428dd9-39dd-4d96-9bdb-05b6c00cfe67" 186 | }, 187 | "outputs": [], 188 | "source": [] 189 | } 190 | ], 191 | "metadata": { 192 | "colab": { 193 | "provenance": [] 194 | }, 195 | "kernelspec": { 196 | "display_name": "Python 3 (ipykernel)", 197 | "language": "python", 198 | "name": "python3" 199 | }, 200 | "language_info": { 201 | "codemirror_mode": { 202 | "name": "ipython", 203 | "version": 3 204 | }, 205 | "file_extension": ".py", 206 | "mimetype": "text/x-python", 207 | "name": "python", 208 | "nbconvert_exporter": "python", 209 | "pygments_lexer": "ipython3", 210 | "version": "3.10.12" 211 | } 212 | }, 213 | "nbformat": 4, 214 | "nbformat_minor": 5 215 | } 216 | -------------------------------------------------------------------------------- /2-Vector Databases with LLMs/2_3-ChromaDB Client.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "fbba28bc-1f60-41fb-81dc-1b58cda63649", 6 | "metadata": { 7 | "id": "fbba28bc-1f60-41fb-81dc-1b58cda63649" 8 | }, 9 | "source": [ 10 | "
\n", 11 | "

Large Language Models Projects

\n", 12 | "

Apply and Implement Strategies for Large Language Models

\n", 13 | "

2_3-ChromaDB Client

\n", 14 | "
\n", 15 | "\n", 16 | "by [Pere Martra](https://www.linkedin.com/in/pere-martra/)\n", 17 | "\n", 18 | "This notebook accesses the Chroma server that has been previously created with the notebook 2_2-ChromaDB Server mode.\n", 19 | "_________" 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": null, 25 | "id": "599371fc-8b8b-468a-9347-b393f68ae88a", 26 | "metadata": { 27 | "id": "599371fc-8b8b-468a-9347-b393f68ae88a" 28 | }, 29 | "outputs": [], 30 | "source": [ 31 | "import chromadb" 32 | ] 33 | }, 34 | { 35 | "cell_type": "code", 36 | "execution_count": null, 37 | "id": "c9b47aca-86b3-4978-8adb-e0e0152423ef", 38 | "metadata": { 39 | "id": "c9b47aca-86b3-4978-8adb-e0e0152423ef" 40 | }, 41 | "outputs": [], 42 | "source": [ 43 | "client = chromadb.HttpClient(host='localhost', port=8000)" 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": null, 49 | "id": "99e6316b-1fff-4aa7-a393-10da2f58707e", 50 | "metadata": { 51 | "id": "99e6316b-1fff-4aa7-a393-10da2f58707e" 52 | }, 53 | "outputs": [], 54 | "source": [ 55 | "collection_local = client.get_collection(name=\"local_news_collection\")\n", 56 | "results = collection_local.query(query_texts=[\"laptop\"], n_results=10 )" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": null, 62 | "id": "3432d427-2236-4300-bad7-676b60245830", 63 | "metadata": { 64 | "id": "3432d427-2236-4300-bad7-676b60245830", 65 | "outputId": "1e584ad1-2bce-4045-dc05-12e85669c1d9" 66 | }, 67 | "outputs": [], 68 | "source": [ 69 | "print (results)" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": null, 75 | "id": "91653be5-7c1e-4f9f-84ed-d76ec573f465", 76 | "metadata": { 77 | "id": "91653be5-7c1e-4f9f-84ed-d76ec573f465" 78 | }, 79 | "outputs": [], 80 | "source": [] 81 | } 82 | ], 83 | "metadata": { 84 | "colab": { 85 | "provenance": [] 86 | }, 87 | "kernelspec": { 88 | "display_name": "Python 3 (ipykernel)", 89 | "language": "python", 90 | "name": "python3" 91 | }, 92 | "language_info": { 93 | "codemirror_mode": { 94 | "name": "ipython", 95 | "version": 3 96 | }, 97 | "file_extension": ".py", 98 | "mimetype": "text/x-python", 99 | "name": "python", 100 | "nbconvert_exporter": "python", 101 | "pygments_lexer": "ipython3", 102 | "version": "3.10.12" 103 | } 104 | }, 105 | "nbformat": 4, 106 | "nbformat_minor": 5 107 | } 108 | -------------------------------------------------------------------------------- /2-Vector Databases with LLMs/readme.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 8 | 17 | 18 |
4 | 5 | 6 | 7 | 9 |

10 | This is the unofficial repository for the book: 11 | Large Language Models: Apply and Implement Strategies for Large Language Models (Apress). 12 | The book is based on the content of this repository, but the notebooks are being updated, and I am incorporating new examples and chapters. 13 | If you are looking for the official repository for the book, with the original notebooks, you should visit the 14 | Apress repository, where you can find all the notebooks in their original format as they appear in the book. Buy it at: [Amazon] [Springer] 15 |

16 |
19 | 20 | A brief introduction to Vector Databases, a technology that will accompany us in many lessons throughout the course. We will work on an example of Retrieval Augmented Generation using information from various news datasets stored in ChromaDB. 21 | 22 | ### Influencing Language Models with Personalized Information using a Vector Database. 23 | | Description | Article | Notebook | 24 | | -------- | --- | ---| 25 | | If there's one aspect gaining importance in the world of large language models, it's exploring how to leverage proprietary information with them. In this lesson, we explore a possible solution that involves storing information in a vector database, ChromaDB in our case, and using it to create enriched prompts. | [Article](https://pub.towardsai.net/harness-the-power-of-vector-databases-influencing-language-models-with-personalized-information-ab2f995f09ba?sk=ea2c5286fbff8430e5128b0c3588dbab) | [Notebook](https://github.com/peremartra/Large-Language-Model-Notebooks-Course/blob/main/2-Vector%20Databases%20with%20LLMs/2_1_Vector_Databases_LLMs.ipynb)| 26 | 27 | ### Creating a ChromaDB server. 28 | | Description | Server | Client | 29 | | ------- | --- | --- | 30 | |In these two notebooks, a ChromaDB server is created from which information is served to a second notebook that contains the client. It is a small example of how to set up a local ChromaDB server that can be used by multiple clients. | [ChromaDB Server](https://github.com/peremartra/Large-Language-Model-Notebooks-Course/blob/main/2-Vector%20Databases%20with%20LLMs/2_2-ChromaDB%20Sever%20mode.ipynb) | [ChromaDB Client](https://github.com/peremartra/Large-Language-Model-Notebooks-Course/blob/main/2-Vector%20Databases%20with%20LLMs/2_3-ChromaDB%20Client.ipynb) | 31 | 32 | ### Semantic Cache for RAG systems ### 33 | | Description | Article | Notebook | 34 | | -------- | --- | ---| 35 | | We enhanced the RAG system by introducing a semantic cache layer capable of determining if a similar question has been asked before. If affirmative, it retrieves information from a cache system created with Faiss instead of accessing the Vector Database. The inspiration and base code of the semantic cache present in this notebook exist thanks to the course: https://maven.com/boring-bot/advanced-llm/1/home from Hamza Farooq.| WIP | [Notebook](https://github.com/peremartra/Large-Language-Model-Notebooks-Course/blob/main/2-Vector%20Databases%20with%20LLMs/semantic_cache_chroma_vector_database.ipynb) | 36 | -------------------------------------------------------------------------------- /3-LangChain/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peremartra/Large-Language-Model-Notebooks-Course/4c655e82cf149bfd302a0e8f32366bda4595eec4/3-LangChain/.DS_Store -------------------------------------------------------------------------------- /3-LangChain/readme.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 8 | 17 | 18 |
4 | 5 | 6 | 7 | 9 |

10 | This is the unofficial repository for the book: 11 | Large Language Models: Apply and Implement Strategies for Large Language Models (Apress). 12 | The book is based on the content of this repository, but the notebooks are being updated, and I am incorporating new examples and chapters. 13 | If you are looking for the official repository for the book, with the original notebooks, you should visit the 14 | Apress repository, where you can find all the notebooks in their original format as they appear in the book. Buy it at: [Amazon] [Springer] 15 |

16 |
19 | 20 | LangChain has been one of the libraries in the universe of large language models that has contributed the most to this revolution. 21 | It allows us to chain calls to Models and other systems, allowing us to build applications based on large language models. In the course, we will use it several times, creating increasingly complex projects. 22 | 23 | ### Retrieval Augmented Generation (RAG). Use the Data from your DataFrames with LLMs. 24 | In this lesson, we used LangChain to enhance the notebook from the previous lesson, where we used data from two datasets to create an enriched prompt. This time, with the help of LangChain, we built a pipeline that is responsible for retrieving data from the vector database and passing it to the Language Model. The notebook is set up to work with two different datasets and two different models. One of the models is trained for Text Generation, while the other is trained for Text2Text Generation. 25 | | [Article](https://medium.com/towards-artificial-intelligence/query-your-dataframes-with-powerful-large-language-models-using-langchain-abe25782def5) | [Notebook](https://github.com/peremartra/Large-Language-Model-Notebooks-Course/blob/main/3-LangChain/3_1_RAG_langchain.ipynb) | 26 | | ------ | ------ | 27 | 28 | ### Create a Moderation system using LangChain. 29 | We will create a comment response system using a two-model pipeline built with LangChain. In this setup, the second model will be responsible for moderating the responses generated by the first model. 30 | 31 | One effective way to prevent our system from generating unwanted responses is by using a second model that has no direct interaction with users to handle response generation. 32 | 33 | This approach can reduce the risk of undesired responses generated by the first model in response to the user's entry. 34 | 35 | 36 | I will create separate notebooks for this task. One will involve models from OpenAI, and the others will utilize open-source models provided by Hugging Face. The results obtained in the three notebooks are very different. The system works much better with the OpenAI, and LLAMA2 models. 37 | | Article | Notebook | 38 | | --- | --- | 39 | | [OpenAI article](https://pub.towardsai.net/create-a-self-moderated-commentary-system-with-langchain-and-openai-406a51ce0c8d?sk=b4903b827e44642f7f7c311cebaef57f) | [OpenAI notebook](https://github.com/peremartra/Large-Language-Model-Notebooks-Course/blob/main/3-LangChain/3_2_OpenAI_Moderation_Chat.ipynb) | 40 | | [Llama2-7B Article](https://levelup.gitconnected.com/create-a-self-moderated-comment-system-with-llama-2-and-langchain-656f482a48be?sk=701ead7afb80e015ea4345943a1aeb1d) | [Llama2-7B Notebook](https://github.com/peremartra/Large-Language-Model-Notebooks-Course/blob/main/3-LangChain/3_2_LLAMA2_Moderation_Chat.ipynb) | 41 | | No Article | [GPT-J Notebook](https://github.com/peremartra/Large-Language-Model-Notebooks-Course/blob/main/3-LangChain/3_2_GPT_Moderation_System.ipynb) | 42 | 43 | ### Create a Data Analyst Assistant using a LLM Agent. 44 | Agents are one of the most powerful tools in the world of Large Language Models. The agent is capable of interpreting the user's request and using the tools and libraries at its disposal until it achieves the expected result. 45 | 46 | With LangChain Agents, we are going to create in just a few lines one of the simplest yet incredibly powerful agents. The agent will act as a Data Analyst Assistant and help us in analyzing data contained in any Excel file. It will be able to identify trends, use models, make forecasts. In summary, we are going to create a simple agent that we can use in our daily work to analyze our data. 47 | | [Article](https://pub.towardsai.net/create-your-own-data-analyst-assistant-with-langchain-agents-722f1cdcdd7e) | [Notebook](https://github.com/peremartra/Large-Language-Model-Notebooks-Course/blob/main/3-LangChain/3_3_Data_Analyst_Agent.ipynb) | 48 | | --- | --- | 49 | 50 | ### Create a Medical ChatBot with LangChain and ChromaDB. 51 | In this example, two technologies seen previously are combined: agents and vector databases. Medical information is stored in ChromaDB, and a LangChain Agent is created, which will fetch it only when necessary to create an enriched prompt that will be sent to the model to answer the user's question. 52 | 53 | In other words, a RAG system is created to assist a Medical ChatBot. 54 | 55 | **Attention!!! Use it only as an example. Nobody should take the boot's recommendations as those of a real doctor. I disclaim all responsibility for the use that may be given to the ChatBot. I have built it only as an example of different technologies.** 56 | | [Article](https://medium.com/towards-artificial-intelligence/how-to-create-a-medical-agent-rag-system-effd0aaacc91?sk=e348bb9e1b949ecc48b4dfeb96381f5a) | [Notebook](https://github.com/peremartra/Large-Language-Model-Notebooks-Course/blob/main/3-LangChain/3_4_Medical_Assistant_Agent.ipynb) / [Gradio Notebook](https://github.com/peremartra/Large-Language-Model-Notebooks-Course/blob/main/3-LangChain/3_4b_Medical_Assistant_Agent_Gradio.ipynb) 57 | | ------ | ------ | 58 | 59 | 60 | 61 | 62 | 63 | -------------------------------------------------------------------------------- /4-Evaluating LLMs/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peremartra/Large-Language-Model-Notebooks-Course/4c655e82cf149bfd302a0e8f32366bda4595eec4/4-Evaluating LLMs/.DS_Store -------------------------------------------------------------------------------- /4-Evaluating LLMs/bleu_evaluation.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "colab_type": "text", 7 | "id": "view-in-github" 8 | }, 9 | "source": [ 10 | "\"Open" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "metadata": { 16 | "id": "tcSolREOl8Gl" 17 | }, 18 | "source": [ 19 | "
\n", 20 | "

Learn by Doing LLM Projects

\n", 21 | "

Understand And Apply Large Language Models

\n", 22 | "

Evaluating translations with BLEU

\n", 23 | " by Pere Martra\n", 24 | "
\n", 25 | "\n", 26 | "
\n", 27 | "\n", 28 | "
\n", 29 | "  \n", 30 | " \n", 31 | " \n", 32 | "
\n", 33 | "
" 34 | ] 35 | }, 36 | { 37 | "cell_type": "markdown", 38 | "metadata": { 39 | "id": "hVAkT7wBZjhZ" 40 | }, 41 | "source": [ 42 | "In this notebook, we will use the BLEU metric to compare the quality of two different approaches for performing translations.\n", 43 | "\n", 44 | "As my primary language is Spanish, I will translate a few lines from the beginning of this chapter from English to Spanish. My translations will be taken as the reference translations. In other words, they will be used as the basis upon which the quality of the automatic translations will be determined.\n", 45 | "\n" 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": null, 51 | "metadata": { 52 | "id": "BDp4f1CQp3hS" 53 | }, 54 | "outputs": [], 55 | "source": [ 56 | "#Sentences to Translate.\n", 57 | "sentences = [\n", 58 | " \"In the previous chapters, you've mainly seen how to work with OpenAI models, and you've had a very practical introduction to Hugging Face's open-source models, the use of embeddings, vector databases, and agents.\",\n", 59 | " \"These have been very practical chapters in which I've tried to gradually introduce concepts that have allowed you, or at least I hope so, to scale up your knowledge and start creating projects using the current technology stack of large language models.\"\n", 60 | " ]" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": null, 66 | "metadata": { 67 | "id": "KwvAUtyGbNQ8" 68 | }, 69 | "outputs": [], 70 | "source": [ 71 | "#Spanish Translation References.\n", 72 | "reference_translations = [\n", 73 | " [\"En los capítulos anteriores has visto mayoritariamente como trabajar con los modelos de OpenAI, y has tenido una introducción muy práctica a los modelos Open Source de Hugging Face, al uso de embeddings, las bases de datos vectoriales, los agentes.\"],\n", 74 | " [\"Han sido capítulos muy prácticos en los que he intentado ir introduciendo conceptos que te han permitido, o eso espero, ir escalando en tus conocimientos y empezar a crear proyectos usando el stack tecnológico actual de los grandes modelos de lenguaje.\"]\n", 75 | " ]" 76 | ] 77 | }, 78 | { 79 | "cell_type": "markdown", 80 | "metadata": { 81 | "id": "VQ3Wm2Z9gCTf" 82 | }, 83 | "source": [ 84 | "We will perform the first translation using the NLLB model, a small model specialized in performing translations, which we will retrieve from Hugging Face." 85 | ] 86 | }, 87 | { 88 | "cell_type": "code", 89 | "execution_count": null, 90 | "metadata": { 91 | "id": "DTO1tNuMrPn-" 92 | }, 93 | "outputs": [], 94 | "source": [ 95 | "import transformers\n", 96 | "from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline\n", 97 | "model_id = \"facebook/nllb-200-distilled-600M\"\n", 98 | "tokenizer = AutoTokenizer.from_pretrained(model_id)\n", 99 | "model = AutoModelForSeq2SeqLM.from_pretrained(model_id)" 100 | ] 101 | }, 102 | { 103 | "cell_type": "markdown", 104 | "metadata": { 105 | "id": "nDlw6xChiHf4" 106 | }, 107 | "source": [ 108 | "When creating the pipeline, we pass the source language and the target language of the translation to it." 109 | ] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "execution_count": null, 114 | "metadata": { 115 | "colab": { 116 | "base_uri": "https://localhost:8080/" 117 | }, 118 | "id": "SEQT4ae9R2M5", 119 | "outputId": "8fbdadf1-6b60-402d-cf01-876bc32479dc" 120 | }, 121 | "outputs": [], 122 | "source": [ 123 | "translator = pipeline('translation', model=model, tokenizer=tokenizer,\n", 124 | " src_lang=\"eng_Latn\", tgt_lang=\"spa_Latn\")" 125 | ] 126 | }, 127 | { 128 | "cell_type": "code", 129 | "execution_count": null, 130 | "metadata": { 131 | "colab": { 132 | "base_uri": "https://localhost:8080/" 133 | }, 134 | "id": "2y7br7o0R3mS", 135 | "outputId": "a22b18b3-34fb-4d2c-a3a5-cc2cbfdd9aca" 136 | }, 137 | "outputs": [], 138 | "source": [ 139 | "translations_nllb = []\n", 140 | "\n", 141 | "for text in sentences:\n", 142 | " print (\"to translate: \" + text)\n", 143 | " translation = \"\"\n", 144 | " translation = translator(text)\n", 145 | "\n", 146 | " #Add the summary to summaries list\n", 147 | " translations_nllb += translation[0].values()" 148 | ] 149 | }, 150 | { 151 | "cell_type": "markdown", 152 | "metadata": { 153 | "id": "G4mfFiNSiZlN" 154 | }, 155 | "source": [ 156 | "Now we have the translations stored in the list 'translations_nllb'." 157 | ] 158 | }, 159 | { 160 | "cell_type": "code", 161 | "execution_count": null, 162 | "metadata": { 163 | "colab": { 164 | "base_uri": "https://localhost:8080/" 165 | }, 166 | "id": "vGS0JZ0GWMe-", 167 | "outputId": "6ba03781-ca31-4ffe-922c-b25547a22488" 168 | }, 169 | "outputs": [], 170 | "source": [ 171 | "translations_nllb" 172 | ] 173 | }, 174 | { 175 | "cell_type": "markdown", 176 | "metadata": { 177 | "id": "QmtO4nZemvRK" 178 | }, 179 | "source": [ 180 | "##Create Translations with Google Traslator.\n", 181 | "\n", 182 | "As a second source for translations, we will use the Google Translator API." 183 | ] 184 | }, 185 | { 186 | "cell_type": "code", 187 | "execution_count": null, 188 | "metadata": { 189 | "colab": { 190 | "base_uri": "https://localhost:8080/" 191 | }, 192 | "id": "XeeOjqI9Uovs", 193 | "outputId": "c050b36a-1d8d-47ee-9c7f-9e393e5179b6" 194 | }, 195 | "outputs": [], 196 | "source": [ 197 | "!pip install -q googletrans==3.1.0a0\n", 198 | "from googletrans import Translator" 199 | ] 200 | }, 201 | { 202 | "cell_type": "code", 203 | "execution_count": null, 204 | "metadata": { 205 | "id": "RNxy4ql9P-ow" 206 | }, 207 | "outputs": [], 208 | "source": [ 209 | "translator_google = Translator()" 210 | ] 211 | }, 212 | { 213 | "cell_type": "code", 214 | "execution_count": null, 215 | "metadata": { 216 | "colab": { 217 | "base_uri": "https://localhost:8080/" 218 | }, 219 | "id": "FAJvQQ_ZQd4i", 220 | "outputId": "de5f202f-787f-4971-e2d9-c9cd05cb3098" 221 | }, 222 | "outputs": [], 223 | "source": [ 224 | "translations_google = []\n", 225 | "\n", 226 | "for text in sentences:\n", 227 | " print (\"to translate: \" + text)\n", 228 | " translation = \"\"\n", 229 | " translation = translator_google.translate(text, dest=\"es\")\n", 230 | "\n", 231 | " #Add the summary to summaries list\n", 232 | " translations_google.append(translation.text)\n", 233 | " print (translation.text)" 234 | ] 235 | }, 236 | { 237 | "cell_type": "markdown", 238 | "metadata": { 239 | "id": "m0M4Z5cUqC5z" 240 | }, 241 | "source": [ 242 | "In this list, we have the translations created by Google." 243 | ] 244 | }, 245 | { 246 | "cell_type": "code", 247 | "execution_count": null, 248 | "metadata": { 249 | "colab": { 250 | "base_uri": "https://localhost:8080/" 251 | }, 252 | "id": "9EkycPnUUL0m", 253 | "outputId": "61bfa17f-111d-4076-9bee-b133546d37a1" 254 | }, 255 | "outputs": [], 256 | "source": [ 257 | "translations_google" 258 | ] 259 | }, 260 | { 261 | "cell_type": "markdown", 262 | "metadata": { 263 | "id": "jXuT5sl8ptCo" 264 | }, 265 | "source": [ 266 | "## Evaluate translations with BLEU\n", 267 | "\n", 268 | "We will use the BLEU implementation from the Evaluate library by Hugging Face." 269 | ] 270 | }, 271 | { 272 | "cell_type": "code", 273 | "execution_count": null, 274 | "metadata": { 275 | "id": "UYNwyeY-p5Ft" 276 | }, 277 | "outputs": [], 278 | "source": [ 279 | "pip install -q evaluate==0.4.1\n", 280 | "import evaluate\n", 281 | "bleu = evaluate.load('bleu')" 282 | ] 283 | }, 284 | { 285 | "cell_type": "code", 286 | "execution_count": null, 287 | "metadata": { 288 | "id": "lRAYkTNDp2qg" 289 | }, 290 | "outputs": [], 291 | "source": [ 292 | "results_nllb = bleu.compute(predictions=translations_nllb, references=reference_translations)\n" 293 | ] 294 | }, 295 | { 296 | "cell_type": "markdown", 297 | "metadata": { 298 | "id": "PSkAJAfWq0SS" 299 | }, 300 | "source": [ 301 | "To obtain the metrics, we pass the translated text and the reference text to the BLEU function.\n", 302 | "\n", 303 | "Note that the translated text is a list of translations:\n", 304 | "[\"Translation1\", \"Translation2\"]\n", 305 | "\n", 306 | "Whereas the reference texts are a list of lists of text. This allows for providing multiple references per translation:\n", 307 | "\n", 308 | "[[\"reference1 Translation1\", \"reference2 Translation1\"],\n", 309 | "[\"reference2 Translation2\", \"reference2 Translation2\"]]\n" 310 | ] 311 | }, 312 | { 313 | "cell_type": "code", 314 | "execution_count": null, 315 | "metadata": { 316 | "id": "qrvknChXUGPv" 317 | }, 318 | "outputs": [], 319 | "source": [ 320 | "results_google = bleu.compute(predictions=translations_google, references=reference_translations)" 321 | ] 322 | }, 323 | { 324 | "cell_type": "code", 325 | "execution_count": null, 326 | "metadata": { 327 | "colab": { 328 | "base_uri": "https://localhost:8080/" 329 | }, 330 | "id": "dop9U2ds2YKE", 331 | "outputId": "89225e04-1c28-4534-f367-caf373969a23" 332 | }, 333 | "outputs": [], 334 | "source": [ 335 | "print(results_nllb)" 336 | ] 337 | }, 338 | { 339 | "cell_type": "code", 340 | "execution_count": null, 341 | "metadata": { 342 | "colab": { 343 | "base_uri": "https://localhost:8080/" 344 | }, 345 | "id": "329dBCZxT0F4", 346 | "outputId": "f7812ae8-0a5f-4ae8-9df0-5f1f3cd7e094" 347 | }, 348 | "outputs": [], 349 | "source": [ 350 | "print(results_google)" 351 | ] 352 | }, 353 | { 354 | "cell_type": "markdown", 355 | "metadata": { 356 | "id": "lZgXkAq3r4mz" 357 | }, 358 | "source": [ 359 | "It appears that the translation performed by the Google API is significantly better than the one performed by the NLLB model." 360 | ] 361 | }, 362 | { 363 | "cell_type": "code", 364 | "execution_count": null, 365 | "metadata": { 366 | "id": "5fvxCA0rr54W" 367 | }, 368 | "outputs": [], 369 | "source": [] 370 | } 371 | ], 372 | "metadata": { 373 | "colab": { 374 | "authorship_tag": "ABX9TyO+p565bsEqwtEDLmIW6e/O", 375 | "include_colab_link": true, 376 | "provenance": [] 377 | }, 378 | "kernelspec": { 379 | "display_name": "Python 3", 380 | "name": "python3" 381 | }, 382 | "language_info": { 383 | "name": "python" 384 | } 385 | }, 386 | "nbformat": 4, 387 | "nbformat_minor": 0 388 | } 389 | -------------------------------------------------------------------------------- /4-Evaluating LLMs/readme.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 8 | 17 | 18 |
4 | 5 | 6 | 7 | 9 |

10 | This is the unofficial repository for the book: 11 | Large Language Models: Apply and Implement Strategies for Large Language Models (Apress). 12 | The book is based on the content of this repository, but the notebooks are being updated, and I am incorporating new examples and chapters. 13 | If you are looking for the official repository for the book, with the original notebooks, you should visit the 14 | Apress repository, where you can find all the notebooks in their original format as they appear in the book. Buy it at: [Amazon] [Springer] 15 |

16 |
19 | 20 | The metrics used to measure the performance of Large Language Models are quite different from the ones we've been using in more traditional models. We're shifting away from metrics like Accuracy, F1 score, or recall, and moving towards metrics like BLEU, ROUGE, or METEOR. 21 | 22 | However, the evaluation of applications with Large Language Models goes far beyond metrics. That's why we will analyze other techniques and tools, such as LangSmith or evaluation using other language models. 23 | 24 | ## Metrics Based in n-Grams. 25 | These metrics are tailored to the specific task assigned to the language model. 26 | 27 | In this section, we'll explore examples of several of these metrics and how to use them to determine whether one model is superior to another for a given task. We'll delve into practical scenarios where these metrics help us make informed decisions about the performance of different models. 28 | ### Evaluating Translations with BLEU. 29 | Bleu is one of the first Metrics stablished to evaluate the quality of translations. In the notebook we compare the quality of a translation made by google with other from an Open Source Model from Hugging Face. 30 | | Article WIP | [Notebook](https://github.com/peremartra/Large-Language-Model-Notebooks-Course/blob/main/4-Evaluating%20LLMs/4_1_bleu_evaluation.ipynb) | 31 | | ------ | ------ | 32 | 33 | ### Evaluating Summarization with ROUGE. 34 | We will explore the usage of the ROUGE metric to measure the quality of summaries generated by a language model. 35 | We are going to use two T5 models, one of them being the t5-Base model and the other a t5-base fine-tuned specifically designed for creating summaries. 36 | 37 | | [Article](https://medium.com/towards-artificial-intelligence/rouge-metrics-evaluating-summaries-in-large-language-models-d200ee7ca0e6) | [Notebook](https://github.com/peremartra/Large-Language-Model-Notebooks-Course/blob/main/4-Evaluating%20LLMs/4_1_rouge_evaluations.ipynb) 38 | | ------ | ------ | 39 | 40 | ## Monitoring, Testing and Evaluating LLMs with LangSmith. 41 | ### Monitor an Agent using LangSmith. 42 | In this initial example, you can observe how to use LangSmith to monitor the traffic between the various components that make up the Agent. The agent is a RAG system that utilizes a vectorial database to construct an enriched prompt and pass it to the model. LangSmith captures both the use of the Agent's tools and the decisions made by the model, providing information at all times about the sent/received data, consumed tokens, the duration of the query, and all of this in a truly user-friendly environment. 43 | | [Article](https://medium.com/towards-artificial-intelligence/tracing-a-llm-agent-with-langsmith-a81975634555) | [Notebook](https://github.com/peremartra/Large-Language-Model-Notebooks-Course/blob/main/4-Evaluating%20LLMs/4_2_tracing_medical_agent.ipynb) | 44 | | ------ | ------ | 45 | 46 | ### Evaluating the quality of summaries using Embedding distance with LangSmith. 47 | Previously in the notebook, Rouge Metrics: Evaluating Summaries, we learned how to use ROUGE to evaluate which summary best approximated the one created by a human. This time, we will use embedding distance and LangSmith to verify which model produces summaries more similar to the reference ones. 48 | | [Article](https://medium.com/towards-artificial-intelligence/evaluating-llm-summaries-using-embedding-distance-with-langsmith-5fb46fdae2a5?sk=24eb18ce187d28547cebd6fd3dd1ddad) | [Notebook](https://github.com/peremartra/Large-Language-Model-Notebooks-Course/blob/main/4-Evaluating%20LLMs/4_2_Evaluating_summaries_embeddings.ipynb) | 49 | | ------ | ------ | 50 | 51 | ## Evaluating Large Language Models with Large Language Models. 52 | In the course, you have already seen several examples of evaluation or moderation using Large Language Models. Now, you will see how a library that is based on evaluating using Large Language Models: giskard. 53 | 54 | ### Evaluating a RAG solution using Giskard. 55 | We take the agent that functions as a medical assistant and incorporate Giskard to evaluate if its responses are correct. In this way, not only the model's response is evaluated, but also the information retrieval in the vector database. Giskard is a solution that allows evaluating a complete RAG solution. 56 | | [Article](https://medium.com/towards-artificial-intelligence/evaluating-a-rag-solution-with-giskard-1bc138fa44af?sk=10811fe2953eb511fb1ffefda326f7a2) | [Notebook](https://github.com/peremartra/Large-Language-Model-Notebooks-Course/blob/main/4-Evaluating%20LLMs/4_3_evaluating_rag_giskard.ipynb) 57 | | ------ | ------ | 58 | 59 | ## Standard rankings. 60 | ### Introduction to the lm-evaluation library from Eluther.ai. 61 | The lm-eval library by EleutherAI provides easy access to academic benchmarks that have become industry standards. It supports the evaluation of both Open Source models and APIs from providers like OpenAI, and even allows for the evaluation of adapters created using techniques such as LoRA. 62 | 63 | In this notebook, I will focus on a small but important feature of the library: evaluating models compatible with Hugging Face's Transformers library. 64 | | Article - WIP| [Notebook](https://github.com/peremartra/Large-Language-Model-Notebooks-Course/blob/main/4-Evaluating%20LLMs/4_4_lm-evaluation-harness.ipynb) 65 | | ------ | ------ | 66 | 67 | 68 | -------------------------------------------------------------------------------- /5-Fine Tuning/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peremartra/Large-Language-Model-Notebooks-Course/4c655e82cf149bfd302a0e8f32366bda4595eec4/5-Fine Tuning/.DS_Store -------------------------------------------------------------------------------- /5-Fine Tuning/readme.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 8 | 17 | 18 |
4 | 5 | 6 | 7 | 9 |

10 | This is the unofficial repository for the book: 11 | Large Language Models: Apply and Implement Strategies for Large Language Models (Apress). 12 | The book is based on the content of this repository, but the notebooks are being updated, and I am incorporating new examples and chapters. 13 | If you are looking for the official repository for the book, with the original notebooks, you should visit the 14 | Apress repository, where you can find all the notebooks in their original format as they appear in the book. Buy it at: [Amazon] [Springer] 15 |

16 |
19 | 20 | In this section, we will explore different techniques such as Prompt Fine Tuning or LoRA, and we will use the Hugging Face PEFT library to efficiently fine-tune Large Language Models. 21 | Additionally, we will explore optimization techniques such as model quantization. 22 | 23 | ### Fine-Tuning with LoRA using PEFT from Hugging Face. 24 | After a brief explanation of how the fine-tuning technique LoRA works, we will fine-tune a model from the Bloom family to teach it to construct prompts that can be used to instruct large language models. 25 | | [Article](https://medium.com/gitconnected/efficient-fine-tuning-with-lora-optimal-training-for-large-language-models-266b63c973ca) | [Notebook](https://github.com/peremartra/Large-Language-Model-Notebooks-Course/blob/main/5-Fine%20Tuning/5_2_LoRA_Tuning.ipynb) | 26 | | --- | --- | 27 | 28 | ### Fine-Tuning a 7B Model in a single 16GB GPU using QLoRA. 29 | We are going to see a brief introduction to quantization, used to reduce the size of big Large Language Models. With quantization you can load big models reducing the memory resources needed. It also applies to the fine-tunig process, you can fine-tune the model in a single GPU without consuming all the resources. After the brief explanation we see an example about how is possible to fine-tune a Bloom 7B Model ina a t4 16GB GPU on Google Colab. 30 | | [Article](https://medium.com/towards-artificial-intelligence/qlora-training-a-large-language-model-on-a-16gb-gpu-00ea965667c1) | [Notebook](https://github.com/peremartra/Large-Language-Model-Notebooks-Course/blob/main/5-Fine%20Tuning/5_3_QLoRA_Tuning.ipynb) 31 | | --- | --- | 32 | 33 | ### Prompt tuning using PEFT Library from Hugging Face. 34 | In this notebook, two models are trained using Prompt Tuning from the PEFT library. This technique not only allows us to train by modifying the weights of very few parameters but also enables us to have different specialized models loaded in memory that use the same foundational model. 35 | 36 | Prompt tuning is an additive technique, and the weights of the pre-trained model are not modified. The weights that we modify in this case are those of virtual tokens that we add to the prompt. 37 | | [Article](https://medium.com/towards-artificial-intelligence/fine-tuning-models-using-prompt-tuning-with-hugging-faces-peft-library-998ae361ee27) | [Notebook](https://github.com/peremartra/Large-Language-Model-Notebooks-Course/blob/main/5-Fine%20Tuning/5_4_Prompt_Tuning.ipynb)| 38 | | --- | --- | 39 | 40 | # Papers used in the Chapter: 41 | [The Power of Scale for Parameter-Efficient Prompt Tuning](https://doi.org/10.48550/arXiv.2104.08691). Prompt tuning using PEFT Library from Hugging Face. 42 | 43 | [LoRA: Low-Rank Adaptation of Large Language Models](https://arxiv.org/abs/2106.09685). Fine-Tuning with LoRA using PEFT from Hugging Face. 44 | 45 | [QLoRA: Efficient Finetuning of Quantized LLMs](https://arxiv.org/abs/2305.14314). Fine Tuning & Optimizacion Lesson. QLoRA Fine-Tuning Sample. 46 | -------------------------------------------------------------------------------- /6-PRUNING/readme.md: -------------------------------------------------------------------------------- 1 | **disclaimer: The pruning section was created after the first edition of the book was published. They are not included in the book’s original content but are intended to supplement and expand on the topics covered.** 2 | 3 | 4 | 5 | 10 | 19 | 20 |
6 | 7 | 8 | 9 | 11 |

12 | This is the unofficial repository for the book: 13 | Large Language Models: Apply and Implement Strategies for Large Language Models (Apress). 14 | The book is based on the content of this repository, but the notebooks are being updated, and I am incorporating new examples and chapters. 15 | If you are looking for the official repository for the book, with the original notebooks, you should visit the 16 | Apress repository, where you can find all the notebooks in their original format as they appear in the book. Buy it at: [Amazon] [Springer] 17 |

18 |
21 | 22 | # Pruning Techniques for Large Language Models 23 | **This section is still under construction. The goal is to build a curriculum that will take us from the most simple pruning techniques to creating a model using the same techniques employed by leading companies in the field, such as Microsoft, Google, Nvidia, or OpenAI, to build their models.** 24 | 25 | 📢 A portion of the structured pruning work in this section was presented at the [STAC Summit 2025 in London](https://www.stacresearch.com/spring2025LON), an international event focused on AI in finance. The talk explored pruning strategies for LLMs models, with a focus on performance-efficiency trade-offs and carbon footprint reduction. 26 | 27 | Pruning is a crucial optimization technique in machine learning, particularly for large language models (LLMs). It involves reducing the size and complexity of a model by eliminating less important components—such as neurons, layers, or weights, while maintaining most of the model’s performance. Pruning helps to make models more efficient, reducing their computational and memory requirements, which is especially important when deploying models on resource-constrained environments like mobile devices or edge servers. 28 | 29 | One of the great advantages of pruning compared to other techniques like quantization is that when selecting parts of the model to remove, you can choose those that contribute less to the model’s output, depending on the intended use. 30 | 31 | ## Structured Width Pruning: Eliminating Less Important Neurons from Feedforward Layers. 32 | In this type of pruning, the neurons that contribute least to the model's output are identified and removed. It is crucial to know or have access to the model's structure, as the pruning process is not applied to all layers. Depending on the need, specific layers are selected for pruning. 33 | 34 | ### Prune a distilGPT2 model using l1 norm to determine less important neurons. 35 | In the first notebook, the pruning process will be applied to the feedforward layers of a distilGPT2 model. This means the model will have reduced weights in those specific layers. The neurons to prune are selected based on their importance scores, which we compute using the L1 norm of their weights. It is a simple aproach, for this first example, that can be used when you want to create a Pruned Model that mimics the Base model in all the areas. 36 | 37 | By altering the model's structure, a new configuration file must be created to ensure it works correctly with the `transformers` library. 38 | 39 | | [Notebook: Pruning a distilGPT2 model.](https://github.com/peremartra/Large-Language-Model-Notebooks-Course/blob/main/6-PRUNING/6_1_pruning_structured_l1_diltilgpt2.ipynb) | 40 | | --- | 41 | 42 | ### Prune a Llama3.2 model. 43 | In this first notebook, we attempt to replicate the pruning process used with the distilGPT2 model but applied to a Llama model. By not taking the model's characteristics into account, the pruning process results in a completely unusable model. This notebook serves as an exercise to understand how crucial it is to know the structure of the models that will undergo pruning. 44 | | [Notebook: Pruning a Llama3.2 model INCORRECT APROACH.](https://github.com/peremartra/Large-Language-Model-Notebooks-Course/blob/main/6-PRUNING/6_2_pruning_structured_llama3.2-1b_KO.ipynb) | 45 | | --- | 46 | 47 | 48 | The second notebook addresses the issues encountered when applying the same pruning process to the Llama model as was used for distilGPT2. 49 | 50 | The correct approach is to treat the MLP layers of the model as pairs rather than individual layers and to calculate neuron importance by considering both layers together. Additionally, we switch to using the maximum absolute weight to decide which neurons remain in the pruned layers. 51 | 52 | With these two simple changes, which adapt the pruning process to the structure of Llama models, we achieve a smaller model that retains a significant portion of the original model's knowledge. 53 | | [Pruning Llama3 Article](https://medium.com/towards-data-science/how-to-prune-llama-3-2-and-similar-large-language-models-cf18e9a2afb6?sk=af4c5e40e967437325050f019b3ae606) | [Notebook: Pruning a Llama3.2 model CORRECT APROACH.](https://github.com/peremartra/Large-Language-Model-Notebooks-Course/blob/main/6-PRUNING/6_3_pruning_structured_llama3.2-1b_OK.ipynb) | 54 | | --- | --- | 55 | 56 | ## Structured Depth Pruning. Eliminating complete blocks from large language models. 57 | ### Depth pruning in a Llama-3.2 model. 58 | In this notebook, we will look at an example of depth pruning, which involves removing entire layers from the model. 59 | The first thing to note is that removing entire layers from a transformer model usually has a significant impact on the model's performance. This is a much more drastic architectural change compared to the simple removal of neurons from the MLP layers, as seen in the previous example. 60 | | [Notebook: Depth pruning a Llama Model.](https://github.com/peremartra/Large-Language-Model-Notebooks-Course/blob/main/6-PRUNING/6_5_pruning_depth_st_llama3.2-1b_OK.ipynb) | 61 | | --- | 62 | 63 | ### Pruning Attention Layers. 64 | This notebook implements the ideas presented in the paper: [What Matters in Transformers? Not All Attention is Needed](https://arxiv.org/abs/2406.15786). 65 | 66 | In this notebook, the attention layers that contribute the least to the model are marked to be bypassed, improving inference efficiency and reducing the model's resource consumption. To identify the layers that contribute the least, a simple activation with a prompt is used, and the cosine similarity between the layer's input and output is measured. The smaller the difference, the less modification the layer introduces. 67 | 68 | The layer selection process implemented in the notebook is iterative. That is, the least contributing layer is selected, and the contribution of the remaining layers is recalculated using the same prompt. This process is repeated until the desired number of layers has been deactivated. 69 | 70 | Since this type of pruning does not alter the model's structure, it does not reduce the model's size. 71 | 72 | | Article: WIP. | [Notebook: Pruning Attention Layers.](https://github.com/peremartra/Large-Language-Model-Notebooks-Course/blob/main/6-PRUNING/6_6_pruning_attention_layers.ipynb) | 73 | | --- | --- | 74 | 75 | 76 | # Knowledge distillation. 77 | Knowledge Distillation involves training a smaller "student" model to mimic a larger, well-trained "teacher" model. The student learns not just from the correct labels but also from the probability distributions (soft targets) that the teacher model produces, effectively transferring the teacher's learned knowledge into a more compact form. 78 | 79 | When combined with Pruning, you first create a pruned version of your base model by removing less important connections. During this process, some knowledge is inevitably lost. To recover this lost knowledge, you can apply Knowledge Distillation using the original base model as the teacher and the pruned model as the student, helping to restore some of the lost performance. 80 | 81 | Both techniques address the same challenge: reducing model size and computational requirements while maintaining performance, making them crucial for deploying AI in resource-constrained environments like mobile devices. 82 | 83 | ## Recovering knwoledge from the base model using KD. 84 | In this notebook, we will use Knowledge distillation to recover some of the knowledge lost during the model pruning process. Llama-3.2-1B will be used as the Teacher model, and the 40% pruned version will be used as the Student model. We will specifically improve the performance on the Lambada benchmark. 85 | | [Notebook: Knowledge Distillation Llama 3.2.](https://github.com/peremartra/Large-Language-Model-Notebooks-Course/blob/main/6-PRUNING/7_1_knowledge_distillation_Llama.ipynb) | 86 | | --- | 87 | 88 | # Bias & Fairness in LLMs. 89 | This section introduces a preliminary line of work focused on detecting bias in LLMs by visualizing neural activations. While still in its early stages, these analyses pave the way for future fairness-aware pruning strategies, where structural pruning decisions also take into account the impact on different demographic or semantic groups. 90 | 91 | ### Visualizing Bias in State of the art Transformer Models. 92 | This notebook introduces techniques for visualizing neural activations in Transformer models, as a first step toward detecting and mitigating bias in language models. 93 | Techniques applied: 94 | * Dimensionality reduction with PCA 95 | * Visualization using heatmaps 96 | * Differential activation analysis between contrastive groups 97 | 98 | | [Article: From Biased to Balanced: Visualizing and Fixing Bias in Transformer Models](https://medium.com/data-science-collective/from-biased-to-balanced-visualizing-and-fixing-bias-in-transformer-models-d1a82f35393c?sk=abd12073ee311c3752da3219a5baf20f) | [Notebook: 8_1_transformer_activations_visualization.ipynb](https://github.com/peremartra/Large-Language-Model-Notebooks-Course/blob/main/6-PRUNING/8_1_transformer_activations_visualization.ipynb) | 99 | | --- | --- | 100 | 101 | Why is it in the Pruning section? 102 | While this work is thematically aligned with fairness and equity evaluation in models, it's temporarily placed here due to its direct connection with upcoming fairness-aware pruning strategies. These strategies explore activation patterns as a criterion for reducing parameters while preserving the representation of different groups. 103 | 104 | This notebook will serve as the foundation for the future section 8-Bias, which will include: 105 | * Tools for automated bias analysis 106 | * Fairness experiments in pruned models 107 | * Integration with the WizardSData library for contrastive dataset generation 108 | 109 | ### References 110 | > Saurav Muralidharan, Sharath Turuvekere Sreenivas, Raviraj Joshi, Marcin Chochowski, Mostofa Patwary, Mohammad Shoeybi, Bryan Catanzaro, Jan Kautz, Pavlo Molchanov, "Compact Language Models via Pruning and Knowledge Distillation," arXiv preprint arXiv:2407.14679, 2024. Available at: [https://doi.org/10.48550/arXiv.2407.14679](https://doi.org/10.48550/arXiv.2407.14679). 111 | 112 | > He, S., Sun, G., Shen, Z., & Li, A. (2024). What matters in transformers? not all attention is needed. arXiv preprint arXiv:2406.15786. https://doi.org/10.48550/arXiv.2406.15786 113 | 114 | 115 | > Kim, B. K., Kim, G., Kim, T. H., Castells, T., Choi, S., Shin, J., & Song, H. K. (2024). Shortened llama: A simple depth pruning for large language models. arXiv preprint arXiv:2402.02834, 11. https://doi.org/10.48550/arXiv.2402.02834 116 | 117 | > Martra, P. (2024, December 26). Exploring GLU Expansion Ratios: Structured Pruning in Llama-3.2 Models. https://doi.org/10.31219/osf.io/qgxea 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | -------------------------------------------------------------------------------- /Datasets/summaries_cnn.csv: -------------------------------------------------------------------------------- 1 | base,finetuned,reference 2 | "best died in hospice in Hickory, north Carolina, of complications from pneumonia. he played bumbling sheriff Rosco P. Coltrane on ""the Dukes of Hazzard"" he was born in Kentucky and raised in rural Indiana.","Jimmie Best was ""the most constantly creative person I have ever known,"" co-star John Schneider says. ""The Dukes of Hazzard"" ran until 1985 and spawned TV movies, an animated series and video games. ""Jimmie Best was my teacher, mentor, close friend and collaborator for 26 years,"" friend says.","James Best, who played the sheriff on ""The Dukes of Hazzard,"" died Monday at 88 . 3 | ""Hazzard"" ran from 1979 to 1985 and was among the most popular shows on TV ." 4 | """it doesn't matter what anyone says, he is presumed to be innocent,"" attorney says. cardiologist's lawyer says allegations against his client are ""completely unsubstantiated"" prosecutors say he pleaded not guilty to all charges. he faces charges in connection with a plot to take out a rival doctor.","Dr. Anthony Moschetto's attorney calls the allegations ""completely unsubstantiated"" He is accused of plotting to kill a rival doctor on Long Island.","A lawyer for Dr. Anthony Moschetto says the charges against him are baseless . 5 | Moschetto, 54, was arrested for selling drugs and weapons, prosecutors say . 6 | Authorities allege Moschetto hired accomplices to burn down the practice of former associate ." 7 | president Barack Obama took part in a roundtable discussion this week on climate change. he refocused on the issue from a public health vantage point. the average american can also do their part to reduce their own carbon footprint.,President Obama says climate change is a public health issue that affects all of us. He says the President wants the average American to take away the health impacts of climate change.,"""No challenge poses more of a public threat than climate change,"" the President says . 8 | He credits the Clean Air Act with making Americans ""a lot"" healthier ." 9 | independent channel in Russia aired Hillary Clinton's first campaign video with a rating stamp. the channel said it didn't want to break the country's anti-gay propaganda law. the video features a gay couple holding hands.,"A Russian TV channel aired Hillary Clinton's first campaign video with a rating stamp. The channel said it didn't want to break Russia's controversial anti-gay propaganda law. The law bans ""propaganda of nontraditional sexual relations around minors""","Presidential hopeful's video, featuring gay couple, gets mature rating in Russia . 10 | Russian TV channel feared airing it would break the country's anti-gay propaganda law . 11 | Clinton announced her support for same-sex marriage in 2013 ." 12 | sen. marco rubio is running for the 2016 presidential nomination. he has been his own worst enemy on immigration reform and Cuba relations. he has embraced a typical conservative approach to immigration.,Ruben Navarrette: Marco Rubio is missing a chance to set himself apart from GOP candidates. He says Rubio has been his own worst enemy on immigration and Cuba policy.,"Raul Reyes: In seeking Latino vote, Marco Rubio his own worst enemy on two key issues: immigration reform, Cuba relations . 13 | He says on health care, climate change and other issues, he breaks from Latinos' positions. Polls show they don't favor him ." 14 | """Orphan Black"" kicks off its second season on saturday. the series returns with a new subtitle, ""Washington's Spies"" and a new Monday night time slot.","The world of Westeros returns for a fifth season in one of the biggest season premieres of the year. ""Game of Thrones"" returns for a fifth season in one of the biggest premieres of the year.","Critically acclaimed series ""Orphan Black"" returns . 15 | ""Turn: Washington's Spies"" starts a second season . 16 | ""Game of Thrones"" is back for season five ." 17 | "ramp agent who fell asleep in plane's cargo hold is permanently banned from working on planes. ""we're going to come back around,"" passenger tells kcpq. ""all of a sudden we heard all this pounding underneath the plane,"" passenger says.","A ramp agent is permanently banned from working on an Alaska Airlines plane. ""I'm trapped in this plane and I called my job, but I'm in this plane,"" a passenger says.","The ramp agent fell asleep in the plane's cargo hold . 18 | He can no longer work on Alaska Airlines flights ." 19 | the Taliban released a biography of mullah Mohammed Omar. the reclusive militant is credited with founding the group in the early 1990s. he is credited with leading rebel groups against the communists.,Biography challenges rumors that Taliban leader Mohammed Omar is not alive. Omar studied at a religious school and was propelled into the arms of the Afghan opposition.,"Mullah Omar, the reclusive founder of the Afghan Taliban, is still in charge, a new biography claims . 20 | An ex-Taliban insider says there have been rumors that the one-eyed militant is dead ." 21 | "Michelle MacLaren is leaving the upcoming ""Wonder Woman"" movie. MacLaren was announced as director of the movie in November.","Michelle MacLaren was announced as director of the upcoming ""Wonder Woman"" The movie, starring Gal Gadot, is still set for release on June 23, 2017.","Michelle MacLaren is no longer set to direct the first ""Wonder Woman"" theatrical movie . 22 | MacLaren left the project over ""creative differences"" 23 | Movie is currently set for 2017 ." 24 | police say they knew a burglar alarm went off but didn't respond to the call. the robbery took place over the weekend in the heart of the jewelry district. a former police official has speculated that the loss could be in the hundreds of thousands of pounds.,A burglar alarm was activated at Hatton Garden Safe Deposit Ltd. on Easter Sunday. Numerous British news organizations put the value of the loss in the hundreds of thousands of pounds.,"British tabloid releases video it says shows the robbery being carried out . 25 | British police say they didn't respond to a burglar alarm in jewelry district . 26 | Police give no value of the amount taken in the heist in London's jewelry district ." 27 | -------------------------------------------------------------------------------- /E1-NL2SQL for big Databases/Select_hs_Tables.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "id": "ecff8312", 6 | "metadata": { 7 | "colab_type": "text", 8 | "id": "view-in-github" 9 | }, 10 | "source": [ 11 | "\"Open" 12 | ] 13 | }, 14 | { 15 | "cell_type": "markdown", 16 | "id": "c1c0362a-d133-4a1b-8161-75b4f53e6bea", 17 | "metadata": { 18 | "id": "c1c0362a-d133-4a1b-8161-75b4f53e6bea" 19 | }, 20 | "source": [ 21 | "
\n", 22 | "

Learn by Doing LLM Projects

\n", 23 | "

Understand And Apply Large Language Models

\n", 24 | "

Architecting a NL2SQL Solution for inmense Enterprise Databases

\n", 25 | "

Testig Selection Tables to Create SQL

\n", 26 | " by Pere Martra\n", 27 | "
\n", 28 | "\n", 29 | "
\n", 30 | "\n", 31 | "
\n", 32 | "  \n", 33 | " \n", 34 | " \n", 35 | "
\n", 36 | "\n", 37 | "
\n", 38 | "
" 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "id": "86bc2813-763c-49f2-9f5d-e1f87a20556f", 44 | "metadata": { 45 | "id": "86bc2813-763c-49f2-9f5d-e1f87a20556f" 46 | }, 47 | "source": [ 48 | "In This notebook we are going to test if using just the name of the table, and a shord definition of its contect we can use a Model like GTP3.5-Turbo to select which tables are necessary to create a SQL Order to answer the user petition." 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": null, 54 | "id": "b571dfeb-97c9-44a5-aa3b-fab008ac51aa", 55 | "metadata": { 56 | "id": "b571dfeb-97c9-44a5-aa3b-fab008ac51aa", 57 | "outputId": "67998bbd-c402-4444-91d9-75c5889e566b" 58 | }, 59 | "outputs": [], 60 | "source": [ 61 | "# Install OpenAI API.\n", 62 | "!pip install openai==1.1.1" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": null, 68 | "id": "7fbbdf5b-4d7f-4496-8f40-9d15ea46d023", 69 | "metadata": { 70 | "id": "7fbbdf5b-4d7f-4496-8f40-9d15ea46d023" 71 | }, 72 | "outputs": [], 73 | "source": [ 74 | "import openai\n", 75 | "openai.api_key='your-api-key'" 76 | ] 77 | }, 78 | { 79 | "cell_type": "code", 80 | "execution_count": null, 81 | "id": "e480bbfb-9a80-4ea6-b792-067e63ae3148", 82 | "metadata": { 83 | "id": "e480bbfb-9a80-4ea6-b792-067e63ae3148" 84 | }, 85 | "outputs": [], 86 | "source": [ 87 | "#Functio to call the model.\n", 88 | "def return_OAI(user_message):\n", 89 | " context = []\n", 90 | " context.append({'role':'system', \"content\": user_message})\n", 91 | "\n", 92 | " response = openai.chat.completions.create(\n", 93 | " model=\"gpt-3.5-turbo\",\n", 94 | " messages=context,\n", 95 | " temperature=0,\n", 96 | " )\n", 97 | "\n", 98 | " return (response.choices[0].message.content)" 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": null, 104 | "id": "6d5d2bdc-d822-4ed8-815e-b3f223730f15", 105 | "metadata": { 106 | "id": "6d5d2bdc-d822-4ed8-815e-b3f223730f15", 107 | "outputId": "61068bf0-41e3-40d9-b453-b76da5b0f086" 108 | }, 109 | "outputs": [], 110 | "source": [ 111 | "#Definition of the tables.\n", 112 | "import pandas as pd\n", 113 | "\n", 114 | "# Table and definitions sample\n", 115 | "data = {'table': ['employees', 'salary', 'studies'],\n", 116 | " 'definition': ['Employee information, name...',\n", 117 | " 'Salary details for each year',\n", 118 | " 'Educational studies, name of the institution, type of studies, level']}\n", 119 | "df = pd.DataFrame(data)\n", 120 | "print(df)" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": null, 126 | "id": "009e681c-d95d-4c9c-b044-5bfd76e3ad95", 127 | "metadata": { 128 | "id": "009e681c-d95d-4c9c-b044-5bfd76e3ad95" 129 | }, 130 | "outputs": [], 131 | "source": [ 132 | "text_tables = '\\n'.join([f\"{row['table']}: {row['definition']}\" for index, row in df.iterrows()])" 133 | ] 134 | }, 135 | { 136 | "cell_type": "code", 137 | "execution_count": null, 138 | "id": "fe03ac25-8d02-4cd1-99a1-be4220c6fd2d", 139 | "metadata": { 140 | "id": "fe03ac25-8d02-4cd1-99a1-be4220c6fd2d", 141 | "outputId": "c1f3aab1-5f26-48fe-fcf9-3780120f5aad" 142 | }, 143 | "outputs": [], 144 | "source": [ 145 | "print(text)" 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": null, 151 | "id": "c7e275ae-f20d-4134-b9b6-d8677dfb544c", 152 | "metadata": { 153 | "id": "c7e275ae-f20d-4134-b9b6-d8677dfb544c" 154 | }, 155 | "outputs": [], 156 | "source": [ 157 | "prompt_question_tables = \"\"\"\n", 158 | "Given the following tables and their content definitions,\n", 159 | "###Tables\n", 160 | "{tables}\n", 161 | "\n", 162 | "Tell me which tables would be necessary to query with SQL to address the user's question below.\n", 163 | "Return the table names in a json format.\n", 164 | "###User Questyion:\n", 165 | "{question}\n", 166 | "\"\"\"\n" 167 | ] 168 | }, 169 | { 170 | "cell_type": "code", 171 | "execution_count": null, 172 | "id": "b1cb5957-2df2-4e5e-9e6a-ace955c9817e", 173 | "metadata": { 174 | "id": "b1cb5957-2df2-4e5e-9e6a-ace955c9817e" 175 | }, 176 | "outputs": [], 177 | "source": [ 178 | "#Creating the prompt, with the user questions and the tables definitions.\n", 179 | "pqt1 = prompt_question_tables.format(tables=text_tables, question=\"Return The name of the best paid employee\")" 180 | ] 181 | }, 182 | { 183 | "cell_type": "code", 184 | "execution_count": null, 185 | "id": "10d30f2b-6c23-4fd6-8038-840fba784cce", 186 | "metadata": { 187 | "id": "10d30f2b-6c23-4fd6-8038-840fba784cce", 188 | "outputId": "9924022c-7b2b-4ec8-e2c2-75bc1c745151" 189 | }, 190 | "outputs": [], 191 | "source": [ 192 | "print(return_OAI(pqt1))" 193 | ] 194 | }, 195 | { 196 | "cell_type": "code", 197 | "execution_count": null, 198 | "id": "57e07083-be8f-4cd0-95bd-c4b909422c6b", 199 | "metadata": { 200 | "id": "57e07083-be8f-4cd0-95bd-c4b909422c6b" 201 | }, 202 | "outputs": [], 203 | "source": [ 204 | "pqt3 = prompt_question_tables.format(tables=text_tables,\n", 205 | " question=\"Return the Education Institution with a higher average salary\")" 206 | ] 207 | }, 208 | { 209 | "cell_type": "code", 210 | "execution_count": null, 211 | "id": "a0eeb79e-caf1-4f48-9897-168d95d2ae37", 212 | "metadata": { 213 | "id": "a0eeb79e-caf1-4f48-9897-168d95d2ae37", 214 | "outputId": "81d77115-9cad-4284-a228-5368bb9aa6fb" 215 | }, 216 | "outputs": [], 217 | "source": [ 218 | "print(return_OAI(pqt3))" 219 | ] 220 | }, 221 | { 222 | "cell_type": "markdown", 223 | "id": "321bb9a2-4937-4e9a-a31b-7049cb8f5aa3", 224 | "metadata": { 225 | "id": "321bb9a2-4937-4e9a-a31b-7049cb8f5aa3" 226 | }, 227 | "source": [ 228 | "# Conclusions\n", 229 | "It is clear that GPT-3.5-Turbo is a model entirely capable of deciding which tables should be used in creating an SQL query.\n", 230 | "\n", 231 | "In a more complex system, we may need to refine the definition and conduct several tests before finalizing them.\n" 232 | ] 233 | }, 234 | { 235 | "cell_type": "code", 236 | "execution_count": null, 237 | "id": "1269aaf2-e203-44da-b3cb-80bf538d67a3", 238 | "metadata": { 239 | "id": "1269aaf2-e203-44da-b3cb-80bf538d67a3" 240 | }, 241 | "outputs": [], 242 | "source": [] 243 | } 244 | ], 245 | "metadata": { 246 | "colab": { 247 | "include_colab_link": true, 248 | "provenance": [] 249 | }, 250 | "kernelspec": { 251 | "display_name": "Python 3 (ipykernel)", 252 | "language": "python", 253 | "name": "python3" 254 | }, 255 | "language_info": { 256 | "codemirror_mode": { 257 | "name": "ipython", 258 | "version": 3 259 | }, 260 | "file_extension": ".py", 261 | "mimetype": "text/x-python", 262 | "name": "python", 263 | "nbconvert_exporter": "python", 264 | "pygments_lexer": "ipython3", 265 | "version": "3.10.12" 266 | } 267 | }, 268 | "nbformat": 4, 269 | "nbformat_minor": 5 270 | } 271 | -------------------------------------------------------------------------------- /E2-Transforming Banks With Embeddings/Readme.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 8 | 17 | 18 |
4 | 5 | 6 | 7 | 9 |

10 | This is the unofficial repository for the book: 11 | Large Language Models: Apply and Implement Strategies for Large Language Models (Apress). 12 | The book is based on the content of this repository, but the notebooks are being updated, and I am incorporating new examples and chapters. 13 | If you are looking for the official repository for the book, with the original notebooks, you should visit the 14 | Apress repository, where you can find all the notebooks in their original format as they appear in the book. Buy it at: [Amazon] [Springer] 15 |

16 |
19 | 20 | # Decoding Risk: Transforming Banks with Customer Embeddings. 21 | 22 | If we were tasked with creating a system using LLM to enhance a bank's decision-making regarding risk assessment or product approval for its clients, the initial thought might be to provide the LLM with client and product information, enabling it to make decisions on whether to grant the product to the client or not. 23 | 24 | But let me tell you, this wouldn't work. We can't solve everything by providing information to an LLM and expecting it to generate an output based on the input data. 25 | 26 | What we're going to set up is a project that will integrate with the bank's other systems, aiming not only to enhance decision-making regarding risk but also to improve maintenance and streamline the overall development of the entity's information systems. 27 | 28 | We'll be altering the format in which we store customer information, and consequently, we'll also be changing how this information travels within the systems. 29 | 30 | *Before we proceed, **a disclaimer:** All diagrams and descriptions of the solutions presented here are simplified to the maximum extent. This is a project that will likely span years and requires an immense development team. Here, I'm only presenting the idea, with a brief outline of how it should be and the advantages it can bring.* 31 | 32 | # Actual Client Risk System. 33 | 34 | Let's start by looking at an example of what a current risk system for any institution might look like. 35 | 36 | image 37 | 38 | I'm going to detail the flow of a request: 39 | 1. A loan request is initiated from one of the channels. 40 | 2. The operation data + customer identifier is passed. 41 | 3. The risk application collects customer data. 42 | 4. The risk application collects product data. 43 | 5. With customer, product, and operation data, a decision is made. 44 | 6. The decision is returned to the channel that initiated the request. 45 | 46 | The process doesn't seem overly complicated, but let's delve a bit deeper. What are we talking about when we refer to user data? 47 | 48 | Is it a snapshot of the moment? In other words: age, gender, account balances, committed balances, available credit cards, relationships with other entities, investments, funds, pension plans, salary, and so forth? 49 | 50 | Well, now we have a better idea about the amount of data that must be taken into account, and we are talking about just a snapshot, the current moment. But wouldn't it be better if we made decisions based on how all these variables have been changing over time? This implies considering even more data. 51 | 52 | As you can imagine, obtaining all this data involves a very high number of calls to different systems within the bank. I've simplified it by referring to "user data," but that's not a singular entity. Behind that label, there's a myriad of applications, each with its own set of data that we need to request. 53 | 54 | In summary, calculating the customer's position is typically done in a batch process that updates every X days, depending on the bank. 55 | 56 | The decision of whether to grant a loan is made by an algorithm or a traditional machine learning model that takes specific product data and precalculated risk positions as input. 57 | 58 | # How can a Large Language Model (LLM) help us improve this process and, above all, simplify it? 59 | 60 | In this project, the key lies not in the deployment of LLMs but rather in crafting embeddings to encapsulate the entirety of customer information. This is a pivotal decision requiring meticulous justification. Let's delve into some advantages of employing embeddings. 61 | 62 | * **Improved Feature Representation**: Can effectively represent complex and unstructured data sources, such as text data from customer interactions, financial reports, and social media profiles. By transforming these raw texts into numerical vectors that capture their underlying semantic meaning, embeddings enable credit risk models to incorporate richer and more informative features, leading to more accurate risk assessments. 63 | * **Similarity Analysis**: Eenable measuring similarity between different entities. This can be useful in identifying similar customer profiles or similar transactions, helping to identify potential risks based on historical patterns. 64 | * **Enhanced Risk Identification**: Can identify hidden patterns and relationships within vast amounts of data, allowing credit risk models to uncover subtle signals that may not be apparent from traditional numerical data. This improved ability to identify and understand risk factors can lead to more precise risk assessments and better-informed lending decisions. 65 | * **Handling Missing Data**: Can handle missing data more gracefully than traditional methods. The model can learn meaningful representations even when certain features are missing, providing more robust credit risk assessments. 66 | * **Reduced Dimensionality**: Exhibit reduced dimensionality compared to the original data representations. This facilitates storage, transmission, and processing of information. 67 | * **Transferability Convenience**: Given that embeddings are numerical representations, they can be easily transferred across various systems and components within the framework. This enables consistency in the utilization of data representations across different segments of the infrastructure. 68 | 69 | These are just a few of the advantages that storing customer information in embeddings can provide. Let's say I am fully convinced, and possibly, even if we haven't entirely convinced the leadership, we have certainly piqued their curiosity to inquire about how the project should be approached. In other words, a preliminary outline of the solution. 70 | 71 | ## First picture of the solution. 72 | The first step is to select a model and train it with a portion of the data we want to store as embeddings. The model selection and training process are crucial. The entity has sufficient data to train a model from scratch using only its own data, without having to resort to a pre-trained model with financial data, which are not particularly abundant. 73 | 74 | The challenge we face is not the quantity of data but rather the selection of which data to use and determining the cost we are willing to assume for training our model. 75 | 76 | From now on, let's refer to this kind of model as FMFD (Foundational Model on Financial Data). And we need to train at least thre of them, one to create the embeddings of the client, the other with the data of the product, and one trained to return the credit default rate. 77 | 78 | The latter model introduces an interesting feature to the project. Classical models are designed to solve a problem by returning a single output. That is, a traditional machine learning model might provide a binary output indicating whether to approve or deny credit, while another could indicate the likelihood of credit default. **In contrast, when using an LLM (Multi-Output Language Model), the output can be multiple, and from a single call, we could obtain various pieces of information**. We might even obtain a list of recommended products, the maximum allowable credit, or an acceptable risk-associated interest rate. It all depends on the data used to train this model. 79 | 80 | image 81 | 82 | Starting from the point where we have already trained the three necessary models, we store the customer embeddings in an embeddings database. This could be a standard database or a vector database. For now, we won't delve into the advantages and disadvantages of using one type of database over another. 83 | 84 | The crucial point is that these embeddings can be continuously calculated as the customer's situation evolves, as it is a lightweight process that can be performed online. Although we also have the option of employing a batch process at regular intervals to handle multiple embeddings simultaneously and optimize resource utilization. 85 | 86 | Let's explore what happens when we receive a transaction that needs to be accepted or rejected using our system. With the transaction data, we generate embeddings for the product to be granted, employing the pre-trained model with product data. This yields specific embeddings for the transaction. 87 | 88 | To make the decision, we'll need the third model, to which we'll feed both embeddings – one for the customer and one for the transaction. There are several options for combining these embeddings: 89 | 90 | * Concatenate the two vectors. 91 | * Add them to a single vector, using, for example, vector summation. 92 | * Pass the data as two independent embeddings. 93 | * Pass the embeddings in a list. 94 | 95 | My preference is to pass them using a single concatenated vector. With this vector as input, the third model is capable of generating a response that, depending on the training performed, can provide more or less information. 96 | 97 | # Conclusion. 98 | 99 | With this solution, we have a system that utilizes nearly real-time information about the customer's position, along with all the historical data, to analyze whether they can obtain one of our financial products. We've increased the quality and quantity of information used for making decisions regarding our customers' risk operations. 100 | 101 | But that's not all; the use of embeddings also simplifies system maintenance and opens up a world of possibilities that are challenging to detail at this moment. We have a system that adapts better to changes in information because the entire system deals with embeddings of fixed length, as opposed to a multitude of fields that can vary. 102 | 103 | The compact size of the embeddings even allows them to reside on the client's device, enabling it to make decisions independently without the need to make a call to the bank's systems. 104 | 105 | ## Preparatory Steps when initiating the project. 106 | As one can imagine, we are not discussing a two-month project here. This initiative would redefine how a banking entity stores and processes information about its clients and products. It's a transformation that could span years, impacting a significant portion of its information systems department to varying degrees. 107 | 108 | A project of this magnitude necessitates a phased approach, allowing for pivots throughout its lifespan and a gradual introduction. 109 | 110 | My recommendation is start with a proof of concept, utilizing a single model but trained using QLoRA for the three identified tasks: Client Embeddings, Product Embeddings, and Decision Making. 111 | 112 | By adopting this approach, we can have three models operational swiftly, with minimal resource requirements for both training and inference. For each of these three models, decisions should be made regarding the data we want/need to use for their training, or in this case, fine-tuning. 113 | __________________________ 114 | Check the FinGPT Model, it can be a good option to use as our FMFD. https://arxiv.org/abs/2306.06031 115 | 116 | This solution has been inspired by this article [The Shaky Foundations of Foundation Models in Healthcare](https://hai.stanford.edu/news/shaky-foundations-foundation-models-healthcare) at Stanford University 117 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) [2023] [Pere Martra Manonelles] 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /LLM_course_diagram.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peremartra/Large-Language-Model-Notebooks-Course/4c655e82cf149bfd302a0e8f32366bda4595eec4/LLM_course_diagram.jpg -------------------------------------------------------------------------------- /P1-NL2SQL/6_2_Azure_NL2SQL_Client.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "colab_type": "text", 7 | "id": "view-in-github" 8 | }, 9 | "source": [ 10 | "\"Open" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "metadata": { 16 | "id": "Paa52inqag9w" 17 | }, 18 | "source": [ 19 | "
\n", 20 | "

Large Language Models Projects

\n", 21 | "

Apply and Implement Strategies for Large Language Models

\n", 22 | "

6.2-Calling Azure OpenAI Services from a Notebook.

\n", 23 | "

\n", 24 | "
\n", 25 | "\n", 26 | "by [Pere Martra](https://www.linkedin.com/in/pere-martra/)\n", 27 | "___________\n", 28 | "Models:\n", 29 | "\n", 30 | "Colab Environment: CPU\n", 31 | "\n", 32 | "Keys:\n", 33 | "* Azure\n", 34 | "* NL2SQL\n", 35 | "___________\n" 36 | ] 37 | }, 38 | { 39 | "cell_type": "markdown", 40 | "metadata": { 41 | "id": "uxFVaxlskUf-" 42 | }, 43 | "source": [ 44 | "In this notebook, we make a call to a GPT3.5 model that we've set up from Azure OpenAI Studio to function as a translator for SQL queries from natural language.\n", 45 | "\n", 46 | "The prompt structure has been created and explained in a [previous article](https://colab.research.google.com/drive/1mbIAv1p6nqtsngXfCG5WrozlvmoJI06F#scrollTo=uxFVaxlskUf-&line=3&uniqifier=1), part of the Large Language Models course." 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": null, 52 | "metadata": { 53 | "colab": { 54 | "base_uri": "https://localhost:8080/" 55 | }, 56 | "id": "Jssm3zDHbKq-", 57 | "outputId": "8bdf2324-ef7e-4355-fb9c-29eaa8139dbb" 58 | }, 59 | "outputs": [], 60 | "source": [ 61 | "#Install openai\n", 62 | "!pip install -q openai==1.30.1" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": null, 68 | "metadata": { 69 | "id": "ZQ48GZBFaaHg" 70 | }, 71 | "outputs": [], 72 | "source": [ 73 | "import os\n", 74 | "import openai" 75 | ] 76 | }, 77 | { 78 | "cell_type": "markdown", 79 | "metadata": { 80 | "id": "ZzKsM7pIEguo" 81 | }, 82 | "source": [ 83 | "\n", 84 | "You can Obtain the **prompt** the **api_base** and the **key** from the Sample Code window on Chat Playground in Azure Open Studio.\n", 85 | "\n", 86 | "![My Image](https://github.com/peremartra/Large-Language-Model-Notebooks-Course/blob/main/img/Martra_Figure_6-13.jpg?raw=true)\n" 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": null, 92 | "metadata": { 93 | "colab": { 94 | "base_uri": "https://localhost:8080/" 95 | }, 96 | "id": "_rWYcJ2gKEae", 97 | "outputId": "8031b629-a557-41dc-a7fc-876512221491" 98 | }, 99 | "outputs": [], 100 | "source": [ 101 | "from getpass import getpass\n", 102 | "os.environ[\"AZURE_OPENAI_KEY\"] = getpass(\"Azure AI key:\")" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": null, 108 | "metadata": { 109 | "id": "B2C_0LNjHFo3" 110 | }, 111 | "outputs": [], 112 | "source": [ 113 | "\n", 114 | "client = AzureOpenAI(\n", 115 | " azure_endpoint = \"https://largelanguagemodelsprojects.openai.azure.com/\",\n", 116 | " api_key=os.getenv(\"AZURE_OPENAI_KEY\"),\n", 117 | " api_version=\"2024-02-15-preview\"\n", 118 | ")\n" 119 | ] 120 | }, 121 | { 122 | "cell_type": "code", 123 | "execution_count": null, 124 | "metadata": { 125 | "id": "VQeyT_dCbGND" 126 | }, 127 | "outputs": [], 128 | "source": [ 129 | "context = [{\"role\":\"system\",\"content\":\"create table employees( \\n ID_Usr INT primary key,\\n name VARCHAR);\\n /*3 example rows\\n select * from employees limit 3;\\n ID_Usr name\\n 1344 George StPierre\\n 2122 Jon jones\\n 1265 Anderson Silva\\n */\\n\\n create table salary(\\n ID_Usr INT,\\n year DATE,\\n salary FLOAT,\\n foreign key (ID_Usr) references employees(ID_Usr));\\n /*3 example rows\\n select * from salary limit 3\\n ID_Usr date salary\\n 1344 01/01/2023 61000\\n 1344 01/01/2022 60000\\n 1265 01/01/2023 55000\\n */\\n\\n create table studies(\\n ID_study INT,\\n ID_Usr INT,\\n educational_level INT, /* 5=phd, 4=Master, 3=Bachelor */\\n Institution VARCHAR,\\n Years DATE,\\n Speciality VARCHAR,\\n primary key (ID_study, ID_Usr),\\n foreign key(ID_Usr) references employees (ID_Usr));\\n /*3 example rows\\n select * from studies limit 3\\n ID_Study ID_Usr educational_level Institution Years Speciality\\n 2782 1344 3 UC San Diego 01/01/2010 Bachelor of Science in Marketing\\n 2334 1344 5 MIT 01/01/2023 Phd. Data Science.\\n 2782 2122 3 UC San Diego 01/01/2010 Bachelor of Science in Marketing\\n */\\n-- Maintain the SQL order simple and efficient as you can, using valid SQL Lite, answer the following questions, returning only SQL code, for the table provided above.\"},{\"role\":\"user\",\"content\":\"How Many employes we have with a salary bigger than 50000?\"},{\"role\":\"assistant\",\"content\":\"SELECT COUNT(*) AS total_employees\\nFROM employees e\\nINNER JOIN salary s ON e.ID_Usr = s.ID_Usr\\nWHERE s.salary > 50000;\"},{\"role\":\"user\",\"content\":\"Return the names of the three people who have had the highest salary increase in the last three years.\"},{\"role\":\"assistant\",\"content\":\"SELECT e.name\\nFROM employees e\\nJOIN salary s ON e.ID_usr = s.ID_usr\\nWHERE s.year >= DATE_SUB(CURDATE(), INTERVAL 3 YEAR)\\nGROUP BY e.name\\nORDER BY (MAX(s.salary) - MIN(s.salary)) DESC\\nLIMIT 3;\"},{\"role\":\"user\",\"content\":\"Give me the name of the worst paid emploee\"},{\"role\":\"assistant\",\"content\":\"SELECT e.name\\nFROM employees e\\nJOIN salary s ON e.ID_usr = s.ID_usr\\nWHERE s.salary = (SELECT MIN(salary) FROM salary);\"}]" 130 | ] 131 | }, 132 | { 133 | "cell_type": "code", 134 | "execution_count": null, 135 | "metadata": { 136 | "id": "j6M7t_pgy-CW" 137 | }, 138 | "outputs": [], 139 | "source": [ 140 | "context = [{\"role\":\"system\",\"content\":\"\"\"\n", 141 | " create table employees(\n", 142 | " ID_Usr INT primary key,\n", 143 | " name VARCHAR);\n", 144 | " /*3 example rows\n", 145 | " select * from employees limit 3;\n", 146 | " ID_Usr name\n", 147 | " 1344 George StPierre\n", 148 | " 2122 Jon jones\n", 149 | " 1265 Anderson Silva\n", 150 | " */\n", 151 | "\n", 152 | " create table salary(\n", 153 | " ID_Usr INT,\n", 154 | " year DATE,\n", 155 | " salary FLOAT,\n", 156 | " foreign key (ID_Usr) references employees(ID_Usr));\n", 157 | " /*3 example rows\n", 158 | " select * from salary limit 3\n", 159 | " ID_Usr date salary\n", 160 | " 1344 01/01/2023 61000\n", 161 | " 1344 01/01/2022 60000\n", 162 | " 1265 01/01/2023 55000\n", 163 | " */\n", 164 | "\n", 165 | " create table studies(\n", 166 | " ID_study INT,\n", 167 | " ID_Usr INT,\n", 168 | " educational_level INT, /* 5=phd, 4=Master, 3=Bachelor */\n", 169 | " Institution VARCHAR,\n", 170 | " Years DATE,\n", 171 | " Speciality VARCHAR,\n", 172 | " primary key (ID_study, ID_Usr),\n", 173 | " foreign key(ID_Usr) references employees (ID_Usr));\n", 174 | " /*3 example rows\n", 175 | " select * from studies limit 3\n", 176 | " ID_Study ID_Usr educational_level Institution Years Speciality\n", 177 | " 2782 1344 3 UC San Diego 01/01/2010 Bachelor of Science in Marketing\n", 178 | " 2334 1344 5 MIT 01/01/2023 Phd. Data Science.\n", 179 | " 2782 2122 3 UC San Diego 01/01/2010 Bachelor of Science in Marketing\n", 180 | " */\n", 181 | " -- Maintain the SQL order simple and efficient as you can,\n", 182 | " using valid SQL Lite, answer the following questions,\n", 183 | " returning only SQL code, for the table provided above.\"\"\"},\n", 184 | " {\"role\":\"user\",\"content\":\"\"\"How Many employes we have with a salary bigger than 50000?\"\"\"},\n", 185 | " {\"role\":\"assistant\",\"content\":\"\"\"\n", 186 | " SELECT COUNT(*) AS total_employees\n", 187 | " FROM employees e\n", 188 | " INNER JOIN salary s ON e.ID_Usr = s.ID_Usr\n", 189 | " WHERE s.salary > 50000;\"\"\"},\n", 190 | " {\"role\":\"user\",\"content\":\"Return the names of the three people who have had the highest salary increase in the last three years.\"},\n", 191 | " {\"role\":\"assistant\",\"content\":\"\"\"\n", 192 | " SELECT e.name\n", 193 | " FROM employees e\n", 194 | " JOIN salary s ON e.ID_usr = s.ID_usr\n", 195 | " WHERE s.year >= DATE_SUB(CURDATE(), INTERVAL 3 YEAR)\n", 196 | " GROUP BY e.name\n", 197 | " ORDER BY (MAX(s.salary) - MIN(s.salary)) DESC\\nLIMIT 3;\"\"\"}]" 198 | ] 199 | }, 200 | { 201 | "cell_type": "code", 202 | "execution_count": null, 203 | "metadata": { 204 | "id": "BcpmXLF1cVGb" 205 | }, 206 | "outputs": [], 207 | "source": [ 208 | "#Functio to call the model.\n", 209 | "def return_CCRMSQL(user_message, context):\n", 210 | "\n", 211 | " newcontext = context.copy()\n", 212 | " newcontext.append({'role':'user', 'content':\"question: \" + user_message})\n", 213 | "\n", 214 | " response = client.chat.completions.create(\n", 215 | " model=\"GPT35NL2SQL\", #Our deployment\n", 216 | " messages = newcontext,\n", 217 | " temperature=0,\n", 218 | " max_tokens=800)\n", 219 | "\n", 220 | " return (response.choices[0].message.content)" 221 | ] 222 | }, 223 | { 224 | "cell_type": "code", 225 | "execution_count": null, 226 | "metadata": { 227 | "colab": { 228 | "base_uri": "https://localhost:8080/" 229 | }, 230 | "id": "VmB27XTWhKOn", 231 | "outputId": "a00e7627-dcdc-4c26-837e-93631ca61bd6" 232 | }, 233 | "outputs": [], 234 | "source": [ 235 | "context_user = context.copy()\n", 236 | "response = return_CCRMSQL(\"What's the name of the best paid employee?\", context_user)\n", 237 | "print(response)\n" 238 | ] 239 | }, 240 | { 241 | "cell_type": "code", 242 | "execution_count": null, 243 | "metadata": { 244 | "colab": { 245 | "base_uri": "https://localhost:8080/" 246 | }, 247 | "id": "svMFswMghM3D", 248 | "outputId": "a2cc72a8-550d-47d7-f187-745020238263" 249 | }, 250 | "outputs": [], 251 | "source": [ 252 | "print(return_CCRMSQL(\"Return the Institution with a higher average salary\", context_user))" 253 | ] 254 | }, 255 | { 256 | "cell_type": "markdown", 257 | "metadata": { 258 | "id": "CxcnI77oqLpl" 259 | }, 260 | "source": [ 261 | "#Conclusions.\n", 262 | "Once you have the prompt to use, set up the OpenAI services in Azure is straightforward.\n", 263 | "\n", 264 | "I observed some differences in the SQL from the one obtained calling directly to OpenAI API.\n" 265 | ] 266 | }, 267 | { 268 | "cell_type": "code", 269 | "execution_count": null, 270 | "metadata": { 271 | "id": "YMVK994yhbnP" 272 | }, 273 | "outputs": [], 274 | "source": [] 275 | } 276 | ], 277 | "metadata": { 278 | "colab": { 279 | "authorship_tag": "ABX9TyPviR8piXLbY+Rsqjm1IQE7", 280 | "include_colab_link": true, 281 | "provenance": [] 282 | }, 283 | "kernelspec": { 284 | "display_name": "Python 3", 285 | "name": "python3" 286 | }, 287 | "language_info": { 288 | "name": "python" 289 | } 290 | }, 291 | "nbformat": 4, 292 | "nbformat_minor": 0 293 | } 294 | -------------------------------------------------------------------------------- /P1-NL2SQL/6_3_AWS_Bedrock_NL2SQL_Client.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "colab_type": "text", 7 | "id": "view-in-github" 8 | }, 9 | "source": [ 10 | "\"Open" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "metadata": { 16 | "id": "Paa52inqag9w" 17 | }, 18 | "source": [ 19 | "
\n", 20 | "

Large Language Models Projects

\n", 21 | "

Apply and Implement Strategies for Large Language Models

\n", 22 | "

6.3-Calling AWS Bedrock from Python.

\n", 23 | "

\n", 24 | "
\n", 25 | "\n", 26 | "by [Pere Martra](https://www.linkedin.com/in/pere-martra/)\n", 27 | "__________\n", 28 | "Models: llama3-8b-instruct-v1\n", 29 | "\n", 30 | "Colab Environment: CPU\n", 31 | "\n", 32 | "Keys:\n", 33 | "* BedRock\n", 34 | "* AWS\n", 35 | "* NL2SQL\n", 36 | "\n", 37 | "__________" 38 | ] 39 | }, 40 | { 41 | "cell_type": "markdown", 42 | "metadata": { 43 | "id": "uxFVaxlskUf-" 44 | }, 45 | "source": [ 46 | "In this notebook, we make a call to a Model from AWS Bedrock that we've set up to work as a translator for SQL queries from natural language.\n" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": null, 52 | "metadata": { 53 | "id": "PQmhudeOj9PJ", 54 | "outputId": "a4d6a38c-90fb-446a-c0bb-c6a2695f8ff5" 55 | }, 56 | "outputs": [], 57 | "source": [ 58 | "pip install -q boto3==1.34.108" 59 | ] 60 | }, 61 | { 62 | "cell_type": "code", 63 | "execution_count": null, 64 | "metadata": { 65 | "id": "Jssm3zDHbKq-" 66 | }, 67 | "outputs": [], 68 | "source": [ 69 | "import boto3\n", 70 | "import json\n", 71 | "from getpass import getpass" 72 | ] 73 | }, 74 | { 75 | "cell_type": "code", 76 | "execution_count": null, 77 | "metadata": { 78 | "colab": { 79 | "base_uri": "https://localhost:8080/" 80 | }, 81 | "id": "UeZIlGHbkfHL", 82 | "outputId": "d7b591a7-a462-4b2f-cce2-70ca0e30e9c4" 83 | }, 84 | "outputs": [], 85 | "source": [ 86 | "aws_access_key_id = getpass('AWS Acces key: ')" 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": null, 92 | "metadata": { 93 | "colab": { 94 | "base_uri": "https://localhost:8080/" 95 | }, 96 | "id": "LjHrlQ3IkfVw", 97 | "outputId": "15023f96-056a-495a-8049-3a8d101578a4" 98 | }, 99 | "outputs": [], 100 | "source": [ 101 | "aws_secret_access_key = getpass('AWS Secret Key: ')" 102 | ] 103 | }, 104 | { 105 | "cell_type": "code", 106 | "execution_count": null, 107 | "metadata": { 108 | "id": "B2C_0LNjHFo3" 109 | }, 110 | "outputs": [], 111 | "source": [ 112 | "client = boto3.client(\"bedrock-runtime\",\n", 113 | " region_name=\"us-west-2\",\n", 114 | " aws_access_key_id = aws_access_key_id,\n", 115 | " aws_secret_access_key= aws_secret_access_key)\n" 116 | ] 117 | }, 118 | { 119 | "cell_type": "code", 120 | "execution_count": null, 121 | "metadata": { 122 | "id": "Ua4SgAVYwrVF" 123 | }, 124 | "outputs": [], 125 | "source": [ 126 | "# Set the model ID, e.g., Llama 3 8B Instruct.\n", 127 | "model_id = \"meta.llama3-8b-instruct-v1:0\"" 128 | ] 129 | }, 130 | { 131 | "cell_type": "code", 132 | "execution_count": null, 133 | "metadata": { 134 | "id": "YMVK994yhbnP" 135 | }, 136 | "outputs": [], 137 | "source": [ 138 | "# Define the user message to send.\n", 139 | "user_message = \"What is the name of the best paid employee?\"" 140 | ] 141 | }, 142 | { 143 | "cell_type": "code", 144 | "execution_count": null, 145 | "metadata": { 146 | "id": "BW89l6qdPEUy" 147 | }, 148 | "outputs": [], 149 | "source": [ 150 | "model_instructions = \"\"\"\n", 151 | "Your task is to convert a question into a SQL query, given a SQL database schema.\n", 152 | "Adhere to these rules:\n", 153 | "- **Deliberately go through the question and database schema word by word to appropriately answer the question.\n", 154 | "- **Return Only SQL Code.\n", 155 | " ### Input\n", 156 | " Generate a SQL query that answers the question below.\n", 157 | " This query will run on a database whose schema is represented in this string:\n", 158 | "\n", 159 | " create table employees(\n", 160 | " ID_Usr INT primary key,-- Unique Id for employee\n", 161 | " name VARCHAR -- Name of employee\n", 162 | " );\n", 163 | "\n", 164 | " create table salary(\n", 165 | " ID_Usr INT,-- Unique Id for employee\n", 166 | " year DATE, -- Date\n", 167 | " salary FLOAT, --Salary of employee\n", 168 | " foreign key (ID_Usr) references employees(ID_Usr) -- Join Employees with salary\n", 169 | " );\n", 170 | "\n", 171 | " create table studies(\n", 172 | " ID_study INT, -- Unique ID study\n", 173 | " ID_Usr INT, -- ID employee\n", 174 | " educational_level INT, -- 5=phd, 4=Master, 3=Bachelor\n", 175 | " Institution VARCHAR, --Name of instituon where eployee studied\n", 176 | " Years DATE, -- Date acomplishement stdy\n", 177 | " Speciality VARCHAR, -- Speciality of studies\n", 178 | " primary key (ID_study, ID_Usr), --Primary Key ID_Usr + ID_Study\n", 179 | " foreign key(ID_Usr) references employees (ID_Usr)\n", 180 | " );\n", 181 | "\n", 182 | "\"\"\"\n" 183 | ] 184 | }, 185 | { 186 | "cell_type": "code", 187 | "execution_count": null, 188 | "metadata": { 189 | "id": "9yc2edu10CbQ" 190 | }, 191 | "outputs": [], 192 | "source": [ 193 | "# Embed the message in Llama 3's prompt format.\n", 194 | "prompt = f\"\"\"\n", 195 | "<|begin_of_text|>\n", 196 | "<|start_header_id|>system<|end_header_id|>\n", 197 | "{model_instructions}\n", 198 | "<|eot_id|>\n", 199 | "<|start_header_id|>user<|end_header_id|>\n", 200 | "{user_message}\n", 201 | "<|eot_id|>\n", 202 | "<|start_header_id|>assistant<|end_header_id|>\n", 203 | "\"\"\"" 204 | ] 205 | }, 206 | { 207 | "cell_type": "code", 208 | "execution_count": null, 209 | "metadata": { 210 | "id": "RE5bDGz8PEUy", 211 | "outputId": "9ecd694b-c1b7-4217-9c3f-58aae49dd7e6" 212 | }, 213 | "outputs": [], 214 | "source": [ 215 | "print (prompt)" 216 | ] 217 | }, 218 | { 219 | "cell_type": "code", 220 | "execution_count": null, 221 | "metadata": { 222 | "id": "tKCmd3S90KET" 223 | }, 224 | "outputs": [], 225 | "source": [ 226 | "# Format the request payload using the model's native structure.\n", 227 | "hyper = {\n", 228 | " \"prompt\": prompt,\n", 229 | " # Optional inference parameters:\n", 230 | " \"max_gen_len\": 512,\n", 231 | " \"temperature\": 0.0\n", 232 | "}" 233 | ] 234 | }, 235 | { 236 | "cell_type": "code", 237 | "execution_count": null, 238 | "metadata": { 239 | "id": "xnkNtdbA0Lv2" 240 | }, 241 | "outputs": [], 242 | "source": [ 243 | "# Encode and send the request.\n", 244 | "response = client.invoke_model(body=json.dumps(hyper), modelId=model_id)" 245 | ] 246 | }, 247 | { 248 | "cell_type": "code", 249 | "execution_count": null, 250 | "metadata": { 251 | "id": "CZfzGp6e0Pfd" 252 | }, 253 | "outputs": [], 254 | "source": [ 255 | "# Decode the native response body.\n", 256 | "model_response = json.loads(response[\"body\"].read())" 257 | ] 258 | }, 259 | { 260 | "cell_type": "code", 261 | "execution_count": null, 262 | "metadata": { 263 | "colab": { 264 | "base_uri": "https://localhost:8080/" 265 | }, 266 | "id": "_HAm1LgD1-af", 267 | "outputId": "44731d22-5e8e-466d-a245-c602ede1bcd9" 268 | }, 269 | "outputs": [], 270 | "source": [ 271 | "# Extract and print the generated text.\n", 272 | "response_text = model_response[\"generation\"]\n", 273 | "print(response_text)" 274 | ] 275 | }, 276 | { 277 | "cell_type": "code", 278 | "execution_count": null, 279 | "metadata": { 280 | "id": "tARgaG9l2AG8" 281 | }, 282 | "outputs": [], 283 | "source": [] 284 | } 285 | ], 286 | "metadata": { 287 | "colab": { 288 | "include_colab_link": true, 289 | "provenance": [] 290 | }, 291 | "kernelspec": { 292 | "display_name": "Python 3 (ipykernel)", 293 | "language": "python", 294 | "name": "python3" 295 | }, 296 | "language_info": { 297 | "codemirror_mode": { 298 | "name": "ipython", 299 | "version": 3 300 | }, 301 | "file_extension": ".py", 302 | "mimetype": "text/x-python", 303 | "name": "python", 304 | "nbconvert_exporter": "python", 305 | "pygments_lexer": "ipython3", 306 | "version": "3.10.14" 307 | } 308 | }, 309 | "nbformat": 4, 310 | "nbformat_minor": 0 311 | } 312 | -------------------------------------------------------------------------------- /P1-NL2SQL/readme.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 8 | 17 | 18 |
4 | 5 | 6 | 7 | 9 |

10 | This is the unofficial repository for the book: 11 | Large Language Models: Apply and Implement Strategies for Large Language Models (Apress). 12 | The book is based on the content of this repository, but the notebooks are being updated, and I am incorporating new examples and chapters. 13 | If you are looking for the official repository for the book, with the original notebooks, you should visit the 14 | Apress repository, where you can find all the notebooks in their original format as they appear in the book. Buy it at: [Amazon] [Springer] 15 |

16 |
19 | 20 | In this straightforward initial project, we are going to develop a SQL generator from natural language. We'll begin by creating the prompt to implement two solutions: one using OpenAI models running on Azure, and the other with an open-source model from Hugging Face. 21 | 22 | image 23 | 24 | ## Prompt Creation 25 | We will create two NL2SQL prompts that adhere to the best practices published in the paper from the University of Ohio (see below). Adapting them to the specific needs of OpenAI and SQLCoder Models. First we will create trhe OpenAI Prompt, then continue adapting the prompt created to SQLCoder, an open-source model trained by Defog from a super efficient Code Llama 7B Model, available on from Hugging Face. 26 | 27 | Although both prompts are based on the same paper, there are slight differences in the creation process. 28 | 29 | Besides creating the prompt, we conduct a few tests and observe how both models generate SQL commands correctly. 30 | | Article | Notebook | 31 | | --------- | --- | 32 | | [Create a NL2SQL prompt for OpenAI](https://pub.towardsai.net/create-a-superprompt-for-natural-language-to-sql-conversion-for-openai-9d19f0efe8f4?sk=88889b3417c97481e6a907e3aef74ca2) | [Notebook for OpenAI](https://github.com/peremartra/Large-Language-Model-Notebooks-Course/blob/main/P1-NL2SQL/nl2sql_prompt_OpenAI.ipynb) | 33 | | Article WIP | [Notebook for SQLCoder](https://github.com/peremartra/Large-Language-Model-Notebooks-Course/blob/main/P1-NL2SQL/nl2sql_prompt_SQLCoder.ipynb) | 34 | 35 | ## Azure Configuration. 36 | In Azure, we will configure the Open Services using the prompt created earlier for OpenAI models. This allows us to conduct tests and set up an inference endpoint to call for obtaining SQL commands. 37 | | Article | Notebook | 38 | | --------- | --- | 39 | | [How To Set up a NL2SQL System With Azure OpenAI Studio.](https://medium.com/towards-artificial-intelligence/how-to-set-up-an-nl2sql-system-with-azure-openai-studio-2fcfc7b57301) | [Using the inference point on Azure.](https://github.com/peremartra/Large-Language-Model-Notebooks-Course/blob/main/P1-NL2SQL/NL2SQL_OpenAI_Azure.ipynb) | 40 | 41 | # Papers used in the project: 42 | * [How to Prompt LLMs for Text-to-SQL: A Study in Zero-shot, Single-domain, and Cross-domain Settings](https://arxiv.org/abs/2305.11853) 43 | -------------------------------------------------------------------------------- /P2-MHF/readme.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 8 | 17 | 18 |
4 | 5 | 6 | 7 | 9 |

10 | This is the unofficial repository for the book: 11 | Large Language Models: Apply and Implement Strategies for Large Language Models (Apress). 12 | The book is based on the content of this repository, but the notebooks are being updated, and I am incorporating new examples and chapters. 13 | If you are looking for the official repository for the book, with the original notebooks, you should visit the 14 | Apress repository, where you can find all the notebooks in their original format as they appear in the book. Buy it at: [Amazon] [Springer] 15 |

16 |
19 | 20 | ## [Create and publish an LLM.](https://github.com/peremartra/Large-Language-Model-Notebooks-Course/blob/main/P2-MHF/readme.md) 21 | In this small project we will create a new model alingning a couple of models a [microsoft-phi-3-model](https://huggingface.co/microsoft/Phi-3-mini-4k-instruct) and a [gemma-2b-it](https://huggingface.co/google/gemma-2b-it) with DPO and then publish it to Hugging Face. 22 | 23 | Alignment is usually the final step taken when creating a model, after fine-tuning. Many people believe that the true revolution of GPT-3.5 was due to the alignment process that OpenAI carried out: Reinforcement Learning by Human Feedback, or RLFH. 24 | 25 | RLHF proved to be a highly efficient technique for controlling the model's responses, and at first, it seemed that it had to be the price to pay for any model that wanted to compete with GPT-3.5. 26 | 27 | Recently RLHF has been displaced by a technique that achieves the same result in a much more efficient way: DPO - Direct Preference Optimization. 28 | 29 | The implementation of DPO that we'll be using in the notebooks is the one developed by Hugging Face in their TRL library, which stands for Transformer Reinforcement Learning. DPO can be considered a reinforcement learning technique, where the model is rewarded during its training phase based on its responses. 30 | 31 | This library greatly simplifies the implementation of DPO. All you have to do is specify the model you want to fine-tune and provide it with a dataset in the necessary format. 32 | 33 | The dataset to be used should have three columns: 34 | * Prompt: The prompt used. 35 | * Chosen: The desired response. 36 | * Rejected: An undesired response. 37 | 38 | The dataset selected for this example is [argilla/distilabel-capybara-dpo-7k-binarized](https://huggingface.co/datasets/argilla/distilabel-capybara-dpo-7k-binarized) 39 | 40 | If you'd like to take a look at the models resulting from two hours of training on an A100 GPU with the Argila dataset, you can do so on its Hugging Face page. 41 | * [martra-open-gemma-2b-it-dpo](https://huggingface.co/oopere/martra-open-gemma-2b-it-dpo) 42 | * [martra-phi-3-mini-dpo](https://huggingface.co/oopere/martra-phi-3-mini-dpo) 43 | 44 | 45 | | Article | Notebook | 46 | | --- | --- | 47 | | WIP | [Aligning with DPO a phi3-3 model.](https://github.com/peremartra/Large-Language-Model-Notebooks-Course/blob/main/P2-MHF/7_2_Aligning_DPO_phi3.ipynb)
[Aligning with DPO a Gemma model.](https://github.com/peremartra/Large-Language-Model-Notebooks-Course/blob/main/P2-MHF/Aligning_DPO_open_gemma-2b-it.ipynb)| 48 | -------------------------------------------------------------------------------- /P3-CustomFinancialLLM/Readme.md: -------------------------------------------------------------------------------- 1 | In this project, we are going to optimize a financial model specialized in sentiment detection for financial reports. 2 | 3 | This is a clear example of a typical task one might encounter in a company. They have an LLM that works well but want to reduce its resource consumption while maintaining its performance. The reasons for this can vary—perhaps they simply want to lower usage costs, or maybe they need to deploy it on smaller devices, such as mobile phones. 4 | 5 | In this case, we will start with the FinGPT model and create a version that can perform the same tasks with the same efficiency while using fewer resources. 6 | 7 | **Steps to follow:** 8 | * Take the base model and measure its performance. We need to determine which benchmarks will provide the insights necessary to confirm that the optimized model consumes fewer resources while maintaining performance. 9 | * Decide which types of pruning can be applied: 10 | * Which part of the model? Embeddings / Attention / MLP. 11 | * Which type of pruning? Width / Depth. 12 | * Apply pruning. 13 | * Recover any lost performance using Knowledge Distillation. 14 | 15 | This is a fairly standard process in the optimization of customized models. We go beyond fine-tuning by modifying the model’s structure to better fit our needs while preserving the knowledge we want to retain. 16 | 17 | ________________________ 18 | 19 | -------------------------------------------------------------------------------- /clean_notebooks.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | import os 3 | import subprocess 4 | from pathlib import Path 5 | import argparse 6 | 7 | def clear_notebook_outputs(root_dir='.'): 8 | """Find all Jupyter notebooks and clear their outputs using nbconvert.""" 9 | # Find all .ipynb files recursively 10 | notebook_files = list(Path(root_dir).rglob('*.ipynb')) 11 | 12 | count = 0 13 | for nb_path in notebook_files: 14 | print(f"Processing: {nb_path}") 15 | try: 16 | # Run jupyter nbconvert command 17 | subprocess.run(['jupyter', 'nbconvert', '--clear-output', '--inplace', str(nb_path)], 18 | check=True, 19 | stdout=subprocess.PIPE, 20 | stderr=subprocess.PIPE) 21 | count += 1 22 | except subprocess.CalledProcessError as e: 23 | print(f"Error processing {nb_path}: {e}") 24 | 25 | print(f"Completed! Processed {count} notebooks.") 26 | 27 | if __name__ == "__main__": 28 | parser = argparse.ArgumentParser(description='Clear outputs from Jupyter notebooks recursively') 29 | parser.add_argument('--dir', type=str, default='.', help='Root directory to search for notebooks (default: current directory)') 30 | 31 | args = parser.parse_args() 32 | clear_notebook_outputs(args.dir) 33 | -------------------------------------------------------------------------------- /img/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peremartra/Large-Language-Model-Notebooks-Course/4c655e82cf149bfd302a0e8f32366bda4595eec4/img/.DS_Store -------------------------------------------------------------------------------- /img/LLM_course_diagram.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peremartra/Large-Language-Model-Notebooks-Course/4c655e82cf149bfd302a0e8f32366bda4595eec4/img/LLM_course_diagram.jpg -------------------------------------------------------------------------------- /img/Large_Language_Models_Projects_Book.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peremartra/Large-Language-Model-Notebooks-Course/4c655e82cf149bfd302a0e8f32366bda4595eec4/img/Large_Language_Models_Projects_Book.jpg -------------------------------------------------------------------------------- /img/Martra_Figure_2-7.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peremartra/Large-Language-Model-Notebooks-Course/4c655e82cf149bfd302a0e8f32366bda4595eec4/img/Martra_Figure_2-7.jpg -------------------------------------------------------------------------------- /img/Martra_Figure_4_1AE_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peremartra/Large-Language-Model-Notebooks-Course/4c655e82cf149bfd302a0e8f32366bda4595eec4/img/Martra_Figure_4_1AE_1.jpg -------------------------------------------------------------------------------- /img/Martra_Figure_4_1AE_2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peremartra/Large-Language-Model-Notebooks-Course/4c655e82cf149bfd302a0e8f32366bda4595eec4/img/Martra_Figure_4_1AE_2.jpg -------------------------------------------------------------------------------- /img/Martra_Figure_4_1AE_3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peremartra/Large-Language-Model-Notebooks-Course/4c655e82cf149bfd302a0e8f32366bda4595eec4/img/Martra_Figure_4_1AE_3.jpg -------------------------------------------------------------------------------- /img/Martra_Figure_4_1AE_Final.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peremartra/Large-Language-Model-Notebooks-Course/4c655e82cf149bfd302a0e8f32366bda4595eec4/img/Martra_Figure_4_1AE_Final.jpg -------------------------------------------------------------------------------- /img/Martra_Figure_4_2SDL_CompareOpenAI_HF.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peremartra/Large-Language-Model-Notebooks-Course/4c655e82cf149bfd302a0e8f32366bda4595eec4/img/Martra_Figure_4_2SDL_CompareOpenAI_HF.jpg -------------------------------------------------------------------------------- /img/Martra_Figure_4_2SDL_CompareTestst.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peremartra/Large-Language-Model-Notebooks-Course/4c655e82cf149bfd302a0e8f32366bda4595eec4/img/Martra_Figure_4_2SDL_CompareTestst.jpg -------------------------------------------------------------------------------- /img/Martra_Figure_4_2SDL_Dataset.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peremartra/Large-Language-Model-Notebooks-Course/4c655e82cf149bfd302a0e8f32366bda4595eec4/img/Martra_Figure_4_2SDL_Dataset.jpg -------------------------------------------------------------------------------- /img/Martra_Figure_4_2SDL_Tests.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peremartra/Large-Language-Model-Notebooks-Course/4c655e82cf149bfd302a0e8f32366bda4595eec4/img/Martra_Figure_4_2SDL_Tests.jpg -------------------------------------------------------------------------------- /img/Martra_Figure_4_Retriever_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peremartra/Large-Language-Model-Notebooks-Course/4c655e82cf149bfd302a0e8f32366bda4595eec4/img/Martra_Figure_4_Retriever_1.jpg -------------------------------------------------------------------------------- /img/Martra_Figure_4_Retriever_2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peremartra/Large-Language-Model-Notebooks-Course/4c655e82cf149bfd302a0e8f32366bda4595eec4/img/Martra_Figure_4_Retriever_2.jpg -------------------------------------------------------------------------------- /img/Martra_Figure_5_Prompt_Tuning.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peremartra/Large-Language-Model-Notebooks-Course/4c655e82cf149bfd302a0e8f32366bda4595eec4/img/Martra_Figure_5_Prompt_Tuning.jpg -------------------------------------------------------------------------------- /img/Martra_Figure_5_Quantization_Graph.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peremartra/Large-Language-Model-Notebooks-Course/4c655e82cf149bfd302a0e8f32366bda4595eec4/img/Martra_Figure_5_Quantization_Graph.jpg -------------------------------------------------------------------------------- /img/Martra_Figure_6-13.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peremartra/Large-Language-Model-Notebooks-Course/4c655e82cf149bfd302a0e8f32366bda4595eec4/img/Martra_Figure_6-13.jpg -------------------------------------------------------------------------------- /img/Martra_Figure_6-18.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peremartra/Large-Language-Model-Notebooks-Course/4c655e82cf149bfd302a0e8f32366bda4595eec4/img/Martra_Figure_6-18.jpg -------------------------------------------------------------------------------- /img/colab.svg: -------------------------------------------------------------------------------- 1 | Open in ColabOpen in Colab 2 | -------------------------------------------------------------------------------- /img/depth_rpunedvsbase.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peremartra/Large-Language-Model-Notebooks-Course/4c655e82cf149bfd302a0e8f32366bda4595eec4/img/depth_rpunedvsbase.png -------------------------------------------------------------------------------- /img/lambada_BooQ_Accuracy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peremartra/Large-Language-Model-Notebooks-Course/4c655e82cf149bfd302a0e8f32366bda4595eec4/img/lambada_BooQ_Accuracy.png -------------------------------------------------------------------------------- /img/langsmith.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peremartra/Large-Language-Model-Notebooks-Course/4c655e82cf149bfd302a0e8f32366bda4595eec4/img/langsmith.png -------------------------------------------------------------------------------- /img/langsmith_API_KEY.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peremartra/Large-Language-Model-Notebooks-Course/4c655e82cf149bfd302a0e8f32366bda4595eec4/img/langsmith_API_KEY.jpg -------------------------------------------------------------------------------- /img/semantic_cache.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peremartra/Large-Language-Model-Notebooks-Course/4c655e82cf149bfd302a0e8f32366bda4595eec4/img/semantic_cache.jpg --------------------------------------------------------------------------------